Tue, 05 Oct 2021 11:19:32 +0200
remove convenience macros
Users should write their own wrappers s.t. the type
information does not have to be repeated on every
call site.
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>
32 #include <assert.h>
34 /* LOW LEVEL LINKED LIST FUNCTIONS */
36 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
38 void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) {
39 size_t i = start_index;
40 void *cur = start;
41 while (i != index && cur != NULL) {
42 cur = *CX_LL_PTR(cur, loc_advance);
43 i < index ? i++ : i--;
44 }
45 return cur;
46 }
48 void *cx_linked_list_last(void *begin, ptrdiff_t loc_next) {
49 if (begin == NULL)
50 return NULL;
52 void *cur = begin;
53 void *last;
54 do {
55 last = cur;
56 } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
58 return last;
59 }
61 void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
62 void *last;
63 if (end == NULL) {
64 assert(begin != NULL);
65 last = cx_linked_list_last(*begin, loc_next);
66 } else {
67 last = *end;
68 }
69 if (last == NULL) {
70 assert(begin != NULL);
71 *begin = new_node;
72 } else {
73 // if there is a last node, update its next pointer
74 void **next = CX_LL_PTR(last, loc_next);
75 *next = new_node;
76 }
78 // if there is an end pointer, update it
79 if (end != NULL) {
80 *end = cx_linked_list_last(new_node, loc_next);
81 }
83 // if the nodes use a prev pointer, update it
84 if (loc_prev >= 0) {
85 void **prev = CX_LL_PTR(new_node, loc_prev);
86 *prev = last;
87 }
88 }
90 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
92 typedef struct cx_linked_list_node cx_linked_list_node;
93 struct cx_linked_list_node {
94 cx_linked_list_node *prev;
95 cx_linked_list_node *next;
96 char payload[];
97 };
99 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
100 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
102 typedef struct {
103 cx_list_s base;
104 cx_linked_list_node *begin;
105 cx_linked_list_node *end;
106 } cx_linked_list;
108 static cx_linked_list_node *cx_ll_node_at(cx_linked_list *list, size_t index) {
109 if (index >= list->base.size) {
110 return NULL;
111 } else if (index > list->base.size / 2) {
112 return cx_linked_list_at(list->end, list->base.size-1, CX_LL_LOC_PREV, index);
113 } else {
114 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
115 }
116 }
118 static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
119 // out-of bounds check
120 if (index > list->size) return 1;
122 // cast a linked list pointer
123 cx_linked_list *ll = (cx_linked_list *) list;
125 // create the new node
126 cx_linked_list_node *node = cxMalloc(list->allocator,
127 sizeof(cx_linked_list_node) + list->itemsize);
129 // sortir if failed
130 if (node == NULL) return 1;
132 // copy payload to the new node
133 memcpy(node->payload, elem, list->itemsize);
135 // check if this is the first node
136 if (ll->begin == NULL) {
137 node->prev = node->next = NULL;
138 ll->begin = ll->end = node;
139 } else {
140 // check if this shall be the new end node
141 if (index == list->size) {
142 ll->end->next = node;
143 node->prev = ll->end;
144 node->next = NULL;
145 ll->end = node;
146 }
147 // check if this shall be the new start node
148 else if (index == 0) {
149 ll->begin->prev = node;
150 node->next = ll->begin;
151 node->prev = NULL;
152 ll->begin = node;
153 } else {
154 // find the node at the current index
155 cx_linked_list_node *cur = cx_ll_node_at(ll, index);
157 // insert before that node
158 // (we know all ptr are non-null because we handled all other cases before)
159 node->next = cur;
160 node->prev = cur->prev;
161 cur->prev = node;
162 node->prev->next = node;
163 }
164 }
166 // increase the size and return
167 list->size++;
168 return 0;
169 }
171 static int cx_ll_add(cx_list_s *list, void *elem) {
172 return cx_ll_insert(list, list->size, elem);
173 }
175 static int cx_ll_remove(cx_list_s *list, size_t index) {
176 cx_linked_list *ll = (cx_linked_list *) list;
177 cx_linked_list_node *node = cx_ll_node_at(ll, index);
179 // out-of-bounds check
180 if (node == NULL) return 1;
182 // change left side connection
183 if (node->prev == NULL) {
184 ll->begin = node->next;
185 } else {
186 node->prev->next = node->next;
187 }
189 // change right side connection
190 if (node->next == NULL) {
191 ll->end = node->prev;
192 } else {
193 node->next->prev = node->prev;
194 }
196 // adjust size
197 list->size--;
199 // free and return
200 cxFree(list->allocator, node);
202 return 0;
203 }
205 static void *cx_ll_at(cx_list_s *list, size_t index) {
206 cx_linked_list *ll = (cx_linked_list *) list;
207 cx_linked_list_node *node = cx_ll_node_at(ll, index);
208 return node == NULL ? NULL : &node->payload;
209 }
211 static size_t cx_ll_find(cx_list_s *list, void *elem) {
212 CxListComparator cmp = list->cmpfunc;
213 cx_linked_list *ll = (cx_linked_list *) list;
215 size_t index;
216 cx_linked_list_node *node = ll->begin;
217 for (index = 0; index < list->size; index++) {
218 void *current = node->payload;
219 if (cmp(current, elem) == 0) {
220 return index;
221 }
222 node = node->next;
223 }
224 return index;
225 }
227 static void *cx_ll_last(cx_list_s *list) {
228 cx_linked_list *ll = (cx_linked_list *) list;
229 cx_linked_list_node *last = ll->end;
230 return last == NULL ? NULL : &last->payload;
231 }
233 static cx_list_class cx_linked_list_class = {
234 cx_ll_add,
235 cx_ll_insert,
236 cx_ll_remove,
237 cx_ll_at,
238 cx_ll_find,
239 cx_ll_last
240 };
242 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
243 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
244 if (list == NULL)
245 return NULL;
247 list->base.cl = &cx_linked_list_class;
248 list->base.allocator = allocator;
249 list->base.cmpfunc = comparator;
250 list->base.itemsize = item_size;
251 list->base.capacity = SIZE_MAX;
252 list->base.size = 0;
254 list->begin = NULL;
255 list->end = NULL;
257 return (CxList) list;
258 }
260 void cxLinkedListDestroy(CxList list) {
261 cx_linked_list *ll = (cx_linked_list *) list;
263 cx_linked_list_node *node = ll->begin;
264 while (node) {
265 void *next = node->next;
266 cxFree(list->allocator, node);
267 node = next;
268 }
270 cxFree(list->allocator, list);
271 }
273 size_t cxLinkedListRecalculateSize(CxList list) {
274 cx_linked_list *ll = (cx_linked_list *) list;
276 if (ll->begin == NULL) {
277 ll->base.size = 0;
278 ll->end = NULL;
279 return 0;
280 } else {
281 cx_linked_list_node *cur = ll->begin;
282 cx_linked_list_node *last;
283 size_t size = 0;
284 do {
285 last = cur;
286 size++;
287 } while ((cur = cur->next) != NULL);
288 ll->end = last;
289 ll->base.size = size;
290 return size;
291 }
292 }