Mon, 27 Sep 2021 18:57:17 +0200
fix mixed up cases in cx_ll_at()
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 struct cx_linked_list_node {
97 void *prev;
98 void *next;
99 char payload[];
100 };
102 #define CX_LL_LOC_PREV offsetof(struct cx_linked_list_node, prev)
103 #define CX_LL_LOC_NEXT offsetof(struct cx_linked_list_node, next)
105 typedef struct {
106 cx_list_s base;
107 void *begin;
108 void *end;
109 ptrdiff_t loc_prev;
110 ptrdiff_t loc_next;
111 } cx_linked_list;
113 static int cx_ll_add(cx_list_s *list, void *elem) {
114 cx_linked_list *ll = (cx_linked_list *) list;
116 struct cx_linked_list_node *node = cxMalloc(list->allocator,
117 sizeof(struct cx_linked_list_node) + list->itemsize);
118 if (node == NULL)
119 return 1;
121 node->next = node->prev = NULL;
122 memcpy(node->payload, elem, list->itemsize);
124 int ret = cx_linked_list_add(
125 &ll->begin, &ll->end,
126 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
127 node
128 );
129 if (ret == 0) {
130 list->size++;
131 return 0;
132 } else {
133 return ret;
134 }
135 }
137 static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
138 cx_linked_list *ll = (cx_linked_list *) list;
139 // TODO: implement using low level API
140 return 1;
141 }
143 static int cx_ll_remove(cx_list_s *list, size_t index) {
144 if (index >= list->size) {
145 return 1;
146 }
147 cx_linked_list *ll = (cx_linked_list *) list;
148 // TODO: implement using low level API
149 return 0;
150 }
152 static void *cx_ll_at(cx_list_s *list, size_t index) {
153 cx_linked_list *ll = (cx_linked_list *) list;
154 struct cx_linked_list_node *node;
155 if (index >= list->size) {
156 node = NULL;
157 } else if (index > list->size / 2) {
158 node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index);
159 } else {
160 node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index);
161 }
162 return node == NULL ? NULL : &node->payload;
163 }
165 static size_t cx_ll_find(cx_list_s *list, void *elem) {
166 CxListComparator cmp = list->cmpfunc;
167 cx_linked_list *ll = (cx_linked_list *) list;
169 size_t index;
170 struct cx_linked_list_node* node = ll->begin;
171 for (index = 0 ; index < list->size ; index++) {
172 void* current = node->payload;
173 if (cmp(current, elem) == 0) {
174 return index;
175 }
176 node = node->next;
177 }
178 return index;
179 }
181 static void *cx_ll_last(cx_list_s *list) {
182 cx_linked_list *ll = (cx_linked_list *) list;
183 struct cx_linked_list_node *last = cx_linked_list_last(NULL, &ll->end, CX_LL_LOC_NEXT);
184 return &last->payload;
185 }
187 cx_list_class cx_linked_list_class = {
188 cx_ll_add,
189 cx_ll_insert,
190 cx_ll_remove,
191 cx_ll_at,
192 cx_ll_find,
193 cx_ll_last
194 };
196 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
197 cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list));
198 if (list == NULL)
199 return NULL;
201 list->base.cl = &cx_linked_list_class;
202 list->base.allocator = allocator;
203 list->base.cmpfunc = comparator;
204 list->base.itemsize = item_size;
205 list->base.capacity = SIZE_MAX;
207 list->begin = NULL;
208 list->loc_prev = CX_LL_LOC_PREV;
209 list->loc_next = CX_LL_LOC_NEXT;
211 CxList result = (CxList) list;
212 cxLinkedListRecalculateSize(result);
213 return result;
214 }
216 void cxLinkedListDestroy(CxList list) {
217 cx_linked_list* ll = (cx_linked_list*) list;
219 struct cx_linked_list_node* node = ll->begin;
220 while (node) {
221 void* next = node->next;
222 cxFree(list->allocator, node);
223 node = next;
224 }
226 cxFree(list->allocator, list);
227 }
229 size_t cxLinkedListRecalculateSize(CxList list) {
230 cx_linked_list *ll = (cx_linked_list *) list;
232 if (ll->begin == NULL) {
233 ll->base.size = 0;
234 ll->end = NULL;
235 return 0;
236 } else {
237 void *cur = ll->begin;
238 void *last;
239 size_t size = 0;
240 do {
241 last = cur;
242 size++;
243 } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
244 ll->end = last;
245 ll->base.size = size;
246 return size;
247 }
248 }