Tue, 28 Sep 2021 18:22:00 +0200
remove unnecessary fields from linked list node and simplifies cx_ll_add()
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
29 #include "cx/linked_list.h"
30 #include <stdint.h>
31 #include <string.h>
33 /* LOW LEVEL LINKED LIST FUNCTIONS */
35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
37 void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) {
38 size_t i = start_index;
39 void *cur = start;
40 while (i != index && cur != NULL) {
41 cur = *CX_LL_PTR(cur, loc_advance);
42 i < index ? i++ : i--;
43 }
44 return cur;
45 }
47 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
48 if (end != NULL) {
49 return *end;
50 } else {
51 if (begin == NULL || *begin == NULL)
52 return NULL;
54 void *cur = *begin;
55 void *last;
56 do {
57 last = cur;
58 } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
60 return last;
61 }
62 }
64 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
65 void *last = cx_linked_list_last(begin, end, loc_next);
66 if (last == NULL) {
67 if (begin == NULL) {
68 // no current list and no begin ptr to write to - we don't find something to append to
69 return 1;
70 } else {
71 // start fresh list
72 *begin = new_node;
73 }
74 } else {
75 // if there is a last node, update its next pointer
76 void **next = CX_LL_PTR(last, loc_next);
77 *next = new_node;
78 }
80 // if there is an end pointer, update it
81 if (end != NULL) {
82 *end = cx_linked_list_last(&new_node, NULL, loc_next);
83 }
85 // if the nodes use a prev pointer, update it
86 if (loc_prev >= 0) {
87 void **prev = CX_LL_PTR(new_node, loc_prev);
88 *prev = last;
89 }
91 return 0;
92 }
94 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
96 typedef struct {
97 void *prev;
98 void *next;
99 char payload[];
100 } cx_linked_list_node;
102 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
103 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
105 typedef struct {
106 cx_list_s base;
107 cx_linked_list_node *begin;
108 cx_linked_list_node *end;
109 } cx_linked_list;
111 static int cx_ll_add(cx_list_s *list, void *elem) {
112 cx_linked_list *ll = (cx_linked_list *) list;
114 cx_linked_list_node *node = cxMalloc(list->allocator,
115 sizeof(cx_linked_list_node) + list->itemsize);
116 if (node == NULL)
117 return 1;
119 memcpy(node->payload, elem, list->itemsize);
120 node->next = NULL;
122 if (ll->begin == NULL) {
123 ll->begin = ll->end = node;
124 } else {
125 // per contract of this linked list we know that end must be non-null
126 ll->end->next = node;
127 ll->end = node;
128 }
129 list->size++;
131 return 0;
132 }
134 static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
135 cx_linked_list *ll = (cx_linked_list *) list;
136 // TODO: implement using low level API
137 return 1;
138 }
140 static int cx_ll_remove(cx_list_s *list, size_t index) {
141 if (index >= list->size) {
142 return 1;
143 }
144 cx_linked_list *ll = (cx_linked_list *) list;
145 // TODO: implement using low level API
146 return 0;
147 }
149 static void *cx_ll_at(cx_list_s *list, size_t index) {
150 cx_linked_list *ll = (cx_linked_list *) list;
151 cx_linked_list_node *node;
152 if (index >= list->size) {
153 node = NULL;
154 } else if (index > list->size / 2) {
155 node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index);
156 } else {
157 node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index);
158 }
159 return node == NULL ? NULL : &node->payload;
160 }
162 static size_t cx_ll_find(cx_list_s *list, void *elem) {
163 CxListComparator cmp = list->cmpfunc;
164 cx_linked_list *ll = (cx_linked_list *) list;
166 size_t index;
167 cx_linked_list_node *node = ll->begin;
168 for (index = 0; index < list->size; index++) {
169 void *current = node->payload;
170 if (cmp(current, elem) == 0) {
171 return index;
172 }
173 node = node->next;
174 }
175 return index;
176 }
178 static void *cx_ll_last(cx_list_s *list) {
179 cx_linked_list *ll = (cx_linked_list *) list;
180 cx_linked_list_node *last = cx_linked_list_last(NULL, (void **) &ll->end, CX_LL_LOC_NEXT);
181 return &last->payload;
182 }
184 cx_list_class cx_linked_list_class = {
185 cx_ll_add,
186 cx_ll_insert,
187 cx_ll_remove,
188 cx_ll_at,
189 cx_ll_find,
190 cx_ll_last
191 };
193 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
194 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
195 if (list == NULL)
196 return NULL;
198 list->base.cl = &cx_linked_list_class;
199 list->base.allocator = allocator;
200 list->base.cmpfunc = comparator;
201 list->base.itemsize = item_size;
202 list->base.capacity = SIZE_MAX;
203 list->base.size = 0;
205 list->begin = NULL;
206 list->end = NULL;
208 return (CxList) list;
209 }
211 void cxLinkedListDestroy(CxList list) {
212 cx_linked_list *ll = (cx_linked_list *) list;
214 cx_linked_list_node *node = ll->begin;
215 while (node) {
216 void *next = node->next;
217 cxFree(list->allocator, node);
218 node = next;
219 }
221 cxFree(list->allocator, list);
222 }
224 size_t cxLinkedListRecalculateSize(CxList list) {
225 cx_linked_list *ll = (cx_linked_list *) list;
227 if (ll->begin == NULL) {
228 ll->base.size = 0;
229 ll->end = NULL;
230 return 0;
231 } else {
232 cx_linked_list_node *cur = ll->begin;
233 cx_linked_list_node *last;
234 size_t size = 0;
235 do {
236 last = cur;
237 size++;
238 } while ((cur = cur->next) != NULL);
239 ll->end = last;
240 ll->base.size = size;
241 return size;
242 }
243 }