Fri, 05 May 2023 19:07:56 +0200
fix cx_linked_list_sort() not working for empty lists
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 <string.h>
32 #include <assert.h>
34 // LOW LEVEL LINKED LIST FUNCTIONS
36 #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
37 #define ll_prev(node) CX_LL_PTR(node, loc_prev)
38 #define ll_next(node) CX_LL_PTR(node, loc_next)
39 #define ll_advance(node) CX_LL_PTR(node, loc_advance)
40 #define ll_data(node) (((char*)(node))+loc_data)
42 void *cx_linked_list_at(
43 void const *start,
44 size_t start_index,
45 ptrdiff_t loc_advance,
46 size_t index
47 ) {
48 assert(start != NULL);
49 assert(loc_advance >= 0);
50 size_t i = start_index;
51 void const *cur = start;
52 while (i != index && cur != NULL) {
53 cur = ll_advance(cur);
54 i < index ? i++ : i--;
55 }
56 return (void *) cur;
57 }
59 ssize_t cx_linked_list_find(
60 void const *start,
61 ptrdiff_t loc_advance,
62 ptrdiff_t loc_data,
63 cx_compare_func cmp_func,
64 void const *elem
65 ) {
66 assert(start != NULL);
67 assert(loc_advance >= 0);
68 assert(loc_data >= 0);
69 assert(cmp_func);
71 void const *node = start;
72 ssize_t index = 0;
73 do {
74 void *current = ll_data(node);
75 if (cmp_func(current, elem) == 0) {
76 return index;
77 }
78 node = ll_advance(node);
79 index++;
80 } while (node != NULL);
81 return -1;
82 }
84 void *cx_linked_list_first(
85 void const *node,
86 ptrdiff_t loc_prev
87 ) {
88 return cx_linked_list_last(node, loc_prev);
89 }
91 void *cx_linked_list_last(
92 void const *node,
93 ptrdiff_t loc_next
94 ) {
95 assert(node != NULL);
96 assert(loc_next >= 0);
98 void const *cur = node;
99 void const *last;
100 do {
101 last = cur;
102 } while ((cur = ll_next(cur)) != NULL);
104 return (void *) last;
105 }
107 void *cx_linked_list_prev(
108 void const *begin,
109 ptrdiff_t loc_next,
110 void const *node
111 ) {
112 assert(begin != NULL);
113 assert(node != NULL);
114 assert(loc_next >= 0);
115 if (begin == node) return NULL;
116 void const *cur = begin;
117 void const *next;
118 while (1) {
119 next = ll_next(cur);
120 if (next == node) return (void *) cur;
121 cur = next;
122 }
123 }
125 void cx_linked_list_link(
126 void *left,
127 void *right,
128 ptrdiff_t loc_prev,
129 ptrdiff_t loc_next
130 ) {
131 assert(loc_next >= 0);
132 ll_next(left) = right;
133 if (loc_prev >= 0) {
134 ll_prev(right) = left;
135 }
136 }
138 void cx_linked_list_unlink(
139 void *left,
140 void *right,
141 ptrdiff_t loc_prev,
142 ptrdiff_t loc_next
143 ) {
144 assert (loc_next >= 0);
145 assert(ll_next(left) == right);
146 ll_next(left) = NULL;
147 if (loc_prev >= 0) {
148 assert(ll_prev(right) == left);
149 ll_prev(right) = NULL;
150 }
151 }
153 void cx_linked_list_add(
154 void **begin,
155 void **end,
156 ptrdiff_t loc_prev,
157 ptrdiff_t loc_next,
158 void *new_node
159 ) {
160 void *last;
161 if (end == NULL) {
162 assert(begin != NULL);
163 last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
164 } else {
165 last = *end;
166 }
167 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
168 }
170 void cx_linked_list_prepend(
171 void **begin,
172 void **end,
173 ptrdiff_t loc_prev,
174 ptrdiff_t loc_next,
175 void *new_node
176 ) {
177 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
178 }
180 void cx_linked_list_insert(
181 void **begin,
182 void **end,
183 ptrdiff_t loc_prev,
184 ptrdiff_t loc_next,
185 void *node,
186 void *new_node
187 ) {
188 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
189 }
191 void cx_linked_list_insert_chain(
192 void **begin,
193 void **end,
194 ptrdiff_t loc_prev,
195 ptrdiff_t loc_next,
196 void *node,
197 void *insert_begin,
198 void *insert_end
199 ) {
200 // find the end of the chain, if not specified
201 if (insert_end == NULL) {
202 insert_end = cx_linked_list_last(insert_begin, loc_next);
203 }
205 // determine the successor
206 void *successor;
207 if (node == NULL) {
208 assert(begin != NULL || (end != NULL && loc_prev >= 0));
209 if (begin != NULL) {
210 successor = *begin;
211 *begin = insert_begin;
212 } else {
213 successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
214 }
215 } else {
216 successor = ll_next(node);
217 cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
218 }
220 if (successor == NULL) {
221 // the list ends with the new chain
222 if (end != NULL) {
223 *end = insert_end;
224 }
225 } else {
226 cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
227 }
228 }
230 void cx_linked_list_remove(
231 void **begin,
232 void **end,
233 ptrdiff_t loc_prev,
234 ptrdiff_t loc_next,
235 void *node
236 ) {
237 assert(node != NULL);
238 assert(loc_next >= 0);
239 assert(loc_prev >= 0 || begin != NULL);
241 // find adjacent nodes
242 void *next = ll_next(node);
243 void *prev;
244 if (loc_prev >= 0) {
245 prev = ll_prev(node);
246 } else {
247 prev = cx_linked_list_prev(*begin, loc_next, node);
248 }
250 // update next pointer of prev node, or set begin
251 if (prev == NULL) {
252 if (begin != NULL) {
253 *begin = next;
254 }
255 } else {
256 ll_next(prev) = next;
257 }
259 // update prev pointer of next node, or set end
260 if (next == NULL) {
261 if (end != NULL) {
262 *end = prev;
263 }
264 } else if (loc_prev >= 0) {
265 ll_prev(next) = prev;
266 }
267 }
269 size_t cx_linked_list_size(
270 void const *node,
271 ptrdiff_t loc_next
272 ) {
273 assert(loc_next >= 0);
274 size_t size = 0;
275 while (node != NULL) {
276 node = ll_next(node);
277 size++;
278 }
279 return size;
280 }
282 #ifndef CX_LINKED_LIST_SORT_SBO_SIZE
283 #define CX_LINKED_LIST_SORT_SBO_SIZE 1024
284 #endif
286 static void *cx_linked_list_sort_merge(
287 ptrdiff_t loc_prev,
288 ptrdiff_t loc_next,
289 ptrdiff_t loc_data,
290 size_t length,
291 void *ls,
292 void *le,
293 void *re,
294 cx_compare_func cmp_func
295 ) {
296 void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE];
297 void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ?
298 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 cx_compare_func cmp_func
347 ) {
348 assert(begin != NULL);
349 assert(loc_next >= 0);
350 assert(loc_data >= 0);
351 assert(cmp_func);
353 void *lc, *ls, *le, *re;
355 // set start node
356 ls = *begin;
358 // early exit when this list is empty
359 if (ls == NULL) return;
361 // check how many elements are already sorted
362 lc = ls;
363 size_t ln = 1;
364 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
365 lc = ll_next(lc);
366 ln++;
367 }
368 le = ll_next(lc);
370 // if first unsorted node is NULL, the list is already completely sorted
371 if (le != NULL) {
372 void *rc;
373 size_t rn = 1;
374 rc = le;
375 // skip already sorted elements
376 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
377 rc = ll_next(rc);
378 rn++;
379 }
380 re = ll_next(rc);
382 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
383 void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
384 ln + rn, ls, le, re, cmp_func);
386 // Something left? Sort it!
387 size_t remainder_length = cx_linked_list_size(re, loc_next);
388 if (remainder_length > 0) {
389 void *remainder = re;
390 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func);
392 // merge sorted list with (also sorted) remainder
393 *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
394 ln + rn + remainder_length,
395 sorted, remainder, NULL, cmp_func);
396 } else {
397 // no remainder - we've got our sorted list
398 *begin = sorted;
399 }
400 if (end) *end = cx_linked_list_last(sorted, loc_next);
401 }
402 }
404 int cx_linked_list_compare(
405 void const *begin_left,
406 void const *begin_right,
407 ptrdiff_t loc_advance,
408 ptrdiff_t loc_data,
409 cx_compare_func 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(left);
415 void const *right_data = ll_data(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 bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
462 typedef struct cx_linked_list_node cx_linked_list_node;
463 struct cx_linked_list_node {
464 cx_linked_list_node *prev;
465 cx_linked_list_node *next;
466 char payload[];
467 };
469 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
470 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
471 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
473 typedef struct {
474 struct cx_list_s base;
475 cx_linked_list_node *begin;
476 cx_linked_list_node *end;
477 } cx_linked_list;
479 static cx_linked_list_node *cx_ll_node_at(
480 cx_linked_list const *list,
481 size_t index
482 ) {
483 if (index >= list->base.size) {
484 return NULL;
485 } else if (index > list->base.size / 2) {
486 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
487 } else {
488 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
489 }
490 }
492 static int cx_ll_insert_at(
493 struct cx_list_s *list,
494 cx_linked_list_node *node,
495 void const *elem
496 ) {
498 // create the new new_node
499 cx_linked_list_node *new_node = cxMalloc(list->allocator,
500 sizeof(cx_linked_list_node) + list->item_size);
502 // sortir if failed
503 if (new_node == NULL) return 1;
505 // initialize new new_node
506 new_node->prev = new_node->next = NULL;
507 memcpy(new_node->payload, elem, list->item_size);
509 // insert
510 cx_linked_list *ll = (cx_linked_list *) list;
511 cx_linked_list_insert_chain(
512 (void **) &ll->begin, (void **) &ll->end,
513 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
514 node, new_node, new_node
515 );
517 // increase the size and return
518 list->size++;
519 return 0;
520 }
522 static size_t cx_ll_insert_array(
523 struct cx_list_s *list,
524 size_t index,
525 void const *array,
526 size_t n
527 ) {
528 // out-of bounds and corner case check
529 if (index > list->size || n == 0) return 0;
531 // find position efficiently
532 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
534 // perform first insert
535 if (0 != cx_ll_insert_at(list, node, array)) {
536 return 1;
537 }
539 // is there more?
540 if (n == 1) return 1;
542 // we now know exactly where we are
543 node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
545 // we can add the remaining nodes and immedately advance to the inserted node
546 char const *source = array;
547 for (size_t i = 1; i < n; i++) {
548 source += list->item_size;
549 if (0 != cx_ll_insert_at(list, node, source)) {
550 return i;
551 }
552 node = node->next;
553 }
554 return n;
555 }
557 static int cx_ll_insert_element(
558 struct cx_list_s *list,
559 size_t index,
560 void const *element
561 ) {
562 return 1 != cx_ll_insert_array(list, index, element, 1);
563 }
565 static int cx_ll_remove(
566 struct cx_list_s *list,
567 size_t index
568 ) {
569 cx_linked_list *ll = (cx_linked_list *) list;
570 cx_linked_list_node *node = cx_ll_node_at(ll, index);
572 // out-of-bounds check
573 if (node == NULL) return 1;
575 // element destruction
576 cx_invoke_destructor(list, node->payload);
578 // remove
579 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
580 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
582 // adjust size
583 list->size--;
585 // free and return
586 cxFree(list->allocator, node);
588 return 0;
589 }
591 static void cx_ll_clear(struct cx_list_s *list) {
592 if (list->size == 0) return;
594 cx_linked_list *ll = (cx_linked_list *) list;
595 cx_linked_list_node *node = ll->begin;
596 while (node != NULL) {
597 cx_invoke_destructor(list, node->payload);
598 cx_linked_list_node *next = node->next;
599 cxFree(list->allocator, node);
600 node = next;
601 }
602 ll->begin = ll->end = NULL;
603 list->size = 0;
604 }
606 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
607 #define CX_LINKED_LIST_SWAP_SBO_SIZE 16
608 #endif
610 static int cx_ll_swap(
611 struct cx_list_s *list,
612 size_t i,
613 size_t j
614 ) {
615 if (i >= list->size || j >= list->size) return 1;
616 if (i == j) return 0;
618 // perform an optimized search that finds both elements in one run
619 cx_linked_list *ll = (cx_linked_list *) list;
620 size_t mid = list->size / 2;
621 size_t left, right;
622 if (i < j) {
623 left = i;
624 right = j;
625 } else {
626 left = j;
627 right = i;
628 }
629 cx_linked_list_node *nleft, *nright;
630 if (left < mid && right < mid) {
631 // case 1: both items left from mid
632 nleft = cx_ll_node_at(ll, left);
633 nright = nleft;
634 for (size_t c = left; c < right; c++) {
635 nright = nright->next;
636 }
637 } else if (left >= mid && right >= mid) {
638 // case 2: both items right from mid
639 nright = cx_ll_node_at(ll, right);
640 nleft = nright;
641 for (size_t c = right; c > left; c--) {
642 nleft = nleft->prev;
643 }
644 } else {
645 // case 3: one item left, one item right
647 // chose the closest to begin / end
648 size_t closest;
649 size_t other;
650 size_t diff2boundary = list->size - right - 1;
651 if (left <= diff2boundary) {
652 closest = left;
653 other = right;
654 nleft = cx_ll_node_at(ll, left);
655 } else {
656 closest = right;
657 other = left;
658 diff2boundary = left;
659 nright = cx_ll_node_at(ll, right);
660 }
662 // is other element closer to us or closer to boundary?
663 if (right - left <= diff2boundary) {
664 // search other element starting from already found element
665 if (closest == left) {
666 nright = nleft;
667 for (size_t c = left; c < right; c++) {
668 nright = nright->next;
669 }
670 } else {
671 nleft = nright;
672 for (size_t c = right; c > left; c--) {
673 nleft = nleft->prev;
674 }
675 }
676 } else {
677 // search other element starting at the boundary
678 if (closest == left) {
679 nright = cx_ll_node_at(ll, other);
680 } else {
681 nleft = cx_ll_node_at(ll, other);
682 }
683 }
684 }
686 if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
687 cx_linked_list_node *prev = nleft->prev;
688 cx_linked_list_node *next = nright->next;
689 cx_linked_list_node *midstart = nleft->next;
690 cx_linked_list_node *midend = nright->prev;
692 if (prev == NULL) {
693 ll->begin = nright;
694 } else {
695 prev->next = nright;
696 }
697 nright->prev = prev;
698 if (midstart == nright) {
699 // special case: both nodes are adjacent
700 nright->next = nleft;
701 nleft->prev = nright;
702 } else {
703 // likely case: a chain is between the two nodes
704 nright->next = midstart;
705 midstart->prev = nright;
706 midend->next = nleft;
707 nleft->prev = midend;
708 }
709 nleft->next = next;
710 if (next == NULL) {
711 ll->end = nleft;
712 } else {
713 next->prev = nleft;
714 }
715 } else {
716 // swap payloads to avoid relinking
717 char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
718 memcpy(buf, nleft->payload, list->item_size);
719 memcpy(nleft->payload, nright->payload, list->item_size);
720 memcpy(nright->payload, buf, list->item_size);
721 }
723 return 0;
724 }
726 static void *cx_ll_at(
727 struct cx_list_s const *list,
728 size_t index
729 ) {
730 cx_linked_list *ll = (cx_linked_list *) list;
731 cx_linked_list_node *node = cx_ll_node_at(ll, index);
732 return node == NULL ? NULL : node->payload;
733 }
735 static ssize_t cx_ll_find(
736 struct cx_list_s const *list,
737 void const *elem
738 ) {
739 return cx_linked_list_find(((cx_linked_list *) list)->begin,
740 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
741 list->cmpfunc, elem);
742 }
744 static void cx_ll_sort(struct cx_list_s *list) {
745 cx_linked_list *ll = (cx_linked_list *) list;
746 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
747 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
748 list->cmpfunc);
749 }
751 static void cx_ll_reverse(struct cx_list_s *list) {
752 cx_linked_list *ll = (cx_linked_list *) list;
753 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
754 }
756 static int cx_ll_compare(
757 struct cx_list_s const *list,
758 struct cx_list_s const *other
759 ) {
760 cx_linked_list *left = (cx_linked_list *) list;
761 cx_linked_list *right = (cx_linked_list *) other;
762 return cx_linked_list_compare(left->begin, right->begin,
763 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
764 list->cmpfunc);
765 }
767 static bool cx_ll_iter_valid(void const *it) {
768 struct cx_iterator_s const *iter = it;
769 return iter->elem_handle != NULL;
770 }
772 static void cx_ll_iter_next(void *it) {
773 struct cx_iterator_base_s *itbase = it;
774 if (itbase->remove) {
775 itbase->remove = false;
776 struct cx_mut_iterator_s *iter = it;
777 struct cx_list_s *list = iter->src_handle;
778 cx_linked_list *ll = iter->src_handle;
779 cx_linked_list_node *node = iter->elem_handle;
780 iter->elem_handle = node->next;
781 cx_invoke_destructor(list, node->payload);
782 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
783 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
784 list->size--;
785 cxFree(list->allocator, node);
786 } else {
787 struct cx_iterator_s *iter = it;
788 iter->index++;
789 cx_linked_list_node *node = iter->elem_handle;
790 iter->elem_handle = node->next;
791 }
792 }
794 static void cx_ll_iter_prev(void *it) {
795 struct cx_iterator_base_s *itbase = it;
796 if (itbase->remove) {
797 itbase->remove = false;
798 struct cx_mut_iterator_s *iter = it;
799 struct cx_list_s *list = iter->src_handle;
800 cx_linked_list *ll = iter->src_handle;
801 cx_linked_list_node *node = iter->elem_handle;
802 iter->elem_handle = node->prev;
803 iter->index--;
804 cx_invoke_destructor(list, node->payload);
805 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
806 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
807 list->size--;
808 cxFree(list->allocator, node);
809 } else {
810 struct cx_iterator_s *iter = it;
811 iter->index--;
812 cx_linked_list_node *node = iter->elem_handle;
813 iter->elem_handle = node->prev;
814 }
815 }
817 static void *cx_ll_iter_current(void const *it) {
818 struct cx_iterator_s const *iter = it;
819 cx_linked_list_node *node = iter->elem_handle;
820 return node->payload;
821 }
823 static bool cx_ll_iter_flag_rm(void *it) {
824 struct cx_iterator_base_s *iter = it;
825 if (iter->mutating) {
826 iter->remove = true;
827 return true;
828 } else {
829 return false;
830 }
831 }
833 static CxIterator cx_ll_iterator(
834 struct cx_list_s const *list,
835 size_t index,
836 bool backwards
837 ) {
838 CxIterator iter;
839 iter.index = index;
840 iter.src_handle = list;
841 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
842 iter.base.valid = cx_ll_iter_valid;
843 iter.base.current = cx_ll_iter_current;
844 iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
845 iter.base.flag_removal = cx_ll_iter_flag_rm;
846 iter.base.mutating = false;
847 iter.base.remove = false;
848 return iter;
849 }
851 static int cx_ll_insert_iter(
852 CxMutIterator *iter,
853 void const *elem,
854 int prepend
855 ) {
856 struct cx_list_s *list = iter->src_handle;
857 cx_linked_list_node *node = iter->elem_handle;
858 if (node != NULL) {
859 assert(prepend >= 0 && prepend <= 1);
860 cx_linked_list_node *choice[2] = {node, node->prev};
861 int result = cx_ll_insert_at(list, choice[prepend], elem);
862 iter->index += prepend * (0 == result);
863 return result;
864 } else {
865 int result = cx_ll_insert_element(list, list->size, elem);
866 iter->index = list->size;
867 return result;
868 }
869 }
871 static void cx_ll_destructor(CxList *list) {
872 cx_linked_list *ll = (cx_linked_list *) list;
874 cx_linked_list_node *node = ll->begin;
875 while (node) {
876 void *next = node->next;
877 cxFree(list->allocator, node);
878 node = next;
879 }
880 // do not free the list pointer, this is just a destructor!
881 }
883 static cx_list_class cx_linked_list_class = {
884 cx_ll_destructor,
885 cx_ll_insert_element,
886 cx_ll_insert_array,
887 cx_ll_insert_iter,
888 cx_ll_remove,
889 cx_ll_clear,
890 cx_ll_swap,
891 cx_ll_at,
892 cx_ll_find,
893 cx_ll_sort,
894 cx_ll_compare,
895 cx_ll_reverse,
896 cx_ll_iterator,
897 };
899 CxList *cxLinkedListCreate(
900 CxAllocator const *allocator,
901 cx_compare_func comparator,
902 size_t item_size
903 ) {
904 if (allocator == NULL) {
905 allocator = cxDefaultAllocator;
906 }
908 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
909 if (list == NULL) return NULL;
911 list->base.cl = &cx_linked_list_class;
912 list->base.allocator = allocator;
913 list->base.cmpfunc = comparator;
915 if (item_size > 0) {
916 list->base.item_size = item_size;
917 } else {
918 cxListStorePointers((CxList *) list);
919 }
921 return (CxList *) list;
922 }