src/linked_list.c

Wed, 08 Feb 2023 20:26:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 08 Feb 2023 20:26:26 +0100
changeset 654
c9d008861178
parent 647
2e6e9d9f2159
parent 650
77021e06b1a8
child 655
7340c4255f1f
permissions
-rw-r--r--

Automated merge

     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 size_t cx_linked_list_find(
    60         void const *start,
    61         ptrdiff_t loc_advance,
    62         ptrdiff_t loc_data,
    63         CxListComparator 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     size_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 index;
    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 static void *cx_linked_list_sort_merge(
   283         ptrdiff_t loc_prev,
   284         ptrdiff_t loc_next,
   285         ptrdiff_t loc_data,
   286         size_t length,
   287         void *ls,
   288         void *le,
   289         void *re,
   290         CxListComparator cmp_func
   291 ) {
   292     const size_t sbo_len = 1024;
   293     void *sbo[sbo_len];
   294     void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
   295     if (sorted == NULL) abort();
   296     void *rc, *lc;
   298     lc = ls;
   299     rc = le;
   300     size_t n = 0;
   301     while (lc && lc != le && rc != re) {
   302         if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
   303             sorted[n] = lc;
   304             lc = ll_next(lc);
   305         } else {
   306             sorted[n] = rc;
   307             rc = ll_next(rc);
   308         }
   309         n++;
   310     }
   311     while (lc && lc != le) {
   312         sorted[n] = lc;
   313         lc = ll_next(lc);
   314         n++;
   315     }
   316     while (rc && rc != re) {
   317         sorted[n] = rc;
   318         rc = ll_next(rc);
   319         n++;
   320     }
   322     // Update pointer
   323     if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
   324     cx_for_n (i, length - 1) {
   325         cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
   326     }
   327     ll_next(sorted[length - 1]) = NULL;
   329     void *ret = sorted[0];
   330     if (sorted != sbo) {
   331         free(sorted);
   332     }
   333     return ret;
   334 }
   336 void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function
   337         void **begin,
   338         void **end,
   339         ptrdiff_t loc_prev,
   340         ptrdiff_t loc_next,
   341         ptrdiff_t loc_data,
   342         CxListComparator cmp_func
   343 ) {
   344     assert(begin != NULL);
   345     assert(loc_next >= 0);
   346     assert(loc_data >= 0);
   347     assert(cmp_func);
   349     void *lc, *ls, *le, *re;
   351     // set start node
   352     ls = *begin;
   354     // check how many elements are already sorted
   355     lc = ls;
   356     size_t ln = 1;
   357     while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
   358         lc = ll_next(lc);
   359         ln++;
   360     }
   361     le = ll_next(lc);
   363     // if first unsorted node is NULL, the list is already completely sorted
   364     if (le != NULL) {
   365         void *rc;
   366         size_t rn = 1;
   367         rc = le;
   368         // skip already sorted elements
   369         while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
   370             rc = ll_next(rc);
   371             rn++;
   372         }
   373         re = ll_next(rc);
   375         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   376         void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
   377                                                  ln + rn, ls, le, re, cmp_func);
   379         // Something left? Sort it!
   380         size_t remainder_length = cx_linked_list_size(re, loc_next);
   381         if (remainder_length > 0) {
   382             void *remainder = re;
   383             cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func);
   385             // merge sorted list with (also sorted) remainder
   386             *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
   387                                                ln + rn + remainder_length,
   388                                                sorted, remainder, NULL, cmp_func);
   389         } else {
   390             // no remainder - we've got our sorted list
   391             *begin = sorted;
   392         }
   393         if (end) *end = cx_linked_list_last(sorted, loc_next);
   394     }
   395 }
   397 int cx_linked_list_compare(
   398         void const *begin_left,
   399         void const *begin_right,
   400         ptrdiff_t loc_advance,
   401         ptrdiff_t loc_data,
   402         CxListComparator cmp_func
   403 ) {
   404     void const *left = begin_left, *right = begin_right;
   406     while (left != NULL && right != NULL) {
   407         void const *left_data = ll_data(left);
   408         void const *right_data = ll_data(right);
   409         int result = cmp_func(left_data, right_data);
   410         if (result != 0) return result;
   411         left = ll_advance(left);
   412         right = ll_advance(right);
   413     }
   415     if (left != NULL) { return 1; }
   416     else if (right != NULL) { return -1; }
   417     else { return 0; }
   418 }
   420 void cx_linked_list_reverse(
   421         void **begin,
   422         void **end,
   423         ptrdiff_t loc_prev,
   424         ptrdiff_t loc_next
   425 ) {
   426     assert(begin != NULL);
   427     assert(loc_next >= 0);
   429     // swap all links
   430     void *prev = NULL;
   431     void *cur = *begin;
   432     while (cur != NULL) {
   433         void *next = ll_next(cur);
   435         ll_next(cur) = prev;
   436         if (loc_prev >= 0) {
   437             ll_prev(cur) = next;
   438         }
   440         prev = cur;
   441         cur = next;
   442     }
   444     // update begin and end
   445     if (end != NULL) {
   446         *end = *begin;
   447     }
   448     *begin = prev;
   449 }
   451 // HIGH LEVEL LINKED LIST IMPLEMENTATION
   453 bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
   455 typedef struct cx_linked_list_node cx_linked_list_node;
   456 struct cx_linked_list_node {
   457     cx_linked_list_node *prev;
   458     cx_linked_list_node *next;
   459     char payload[];
   460 };
   462 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
   463 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
   464 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
   466 typedef struct {
   467     struct cx_list_s base;
   468     cx_linked_list_node *begin;
   469     cx_linked_list_node *end;
   470 } cx_linked_list;
   472 static cx_linked_list_node *cx_ll_node_at(
   473         cx_linked_list const *list,
   474         size_t index
   475 ) {
   476     if (index >= list->base.size) {
   477         return NULL;
   478     } else if (index > list->base.size / 2) {
   479         return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
   480     } else {
   481         return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
   482     }
   483 }
   485 static int cx_ll_insert_at(
   486         struct cx_list_s *list,
   487         cx_linked_list_node *node,
   488         void const *elem
   489 ) {
   491     // create the new new_node
   492     cx_linked_list_node *new_node = cxMalloc(list->allocator,
   493                                              sizeof(cx_linked_list_node) + list->itemsize);
   495     // sortir if failed
   496     if (new_node == NULL) return 1;
   498     // initialize new new_node
   499     new_node->prev = new_node->next = NULL;
   500     memcpy(new_node->payload, elem, list->itemsize);
   502     // insert
   503     cx_linked_list *ll = (cx_linked_list *) list;
   504     cx_linked_list_insert_chain(
   505             (void **) &ll->begin, (void **) &ll->end,
   506             CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
   507             node, new_node, new_node
   508     );
   510     // increase the size and return
   511     list->size++;
   512     return 0;
   513 }
   515 static size_t cx_ll_insert_array(
   516         struct cx_list_s *list,
   517         size_t index,
   518         void const *array,
   519         size_t n
   520 ) {
   521     // out-of bounds and corner case check
   522     if (index > list->size || n == 0) return 0;
   524     // find position efficiently
   525     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
   527     // perform first insert
   528     if (0 != cx_ll_insert_at(list, node, array)) {
   529         return 1;
   530     }
   532     // is there more?
   533     if (n == 1) return 1;
   535     // we now know exactly where we are
   536     node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
   538     // we can add the remaining nodes and immedately advance to the inserted node
   539     char const *source = array;
   540     for (size_t i = 1; i < n; i++) {
   541         source += list->itemsize;
   542         if (0 != cx_ll_insert_at(list, node, source)) {
   543             return i;
   544         }
   545         node = node->next;
   546     }
   547     return n;
   548 }
   550 static int cx_ll_insert_element(
   551         struct cx_list_s *list,
   552         size_t index,
   553         void const *element
   554 ) {
   555     return 1 != cx_ll_insert_array(list, index, element, 1);
   556 }
   558 static int cx_ll_remove(
   559         struct cx_list_s *list,
   560         size_t index
   561 ) {
   562     cx_linked_list *ll = (cx_linked_list *) list;
   563     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   565     // out-of-bounds check
   566     if (node == NULL) return 1;
   568     // remove
   569     cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   570                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   572     // adjust size
   573     list->size--;
   575     // free and return
   576     cxFree(list->allocator, node);
   578     return 0;
   579 }
   581 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
   582 #define CX_LINKED_LIST_SWAP_SBO_SIZE 16
   583 #endif
   585 static int cx_ll_swap(
   586         struct cx_list_s *list,
   587         size_t i,
   588         size_t j
   589 ) {
   590     if (i >= list->size || j >= list->size) return 1;
   591     if (i == j) return 0;
   593     // perform an optimized search that finds both elements in one run
   594     cx_linked_list *ll = (cx_linked_list *) list;
   595     size_t mid = list->size / 2;
   596     size_t left, right;
   597     if (i < j) {
   598         left = i;
   599         right = j;
   600     } else {
   601         left = j;
   602         right = i;
   603     }
   604     cx_linked_list_node *nleft, *nright;
   605     if (left < mid && right < mid) {
   606         // case 1: both items left from mid
   607         nleft = cx_ll_node_at(ll, left);
   608         nright = nleft;
   609         for (size_t c = left; c < right; c++) {
   610             nright = nright->next;
   611         }
   612     } else if (left >= mid && right >= mid) {
   613         // case 2: both items right from mid
   614         nright = cx_ll_node_at(ll, right);
   615         nleft = nright;
   616         for (size_t c = right; c > left; c--) {
   617             nleft = nleft->prev;
   618         }
   619     } else {
   620         // case 3: one item left, one item right
   622         // chose the closest to begin / end
   623         size_t closest;
   624         size_t other;
   625         size_t diff2boundary = list->size - right - 1;
   626         if (left <= diff2boundary) {
   627             closest = left;
   628             other = right;
   629             nleft = cx_ll_node_at(ll, left);
   630         } else {
   631             closest = right;
   632             other = left;
   633             diff2boundary = left;
   634             nright = cx_ll_node_at(ll, right);
   635         }
   637         // is other element closer to us or closer to boundary?
   638         if (right - left <= diff2boundary) {
   639             // search other element starting from already found element
   640             if (closest == left) {
   641                 nright = nleft;
   642                 for (size_t c = left; c < right; c++) {
   643                     nright = nright->next;
   644                 }
   645             } else {
   646                 nleft = nright;
   647                 for (size_t c = right; c > left; c--) {
   648                     nleft = nleft->prev;
   649                 }
   650             }
   651         } else {
   652             // search other element starting at the boundary
   653             if (closest == left) {
   654                 nright = cx_ll_node_at(ll, other);
   655             } else {
   656                 nleft = cx_ll_node_at(ll, other);
   657             }
   658         }
   659     }
   661     if (list->itemsize > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
   662         cx_linked_list_node *prev = nleft->prev;
   663         cx_linked_list_node *next = nright->next;
   664         cx_linked_list_node *midstart = nleft->next;
   665         cx_linked_list_node *midend = nright->prev;
   667         if (prev == NULL) {
   668             ll->begin = nright;
   669         } else {
   670             prev->next = nright;
   671         }
   672         nright->prev = prev;
   673         if (midstart == nright) {
   674             // special case: both nodes are adjacent
   675             nright->next = nleft;
   676             nleft->prev = nright;
   677         } else {
   678             // likely case: a chain is between the two nodes
   679             nright->next = midstart;
   680             midstart->prev = nright;
   681             midend->next = nleft;
   682             nleft->prev = midend;
   683         }
   684         nleft->next = next;
   685         if (next == NULL) {
   686             ll->end = nleft;
   687         } else {
   688             next->prev = nleft;
   689         }
   690     } else {
   691         // swap payloads to avoid relinking
   692         char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
   693         memcpy(buf, nleft->payload, list->itemsize);
   694         memcpy(nleft->payload, nright->payload, list->itemsize);
   695         memcpy(nright->payload, buf, list->itemsize);
   696     }
   698     return 0;
   699 }
   701 static void *cx_ll_at(
   702         struct cx_list_s const *list,
   703         size_t index
   704 ) {
   705     cx_linked_list *ll = (cx_linked_list *) list;
   706     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   707     return node == NULL ? NULL : node->payload;
   708 }
   710 static size_t cx_ll_find(
   711         struct cx_list_s const *list,
   712         void const *elem
   713 ) {
   714     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   715                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   716                                list->cmpfunc, elem);
   717 }
   719 static void cx_ll_sort(struct cx_list_s *list) {
   720     cx_linked_list *ll = (cx_linked_list *) list;
   721     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   722                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   723                         list->cmpfunc);
   724 }
   726 static void cx_ll_reverse(struct cx_list_s *list) {
   727     cx_linked_list *ll = (cx_linked_list *) list;
   728     cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
   729 }
   731 static int cx_ll_compare(
   732         struct cx_list_s const *list,
   733         struct cx_list_s const *other
   734 ) {
   735     cx_linked_list *left = (cx_linked_list *) list;
   736     cx_linked_list *right = (cx_linked_list *) other;
   737     return cx_linked_list_compare(left->begin, right->begin,
   738                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   739                                   list->cmpfunc);
   740 }
   742 static bool cx_ll_iter_valid(void const *it) {
   743     struct cx_iterator_s const *iter = it;
   744     return iter->elem_handle != NULL;
   745 }
   747 static void cx_ll_iter_next(void *it) {
   748     struct cx_iterator_base_s *itbase = it;
   749     if (itbase->remove) {
   750         itbase->remove = false;
   751         struct cx_mut_iterator_s *iter = it;
   752         cx_linked_list *ll = iter->src_handle;
   753         cx_linked_list_node *node = iter->elem_handle;
   754         iter->elem_handle = node->next;
   755         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   756                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   757         ll->base.size--;
   758         cxFree(ll->base.allocator, node);
   759     } else {
   760         struct cx_iterator_s *iter = it;
   761         iter->index++;
   762         cx_linked_list_node *node = iter->elem_handle;
   763         iter->elem_handle = node->next;
   764     }
   765 }
   767 static void *cx_ll_iter_current(void const *it) {
   768     struct cx_iterator_s const *iter = it;
   769     cx_linked_list_node *node = iter->elem_handle;
   770     return node->payload;
   771 }
   773 static bool cx_ll_iter_flag_rm(void *it) {
   774     struct cx_iterator_base_s *iter = it;
   775     if (iter->mutating) {
   776         iter->remove = true;
   777         return true;
   778     } else {
   779         return false;
   780     }
   781 }
   783 static CxIterator cx_ll_iterator(
   784         struct cx_list_s const *list,
   785         size_t index
   786 ) {
   787     CxIterator iter;
   788     iter.index = index;
   789     iter.src_handle = list;
   790     iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
   791     iter.base.valid = cx_ll_iter_valid;
   792     iter.base.current = cx_ll_iter_current;
   793     iter.base.next = cx_ll_iter_next;
   794     iter.base.flag_removal = cx_ll_iter_flag_rm;
   795     iter.base.mutating = false;
   796     iter.base.remove = false;
   797     return iter;
   798 }
   800 static int cx_ll_insert_iter(
   801         CxMutIterator *iter,
   802         void const *elem,
   803         int prepend
   804 ) {
   805     struct cx_list_s *list = iter->src_handle;
   806     cx_linked_list_node *node = iter->elem_handle;
   807     if (node != NULL) {
   808         assert(prepend >= 0 && prepend <= 1);
   809         cx_linked_list_node *choice[2] = {node, node->prev};
   810         int result = cx_ll_insert_at(list, choice[prepend], elem);
   811         iter->index += prepend * (0 == result);
   812         return result;
   813     } else {
   814         int result = cx_ll_insert_element(list, list->size, elem);
   815         iter->index = list->size;
   816         return result;
   817     }
   818 }
   820 static void cx_ll_destructor(CxList *list) {
   821     cx_linked_list *ll = (cx_linked_list *) list;
   823     cx_linked_list_node *node = ll->begin;
   824     while (node) {
   825         void *next = node->next;
   826         cxFree(list->allocator, node);
   827         node = next;
   828     }
   829     // do not free the list pointer, this is just a destructor!
   830 }
   832 static cx_list_class cx_linked_list_class = {
   833         cx_ll_destructor,
   834         cx_ll_insert_element,
   835         cx_ll_insert_array,
   836         cx_ll_insert_iter,
   837         cx_ll_remove,
   838         cx_ll_swap,
   839         cx_ll_at,
   840         cx_ll_find,
   841         cx_ll_sort,
   842         cx_ll_compare,
   843         cx_ll_reverse,
   844         cx_ll_iterator,
   845 };
   847 CxList *cxLinkedListCreate(
   848         CxAllocator const *allocator,
   849         CxListComparator comparator,
   850         size_t item_size
   851 ) {
   852     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   853     if (list == NULL) return NULL;
   855     list->base.cl = &cx_linked_list_class;
   856     list->base.allocator = allocator;
   857     list->base.cmpfunc = comparator;
   858     list->base.itemsize = item_size;
   859     list->base.capacity = SIZE_MAX;
   861     return (CxList *) list;
   862 }

mercurial