Sat, 26 Nov 2022 16:58:41 +0100
separate iterators and mutating iterators
Trade tons of code duplication for const-correctness.
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 "cx/utils.h"
31 #include <stdint.h>
32 #include <string.h>
33 #include <assert.h>
35 // LOW LEVEL LINKED LIST FUNCTIONS
37 #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
38 #define ll_prev(node) CX_LL_PTR(node, loc_prev)
39 #define ll_next(node) CX_LL_PTR(node, loc_next)
40 #define ll_advance(node) CX_LL_PTR(node, loc_advance)
41 #define ll_data_f(node, follow_ptr) ((follow_ptr)?CX_LL_PTR(node, loc_data):(((char*)(node))+loc_data))
42 #define ll_data(node) ll_data_f(node,follow_ptr)
44 void *cx_linked_list_at(
45 void const *start,
46 size_t start_index,
47 ptrdiff_t loc_advance,
48 size_t index
49 ) {
50 assert(start != NULL);
51 assert(loc_advance >= 0);
52 size_t i = start_index;
53 void const *cur = start;
54 while (i != index && cur != NULL) {
55 cur = ll_advance(cur);
56 i < index ? i++ : i--;
57 }
58 return (void *) cur;
59 }
61 size_t cx_linked_list_find(
62 void const *start,
63 ptrdiff_t loc_advance,
64 ptrdiff_t loc_data,
65 bool follow_ptr,
66 CxListComparator cmp_func,
67 void const *elem
68 ) {
69 assert(start != NULL);
70 assert(loc_advance >= 0);
71 assert(loc_data >= 0);
72 assert(cmp_func);
74 void const *node = start;
75 size_t index = 0;
76 do {
77 void *current = ll_data(node);
78 if (cmp_func(current, elem) == 0) {
79 return index;
80 }
81 node = ll_advance(node);
82 index++;
83 } while (node != NULL);
84 return index;
85 }
87 void *cx_linked_list_first(
88 void const *node,
89 ptrdiff_t loc_prev
90 ) {
91 return cx_linked_list_last(node, loc_prev);
92 }
94 void *cx_linked_list_last(
95 void const *node,
96 ptrdiff_t loc_next
97 ) {
98 assert(node != NULL);
99 assert(loc_next >= 0);
101 void const *cur = node;
102 void const *last;
103 do {
104 last = cur;
105 } while ((cur = ll_next(cur)) != NULL);
107 return (void *) last;
108 }
110 void *cx_linked_list_prev(
111 void const *begin,
112 ptrdiff_t loc_next,
113 void const *node
114 ) {
115 assert(begin != NULL);
116 assert(node != NULL);
117 assert(loc_next >= 0);
118 if (begin == node) return NULL;
119 void const *cur = begin;
120 void const *next;
121 while (1) {
122 next = ll_next(cur);
123 if (next == node) return (void *) cur;
124 cur = next;
125 }
126 }
128 void cx_linked_list_link(
129 void *left,
130 void *right,
131 ptrdiff_t loc_prev,
132 ptrdiff_t loc_next
133 ) {
134 assert(loc_next >= 0);
135 ll_next(left) = right;
136 if (loc_prev >= 0) {
137 ll_prev(right) = left;
138 }
139 }
141 void cx_linked_list_unlink(
142 void *left,
143 void *right,
144 ptrdiff_t loc_prev,
145 ptrdiff_t loc_next
146 ) {
147 assert (loc_next >= 0);
148 assert(ll_next(left) == right);
149 ll_next(left) = NULL;
150 if (loc_prev >= 0) {
151 assert(ll_prev(right) == left);
152 ll_prev(right) = NULL;
153 }
154 }
156 void cx_linked_list_add(
157 void **begin,
158 void **end,
159 ptrdiff_t loc_prev,
160 ptrdiff_t loc_next,
161 void *new_node
162 ) {
163 void *last;
164 if (end == NULL) {
165 assert(begin != NULL);
166 last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
167 } else {
168 last = *end;
169 }
170 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
171 }
173 void cx_linked_list_prepend(
174 void **begin,
175 void **end,
176 ptrdiff_t loc_prev,
177 ptrdiff_t loc_next,
178 void *new_node
179 ) {
180 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
181 }
183 void cx_linked_list_insert(
184 void **begin,
185 void **end,
186 ptrdiff_t loc_prev,
187 ptrdiff_t loc_next,
188 void *node,
189 void *new_node
190 ) {
191 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
192 }
194 void cx_linked_list_insert_chain(
195 void **begin,
196 void **end,
197 ptrdiff_t loc_prev,
198 ptrdiff_t loc_next,
199 void *node,
200 void *insert_begin,
201 void *insert_end
202 ) {
203 // find the end of the chain, if not specified
204 if (insert_end == NULL) {
205 insert_end = cx_linked_list_last(insert_begin, loc_next);
206 }
208 // determine the successor
209 void *successor;
210 if (node == NULL) {
211 assert(begin != NULL || (end != NULL && loc_prev >= 0));
212 if (begin != NULL) {
213 successor = *begin;
214 *begin = insert_begin;
215 } else {
216 successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
217 }
218 } else {
219 successor = ll_next(node);
220 cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
221 }
223 if (successor == NULL) {
224 // the list ends with the new chain
225 if (end != NULL) {
226 *end = insert_end;
227 }
228 } else {
229 cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
230 }
231 }
233 void cx_linked_list_remove(
234 void **begin,
235 void **end,
236 ptrdiff_t loc_prev,
237 ptrdiff_t loc_next,
238 void *node
239 ) {
240 assert(node != NULL);
241 assert(loc_next >= 0);
242 assert(loc_prev >= 0 || begin != NULL);
244 // find adjacent nodes
245 void *next = ll_next(node);
246 void *prev;
247 if (loc_prev >= 0) {
248 prev = ll_prev(node);
249 } else {
250 prev = cx_linked_list_prev(*begin, loc_next, node);
251 }
253 // update next pointer of prev node, or set begin
254 if (prev == NULL) {
255 if (begin != NULL) {
256 *begin = next;
257 }
258 } else {
259 ll_next(prev) = next;
260 }
262 // update prev pointer of next node, or set end
263 if (next == NULL) {
264 if (end != NULL) {
265 *end = prev;
266 }
267 } else if (loc_prev >= 0) {
268 ll_prev(next) = prev;
269 }
270 }
272 size_t cx_linked_list_size(
273 void const *node,
274 ptrdiff_t loc_next
275 ) {
276 assert(loc_next >= 0);
277 size_t size = 0;
278 while (node != NULL) {
279 node = ll_next(node);
280 size++;
281 }
282 return size;
283 }
285 static void *cx_linked_list_sort_merge(
286 ptrdiff_t loc_prev,
287 ptrdiff_t loc_next,
288 ptrdiff_t loc_data,
289 bool follow_ptr,
290 size_t length,
291 void *ls,
292 void *le,
293 void *re,
294 CxListComparator cmp_func
295 ) {
296 const size_t sbo_len = 1024;
297 void *sbo[sbo_len];
298 void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
299 if (sorted == NULL) abort();
300 void *rc, *lc;
302 lc = ls;
303 rc = le;
304 size_t n = 0;
305 while (lc && lc != le && rc != re) {
306 if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
307 sorted[n] = lc;
308 lc = ll_next(lc);
309 } else {
310 sorted[n] = rc;
311 rc = ll_next(rc);
312 }
313 n++;
314 }
315 while (lc && lc != le) {
316 sorted[n] = lc;
317 lc = ll_next(lc);
318 n++;
319 }
320 while (rc && rc != re) {
321 sorted[n] = rc;
322 rc = ll_next(rc);
323 n++;
324 }
326 // Update pointer
327 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
328 cx_for_n (i, length - 1) {
329 cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
330 }
331 ll_next(sorted[length - 1]) = NULL;
333 void *ret = sorted[0];
334 if (sorted != sbo) {
335 free(sorted);
336 }
337 return ret;
338 }
340 void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function
341 void **begin,
342 void **end,
343 ptrdiff_t loc_prev,
344 ptrdiff_t loc_next,
345 ptrdiff_t loc_data,
346 bool follow_ptr,
347 CxListComparator cmp_func
348 ) {
349 assert(begin != NULL);
350 assert(loc_next >= 0);
351 assert(loc_data >= 0);
352 assert(cmp_func);
354 void *lc, *ls, *le, *re;
356 // set start node
357 ls = *begin;
359 // check how many elements are already sorted
360 lc = ls;
361 size_t ln = 1;
362 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
363 lc = ll_next(lc);
364 ln++;
365 }
366 le = ll_next(lc);
368 // if first unsorted node is NULL, the list is already completely sorted
369 if (le != NULL) {
370 void *rc;
371 size_t rn = 1;
372 rc = le;
373 // skip already sorted elements
374 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
375 rc = ll_next(rc);
376 rn++;
377 }
378 re = ll_next(rc);
380 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
381 void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
382 ln + rn, ls, le, re, cmp_func);
384 // Something left? Sort it!
385 size_t remainder_length = cx_linked_list_size(re, loc_next);
386 if (remainder_length > 0) {
387 void *remainder = re;
388 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func);
390 // merge sorted list with (also sorted) remainder
391 *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
392 ln + rn + remainder_length,
393 sorted, remainder, NULL, cmp_func);
394 } else {
395 // no remainder - we've got our sorted list
396 *begin = sorted;
397 }
398 if (end) *end = cx_linked_list_last(sorted, loc_next);
399 }
400 }
402 int cx_linked_list_compare(
403 void const *begin_left,
404 void const *begin_right,
405 ptrdiff_t loc_advance,
406 ptrdiff_t loc_data,
407 bool follow_ptr_left,
408 bool follow_ptr_right,
409 CxListComparator cmp_func
410 ) {
411 void const *left = begin_left, *right = begin_right;
413 while (left != NULL && right != NULL) {
414 void const *left_data = ll_data_f(left, follow_ptr_left);
415 void const *right_data = ll_data_f(right, follow_ptr_right);
416 int result = cmp_func(left_data, right_data);
417 if (result != 0) return result;
418 left = ll_advance(left);
419 right = ll_advance(right);
420 }
422 if (left != NULL) { return 1; }
423 else if (right != NULL) { return -1; }
424 else { return 0; }
425 }
427 void cx_linked_list_reverse(
428 void **begin,
429 void **end,
430 ptrdiff_t loc_prev,
431 ptrdiff_t loc_next
432 ) {
433 assert(begin != NULL);
434 assert(loc_next >= 0);
436 // swap all links
437 void *prev = NULL;
438 void *cur = *begin;
439 while (cur != NULL) {
440 void *next = ll_next(cur);
442 ll_next(cur) = prev;
443 if (loc_prev >= 0) {
444 ll_prev(cur) = next;
445 }
447 prev = cur;
448 cur = next;
449 }
451 // update begin and end
452 if (end != NULL) {
453 *end = *begin;
454 }
455 *begin = prev;
456 }
458 // HIGH LEVEL LINKED LIST IMPLEMENTATION
460 typedef struct cx_linked_list_node cx_linked_list_node;
461 struct cx_linked_list_node {
462 cx_linked_list_node *prev;
463 cx_linked_list_node *next;
464 char payload[];
465 };
467 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
468 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
469 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
471 typedef struct {
472 struct cx_list_s base;
473 cx_linked_list_node *begin;
474 cx_linked_list_node *end;
475 bool follow_ptr;
476 } cx_linked_list;
478 static cx_linked_list_node *cx_ll_node_at(
479 cx_linked_list const *list,
480 size_t index
481 ) {
482 if (index >= list->base.size) {
483 return NULL;
484 } else if (index > list->base.size / 2) {
485 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
486 } else {
487 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
488 }
489 }
491 static int cx_ll_insert_at(
492 struct cx_list_s *list,
493 cx_linked_list_node *node,
494 void const *elem
495 ) {
497 // create the new new_node
498 cx_linked_list_node *new_node = cxMalloc(list->allocator,
499 sizeof(cx_linked_list_node) + list->itemsize);
501 // sortir if failed
502 if (new_node == NULL) return 1;
504 // initialize new new_node
505 new_node->prev = new_node->next = NULL;
506 memcpy(new_node->payload, elem, list->itemsize);
508 // insert
509 cx_linked_list *ll = (cx_linked_list *) list;
510 cx_linked_list_insert_chain(
511 (void **) &ll->begin, (void **) &ll->end,
512 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
513 node, new_node, new_node
514 );
516 // increase the size and return
517 list->size++;
518 return 0;
519 }
521 static int cx_ll_insert(
522 struct cx_list_s *list,
523 size_t index,
524 void const *elem
525 ) {
526 // out-of bounds check
527 if (index > list->size) return 1;
529 // find position efficiently
530 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
532 // perform insert
533 return cx_ll_insert_at(list, node, elem);
534 }
536 static int cx_ll_add(
537 struct cx_list_s *list,
538 void const *elem
539 ) {
540 return cx_ll_insert(list, list->size, elem);
541 }
543 static size_t cx_ll_add_array(
544 struct cx_list_s *list,
545 void const *array,
546 size_t n
547 ) {
548 // TODO: redirect to cx_ll_insert_array
549 cx_for_n (i, n) {
550 if (cx_ll_add(list, ((char const *) array) + i * list->itemsize)) {
551 return i;
552 }
553 }
554 return n;
555 }
557 static int cx_pll_insert(
558 struct cx_list_s *list,
559 size_t index,
560 void const *elem
561 ) {
562 return cx_ll_insert(list, index, &elem);
563 }
565 static int cx_pll_add(
566 struct cx_list_s *list,
567 void const *elem
568 ) {
569 return cx_ll_insert(list, list->size, &elem);
570 }
572 static int cx_ll_remove(
573 struct cx_list_s *list,
574 size_t index
575 ) {
576 cx_linked_list *ll = (cx_linked_list *) list;
577 cx_linked_list_node *node = cx_ll_node_at(ll, index);
579 // out-of-bounds check
580 if (node == NULL) return 1;
582 // remove
583 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
584 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
586 // adjust size
587 list->size--;
589 // free and return
590 cxFree(list->allocator, node);
592 return 0;
593 }
595 static void *cx_ll_at(
596 struct cx_list_s const *list,
597 size_t index
598 ) {
599 cx_linked_list *ll = (cx_linked_list *) list;
600 cx_linked_list_node *node = cx_ll_node_at(ll, index);
601 return node == NULL ? NULL : node->payload;
602 }
604 static void *cx_pll_at(
605 struct cx_list_s const *list,
606 size_t index
607 ) {
608 cx_linked_list *ll = (cx_linked_list *) list;
609 cx_linked_list_node *node = cx_ll_node_at(ll, index);
610 return node == NULL ? NULL : *(void **) node->payload;
611 }
613 static size_t cx_ll_find(
614 struct cx_list_s const *list,
615 void const *elem
616 ) {
617 cx_linked_list *ll = (cx_linked_list *) list;
618 return cx_linked_list_find(((cx_linked_list *) list)->begin,
619 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
620 ll->follow_ptr, list->cmpfunc, elem);
621 }
623 static void cx_ll_sort(struct cx_list_s *list) {
624 cx_linked_list *ll = (cx_linked_list *) list;
625 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
626 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
627 ll->follow_ptr, list->cmpfunc);
628 }
630 static void cx_ll_reverse(struct cx_list_s *list) {
631 cx_linked_list *ll = (cx_linked_list *) list;
632 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
633 }
635 static int cx_ll_compare(
636 struct cx_list_s const *list,
637 struct cx_list_s const *other
638 ) {
639 cx_linked_list *left = (cx_linked_list *) list;
640 cx_linked_list *right = (cx_linked_list *) other;
641 return cx_linked_list_compare(left->begin, right->begin,
642 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
643 left->follow_ptr, right->follow_ptr, list->cmpfunc);
644 }
646 static bool cx_ll_iter_valid(void const *it) {
647 struct cx_iterator_s const *iter = it;
648 return iter->elem_handle != NULL;
649 }
651 static void cx_ll_iter_next(void *it) {
652 struct cx_iterator_base_s *itbase = it;
653 if (itbase->remove) {
654 itbase->remove = false;
655 struct cx_mut_iterator_s *iter = it;
656 cx_linked_list *ll = iter->src_handle;
657 cx_linked_list_node *node = iter->elem_handle;
658 iter->elem_handle = node->next;
659 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
660 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
661 ll->base.size--;
662 cxFree(ll->base.allocator, node);
663 } else {
664 struct cx_iterator_s *iter = it;
665 iter->index++;
666 cx_linked_list_node *node = iter->elem_handle;
667 iter->elem_handle = node->next;
668 }
669 }
671 static void *cx_ll_iter_current(void const *it) {
672 struct cx_iterator_s const *iter = it;
673 cx_linked_list_node *node = iter->elem_handle;
674 return node->payload;
675 }
677 static void *cx_pll_iter_current(void const *it) {
678 struct cx_iterator_s const *iter = it;
679 cx_linked_list_node *node = iter->elem_handle;
680 return *(void **) node->payload;
681 }
683 static bool cx_ll_iter_flag_rm(void *it) {
684 struct cx_iterator_base_s *iter = it;
685 if (iter->mutating) {
686 iter->remove = true;
687 return true;
688 } else {
689 return false;
690 }
691 }
693 static CxIterator cx_ll_iterator(
694 struct cx_list_s const *list,
695 size_t index
696 ) {
697 CxIterator iter;
698 iter.index = index;
699 iter.src_handle = list;
700 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
701 iter.base.valid = cx_ll_iter_valid;
702 iter.base.current = cx_ll_iter_current;
703 iter.base.next = cx_ll_iter_next;
704 iter.base.flag_removal = cx_ll_iter_flag_rm;
705 iter.base.mutating = false;
706 iter.base.remove = false;
707 return iter;
708 }
710 static CxIterator cx_pll_iterator(
711 struct cx_list_s const *list,
712 size_t index
713 ) {
714 CxIterator iter = cx_ll_iterator(list, index);
715 iter.base.current = cx_pll_iter_current;
716 return iter;
717 }
719 static CxMutIterator cx_ll_mut_iterator(
720 struct cx_list_s *list,
721 size_t index
722 ) {
723 CxIterator it = cx_ll_iterator(list, index);
724 it.base.mutating = true;
726 // we know the iterators share the same memory layout
727 CxMutIterator iter;
728 memcpy(&iter, &it, sizeof(CxMutIterator));
729 return iter;
730 }
732 static CxMutIterator cx_pll_mut_iterator(
733 struct cx_list_s *list,
734 size_t index
735 ) {
736 CxMutIterator iter = cx_ll_mut_iterator(list, index);
737 iter.base.current = cx_pll_iter_current;
738 return iter;
739 }
741 static int cx_ll_insert_iter(
742 CxMutIterator *iter,
743 void const *elem,
744 int prepend
745 ) {
746 struct cx_list_s *list = iter->src_handle;
747 cx_linked_list_node *node = iter->elem_handle;
748 if (node != NULL) {
749 assert(prepend >= 0 && prepend <= 1);
750 cx_linked_list_node *choice[2] = {node, node->prev};
751 int result = cx_ll_insert_at(list, choice[prepend], elem);
752 iter->index += prepend * (0 == result);
753 return result;
754 } else {
755 int result = cx_ll_insert(list, list->size, elem);
756 iter->index = list->size;
757 return result;
758 }
759 }
761 static int cx_pll_insert_iter(
762 CxMutIterator *iter,
763 void const *elem,
764 int prepend
765 ) {
766 return cx_ll_insert_iter(iter, &elem, prepend);
767 }
769 static void cx_ll_destructor(CxList *list) {
770 cx_linked_list *ll = (cx_linked_list *) list;
772 cx_linked_list_node *node = ll->begin;
773 while (node) {
774 void *next = node->next;
775 cxFree(list->allocator, node);
776 node = next;
777 }
778 // do not free the list pointer, this is just a destructor!
779 }
781 static cx_list_class cx_linked_list_class = {
782 cx_ll_destructor,
783 cx_ll_add,
784 cx_ll_add_array,
785 cx_ll_insert,
786 cx_ll_insert_iter,
787 cx_ll_remove,
788 cx_ll_at,
789 cx_ll_find,
790 cx_ll_sort,
791 cx_ll_compare,
792 cx_ll_reverse,
793 cx_ll_iterator,
794 cx_ll_mut_iterator,
795 };
797 static cx_list_class cx_pointer_linked_list_class = {
798 cx_ll_destructor,
799 cx_pll_add,
800 cx_ll_add_array,
801 cx_pll_insert,
802 cx_pll_insert_iter,
803 cx_ll_remove,
804 cx_pll_at,
805 cx_ll_find,
806 cx_ll_sort,
807 cx_ll_compare,
808 cx_ll_reverse,
809 cx_pll_iterator,
810 cx_pll_mut_iterator,
811 };
813 CxList *cxLinkedListCreate(
814 CxAllocator const *allocator,
815 CxListComparator comparator,
816 size_t item_size
817 ) {
818 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
819 if (list == NULL) return NULL;
821 list->follow_ptr = false;
822 list->base.cl = &cx_linked_list_class;
823 list->base.allocator = allocator;
824 list->base.cmpfunc = comparator;
825 list->base.itemsize = item_size;
826 list->base.capacity = SIZE_MAX;
828 return (CxList *) list;
829 }
831 CxList *cxPointerLinkedListCreate(
832 CxAllocator const *allocator,
833 CxListComparator comparator
834 ) {
835 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
836 if (list == NULL) return NULL;
838 list->follow_ptr = true;
839 list->base.cl = &cx_pointer_linked_list_class;
840 list->base.allocator = allocator;
841 list->base.cmpfunc = comparator;
842 list->base.itemsize = sizeof(void *);
843 list->base.capacity = SIZE_MAX;
845 return (CxList *) list;
846 }