Mon, 18 Dec 2023 16:14:07 +0100
make cx_cmp_ptr default comparator for pointer lists - relates to #340
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 "cx/compare.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(node) (((char*)(node))+loc_data)
43 void *cx_linked_list_at(
44 void const *start,
45 size_t start_index,
46 ptrdiff_t loc_advance,
47 size_t index
48 ) {
49 assert(start != NULL);
50 assert(loc_advance >= 0);
51 size_t i = start_index;
52 void const *cur = start;
53 while (i != index && cur != NULL) {
54 cur = ll_advance(cur);
55 i < index ? i++ : i--;
56 }
57 return (void *) cur;
58 }
60 ssize_t cx_linked_list_find(
61 void const *start,
62 ptrdiff_t loc_advance,
63 ptrdiff_t loc_data,
64 cx_compare_func cmp_func,
65 void const *elem
66 ) {
67 assert(start != NULL);
68 assert(loc_advance >= 0);
69 assert(loc_data >= 0);
70 assert(cmp_func);
72 void const *node = start;
73 ssize_t index = 0;
74 do {
75 void *current = ll_data(node);
76 if (cmp_func(current, elem) == 0) {
77 return index;
78 }
79 node = ll_advance(node);
80 index++;
81 } while (node != NULL);
82 return -1;
83 }
85 void *cx_linked_list_first(
86 void const *node,
87 ptrdiff_t loc_prev
88 ) {
89 return cx_linked_list_last(node, loc_prev);
90 }
92 void *cx_linked_list_last(
93 void const *node,
94 ptrdiff_t loc_next
95 ) {
96 assert(node != NULL);
97 assert(loc_next >= 0);
99 void const *cur = node;
100 void const *last;
101 do {
102 last = cur;
103 } while ((cur = ll_next(cur)) != NULL);
105 return (void *) last;
106 }
108 void *cx_linked_list_prev(
109 void const *begin,
110 ptrdiff_t loc_next,
111 void const *node
112 ) {
113 assert(begin != NULL);
114 assert(node != NULL);
115 assert(loc_next >= 0);
116 if (begin == node) return NULL;
117 void const *cur = begin;
118 void const *next;
119 while (1) {
120 next = ll_next(cur);
121 if (next == node) return (void *) cur;
122 cur = next;
123 }
124 }
126 void cx_linked_list_link(
127 void *left,
128 void *right,
129 ptrdiff_t loc_prev,
130 ptrdiff_t loc_next
131 ) {
132 assert(loc_next >= 0);
133 ll_next(left) = right;
134 if (loc_prev >= 0) {
135 ll_prev(right) = left;
136 }
137 }
139 void cx_linked_list_unlink(
140 void *left,
141 void *right,
142 ptrdiff_t loc_prev,
143 ptrdiff_t loc_next
144 ) {
145 assert (loc_next >= 0);
146 assert(ll_next(left) == right);
147 ll_next(left) = NULL;
148 if (loc_prev >= 0) {
149 assert(ll_prev(right) == left);
150 ll_prev(right) = NULL;
151 }
152 }
154 void cx_linked_list_add(
155 void **begin,
156 void **end,
157 ptrdiff_t loc_prev,
158 ptrdiff_t loc_next,
159 void *new_node
160 ) {
161 void *last;
162 if (end == NULL) {
163 assert(begin != NULL);
164 last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
165 } else {
166 last = *end;
167 }
168 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
169 }
171 void cx_linked_list_prepend(
172 void **begin,
173 void **end,
174 ptrdiff_t loc_prev,
175 ptrdiff_t loc_next,
176 void *new_node
177 ) {
178 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
179 }
181 void cx_linked_list_insert(
182 void **begin,
183 void **end,
184 ptrdiff_t loc_prev,
185 ptrdiff_t loc_next,
186 void *node,
187 void *new_node
188 ) {
189 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
190 }
192 void cx_linked_list_insert_chain(
193 void **begin,
194 void **end,
195 ptrdiff_t loc_prev,
196 ptrdiff_t loc_next,
197 void *node,
198 void *insert_begin,
199 void *insert_end
200 ) {
201 // find the end of the chain, if not specified
202 if (insert_end == NULL) {
203 insert_end = cx_linked_list_last(insert_begin, loc_next);
204 }
206 // determine the successor
207 void *successor;
208 if (node == NULL) {
209 assert(begin != NULL || (end != NULL && loc_prev >= 0));
210 if (begin != NULL) {
211 successor = *begin;
212 *begin = insert_begin;
213 } else {
214 successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
215 }
216 } else {
217 successor = ll_next(node);
218 cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
219 }
221 if (successor == NULL) {
222 // the list ends with the new chain
223 if (end != NULL) {
224 *end = insert_end;
225 }
226 } else {
227 cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
228 }
229 }
231 void cx_linked_list_remove(
232 void **begin,
233 void **end,
234 ptrdiff_t loc_prev,
235 ptrdiff_t loc_next,
236 void *node
237 ) {
238 assert(node != NULL);
239 assert(loc_next >= 0);
240 assert(loc_prev >= 0 || begin != NULL);
242 // find adjacent nodes
243 void *next = ll_next(node);
244 void *prev;
245 if (loc_prev >= 0) {
246 prev = ll_prev(node);
247 } else {
248 prev = cx_linked_list_prev(*begin, loc_next, node);
249 }
251 // update next pointer of prev node, or set begin
252 if (prev == NULL) {
253 if (begin != NULL) {
254 *begin = next;
255 }
256 } else {
257 ll_next(prev) = next;
258 }
260 // update prev pointer of next node, or set end
261 if (next == NULL) {
262 if (end != NULL) {
263 *end = prev;
264 }
265 } else if (loc_prev >= 0) {
266 ll_prev(next) = prev;
267 }
268 }
270 size_t cx_linked_list_size(
271 void const *node,
272 ptrdiff_t loc_next
273 ) {
274 assert(loc_next >= 0);
275 size_t size = 0;
276 while (node != NULL) {
277 node = ll_next(node);
278 size++;
279 }
280 return size;
281 }
283 #ifndef CX_LINKED_LIST_SORT_SBO_SIZE
284 #define CX_LINKED_LIST_SORT_SBO_SIZE 1024
285 #endif
287 static void cx_linked_list_sort_merge(
288 ptrdiff_t loc_prev,
289 ptrdiff_t loc_next,
290 ptrdiff_t loc_data,
291 size_t length,
292 void *ls,
293 void *le,
294 void *re,
295 cx_compare_func cmp_func,
296 void **begin,
297 void **end
298 ) {
299 void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE];
300 void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ?
301 malloc(sizeof(void *) * length) : sbo;
302 if (sorted == NULL) abort();
303 void *rc, *lc;
305 lc = ls;
306 rc = le;
307 size_t n = 0;
308 while (lc && lc != le && rc != re) {
309 if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
310 sorted[n] = lc;
311 lc = ll_next(lc);
312 } else {
313 sorted[n] = rc;
314 rc = ll_next(rc);
315 }
316 n++;
317 }
318 while (lc && lc != le) {
319 sorted[n] = lc;
320 lc = ll_next(lc);
321 n++;
322 }
323 while (rc && rc != re) {
324 sorted[n] = rc;
325 rc = ll_next(rc);
326 n++;
327 }
329 // Update pointer
330 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
331 cx_for_n (i, length - 1) {
332 cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
333 }
334 ll_next(sorted[length - 1]) = NULL;
336 *begin = sorted[0];
337 *end = sorted[length-1];
338 if (sorted != sbo) {
339 free(sorted);
340 }
341 }
343 void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function
344 void **begin,
345 void **end,
346 ptrdiff_t loc_prev,
347 ptrdiff_t loc_next,
348 ptrdiff_t loc_data,
349 cx_compare_func cmp_func
350 ) {
351 assert(begin != NULL);
352 assert(loc_next >= 0);
353 assert(loc_data >= 0);
354 assert(cmp_func);
356 void *lc, *ls, *le, *re;
358 // set start node
359 ls = *begin;
361 // early exit when this list is empty
362 if (ls == NULL) return;
364 // check how many elements are already sorted
365 lc = ls;
366 size_t ln = 1;
367 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
368 lc = ll_next(lc);
369 ln++;
370 }
371 le = ll_next(lc);
373 // if first unsorted node is NULL, the list is already completely sorted
374 if (le != NULL) {
375 void *rc;
376 size_t rn = 1;
377 rc = le;
378 // skip already sorted elements
379 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
380 rc = ll_next(rc);
381 rn++;
382 }
383 re = ll_next(rc);
385 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
386 void *sorted_begin, *sorted_end;
387 cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
388 ln + rn, ls, le, re, cmp_func,
389 &sorted_begin, &sorted_end);
391 // Something left? Sort it!
392 size_t remainder_length = cx_linked_list_size(re, loc_next);
393 if (remainder_length > 0) {
394 void *remainder = re;
395 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func);
397 // merge sorted list with (also sorted) remainder
398 cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
399 ln + rn + remainder_length,
400 sorted_begin, remainder, NULL, cmp_func,
401 &sorted_begin, &sorted_end);
402 }
403 *begin = sorted_begin;
404 if (end) *end = sorted_end;
405 }
406 }
408 int cx_linked_list_compare(
409 void const *begin_left,
410 void const *begin_right,
411 ptrdiff_t loc_advance,
412 ptrdiff_t loc_data,
413 cx_compare_func cmp_func
414 ) {
415 void const *left = begin_left, *right = begin_right;
417 while (left != NULL && right != NULL) {
418 void const *left_data = ll_data(left);
419 void const *right_data = ll_data(right);
420 int result = cmp_func(left_data, right_data);
421 if (result != 0) return result;
422 left = ll_advance(left);
423 right = ll_advance(right);
424 }
426 if (left != NULL) { return 1; }
427 else if (right != NULL) { return -1; }
428 else { return 0; }
429 }
431 void cx_linked_list_reverse(
432 void **begin,
433 void **end,
434 ptrdiff_t loc_prev,
435 ptrdiff_t loc_next
436 ) {
437 assert(begin != NULL);
438 assert(loc_next >= 0);
440 // swap all links
441 void *prev = NULL;
442 void *cur = *begin;
443 while (cur != NULL) {
444 void *next = ll_next(cur);
446 ll_next(cur) = prev;
447 if (loc_prev >= 0) {
448 ll_prev(cur) = next;
449 }
451 prev = cur;
452 cur = next;
453 }
455 // update begin and end
456 if (end != NULL) {
457 *end = *begin;
458 }
459 *begin = prev;
460 }
462 // HIGH LEVEL LINKED LIST IMPLEMENTATION
464 bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
466 typedef struct cx_linked_list_node cx_linked_list_node;
467 struct cx_linked_list_node {
468 cx_linked_list_node *prev;
469 cx_linked_list_node *next;
470 char payload[];
471 };
473 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
474 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
475 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
477 typedef struct {
478 struct cx_list_s base;
479 cx_linked_list_node *begin;
480 cx_linked_list_node *end;
481 } cx_linked_list;
483 static cx_linked_list_node *cx_ll_node_at(
484 cx_linked_list const *list,
485 size_t index
486 ) {
487 if (index >= list->base.size) {
488 return NULL;
489 } else if (index > list->base.size / 2) {
490 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
491 } else {
492 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
493 }
494 }
496 static int cx_ll_insert_at(
497 struct cx_list_s *list,
498 cx_linked_list_node *node,
499 void const *elem
500 ) {
502 // create the new new_node
503 cx_linked_list_node *new_node = cxMalloc(list->allocator,
504 sizeof(cx_linked_list_node) + list->item_size);
506 // sortir if failed
507 if (new_node == NULL) return 1;
509 // initialize new new_node
510 new_node->prev = new_node->next = NULL;
511 memcpy(new_node->payload, elem, list->item_size);
513 // insert
514 cx_linked_list *ll = (cx_linked_list *) list;
515 cx_linked_list_insert_chain(
516 (void **) &ll->begin, (void **) &ll->end,
517 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
518 node, new_node, new_node
519 );
521 // increase the size and return
522 list->size++;
523 return 0;
524 }
526 static size_t cx_ll_insert_array(
527 struct cx_list_s *list,
528 size_t index,
529 void const *array,
530 size_t n
531 ) {
532 // out-of bounds and corner case check
533 if (index > list->size || n == 0) return 0;
535 // find position efficiently
536 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
538 // perform first insert
539 if (0 != cx_ll_insert_at(list, node, array)) {
540 return 1;
541 }
543 // is there more?
544 if (n == 1) return 1;
546 // we now know exactly where we are
547 node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
549 // we can add the remaining nodes and immedately advance to the inserted node
550 char const *source = array;
551 for (size_t i = 1; i < n; i++) {
552 source += list->item_size;
553 if (0 != cx_ll_insert_at(list, node, source)) {
554 return i;
555 }
556 node = node->next;
557 }
558 return n;
559 }
561 static int cx_ll_insert_element(
562 struct cx_list_s *list,
563 size_t index,
564 void const *element
565 ) {
566 return 1 != cx_ll_insert_array(list, index, element, 1);
567 }
569 static int cx_ll_remove(
570 struct cx_list_s *list,
571 size_t index
572 ) {
573 cx_linked_list *ll = (cx_linked_list *) list;
574 cx_linked_list_node *node = cx_ll_node_at(ll, index);
576 // out-of-bounds check
577 if (node == NULL) return 1;
579 // element destruction
580 cx_invoke_destructor(list, node->payload);
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_clear(struct cx_list_s *list) {
596 if (list->size == 0) return;
598 cx_linked_list *ll = (cx_linked_list *) list;
599 cx_linked_list_node *node = ll->begin;
600 while (node != NULL) {
601 cx_invoke_destructor(list, node->payload);
602 cx_linked_list_node *next = node->next;
603 cxFree(list->allocator, node);
604 node = next;
605 }
606 ll->begin = ll->end = NULL;
607 list->size = 0;
608 }
610 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
611 #define CX_LINKED_LIST_SWAP_SBO_SIZE 128
612 #endif
614 static int cx_ll_swap(
615 struct cx_list_s *list,
616 size_t i,
617 size_t j
618 ) {
619 if (i >= list->size || j >= list->size) return 1;
620 if (i == j) return 0;
622 // perform an optimized search that finds both elements in one run
623 cx_linked_list *ll = (cx_linked_list *) list;
624 size_t mid = list->size / 2;
625 size_t left, right;
626 if (i < j) {
627 left = i;
628 right = j;
629 } else {
630 left = j;
631 right = i;
632 }
633 cx_linked_list_node *nleft, *nright;
634 if (left < mid && right < mid) {
635 // case 1: both items left from mid
636 nleft = cx_ll_node_at(ll, left);
637 nright = nleft;
638 for (size_t c = left; c < right; c++) {
639 nright = nright->next;
640 }
641 } else if (left >= mid && right >= mid) {
642 // case 2: both items right from mid
643 nright = cx_ll_node_at(ll, right);
644 nleft = nright;
645 for (size_t c = right; c > left; c--) {
646 nleft = nleft->prev;
647 }
648 } else {
649 // case 3: one item left, one item right
651 // chose the closest to begin / end
652 size_t closest;
653 size_t other;
654 size_t diff2boundary = list->size - right - 1;
655 if (left <= diff2boundary) {
656 closest = left;
657 other = right;
658 nleft = cx_ll_node_at(ll, left);
659 } else {
660 closest = right;
661 other = left;
662 diff2boundary = left;
663 nright = cx_ll_node_at(ll, right);
664 }
666 // is other element closer to us or closer to boundary?
667 if (right - left <= diff2boundary) {
668 // search other element starting from already found element
669 if (closest == left) {
670 nright = nleft;
671 for (size_t c = left; c < right; c++) {
672 nright = nright->next;
673 }
674 } else {
675 nleft = nright;
676 for (size_t c = right; c > left; c--) {
677 nleft = nleft->prev;
678 }
679 }
680 } else {
681 // search other element starting at the boundary
682 if (closest == left) {
683 nright = cx_ll_node_at(ll, other);
684 } else {
685 nleft = cx_ll_node_at(ll, other);
686 }
687 }
688 }
690 if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
691 cx_linked_list_node *prev = nleft->prev;
692 cx_linked_list_node *next = nright->next;
693 cx_linked_list_node *midstart = nleft->next;
694 cx_linked_list_node *midend = nright->prev;
696 if (prev == NULL) {
697 ll->begin = nright;
698 } else {
699 prev->next = nright;
700 }
701 nright->prev = prev;
702 if (midstart == nright) {
703 // special case: both nodes are adjacent
704 nright->next = nleft;
705 nleft->prev = nright;
706 } else {
707 // likely case: a chain is between the two nodes
708 nright->next = midstart;
709 midstart->prev = nright;
710 midend->next = nleft;
711 nleft->prev = midend;
712 }
713 nleft->next = next;
714 if (next == NULL) {
715 ll->end = nleft;
716 } else {
717 next->prev = nleft;
718 }
719 } else {
720 // swap payloads to avoid relinking
721 char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
722 memcpy(buf, nleft->payload, list->item_size);
723 memcpy(nleft->payload, nright->payload, list->item_size);
724 memcpy(nright->payload, buf, list->item_size);
725 }
727 return 0;
728 }
730 static void *cx_ll_at(
731 struct cx_list_s const *list,
732 size_t index
733 ) {
734 cx_linked_list *ll = (cx_linked_list *) list;
735 cx_linked_list_node *node = cx_ll_node_at(ll, index);
736 return node == NULL ? NULL : node->payload;
737 }
739 static ssize_t cx_ll_find(
740 struct cx_list_s const *list,
741 void const *elem
742 ) {
743 return cx_linked_list_find(((cx_linked_list *) list)->begin,
744 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
745 list->cmpfunc, elem);
746 }
748 static void cx_ll_sort(struct cx_list_s *list) {
749 cx_linked_list *ll = (cx_linked_list *) list;
750 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
751 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
752 list->cmpfunc);
753 }
755 static void cx_ll_reverse(struct cx_list_s *list) {
756 cx_linked_list *ll = (cx_linked_list *) list;
757 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
758 }
760 static int cx_ll_compare(
761 struct cx_list_s const *list,
762 struct cx_list_s const *other
763 ) {
764 cx_linked_list *left = (cx_linked_list *) list;
765 cx_linked_list *right = (cx_linked_list *) other;
766 return cx_linked_list_compare(left->begin, right->begin,
767 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
768 list->cmpfunc);
769 }
771 static bool cx_ll_iter_valid(void const *it) {
772 struct cx_iterator_s const *iter = it;
773 return iter->elem_handle != NULL;
774 }
776 static void cx_ll_iter_next(void *it) {
777 struct cx_iterator_base_s *itbase = it;
778 if (itbase->remove) {
779 itbase->remove = false;
780 struct cx_mut_iterator_s *iter = it;
781 struct cx_list_s *list = iter->src_handle;
782 cx_linked_list *ll = iter->src_handle;
783 cx_linked_list_node *node = iter->elem_handle;
784 iter->elem_handle = node->next;
785 cx_invoke_destructor(list, node->payload);
786 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
787 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
788 list->size--;
789 cxFree(list->allocator, node);
790 } else {
791 struct cx_iterator_s *iter = it;
792 iter->index++;
793 cx_linked_list_node *node = iter->elem_handle;
794 iter->elem_handle = node->next;
795 }
796 }
798 static void cx_ll_iter_prev(void *it) {
799 struct cx_iterator_base_s *itbase = it;
800 if (itbase->remove) {
801 itbase->remove = false;
802 struct cx_mut_iterator_s *iter = it;
803 struct cx_list_s *list = iter->src_handle;
804 cx_linked_list *ll = iter->src_handle;
805 cx_linked_list_node *node = iter->elem_handle;
806 iter->elem_handle = node->prev;
807 iter->index--;
808 cx_invoke_destructor(list, node->payload);
809 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
810 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
811 list->size--;
812 cxFree(list->allocator, node);
813 } else {
814 struct cx_iterator_s *iter = it;
815 iter->index--;
816 cx_linked_list_node *node = iter->elem_handle;
817 iter->elem_handle = node->prev;
818 }
819 }
821 static void *cx_ll_iter_current(void const *it) {
822 struct cx_iterator_s const *iter = it;
823 cx_linked_list_node *node = iter->elem_handle;
824 return node->payload;
825 }
827 static bool cx_ll_iter_flag_rm(void *it) {
828 struct cx_iterator_base_s *iter = it;
829 if (iter->mutating) {
830 iter->remove = true;
831 return true;
832 } else {
833 return false;
834 }
835 }
837 static CxIterator cx_ll_iterator(
838 struct cx_list_s const *list,
839 size_t index,
840 bool backwards
841 ) {
842 CxIterator iter;
843 iter.index = index;
844 iter.src_handle = list;
845 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
846 iter.base.valid = cx_ll_iter_valid;
847 iter.base.current = cx_ll_iter_current;
848 iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
849 iter.base.flag_removal = cx_ll_iter_flag_rm;
850 iter.base.mutating = false;
851 iter.base.remove = false;
852 return iter;
853 }
855 static int cx_ll_insert_iter(
856 CxMutIterator *iter,
857 void const *elem,
858 int prepend
859 ) {
860 struct cx_list_s *list = iter->src_handle;
861 cx_linked_list_node *node = iter->elem_handle;
862 if (node != NULL) {
863 assert(prepend >= 0 && prepend <= 1);
864 cx_linked_list_node *choice[2] = {node, node->prev};
865 int result = cx_ll_insert_at(list, choice[prepend], elem);
866 iter->index += prepend * (0 == result);
867 return result;
868 } else {
869 int result = cx_ll_insert_element(list, list->size, elem);
870 iter->index = list->size;
871 return result;
872 }
873 }
875 static void cx_ll_destructor(CxList *list) {
876 cx_linked_list *ll = (cx_linked_list *) list;
878 cx_linked_list_node *node = ll->begin;
879 while (node) {
880 cx_invoke_destructor(list, node->payload);
881 void *next = node->next;
882 cxFree(list->allocator, node);
883 node = next;
884 }
886 cxFree(list->allocator, list);
887 }
889 static cx_list_class cx_linked_list_class = {
890 cx_ll_destructor,
891 cx_ll_insert_element,
892 cx_ll_insert_array,
893 cx_ll_insert_iter,
894 cx_ll_remove,
895 cx_ll_clear,
896 cx_ll_swap,
897 cx_ll_at,
898 cx_ll_find,
899 cx_ll_sort,
900 cx_ll_compare,
901 cx_ll_reverse,
902 cx_ll_iterator,
903 };
905 CxList *cxLinkedListCreate(
906 CxAllocator const *allocator,
907 cx_compare_func comparator,
908 size_t item_size
909 ) {
910 if (allocator == NULL) {
911 allocator = cxDefaultAllocator;
912 }
914 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
915 if (list == NULL) return NULL;
917 list->base.cl = &cx_linked_list_class;
918 list->base.allocator = allocator;
920 if (item_size > 0) {
921 list->base.item_size = item_size;
922 list->base.cmpfunc = comparator;
923 } else {
924 list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
925 cxListStorePointers((CxList *) list);
926 }
928 return (CxList *) list;
929 }