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 size_t cx_linked_list_find(
61 ptrdiff_t loc_advance,
63 CxListComparator 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 CxListComparator cmp_func
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();
305 while (lc && lc != le && rc != re) {
306 if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
315 while (lc && lc != le) {
320 while (rc && rc != re) {
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);
331 ll_next(sorted[length - 1]) = NULL;
333 void *ret = sorted[0];
340 void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function
346 CxListComparator cmp_func
348 assert(begin != NULL);
349 assert(loc_next >= 0);
350 assert(loc_data >= 0);
353 void *lc, *ls, *le, *re;
358 // check how many elements are already sorted
361 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
367 // if first unsorted node is NULL, the list is already completely sorted
372 // skip already sorted elements
373 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
379 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
380 void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
381 ln + rn, ls, le, re, cmp_func);
383 // Something left? Sort it!
384 size_t remainder_length = cx_linked_list_size(re, loc_next);
385 if (remainder_length > 0) {
386 void *remainder = re;
387 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func);
389 // merge sorted list with (also sorted) remainder
390 *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
391 ln + rn + remainder_length,
392 sorted, remainder, NULL, cmp_func);
394 // no remainder - we've got our sorted list
397 if (end) *end = cx_linked_list_last(sorted, loc_next);
401 int cx_linked_list_compare(
402 void const *begin_left,
403 void const *begin_right,
404 ptrdiff_t loc_advance,
406 CxListComparator cmp_func
408 void const *left = begin_left, *right = begin_right;
410 while (left != NULL && right != NULL) {
411 void const *left_data = ll_data(left);
412 void const *right_data = ll_data(right);
413 int result = cmp_func(left_data, right_data);
414 if (result != 0) return result;
415 left = ll_advance(left);
416 right = ll_advance(right);
419 if (left != NULL) { return 1; }
420 else if (right != NULL) { return -1; }
424 void cx_linked_list_reverse(
430 assert(begin != NULL);
431 assert(loc_next >= 0);
436 while (cur != NULL) {
437 void *next = ll_next(cur);
448 // update begin and end
455 // HIGH LEVEL LINKED LIST IMPLEMENTATION
457 bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
459 typedef struct cx_linked_list_node cx_linked_list_node;
460 struct cx_linked_list_node {
461 cx_linked_list_node *prev;
462 cx_linked_list_node *next;
466 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
467 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
468 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
471 struct cx_list_s base;
472 cx_linked_list_node *begin;
473 cx_linked_list_node *end;
476 static cx_linked_list_node *cx_ll_node_at(
477 cx_linked_list const *list,
480 if (index >= list->base.size) {
482 } else if (index > list->base.size / 2) {
483 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
485 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
489 static int cx_ll_insert_at(
490 struct cx_list_s *list,
491 cx_linked_list_node *node,
495 // create the new new_node
496 cx_linked_list_node *new_node = cxMalloc(list->allocator,
497 sizeof(cx_linked_list_node) + list->itemsize);
500 if (new_node == NULL) return 1;
502 // initialize new new_node
503 new_node->prev = new_node->next = NULL;
504 memcpy(new_node->payload, elem, list->itemsize);
507 cx_linked_list *ll = (cx_linked_list *) list;
508 cx_linked_list_insert_chain(
509 (void **) &ll->begin, (void **) &ll->end,
510 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
511 node, new_node, new_node
514 // increase the size and return
519 static size_t cx_ll_insert_array(
520 struct cx_list_s *list,
525 // out-of bounds and corner case check
526 if (index > list->size || n == 0) return 0;
528 // find position efficiently
529 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
531 // perform first insert
532 if (0 != cx_ll_insert_at(list, node, array)) {
537 if (n == 1) return 1;
539 // we now know exactly where we are
540 node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
542 // we can add the remaining nodes and immedately advance to the inserted node
543 char const *source = array;
544 for (size_t i = 1; i < n; i++) {
545 source += list->itemsize;
546 if (0 != cx_ll_insert_at(list, node, source)) {
554 static int cx_ll_insert_element(
555 struct cx_list_s *list,
559 return 1 != cx_ll_insert_array(list, index, element, 1);
562 static int cx_ll_remove(
563 struct cx_list_s *list,
566 cx_linked_list *ll = (cx_linked_list *) list;
567 cx_linked_list_node *node = cx_ll_node_at(ll, index);
569 // out-of-bounds check
570 if (node == NULL) return 1;
572 // element destruction
573 if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
574 cx_list_invoke_destructor(list, node->payload);
578 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
579 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
585 cxFree(list->allocator, node);
590 static void cx_ll_clear(struct cx_list_s *list) {
591 if (list->size == 0) return;
593 cx_linked_list *ll = (cx_linked_list *) list;
594 cx_linked_list_node *node = ll->begin;
596 // looks super redundant, but avoids repeatedly checking
597 // the destructor type for each element
598 switch (list->content_destructor_type) {
599 case CX_DESTRUCTOR_SIMPLE: {
600 while (node != NULL) {
601 cx_list_invoke_simple_destructor(list, node->payload);
602 cx_linked_list_node *next = node->next;
603 cxFree(list->allocator, node);
608 case CX_DESTRUCTOR_ADVANCED: {
609 while (node != NULL) {
610 cx_list_invoke_advanced_destructor(list, node->payload);
611 cx_linked_list_node *next = node->next;
612 cxFree(list->allocator, node);
617 case CX_DESTRUCTOR_NONE: {
618 while (node != NULL) {
619 cx_linked_list_node *next = node->next;
620 cxFree(list->allocator, node);
627 ll->begin = ll->end = NULL;
631 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
632 #define CX_LINKED_LIST_SWAP_SBO_SIZE 16
635 static int cx_ll_swap(
636 struct cx_list_s *list,
640 if (i >= list->size || j >= list->size) return 1;
641 if (i == j) return 0;
643 // perform an optimized search that finds both elements in one run
644 cx_linked_list *ll = (cx_linked_list *) list;
645 size_t mid = list->size / 2;
654 cx_linked_list_node *nleft, *nright;
655 if (left < mid && right < mid) {
656 // case 1: both items left from mid
657 nleft = cx_ll_node_at(ll, left);
659 for (size_t c = left; c < right; c++) {
660 nright = nright->next;
662 } else if (left >= mid && right >= mid) {
663 // case 2: both items right from mid
664 nright = cx_ll_node_at(ll, right);
666 for (size_t c = right; c > left; c--) {
670 // case 3: one item left, one item right
672 // chose the closest to begin / end
675 size_t diff2boundary = list->size - right - 1;
676 if (left <= diff2boundary) {
679 nleft = cx_ll_node_at(ll, left);
683 diff2boundary = left;
684 nright = cx_ll_node_at(ll, right);
687 // is other element closer to us or closer to boundary?
688 if (right - left <= diff2boundary) {
689 // search other element starting from already found element
690 if (closest == left) {
692 for (size_t c = left; c < right; c++) {
693 nright = nright->next;
697 for (size_t c = right; c > left; c--) {
702 // search other element starting at the boundary
703 if (closest == left) {
704 nright = cx_ll_node_at(ll, other);
706 nleft = cx_ll_node_at(ll, other);
711 if (list->itemsize > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
712 cx_linked_list_node *prev = nleft->prev;
713 cx_linked_list_node *next = nright->next;
714 cx_linked_list_node *midstart = nleft->next;
715 cx_linked_list_node *midend = nright->prev;
723 if (midstart == nright) {
724 // special case: both nodes are adjacent
725 nright->next = nleft;
726 nleft->prev = nright;
728 // likely case: a chain is between the two nodes
729 nright->next = midstart;
730 midstart->prev = nright;
731 midend->next = nleft;
732 nleft->prev = midend;
741 // swap payloads to avoid relinking
742 char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
743 memcpy(buf, nleft->payload, list->itemsize);
744 memcpy(nleft->payload, nright->payload, list->itemsize);
745 memcpy(nright->payload, buf, list->itemsize);
751 static void *cx_ll_at(
752 struct cx_list_s const *list,
755 cx_linked_list *ll = (cx_linked_list *) list;
756 cx_linked_list_node *node = cx_ll_node_at(ll, index);
757 return node == NULL ? NULL : node->payload;
760 static size_t cx_ll_find(
761 struct cx_list_s const *list,
764 return cx_linked_list_find(((cx_linked_list *) list)->begin,
765 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
766 list->cmpfunc, elem);
769 static void cx_ll_sort(struct cx_list_s *list) {
770 cx_linked_list *ll = (cx_linked_list *) list;
771 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
772 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
776 static void cx_ll_reverse(struct cx_list_s *list) {
777 cx_linked_list *ll = (cx_linked_list *) list;
778 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
781 static int cx_ll_compare(
782 struct cx_list_s const *list,
783 struct cx_list_s const *other
785 cx_linked_list *left = (cx_linked_list *) list;
786 cx_linked_list *right = (cx_linked_list *) other;
787 return cx_linked_list_compare(left->begin, right->begin,
788 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
792 static bool cx_ll_iter_valid(void const *it) {
793 struct cx_iterator_s const *iter = it;
794 return iter->elem_handle != NULL;
797 static void cx_ll_iter_next(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->next;
806 if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
807 cx_list_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);
812 cxFree(list->allocator, node);
814 struct cx_iterator_s *iter = it;
816 cx_linked_list_node *node = iter->elem_handle;
817 iter->elem_handle = node->next;
821 static void cx_ll_iter_prev(void *it) {
822 struct cx_iterator_base_s *itbase = it;
823 if (itbase->remove) {
824 itbase->remove = false;
825 struct cx_mut_iterator_s *iter = it;
826 struct cx_list_s *list = iter->src_handle;
827 cx_linked_list *ll = iter->src_handle;
828 cx_linked_list_node *node = iter->elem_handle;
829 iter->elem_handle = node->prev;
831 if (list->content_destructor_type != CX_DESTRUCTOR_NONE) {
832 cx_list_invoke_destructor(list, node->payload);
834 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
835 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
837 cxFree(list->allocator, node);
839 struct cx_iterator_s *iter = it;
841 cx_linked_list_node *node = iter->elem_handle;
842 iter->elem_handle = node->prev;
846 static void *cx_ll_iter_current(void const *it) {
847 struct cx_iterator_s const *iter = it;
848 cx_linked_list_node *node = iter->elem_handle;
849 return node->payload;
852 static bool cx_ll_iter_flag_rm(void *it) {
853 struct cx_iterator_base_s *iter = it;
854 if (iter->mutating) {
862 static CxIterator cx_ll_iterator(
863 struct cx_list_s const *list,
869 iter.src_handle = list;
870 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
871 iter.base.valid = cx_ll_iter_valid;
872 iter.base.current = cx_ll_iter_current;
873 iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
874 iter.base.flag_removal = cx_ll_iter_flag_rm;
875 iter.base.mutating = false;
876 iter.base.remove = false;
880 static int cx_ll_insert_iter(
885 struct cx_list_s *list = iter->src_handle;
886 cx_linked_list_node *node = iter->elem_handle;
888 assert(prepend >= 0 && prepend <= 1);
889 cx_linked_list_node *choice[2] = {node, node->prev};
890 int result = cx_ll_insert_at(list, choice[prepend], elem);
891 iter->index += prepend * (0 == result);
894 int result = cx_ll_insert_element(list, list->size, elem);
895 iter->index = list->size;
900 static void cx_ll_destructor(CxList *list) {
901 cx_linked_list *ll = (cx_linked_list *) list;
903 cx_linked_list_node *node = ll->begin;
905 void *next = node->next;
906 cxFree(list->allocator, node);
909 // do not free the list pointer, this is just a destructor!
912 static cx_list_class cx_linked_list_class = {
914 cx_ll_insert_element,
928 CxList *cxLinkedListCreate(
929 CxAllocator const *allocator,
930 CxListComparator comparator,
933 if (allocator == NULL) {
934 allocator = cxDefaultAllocator;
937 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
938 if (list == NULL) return NULL;
940 list->base.cl = &cx_linked_list_class;
941 list->base.allocator = allocator;
942 list->base.cmpfunc = comparator;
943 list->base.capacity = SIZE_MAX;
946 list->base.itemsize = item_size;
948 cxListStorePointers((CxList *) list);
951 return (CxList *) list;