src/linked_list.c

Sat, 22 Apr 2023 14:21:02 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 22 Apr 2023 14:21:02 +0200
changeset 699
35b2b99ee523
parent 677
b09aae58bba4
child 702
3390b58ad15a
permissions
-rw-r--r--

make list find return a negative value when elem not found

     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     // check how many elements are already sorted
   359     lc = ls;
   360     size_t ln = 1;
   361     while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
   362         lc = ll_next(lc);
   363         ln++;
   364     }
   365     le = ll_next(lc);
   367     // if first unsorted node is NULL, the list is already completely sorted
   368     if (le != NULL) {
   369         void *rc;
   370         size_t rn = 1;
   371         rc = le;
   372         // skip already sorted elements
   373         while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
   374             rc = ll_next(rc);
   375             rn++;
   376         }
   377         re = ll_next(rc);
   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);
   393         } else {
   394             // no remainder - we've got our sorted list
   395             *begin = sorted;
   396         }
   397         if (end) *end = cx_linked_list_last(sorted, loc_next);
   398     }
   399 }
   401 int cx_linked_list_compare(
   402         void const *begin_left,
   403         void const *begin_right,
   404         ptrdiff_t loc_advance,
   405         ptrdiff_t loc_data,
   406         cx_compare_func cmp_func
   407 ) {
   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);
   417     }
   419     if (left != NULL) { return 1; }
   420     else if (right != NULL) { return -1; }
   421     else { return 0; }
   422 }
   424 void cx_linked_list_reverse(
   425         void **begin,
   426         void **end,
   427         ptrdiff_t loc_prev,
   428         ptrdiff_t loc_next
   429 ) {
   430     assert(begin != NULL);
   431     assert(loc_next >= 0);
   433     // swap all links
   434     void *prev = NULL;
   435     void *cur = *begin;
   436     while (cur != NULL) {
   437         void *next = ll_next(cur);
   439         ll_next(cur) = prev;
   440         if (loc_prev >= 0) {
   441             ll_prev(cur) = next;
   442         }
   444         prev = cur;
   445         cur = next;
   446     }
   448     // update begin and end
   449     if (end != NULL) {
   450         *end = *begin;
   451     }
   452     *begin = prev;
   453 }
   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;
   463     char payload[];
   464 };
   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)
   470 typedef struct {
   471     struct cx_list_s base;
   472     cx_linked_list_node *begin;
   473     cx_linked_list_node *end;
   474 } cx_linked_list;
   476 static cx_linked_list_node *cx_ll_node_at(
   477         cx_linked_list const *list,
   478         size_t index
   479 ) {
   480     if (index >= list->base.size) {
   481         return NULL;
   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);
   484     } else {
   485         return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
   486     }
   487 }
   489 static int cx_ll_insert_at(
   490         struct cx_list_s *list,
   491         cx_linked_list_node *node,
   492         void const *elem
   493 ) {
   495     // create the new new_node
   496     cx_linked_list_node *new_node = cxMalloc(list->allocator,
   497                                              sizeof(cx_linked_list_node) + list->item_size);
   499     // sortir if failed
   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->item_size);
   506     // insert
   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
   512     );
   514     // increase the size and return
   515     list->size++;
   516     return 0;
   517 }
   519 static size_t cx_ll_insert_array(
   520         struct cx_list_s *list,
   521         size_t index,
   522         void const *array,
   523         size_t n
   524 ) {
   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)) {
   533         return 1;
   534     }
   536     // is there more?
   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->item_size;
   546         if (0 != cx_ll_insert_at(list, node, source)) {
   547             return i;
   548         }
   549         node = node->next;
   550     }
   551     return n;
   552 }
   554 static int cx_ll_insert_element(
   555         struct cx_list_s *list,
   556         size_t index,
   557         void const *element
   558 ) {
   559     return 1 != cx_ll_insert_array(list, index, element, 1);
   560 }
   562 static int cx_ll_remove(
   563         struct cx_list_s *list,
   564         size_t index
   565 ) {
   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     cx_invoke_destructor(list, node->payload);
   575     // remove
   576     cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   577                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   579     // adjust size
   580     list->size--;
   582     // free and return
   583     cxFree(list->allocator, node);
   585     return 0;
   586 }
   588 static void cx_ll_clear(struct cx_list_s *list) {
   589     if (list->size == 0) return;
   591     cx_linked_list *ll = (cx_linked_list *) list;
   592     cx_linked_list_node *node = ll->begin;
   593     while (node != NULL) {
   594         cx_invoke_destructor(list, node->payload);
   595         cx_linked_list_node *next = node->next;
   596         cxFree(list->allocator, node);
   597         node = next;
   598     }
   599     ll->begin = ll->end = NULL;
   600     list->size = 0;
   601 }
   603 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
   604 #define CX_LINKED_LIST_SWAP_SBO_SIZE 16
   605 #endif
   607 static int cx_ll_swap(
   608         struct cx_list_s *list,
   609         size_t i,
   610         size_t j
   611 ) {
   612     if (i >= list->size || j >= list->size) return 1;
   613     if (i == j) return 0;
   615     // perform an optimized search that finds both elements in one run
   616     cx_linked_list *ll = (cx_linked_list *) list;
   617     size_t mid = list->size / 2;
   618     size_t left, right;
   619     if (i < j) {
   620         left = i;
   621         right = j;
   622     } else {
   623         left = j;
   624         right = i;
   625     }
   626     cx_linked_list_node *nleft, *nright;
   627     if (left < mid && right < mid) {
   628         // case 1: both items left from mid
   629         nleft = cx_ll_node_at(ll, left);
   630         nright = nleft;
   631         for (size_t c = left; c < right; c++) {
   632             nright = nright->next;
   633         }
   634     } else if (left >= mid && right >= mid) {
   635         // case 2: both items right from mid
   636         nright = cx_ll_node_at(ll, right);
   637         nleft = nright;
   638         for (size_t c = right; c > left; c--) {
   639             nleft = nleft->prev;
   640         }
   641     } else {
   642         // case 3: one item left, one item right
   644         // chose the closest to begin / end
   645         size_t closest;
   646         size_t other;
   647         size_t diff2boundary = list->size - right - 1;
   648         if (left <= diff2boundary) {
   649             closest = left;
   650             other = right;
   651             nleft = cx_ll_node_at(ll, left);
   652         } else {
   653             closest = right;
   654             other = left;
   655             diff2boundary = left;
   656             nright = cx_ll_node_at(ll, right);
   657         }
   659         // is other element closer to us or closer to boundary?
   660         if (right - left <= diff2boundary) {
   661             // search other element starting from already found element
   662             if (closest == left) {
   663                 nright = nleft;
   664                 for (size_t c = left; c < right; c++) {
   665                     nright = nright->next;
   666                 }
   667             } else {
   668                 nleft = nright;
   669                 for (size_t c = right; c > left; c--) {
   670                     nleft = nleft->prev;
   671                 }
   672             }
   673         } else {
   674             // search other element starting at the boundary
   675             if (closest == left) {
   676                 nright = cx_ll_node_at(ll, other);
   677             } else {
   678                 nleft = cx_ll_node_at(ll, other);
   679             }
   680         }
   681     }
   683     if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
   684         cx_linked_list_node *prev = nleft->prev;
   685         cx_linked_list_node *next = nright->next;
   686         cx_linked_list_node *midstart = nleft->next;
   687         cx_linked_list_node *midend = nright->prev;
   689         if (prev == NULL) {
   690             ll->begin = nright;
   691         } else {
   692             prev->next = nright;
   693         }
   694         nright->prev = prev;
   695         if (midstart == nright) {
   696             // special case: both nodes are adjacent
   697             nright->next = nleft;
   698             nleft->prev = nright;
   699         } else {
   700             // likely case: a chain is between the two nodes
   701             nright->next = midstart;
   702             midstart->prev = nright;
   703             midend->next = nleft;
   704             nleft->prev = midend;
   705         }
   706         nleft->next = next;
   707         if (next == NULL) {
   708             ll->end = nleft;
   709         } else {
   710             next->prev = nleft;
   711         }
   712     } else {
   713         // swap payloads to avoid relinking
   714         char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
   715         memcpy(buf, nleft->payload, list->item_size);
   716         memcpy(nleft->payload, nright->payload, list->item_size);
   717         memcpy(nright->payload, buf, list->item_size);
   718     }
   720     return 0;
   721 }
   723 static void *cx_ll_at(
   724         struct cx_list_s const *list,
   725         size_t index
   726 ) {
   727     cx_linked_list *ll = (cx_linked_list *) list;
   728     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   729     return node == NULL ? NULL : node->payload;
   730 }
   732 static ssize_t cx_ll_find(
   733         struct cx_list_s const *list,
   734         void const *elem
   735 ) {
   736     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   737                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   738                                list->cmpfunc, elem);
   739 }
   741 static void cx_ll_sort(struct cx_list_s *list) {
   742     cx_linked_list *ll = (cx_linked_list *) list;
   743     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   744                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   745                         list->cmpfunc);
   746 }
   748 static void cx_ll_reverse(struct cx_list_s *list) {
   749     cx_linked_list *ll = (cx_linked_list *) list;
   750     cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
   751 }
   753 static int cx_ll_compare(
   754         struct cx_list_s const *list,
   755         struct cx_list_s const *other
   756 ) {
   757     cx_linked_list *left = (cx_linked_list *) list;
   758     cx_linked_list *right = (cx_linked_list *) other;
   759     return cx_linked_list_compare(left->begin, right->begin,
   760                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   761                                   list->cmpfunc);
   762 }
   764 static bool cx_ll_iter_valid(void const *it) {
   765     struct cx_iterator_s const *iter = it;
   766     return iter->elem_handle != NULL;
   767 }
   769 static void cx_ll_iter_next(void *it) {
   770     struct cx_iterator_base_s *itbase = it;
   771     if (itbase->remove) {
   772         itbase->remove = false;
   773         struct cx_mut_iterator_s *iter = it;
   774         struct cx_list_s *list = iter->src_handle;
   775         cx_linked_list *ll = iter->src_handle;
   776         cx_linked_list_node *node = iter->elem_handle;
   777         iter->elem_handle = node->next;
   778         cx_invoke_destructor(list, node->payload);
   779         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   780                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   781         list->size--;
   782         cxFree(list->allocator, node);
   783     } else {
   784         struct cx_iterator_s *iter = it;
   785         iter->index++;
   786         cx_linked_list_node *node = iter->elem_handle;
   787         iter->elem_handle = node->next;
   788     }
   789 }
   791 static void cx_ll_iter_prev(void *it) {
   792     struct cx_iterator_base_s *itbase = it;
   793     if (itbase->remove) {
   794         itbase->remove = false;
   795         struct cx_mut_iterator_s *iter = it;
   796         struct cx_list_s *list = iter->src_handle;
   797         cx_linked_list *ll = iter->src_handle;
   798         cx_linked_list_node *node = iter->elem_handle;
   799         iter->elem_handle = node->prev;
   800         iter->index--;
   801         cx_invoke_destructor(list, node->payload);
   802         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   803                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   804         list->size--;
   805         cxFree(list->allocator, node);
   806     } else {
   807         struct cx_iterator_s *iter = it;
   808         iter->index--;
   809         cx_linked_list_node *node = iter->elem_handle;
   810         iter->elem_handle = node->prev;
   811     }
   812 }
   814 static void *cx_ll_iter_current(void const *it) {
   815     struct cx_iterator_s const *iter = it;
   816     cx_linked_list_node *node = iter->elem_handle;
   817     return node->payload;
   818 }
   820 static bool cx_ll_iter_flag_rm(void *it) {
   821     struct cx_iterator_base_s *iter = it;
   822     if (iter->mutating) {
   823         iter->remove = true;
   824         return true;
   825     } else {
   826         return false;
   827     }
   828 }
   830 static CxIterator cx_ll_iterator(
   831         struct cx_list_s const *list,
   832         size_t index,
   833         bool backwards
   834 ) {
   835     CxIterator iter;
   836     iter.index = index;
   837     iter.src_handle = list;
   838     iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
   839     iter.base.valid = cx_ll_iter_valid;
   840     iter.base.current = cx_ll_iter_current;
   841     iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
   842     iter.base.flag_removal = cx_ll_iter_flag_rm;
   843     iter.base.mutating = false;
   844     iter.base.remove = false;
   845     return iter;
   846 }
   848 static int cx_ll_insert_iter(
   849         CxMutIterator *iter,
   850         void const *elem,
   851         int prepend
   852 ) {
   853     struct cx_list_s *list = iter->src_handle;
   854     cx_linked_list_node *node = iter->elem_handle;
   855     if (node != NULL) {
   856         assert(prepend >= 0 && prepend <= 1);
   857         cx_linked_list_node *choice[2] = {node, node->prev};
   858         int result = cx_ll_insert_at(list, choice[prepend], elem);
   859         iter->index += prepend * (0 == result);
   860         return result;
   861     } else {
   862         int result = cx_ll_insert_element(list, list->size, elem);
   863         iter->index = list->size;
   864         return result;
   865     }
   866 }
   868 static void cx_ll_destructor(CxList *list) {
   869     cx_linked_list *ll = (cx_linked_list *) list;
   871     cx_linked_list_node *node = ll->begin;
   872     while (node) {
   873         void *next = node->next;
   874         cxFree(list->allocator, node);
   875         node = next;
   876     }
   877     // do not free the list pointer, this is just a destructor!
   878 }
   880 static cx_list_class cx_linked_list_class = {
   881         cx_ll_destructor,
   882         cx_ll_insert_element,
   883         cx_ll_insert_array,
   884         cx_ll_insert_iter,
   885         cx_ll_remove,
   886         cx_ll_clear,
   887         cx_ll_swap,
   888         cx_ll_at,
   889         cx_ll_find,
   890         cx_ll_sort,
   891         cx_ll_compare,
   892         cx_ll_reverse,
   893         cx_ll_iterator,
   894 };
   896 CxList *cxLinkedListCreate(
   897         CxAllocator const *allocator,
   898         cx_compare_func comparator,
   899         size_t item_size
   900 ) {
   901     if (allocator == NULL) {
   902         allocator = cxDefaultAllocator;
   903     }
   905     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   906     if (list == NULL) return NULL;
   908     list->base.cl = &cx_linked_list_class;
   909     list->base.allocator = allocator;
   910     list->base.cmpfunc = comparator;
   912     if (item_size > 0) {
   913         list->base.item_size = item_size;
   914     } else {
   915         cxListStorePointers((CxList *) list);
   916     }
   918     return (CxList *) list;
   919 }

mercurial