2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 #include "cx/linked_list.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(
45 ptrdiff_t loc_advance,
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--;
59 ssize_t cx_linked_list_find(
61 ptrdiff_t loc_advance,
63 cx_compare_func cmp_func,
66 assert(start != NULL);
67 assert(loc_advance >= 0);
68 assert(loc_data >= 0);
71 void const *node = start;
74 void *current = ll_data(node);
75 if (cmp_func(current, elem) == 0) {
78 node = ll_advance(node);
80 } while (node != NULL);
84 void *cx_linked_list_first(
88 return cx_linked_list_last(node, loc_prev);
91 void *cx_linked_list_last(
96 assert(loc_next >= 0);
98 void const *cur = node;
102 } while ((cur = ll_next(cur)) != NULL);
104 return (void *) last;
107 void *cx_linked_list_prev(
112 assert(begin != NULL);
113 assert(node != NULL);
114 assert(loc_next >= 0);
115 if (begin == node) return NULL;
116 void const *cur = begin;
120 if (next == node) return (void *) cur;
125 void cx_linked_list_link(
131 assert(loc_next >= 0);
132 ll_next(left) = right;
134 ll_prev(right) = left;
138 void cx_linked_list_unlink(
144 assert (loc_next >= 0);
145 assert(ll_next(left) == right);
146 ll_next(left) = NULL;
148 assert(ll_prev(right) == left);
149 ll_prev(right) = NULL;
153 void cx_linked_list_add(
162 assert(begin != NULL);
163 last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
167 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
170 void cx_linked_list_prepend(
177 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
180 void cx_linked_list_insert(
188 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
191 void cx_linked_list_insert_chain(
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);
205 // determine the successor
208 assert(begin != NULL || (end != NULL && loc_prev >= 0));
211 *begin = insert_begin;
213 successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
216 successor = ll_next(node);
217 cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
220 if (successor == NULL) {
221 // the list ends with the new chain
226 cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
230 void cx_linked_list_remove(
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);
245 prev = ll_prev(node);
247 prev = cx_linked_list_prev(*begin, loc_next, node);
250 // update next pointer of prev node, or set begin
256 ll_next(prev) = next;
259 // update prev pointer of next node, or set end
264 } else if (loc_prev >= 0) {
265 ll_prev(next) = prev;
269 size_t cx_linked_list_size(
273 assert(loc_next >= 0);
275 while (node != NULL) {
276 node = ll_next(node);
282 #ifndef CX_LINKED_LIST_SORT_SBO_SIZE
283 #define CX_LINKED_LIST_SORT_SBO_SIZE 1024
286 static void cx_linked_list_sort_merge(
294 cx_compare_func cmp_func,
298 void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE];
299 void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ?
300 malloc(sizeof(void *) * length) : sbo;
301 if (sorted == NULL) abort();
307 while (lc && lc != le && rc != re) {
308 if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
317 while (lc && lc != le) {
322 while (rc && rc != re) {
329 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
330 cx_for_n (i, length - 1) {
331 cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
333 ll_next(sorted[length - 1]) = NULL;
336 *end = sorted[length-1];
342 void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function
348 cx_compare_func cmp_func
350 assert(begin != NULL);
351 assert(loc_next >= 0);
352 assert(loc_data >= 0);
355 void *lc, *ls, *le, *re;
360 // early exit when this list is empty
361 if (ls == NULL) return;
363 // check how many elements are already sorted
366 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
372 // if first unsorted node is NULL, the list is already completely sorted
377 // skip already sorted elements
378 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
384 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
385 void *sorted_begin, *sorted_end;
386 cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
387 ln + rn, ls, le, re, cmp_func,
388 &sorted_begin, &sorted_end);
390 // Something left? Sort it!
391 size_t remainder_length = cx_linked_list_size(re, loc_next);
392 if (remainder_length > 0) {
393 void *remainder = re;
394 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func);
396 // merge sorted list with (also sorted) remainder
397 cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
398 ln + rn + remainder_length,
399 sorted_begin, remainder, NULL, cmp_func,
400 &sorted_begin, &sorted_end);
402 *begin = sorted_begin;
403 if (end) *end = sorted_end;
407 int cx_linked_list_compare(
408 void const *begin_left,
409 void const *begin_right,
410 ptrdiff_t loc_advance,
412 cx_compare_func cmp_func
414 void const *left = begin_left, *right = begin_right;
416 while (left != NULL && right != NULL) {
417 void const *left_data = ll_data(left);
418 void const *right_data = ll_data(right);
419 int result = cmp_func(left_data, right_data);
420 if (result != 0) return result;
421 left = ll_advance(left);
422 right = ll_advance(right);
425 if (left != NULL) { return 1; }
426 else if (right != NULL) { return -1; }
430 void cx_linked_list_reverse(
436 assert(begin != NULL);
437 assert(loc_next >= 0);
442 while (cur != NULL) {
443 void *next = ll_next(cur);
454 // update begin and end
461 // HIGH LEVEL LINKED LIST IMPLEMENTATION
463 bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
465 typedef struct cx_linked_list_node cx_linked_list_node;
466 struct cx_linked_list_node {
467 cx_linked_list_node *prev;
468 cx_linked_list_node *next;
472 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
473 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
474 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
477 struct cx_list_s base;
478 cx_linked_list_node *begin;
479 cx_linked_list_node *end;
482 static cx_linked_list_node *cx_ll_node_at(
483 cx_linked_list const *list,
486 if (index >= list->base.size) {
488 } else if (index > list->base.size / 2) {
489 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
491 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
495 static int cx_ll_insert_at(
496 struct cx_list_s *list,
497 cx_linked_list_node *node,
501 // create the new new_node
502 cx_linked_list_node *new_node = cxMalloc(list->allocator,
503 sizeof(cx_linked_list_node) + list->item_size);
506 if (new_node == NULL) return 1;
508 // initialize new new_node
509 new_node->prev = new_node->next = NULL;
510 memcpy(new_node->payload, elem, list->item_size);
513 cx_linked_list *ll = (cx_linked_list *) list;
514 cx_linked_list_insert_chain(
515 (void **) &ll->begin, (void **) &ll->end,
516 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
517 node, new_node, new_node
520 // increase the size and return
525 static size_t cx_ll_insert_array(
526 struct cx_list_s *list,
531 // out-of bounds and corner case check
532 if (index > list->size || n == 0) return 0;
534 // find position efficiently
535 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
537 // perform first insert
538 if (0 != cx_ll_insert_at(list, node, array)) {
543 if (n == 1) return 1;
545 // we now know exactly where we are
546 node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
548 // we can add the remaining nodes and immedately advance to the inserted node
549 char const *source = array;
550 for (size_t i = 1; i < n; i++) {
551 source += list->item_size;
552 if (0 != cx_ll_insert_at(list, node, source)) {
560 static int cx_ll_insert_element(
561 struct cx_list_s *list,
565 return 1 != cx_ll_insert_array(list, index, element, 1);
568 static int cx_ll_remove(
569 struct cx_list_s *list,
572 cx_linked_list *ll = (cx_linked_list *) list;
573 cx_linked_list_node *node = cx_ll_node_at(ll, index);
575 // out-of-bounds check
576 if (node == NULL) return 1;
578 // element destruction
579 cx_invoke_destructor(list, node->payload);
582 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
583 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
589 cxFree(list->allocator, node);
594 static void cx_ll_clear(struct cx_list_s *list) {
595 if (list->size == 0) return;
597 cx_linked_list *ll = (cx_linked_list *) list;
598 cx_linked_list_node *node = ll->begin;
599 while (node != NULL) {
600 cx_invoke_destructor(list, node->payload);
601 cx_linked_list_node *next = node->next;
602 cxFree(list->allocator, node);
605 ll->begin = ll->end = NULL;
609 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
610 #define CX_LINKED_LIST_SWAP_SBO_SIZE 128
613 static int cx_ll_swap(
614 struct cx_list_s *list,
618 if (i >= list->size || j >= list->size) return 1;
619 if (i == j) return 0;
621 // perform an optimized search that finds both elements in one run
622 cx_linked_list *ll = (cx_linked_list *) list;
623 size_t mid = list->size / 2;
632 cx_linked_list_node *nleft, *nright;
633 if (left < mid && right < mid) {
634 // case 1: both items left from mid
635 nleft = cx_ll_node_at(ll, left);
637 for (size_t c = left; c < right; c++) {
638 nright = nright->next;
640 } else if (left >= mid && right >= mid) {
641 // case 2: both items right from mid
642 nright = cx_ll_node_at(ll, right);
644 for (size_t c = right; c > left; c--) {
648 // case 3: one item left, one item right
650 // chose the closest to begin / end
653 size_t diff2boundary = list->size - right - 1;
654 if (left <= diff2boundary) {
657 nleft = cx_ll_node_at(ll, left);
661 diff2boundary = left;
662 nright = cx_ll_node_at(ll, right);
665 // is other element closer to us or closer to boundary?
666 if (right - left <= diff2boundary) {
667 // search other element starting from already found element
668 if (closest == left) {
670 for (size_t c = left; c < right; c++) {
671 nright = nright->next;
675 for (size_t c = right; c > left; c--) {
680 // search other element starting at the boundary
681 if (closest == left) {
682 nright = cx_ll_node_at(ll, other);
684 nleft = cx_ll_node_at(ll, other);
689 if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
690 cx_linked_list_node *prev = nleft->prev;
691 cx_linked_list_node *next = nright->next;
692 cx_linked_list_node *midstart = nleft->next;
693 cx_linked_list_node *midend = nright->prev;
701 if (midstart == nright) {
702 // special case: both nodes are adjacent
703 nright->next = nleft;
704 nleft->prev = nright;
706 // likely case: a chain is between the two nodes
707 nright->next = midstart;
708 midstart->prev = nright;
709 midend->next = nleft;
710 nleft->prev = midend;
719 // swap payloads to avoid relinking
720 char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
721 memcpy(buf, nleft->payload, list->item_size);
722 memcpy(nleft->payload, nright->payload, list->item_size);
723 memcpy(nright->payload, buf, list->item_size);
729 static void *cx_ll_at(
730 struct cx_list_s const *list,
733 cx_linked_list *ll = (cx_linked_list *) list;
734 cx_linked_list_node *node = cx_ll_node_at(ll, index);
735 return node == NULL ? NULL : node->payload;
738 static ssize_t cx_ll_find(
739 struct cx_list_s const *list,
742 return cx_linked_list_find(((cx_linked_list *) list)->begin,
743 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
744 list->cmpfunc, elem);
747 static void cx_ll_sort(struct cx_list_s *list) {
748 cx_linked_list *ll = (cx_linked_list *) list;
749 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
750 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
754 static void cx_ll_reverse(struct cx_list_s *list) {
755 cx_linked_list *ll = (cx_linked_list *) list;
756 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
759 static int cx_ll_compare(
760 struct cx_list_s const *list,
761 struct cx_list_s const *other
763 cx_linked_list *left = (cx_linked_list *) list;
764 cx_linked_list *right = (cx_linked_list *) other;
765 return cx_linked_list_compare(left->begin, right->begin,
766 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
770 static bool cx_ll_iter_valid(void const *it) {
771 struct cx_iterator_s const *iter = it;
772 return iter->elem_handle != NULL;
775 static void cx_ll_iter_next(void *it) {
776 struct cx_iterator_base_s *itbase = it;
777 if (itbase->remove) {
778 itbase->remove = false;
779 struct cx_mut_iterator_s *iter = it;
780 struct cx_list_s *list = iter->src_handle;
781 cx_linked_list *ll = iter->src_handle;
782 cx_linked_list_node *node = iter->elem_handle;
783 iter->elem_handle = node->next;
784 cx_invoke_destructor(list, node->payload);
785 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
786 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
788 cxFree(list->allocator, node);
790 struct cx_iterator_s *iter = it;
792 cx_linked_list_node *node = iter->elem_handle;
793 iter->elem_handle = node->next;
797 static void cx_ll_iter_prev(void *it) {
798 struct cx_iterator_base_s *itbase = it;
799 if (itbase->remove) {
800 itbase->remove = false;
801 struct cx_mut_iterator_s *iter = it;
802 struct cx_list_s *list = iter->src_handle;
803 cx_linked_list *ll = iter->src_handle;
804 cx_linked_list_node *node = iter->elem_handle;
805 iter->elem_handle = node->prev;
807 cx_invoke_destructor(list, node->payload);
808 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
809 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
811 cxFree(list->allocator, node);
813 struct cx_iterator_s *iter = it;
815 cx_linked_list_node *node = iter->elem_handle;
816 iter->elem_handle = node->prev;
820 static void *cx_ll_iter_current(void const *it) {
821 struct cx_iterator_s const *iter = it;
822 cx_linked_list_node *node = iter->elem_handle;
823 return node->payload;
826 static bool cx_ll_iter_flag_rm(void *it) {
827 struct cx_iterator_base_s *iter = it;
828 if (iter->mutating) {
836 static CxIterator cx_ll_iterator(
837 struct cx_list_s const *list,
843 iter.src_handle = list;
844 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
845 iter.base.valid = cx_ll_iter_valid;
846 iter.base.current = cx_ll_iter_current;
847 iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
848 iter.base.flag_removal = cx_ll_iter_flag_rm;
849 iter.base.mutating = false;
850 iter.base.remove = false;
854 static int cx_ll_insert_iter(
859 struct cx_list_s *list = iter->src_handle;
860 cx_linked_list_node *node = iter->elem_handle;
862 assert(prepend >= 0 && prepend <= 1);
863 cx_linked_list_node *choice[2] = {node, node->prev};
864 int result = cx_ll_insert_at(list, choice[prepend], elem);
865 iter->index += prepend * (0 == result);
868 int result = cx_ll_insert_element(list, list->size, elem);
869 iter->index = list->size;
874 static void cx_ll_destructor(CxList *list) {
875 cx_linked_list *ll = (cx_linked_list *) list;
877 cx_linked_list_node *node = ll->begin;
879 cx_invoke_destructor(list, node->payload);
880 void *next = node->next;
881 cxFree(list->allocator, node);
885 cxFree(list->allocator, list);
888 static cx_list_class cx_linked_list_class = {
890 cx_ll_insert_element,
904 CxList *cxLinkedListCreate(
905 CxAllocator const *allocator,
906 cx_compare_func comparator,
909 if (allocator == NULL) {
910 allocator = cxDefaultAllocator;
913 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
914 if (list == NULL) return NULL;
916 list->base.cl = &cx_linked_list_class;
917 list->base.allocator = allocator;
918 list->base.cmpfunc = comparator;
921 list->base.item_size = item_size;
923 cxListStorePointers((CxList *) list);
926 return (CxList *) list;