src/linked_list.c

Mon, 18 Dec 2023 18:22:53 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 18:22:53 +0100
changeset 764
ccbdbd088455
parent 763
741a2040fa33
child 807
c8d692131b1e
permissions
-rw-r--r--

add cxListFindRemove and cx_linked_list_find_node

resolves #339

     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 "cx/compare.h"
    32 #include <string.h>
    33 #include <assert.h>
    35 // LOW LEVEL LINKED LIST FUNCTIONS
    37 #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
    38 #define ll_prev(node) CX_LL_PTR(node, loc_prev)
    39 #define ll_next(node) CX_LL_PTR(node, loc_next)
    40 #define ll_advance(node) CX_LL_PTR(node, loc_advance)
    41 #define ll_data(node) (((char*)(node))+loc_data)
    43 void *cx_linked_list_at(
    44         void const *start,
    45         size_t start_index,
    46         ptrdiff_t loc_advance,
    47         size_t index
    48 ) {
    49     assert(start != NULL);
    50     assert(loc_advance >= 0);
    51     size_t i = start_index;
    52     void const *cur = start;
    53     while (i != index && cur != NULL) {
    54         cur = ll_advance(cur);
    55         i < index ? i++ : i--;
    56     }
    57     return (void *) cur;
    58 }
    60 ssize_t cx_linked_list_find(
    61         void const *start,
    62         ptrdiff_t loc_advance,
    63         ptrdiff_t loc_data,
    64         cx_compare_func cmp_func,
    65         void const *elem
    66 ) {
    67     void *dummy;
    68     return cx_linked_list_find_node(
    69             &dummy, start,
    70             loc_advance, loc_data,
    71             cmp_func, elem
    72     );
    73 }
    75 ssize_t cx_linked_list_find_node(
    76         void **result,
    77         void const *start,
    78         ptrdiff_t loc_advance,
    79         ptrdiff_t loc_data,
    80         cx_compare_func cmp_func,
    81         void const *elem
    82 ) {
    83     assert(result != NULL);
    84     assert(start != NULL);
    85     assert(loc_advance >= 0);
    86     assert(loc_data >= 0);
    87     assert(cmp_func);
    89     void const *node = start;
    90     ssize_t index = 0;
    91     do {
    92         void *current = ll_data(node);
    93         if (cmp_func(current, elem) == 0) {
    94             *result = (void*) node;
    95             return index;
    96         }
    97         node = ll_advance(node);
    98         index++;
    99     } while (node != NULL);
   100     *result = NULL;
   101     return -1;
   102 }
   104 void *cx_linked_list_first(
   105         void const *node,
   106         ptrdiff_t loc_prev
   107 ) {
   108     return cx_linked_list_last(node, loc_prev);
   109 }
   111 void *cx_linked_list_last(
   112         void const *node,
   113         ptrdiff_t loc_next
   114 ) {
   115     assert(node != NULL);
   116     assert(loc_next >= 0);
   118     void const *cur = node;
   119     void const *last;
   120     do {
   121         last = cur;
   122     } while ((cur = ll_next(cur)) != NULL);
   124     return (void *) last;
   125 }
   127 void *cx_linked_list_prev(
   128         void const *begin,
   129         ptrdiff_t loc_next,
   130         void const *node
   131 ) {
   132     assert(begin != NULL);
   133     assert(node != NULL);
   134     assert(loc_next >= 0);
   135     if (begin == node) return NULL;
   136     void const *cur = begin;
   137     void const *next;
   138     while (1) {
   139         next = ll_next(cur);
   140         if (next == node) return (void *) cur;
   141         cur = next;
   142     }
   143 }
   145 void cx_linked_list_link(
   146         void *left,
   147         void *right,
   148         ptrdiff_t loc_prev,
   149         ptrdiff_t loc_next
   150 ) {
   151     assert(loc_next >= 0);
   152     ll_next(left) = right;
   153     if (loc_prev >= 0) {
   154         ll_prev(right) = left;
   155     }
   156 }
   158 void cx_linked_list_unlink(
   159         void *left,
   160         void *right,
   161         ptrdiff_t loc_prev,
   162         ptrdiff_t loc_next
   163 ) {
   164     assert (loc_next >= 0);
   165     assert(ll_next(left) == right);
   166     ll_next(left) = NULL;
   167     if (loc_prev >= 0) {
   168         assert(ll_prev(right) == left);
   169         ll_prev(right) = NULL;
   170     }
   171 }
   173 void cx_linked_list_add(
   174         void **begin,
   175         void **end,
   176         ptrdiff_t loc_prev,
   177         ptrdiff_t loc_next,
   178         void *new_node
   179 ) {
   180     void *last;
   181     if (end == NULL) {
   182         assert(begin != NULL);
   183         last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
   184     } else {
   185         last = *end;
   186     }
   187     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
   188 }
   190 void cx_linked_list_prepend(
   191         void **begin,
   192         void **end,
   193         ptrdiff_t loc_prev,
   194         ptrdiff_t loc_next,
   195         void *new_node
   196 ) {
   197     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
   198 }
   200 void cx_linked_list_insert(
   201         void **begin,
   202         void **end,
   203         ptrdiff_t loc_prev,
   204         ptrdiff_t loc_next,
   205         void *node,
   206         void *new_node
   207 ) {
   208     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
   209 }
   211 void cx_linked_list_insert_chain(
   212         void **begin,
   213         void **end,
   214         ptrdiff_t loc_prev,
   215         ptrdiff_t loc_next,
   216         void *node,
   217         void *insert_begin,
   218         void *insert_end
   219 ) {
   220     // find the end of the chain, if not specified
   221     if (insert_end == NULL) {
   222         insert_end = cx_linked_list_last(insert_begin, loc_next);
   223     }
   225     // determine the successor
   226     void *successor;
   227     if (node == NULL) {
   228         assert(begin != NULL || (end != NULL && loc_prev >= 0));
   229         if (begin != NULL) {
   230             successor = *begin;
   231             *begin = insert_begin;
   232         } else {
   233             successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
   234         }
   235     } else {
   236         successor = ll_next(node);
   237         cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
   238     }
   240     if (successor == NULL) {
   241         // the list ends with the new chain
   242         if (end != NULL) {
   243             *end = insert_end;
   244         }
   245     } else {
   246         cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
   247     }
   248 }
   250 void cx_linked_list_remove(
   251         void **begin,
   252         void **end,
   253         ptrdiff_t loc_prev,
   254         ptrdiff_t loc_next,
   255         void *node
   256 ) {
   257     assert(node != NULL);
   258     assert(loc_next >= 0);
   259     assert(loc_prev >= 0 || begin != NULL);
   261     // find adjacent nodes
   262     void *next = ll_next(node);
   263     void *prev;
   264     if (loc_prev >= 0) {
   265         prev = ll_prev(node);
   266     } else {
   267         prev = cx_linked_list_prev(*begin, loc_next, node);
   268     }
   270     // update next pointer of prev node, or set begin
   271     if (prev == NULL) {
   272         if (begin != NULL) {
   273             *begin = next;
   274         }
   275     } else {
   276         ll_next(prev) = next;
   277     }
   279     // update prev pointer of next node, or set end
   280     if (next == NULL) {
   281         if (end != NULL) {
   282             *end = prev;
   283         }
   284     } else if (loc_prev >= 0) {
   285         ll_prev(next) = prev;
   286     }
   287 }
   289 size_t cx_linked_list_size(
   290         void const *node,
   291         ptrdiff_t loc_next
   292 ) {
   293     assert(loc_next >= 0);
   294     size_t size = 0;
   295     while (node != NULL) {
   296         node = ll_next(node);
   297         size++;
   298     }
   299     return size;
   300 }
   302 #ifndef CX_LINKED_LIST_SORT_SBO_SIZE
   303 #define CX_LINKED_LIST_SORT_SBO_SIZE 1024
   304 #endif
   306 static void cx_linked_list_sort_merge(
   307         ptrdiff_t loc_prev,
   308         ptrdiff_t loc_next,
   309         ptrdiff_t loc_data,
   310         size_t length,
   311         void *ls,
   312         void *le,
   313         void *re,
   314         cx_compare_func cmp_func,
   315         void **begin,
   316         void **end
   317 ) {
   318     void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE];
   319     void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ?
   320                     malloc(sizeof(void *) * length) : sbo;
   321     if (sorted == NULL) abort();
   322     void *rc, *lc;
   324     lc = ls;
   325     rc = le;
   326     size_t n = 0;
   327     while (lc && lc != le && rc != re) {
   328         if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
   329             sorted[n] = lc;
   330             lc = ll_next(lc);
   331         } else {
   332             sorted[n] = rc;
   333             rc = ll_next(rc);
   334         }
   335         n++;
   336     }
   337     while (lc && lc != le) {
   338         sorted[n] = lc;
   339         lc = ll_next(lc);
   340         n++;
   341     }
   342     while (rc && rc != re) {
   343         sorted[n] = rc;
   344         rc = ll_next(rc);
   345         n++;
   346     }
   348     // Update pointer
   349     if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
   350     cx_for_n (i, length - 1) {
   351         cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
   352     }
   353     ll_next(sorted[length - 1]) = NULL;
   355     *begin = sorted[0];
   356     *end = sorted[length-1];
   357     if (sorted != sbo) {
   358         free(sorted);
   359     }
   360 }
   362 void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function
   363         void **begin,
   364         void **end,
   365         ptrdiff_t loc_prev,
   366         ptrdiff_t loc_next,
   367         ptrdiff_t loc_data,
   368         cx_compare_func cmp_func
   369 ) {
   370     assert(begin != NULL);
   371     assert(loc_next >= 0);
   372     assert(loc_data >= 0);
   373     assert(cmp_func);
   375     void *lc, *ls, *le, *re;
   377     // set start node
   378     ls = *begin;
   380     // early exit when this list is empty
   381     if (ls == NULL) return;
   383     // check how many elements are already sorted
   384     lc = ls;
   385     size_t ln = 1;
   386     while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
   387         lc = ll_next(lc);
   388         ln++;
   389     }
   390     le = ll_next(lc);
   392     // if first unsorted node is NULL, the list is already completely sorted
   393     if (le != NULL) {
   394         void *rc;
   395         size_t rn = 1;
   396         rc = le;
   397         // skip already sorted elements
   398         while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
   399             rc = ll_next(rc);
   400             rn++;
   401         }
   402         re = ll_next(rc);
   404         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   405         void *sorted_begin, *sorted_end;
   406         cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
   407                                   ln + rn, ls, le, re, cmp_func,
   408                                   &sorted_begin, &sorted_end);
   410         // Something left? Sort it!
   411         size_t remainder_length = cx_linked_list_size(re, loc_next);
   412         if (remainder_length > 0) {
   413             void *remainder = re;
   414             cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, cmp_func);
   416             // merge sorted list with (also sorted) remainder
   417             cx_linked_list_sort_merge(loc_prev, loc_next, loc_data,
   418                                       ln + rn + remainder_length,
   419                                       sorted_begin, remainder, NULL, cmp_func,
   420                                       &sorted_begin, &sorted_end);
   421         }
   422         *begin = sorted_begin;
   423         if (end) *end = sorted_end;
   424     }
   425 }
   427 int cx_linked_list_compare(
   428         void const *begin_left,
   429         void const *begin_right,
   430         ptrdiff_t loc_advance,
   431         ptrdiff_t loc_data,
   432         cx_compare_func cmp_func
   433 ) {
   434     void const *left = begin_left, *right = begin_right;
   436     while (left != NULL && right != NULL) {
   437         void const *left_data = ll_data(left);
   438         void const *right_data = ll_data(right);
   439         int result = cmp_func(left_data, right_data);
   440         if (result != 0) return result;
   441         left = ll_advance(left);
   442         right = ll_advance(right);
   443     }
   445     if (left != NULL) { return 1; }
   446     else if (right != NULL) { return -1; }
   447     else { return 0; }
   448 }
   450 void cx_linked_list_reverse(
   451         void **begin,
   452         void **end,
   453         ptrdiff_t loc_prev,
   454         ptrdiff_t loc_next
   455 ) {
   456     assert(begin != NULL);
   457     assert(loc_next >= 0);
   459     // swap all links
   460     void *prev = NULL;
   461     void *cur = *begin;
   462     while (cur != NULL) {
   463         void *next = ll_next(cur);
   465         ll_next(cur) = prev;
   466         if (loc_prev >= 0) {
   467             ll_prev(cur) = next;
   468         }
   470         prev = cur;
   471         cur = next;
   472     }
   474     // update begin and end
   475     if (end != NULL) {
   476         *end = *begin;
   477     }
   478     *begin = prev;
   479 }
   481 // HIGH LEVEL LINKED LIST IMPLEMENTATION
   483 bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
   485 typedef struct cx_linked_list_node cx_linked_list_node;
   486 struct cx_linked_list_node {
   487     cx_linked_list_node *prev;
   488     cx_linked_list_node *next;
   489     char payload[];
   490 };
   492 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
   493 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
   494 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
   496 typedef struct {
   497     struct cx_list_s base;
   498     cx_linked_list_node *begin;
   499     cx_linked_list_node *end;
   500 } cx_linked_list;
   502 static cx_linked_list_node *cx_ll_node_at(
   503         cx_linked_list const *list,
   504         size_t index
   505 ) {
   506     if (index >= list->base.size) {
   507         return NULL;
   508     } else if (index > list->base.size / 2) {
   509         return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
   510     } else {
   511         return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
   512     }
   513 }
   515 static int cx_ll_insert_at(
   516         struct cx_list_s *list,
   517         cx_linked_list_node *node,
   518         void const *elem
   519 ) {
   521     // create the new new_node
   522     cx_linked_list_node *new_node = cxMalloc(list->allocator,
   523                                              sizeof(cx_linked_list_node) + list->item_size);
   525     // sortir if failed
   526     if (new_node == NULL) return 1;
   528     // initialize new new_node
   529     new_node->prev = new_node->next = NULL;
   530     memcpy(new_node->payload, elem, list->item_size);
   532     // insert
   533     cx_linked_list *ll = (cx_linked_list *) list;
   534     cx_linked_list_insert_chain(
   535             (void **) &ll->begin, (void **) &ll->end,
   536             CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
   537             node, new_node, new_node
   538     );
   540     // increase the size and return
   541     list->size++;
   542     return 0;
   543 }
   545 static size_t cx_ll_insert_array(
   546         struct cx_list_s *list,
   547         size_t index,
   548         void const *array,
   549         size_t n
   550 ) {
   551     // out-of bounds and corner case check
   552     if (index > list->size || n == 0) return 0;
   554     // find position efficiently
   555     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
   557     // perform first insert
   558     if (0 != cx_ll_insert_at(list, node, array)) {
   559         return 1;
   560     }
   562     // is there more?
   563     if (n == 1) return 1;
   565     // we now know exactly where we are
   566     node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
   568     // we can add the remaining nodes and immedately advance to the inserted node
   569     char const *source = array;
   570     for (size_t i = 1; i < n; i++) {
   571         source += list->item_size;
   572         if (0 != cx_ll_insert_at(list, node, source)) {
   573             return i;
   574         }
   575         node = node->next;
   576     }
   577     return n;
   578 }
   580 static int cx_ll_insert_element(
   581         struct cx_list_s *list,
   582         size_t index,
   583         void const *element
   584 ) {
   585     return 1 != cx_ll_insert_array(list, index, element, 1);
   586 }
   588 static int cx_ll_remove(
   589         struct cx_list_s *list,
   590         size_t index
   591 ) {
   592     cx_linked_list *ll = (cx_linked_list *) list;
   593     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   595     // out-of-bounds check
   596     if (node == NULL) return 1;
   598     // element destruction
   599     cx_invoke_destructor(list, node->payload);
   601     // remove
   602     cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   603                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   605     // adjust size
   606     list->size--;
   608     // free and return
   609     cxFree(list->allocator, node);
   611     return 0;
   612 }
   614 static void cx_ll_clear(struct cx_list_s *list) {
   615     if (list->size == 0) return;
   617     cx_linked_list *ll = (cx_linked_list *) list;
   618     cx_linked_list_node *node = ll->begin;
   619     while (node != NULL) {
   620         cx_invoke_destructor(list, node->payload);
   621         cx_linked_list_node *next = node->next;
   622         cxFree(list->allocator, node);
   623         node = next;
   624     }
   625     ll->begin = ll->end = NULL;
   626     list->size = 0;
   627 }
   629 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
   630 #define CX_LINKED_LIST_SWAP_SBO_SIZE 128
   631 #endif
   633 static int cx_ll_swap(
   634         struct cx_list_s *list,
   635         size_t i,
   636         size_t j
   637 ) {
   638     if (i >= list->size || j >= list->size) return 1;
   639     if (i == j) return 0;
   641     // perform an optimized search that finds both elements in one run
   642     cx_linked_list *ll = (cx_linked_list *) list;
   643     size_t mid = list->size / 2;
   644     size_t left, right;
   645     if (i < j) {
   646         left = i;
   647         right = j;
   648     } else {
   649         left = j;
   650         right = i;
   651     }
   652     cx_linked_list_node *nleft, *nright;
   653     if (left < mid && right < mid) {
   654         // case 1: both items left from mid
   655         nleft = cx_ll_node_at(ll, left);
   656         nright = nleft;
   657         for (size_t c = left; c < right; c++) {
   658             nright = nright->next;
   659         }
   660     } else if (left >= mid && right >= mid) {
   661         // case 2: both items right from mid
   662         nright = cx_ll_node_at(ll, right);
   663         nleft = nright;
   664         for (size_t c = right; c > left; c--) {
   665             nleft = nleft->prev;
   666         }
   667     } else {
   668         // case 3: one item left, one item right
   670         // chose the closest to begin / end
   671         size_t closest;
   672         size_t other;
   673         size_t diff2boundary = list->size - right - 1;
   674         if (left <= diff2boundary) {
   675             closest = left;
   676             other = right;
   677             nleft = cx_ll_node_at(ll, left);
   678         } else {
   679             closest = right;
   680             other = left;
   681             diff2boundary = left;
   682             nright = cx_ll_node_at(ll, right);
   683         }
   685         // is other element closer to us or closer to boundary?
   686         if (right - left <= diff2boundary) {
   687             // search other element starting from already found element
   688             if (closest == left) {
   689                 nright = nleft;
   690                 for (size_t c = left; c < right; c++) {
   691                     nright = nright->next;
   692                 }
   693             } else {
   694                 nleft = nright;
   695                 for (size_t c = right; c > left; c--) {
   696                     nleft = nleft->prev;
   697                 }
   698             }
   699         } else {
   700             // search other element starting at the boundary
   701             if (closest == left) {
   702                 nright = cx_ll_node_at(ll, other);
   703             } else {
   704                 nleft = cx_ll_node_at(ll, other);
   705             }
   706         }
   707     }
   709     if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
   710         cx_linked_list_node *prev = nleft->prev;
   711         cx_linked_list_node *next = nright->next;
   712         cx_linked_list_node *midstart = nleft->next;
   713         cx_linked_list_node *midend = nright->prev;
   715         if (prev == NULL) {
   716             ll->begin = nright;
   717         } else {
   718             prev->next = nright;
   719         }
   720         nright->prev = prev;
   721         if (midstart == nright) {
   722             // special case: both nodes are adjacent
   723             nright->next = nleft;
   724             nleft->prev = nright;
   725         } else {
   726             // likely case: a chain is between the two nodes
   727             nright->next = midstart;
   728             midstart->prev = nright;
   729             midend->next = nleft;
   730             nleft->prev = midend;
   731         }
   732         nleft->next = next;
   733         if (next == NULL) {
   734             ll->end = nleft;
   735         } else {
   736             next->prev = nleft;
   737         }
   738     } else {
   739         // swap payloads to avoid relinking
   740         char buf[CX_LINKED_LIST_SWAP_SBO_SIZE];
   741         memcpy(buf, nleft->payload, list->item_size);
   742         memcpy(nleft->payload, nright->payload, list->item_size);
   743         memcpy(nright->payload, buf, list->item_size);
   744     }
   746     return 0;
   747 }
   749 static void *cx_ll_at(
   750         struct cx_list_s const *list,
   751         size_t index
   752 ) {
   753     cx_linked_list *ll = (cx_linked_list *) list;
   754     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   755     return node == NULL ? NULL : node->payload;
   756 }
   758 static ssize_t cx_ll_find_remove(
   759         struct cx_list_s *list,
   760         void const *elem,
   761         bool remove
   762 ) {
   763     if (remove) {
   764         cx_linked_list *ll = ((cx_linked_list *) list);
   765         cx_linked_list_node *node;
   766         ssize_t index = cx_linked_list_find_node(
   767                 (void **) &node,
   768                 ll->begin,
   769                 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   770                 list->cmpfunc, elem
   771         );
   772         if (node != NULL) {
   773             cx_invoke_destructor(list, node->payload);
   774             cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   775                                   CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   776             list->size--;
   777             cxFree(list->allocator, node);
   778         }
   779         return index;
   780     } else {
   781         return cx_linked_list_find(
   782                 ((cx_linked_list *) list)->begin,
   783                 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   784                 list->cmpfunc, elem
   785         );
   786     }
   787 }
   789 static void cx_ll_sort(struct cx_list_s *list) {
   790     cx_linked_list *ll = (cx_linked_list *) list;
   791     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   792                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   793                         list->cmpfunc);
   794 }
   796 static void cx_ll_reverse(struct cx_list_s *list) {
   797     cx_linked_list *ll = (cx_linked_list *) list;
   798     cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
   799 }
   801 static int cx_ll_compare(
   802         struct cx_list_s const *list,
   803         struct cx_list_s const *other
   804 ) {
   805     cx_linked_list *left = (cx_linked_list *) list;
   806     cx_linked_list *right = (cx_linked_list *) other;
   807     return cx_linked_list_compare(left->begin, right->begin,
   808                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   809                                   list->cmpfunc);
   810 }
   812 static bool cx_ll_iter_valid(void const *it) {
   813     struct cx_iterator_s const *iter = it;
   814     return iter->elem_handle != NULL;
   815 }
   817 static void cx_ll_iter_next(void *it) {
   818     struct cx_iterator_base_s *itbase = it;
   819     if (itbase->remove) {
   820         itbase->remove = false;
   821         struct cx_mut_iterator_s *iter = it;
   822         struct cx_list_s *list = iter->src_handle;
   823         cx_linked_list *ll = iter->src_handle;
   824         cx_linked_list_node *node = iter->elem_handle;
   825         iter->elem_handle = node->next;
   826         cx_invoke_destructor(list, node->payload);
   827         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   828                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   829         list->size--;
   830         cxFree(list->allocator, node);
   831     } else {
   832         struct cx_iterator_s *iter = it;
   833         iter->index++;
   834         cx_linked_list_node *node = iter->elem_handle;
   835         iter->elem_handle = node->next;
   836     }
   837 }
   839 static void cx_ll_iter_prev(void *it) {
   840     struct cx_iterator_base_s *itbase = it;
   841     if (itbase->remove) {
   842         itbase->remove = false;
   843         struct cx_mut_iterator_s *iter = it;
   844         struct cx_list_s *list = iter->src_handle;
   845         cx_linked_list *ll = iter->src_handle;
   846         cx_linked_list_node *node = iter->elem_handle;
   847         iter->elem_handle = node->prev;
   848         iter->index--;
   849         cx_invoke_destructor(list, node->payload);
   850         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   851                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   852         list->size--;
   853         cxFree(list->allocator, node);
   854     } else {
   855         struct cx_iterator_s *iter = it;
   856         iter->index--;
   857         cx_linked_list_node *node = iter->elem_handle;
   858         iter->elem_handle = node->prev;
   859     }
   860 }
   862 static void *cx_ll_iter_current(void const *it) {
   863     struct cx_iterator_s const *iter = it;
   864     cx_linked_list_node *node = iter->elem_handle;
   865     return node->payload;
   866 }
   868 static bool cx_ll_iter_flag_rm(void *it) {
   869     struct cx_iterator_base_s *iter = it;
   870     if (iter->mutating) {
   871         iter->remove = true;
   872         return true;
   873     } else {
   874         return false;
   875     }
   876 }
   878 static CxIterator cx_ll_iterator(
   879         struct cx_list_s const *list,
   880         size_t index,
   881         bool backwards
   882 ) {
   883     CxIterator iter;
   884     iter.index = index;
   885     iter.src_handle = list;
   886     iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
   887     iter.base.valid = cx_ll_iter_valid;
   888     iter.base.current = cx_ll_iter_current;
   889     iter.base.next = backwards ? cx_ll_iter_prev : cx_ll_iter_next;
   890     iter.base.flag_removal = cx_ll_iter_flag_rm;
   891     iter.base.mutating = false;
   892     iter.base.remove = false;
   893     return iter;
   894 }
   896 static int cx_ll_insert_iter(
   897         CxMutIterator *iter,
   898         void const *elem,
   899         int prepend
   900 ) {
   901     struct cx_list_s *list = iter->src_handle;
   902     cx_linked_list_node *node = iter->elem_handle;
   903     if (node != NULL) {
   904         assert(prepend >= 0 && prepend <= 1);
   905         cx_linked_list_node *choice[2] = {node, node->prev};
   906         int result = cx_ll_insert_at(list, choice[prepend], elem);
   907         iter->index += prepend * (0 == result);
   908         return result;
   909     } else {
   910         int result = cx_ll_insert_element(list, list->size, elem);
   911         iter->index = list->size;
   912         return result;
   913     }
   914 }
   916 static void cx_ll_destructor(CxList *list) {
   917     cx_linked_list *ll = (cx_linked_list *) list;
   919     cx_linked_list_node *node = ll->begin;
   920     while (node) {
   921         cx_invoke_destructor(list, node->payload);
   922         void *next = node->next;
   923         cxFree(list->allocator, node);
   924         node = next;
   925     }
   927     cxFree(list->allocator, list);
   928 }
   930 static cx_list_class cx_linked_list_class = {
   931         cx_ll_destructor,
   932         cx_ll_insert_element,
   933         cx_ll_insert_array,
   934         cx_ll_insert_iter,
   935         cx_ll_remove,
   936         cx_ll_clear,
   937         cx_ll_swap,
   938         cx_ll_at,
   939         cx_ll_find_remove,
   940         cx_ll_sort,
   941         cx_ll_compare,
   942         cx_ll_reverse,
   943         cx_ll_iterator,
   944 };
   946 CxList *cxLinkedListCreate(
   947         CxAllocator const *allocator,
   948         cx_compare_func comparator,
   949         size_t item_size
   950 ) {
   951     if (allocator == NULL) {
   952         allocator = cxDefaultAllocator;
   953     }
   955     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   956     if (list == NULL) return NULL;
   958     list->base.cl = &cx_linked_list_class;
   959     list->base.allocator = allocator;
   961     if (item_size > 0) {
   962         list->base.item_size = item_size;
   963         list->base.cmpfunc = comparator;
   964     } else {
   965         list->base.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator;
   966         cxListStorePointers((CxList *) list);
   967     }
   969     return (CxList *) list;
   970 }

mercurial