src/linked_list.c

Mon, 23 Jan 2023 20:22:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 23 Jan 2023 20:22:11 +0100
changeset 638
eafb45eefc51
parent 630
ac5e7f789048
child 639
309e8b08c60e
permissions
-rw-r--r--

add cxListInsertArray() - fixes #224

     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 <stdint.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_f(node, follow_ptr) ((follow_ptr)?CX_LL_PTR(node, loc_data):(((char*)(node))+loc_data))
    42 #define ll_data(node) ll_data_f(node,follow_ptr)
    44 void *cx_linked_list_at(
    45         void const *start,
    46         size_t start_index,
    47         ptrdiff_t loc_advance,
    48         size_t index
    49 ) {
    50     assert(start != NULL);
    51     assert(loc_advance >= 0);
    52     size_t i = start_index;
    53     void const *cur = start;
    54     while (i != index && cur != NULL) {
    55         cur = ll_advance(cur);
    56         i < index ? i++ : i--;
    57     }
    58     return (void *) cur;
    59 }
    61 size_t cx_linked_list_find(
    62         void const *start,
    63         ptrdiff_t loc_advance,
    64         ptrdiff_t loc_data,
    65         bool follow_ptr,
    66         CxListComparator cmp_func,
    67         void const *elem
    68 ) {
    69     assert(start != NULL);
    70     assert(loc_advance >= 0);
    71     assert(loc_data >= 0);
    72     assert(cmp_func);
    74     void const *node = start;
    75     size_t index = 0;
    76     do {
    77         void *current = ll_data(node);
    78         if (cmp_func(current, elem) == 0) {
    79             return index;
    80         }
    81         node = ll_advance(node);
    82         index++;
    83     } while (node != NULL);
    84     return index;
    85 }
    87 void *cx_linked_list_first(
    88         void const *node,
    89         ptrdiff_t loc_prev
    90 ) {
    91     return cx_linked_list_last(node, loc_prev);
    92 }
    94 void *cx_linked_list_last(
    95         void const *node,
    96         ptrdiff_t loc_next
    97 ) {
    98     assert(node != NULL);
    99     assert(loc_next >= 0);
   101     void const *cur = node;
   102     void const *last;
   103     do {
   104         last = cur;
   105     } while ((cur = ll_next(cur)) != NULL);
   107     return (void *) last;
   108 }
   110 void *cx_linked_list_prev(
   111         void const *begin,
   112         ptrdiff_t loc_next,
   113         void const *node
   114 ) {
   115     assert(begin != NULL);
   116     assert(node != NULL);
   117     assert(loc_next >= 0);
   118     if (begin == node) return NULL;
   119     void const *cur = begin;
   120     void const *next;
   121     while (1) {
   122         next = ll_next(cur);
   123         if (next == node) return (void *) cur;
   124         cur = next;
   125     }
   126 }
   128 void cx_linked_list_link(
   129         void *left,
   130         void *right,
   131         ptrdiff_t loc_prev,
   132         ptrdiff_t loc_next
   133 ) {
   134     assert(loc_next >= 0);
   135     ll_next(left) = right;
   136     if (loc_prev >= 0) {
   137         ll_prev(right) = left;
   138     }
   139 }
   141 void cx_linked_list_unlink(
   142         void *left,
   143         void *right,
   144         ptrdiff_t loc_prev,
   145         ptrdiff_t loc_next
   146 ) {
   147     assert (loc_next >= 0);
   148     assert(ll_next(left) == right);
   149     ll_next(left) = NULL;
   150     if (loc_prev >= 0) {
   151         assert(ll_prev(right) == left);
   152         ll_prev(right) = NULL;
   153     }
   154 }
   156 void cx_linked_list_add(
   157         void **begin,
   158         void **end,
   159         ptrdiff_t loc_prev,
   160         ptrdiff_t loc_next,
   161         void *new_node
   162 ) {
   163     void *last;
   164     if (end == NULL) {
   165         assert(begin != NULL);
   166         last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
   167     } else {
   168         last = *end;
   169     }
   170     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
   171 }
   173 void cx_linked_list_prepend(
   174         void **begin,
   175         void **end,
   176         ptrdiff_t loc_prev,
   177         ptrdiff_t loc_next,
   178         void *new_node
   179 ) {
   180     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
   181 }
   183 void cx_linked_list_insert(
   184         void **begin,
   185         void **end,
   186         ptrdiff_t loc_prev,
   187         ptrdiff_t loc_next,
   188         void *node,
   189         void *new_node
   190 ) {
   191     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
   192 }
   194 void cx_linked_list_insert_chain(
   195         void **begin,
   196         void **end,
   197         ptrdiff_t loc_prev,
   198         ptrdiff_t loc_next,
   199         void *node,
   200         void *insert_begin,
   201         void *insert_end
   202 ) {
   203     // find the end of the chain, if not specified
   204     if (insert_end == NULL) {
   205         insert_end = cx_linked_list_last(insert_begin, loc_next);
   206     }
   208     // determine the successor
   209     void *successor;
   210     if (node == NULL) {
   211         assert(begin != NULL || (end != NULL && loc_prev >= 0));
   212         if (begin != NULL) {
   213             successor = *begin;
   214             *begin = insert_begin;
   215         } else {
   216             successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
   217         }
   218     } else {
   219         successor = ll_next(node);
   220         cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
   221     }
   223     if (successor == NULL) {
   224         // the list ends with the new chain
   225         if (end != NULL) {
   226             *end = insert_end;
   227         }
   228     } else {
   229         cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
   230     }
   231 }
   233 void cx_linked_list_remove(
   234         void **begin,
   235         void **end,
   236         ptrdiff_t loc_prev,
   237         ptrdiff_t loc_next,
   238         void *node
   239 ) {
   240     assert(node != NULL);
   241     assert(loc_next >= 0);
   242     assert(loc_prev >= 0 || begin != NULL);
   244     // find adjacent nodes
   245     void *next = ll_next(node);
   246     void *prev;
   247     if (loc_prev >= 0) {
   248         prev = ll_prev(node);
   249     } else {
   250         prev = cx_linked_list_prev(*begin, loc_next, node);
   251     }
   253     // update next pointer of prev node, or set begin
   254     if (prev == NULL) {
   255         if (begin != NULL) {
   256             *begin = next;
   257         }
   258     } else {
   259         ll_next(prev) = next;
   260     }
   262     // update prev pointer of next node, or set end
   263     if (next == NULL) {
   264         if (end != NULL) {
   265             *end = prev;
   266         }
   267     } else if (loc_prev >= 0) {
   268         ll_prev(next) = prev;
   269     }
   270 }
   272 size_t cx_linked_list_size(
   273         void const *node,
   274         ptrdiff_t loc_next
   275 ) {
   276     assert(loc_next >= 0);
   277     size_t size = 0;
   278     while (node != NULL) {
   279         node = ll_next(node);
   280         size++;
   281     }
   282     return size;
   283 }
   285 static void *cx_linked_list_sort_merge(
   286         ptrdiff_t loc_prev,
   287         ptrdiff_t loc_next,
   288         ptrdiff_t loc_data,
   289         bool follow_ptr,
   290         size_t length,
   291         void *ls,
   292         void *le,
   293         void *re,
   294         CxListComparator cmp_func
   295 ) {
   296     const size_t sbo_len = 1024;
   297     void *sbo[sbo_len];
   298     void **sorted = (length >= sbo_len) ? 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         bool follow_ptr,
   347         CxListComparator cmp_func
   348 ) {
   349     assert(begin != NULL);
   350     assert(loc_next >= 0);
   351     assert(loc_data >= 0);
   352     assert(cmp_func);
   354     void *lc, *ls, *le, *re;
   356     // set start node
   357     ls = *begin;
   359     // check how many elements are already sorted
   360     lc = ls;
   361     size_t ln = 1;
   362     while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
   363         lc = ll_next(lc);
   364         ln++;
   365     }
   366     le = ll_next(lc);
   368     // if first unsorted node is NULL, the list is already completely sorted
   369     if (le != NULL) {
   370         void *rc;
   371         size_t rn = 1;
   372         rc = le;
   373         // skip already sorted elements
   374         while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
   375             rc = ll_next(rc);
   376             rn++;
   377         }
   378         re = ll_next(rc);
   380         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   381         void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
   382                                                  ln + rn, ls, le, re, cmp_func);
   384         // Something left? Sort it!
   385         size_t remainder_length = cx_linked_list_size(re, loc_next);
   386         if (remainder_length > 0) {
   387             void *remainder = re;
   388             cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func);
   390             // merge sorted list with (also sorted) remainder
   391             *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
   392                                                ln + rn + remainder_length,
   393                                                sorted, remainder, NULL, cmp_func);
   394         } else {
   395             // no remainder - we've got our sorted list
   396             *begin = sorted;
   397         }
   398         if (end) *end = cx_linked_list_last(sorted, loc_next);
   399     }
   400 }
   402 int cx_linked_list_compare(
   403         void const *begin_left,
   404         void const *begin_right,
   405         ptrdiff_t loc_advance,
   406         ptrdiff_t loc_data,
   407         bool follow_ptr_left,
   408         bool follow_ptr_right,
   409         CxListComparator cmp_func
   410 ) {
   411     void const *left = begin_left, *right = begin_right;
   413     while (left != NULL && right != NULL) {
   414         void const *left_data = ll_data_f(left, follow_ptr_left);
   415         void const *right_data = ll_data_f(right, follow_ptr_right);
   416         int result = cmp_func(left_data, right_data);
   417         if (result != 0) return result;
   418         left = ll_advance(left);
   419         right = ll_advance(right);
   420     }
   422     if (left != NULL) { return 1; }
   423     else if (right != NULL) { return -1; }
   424     else { return 0; }
   425 }
   427 void cx_linked_list_reverse(
   428         void **begin,
   429         void **end,
   430         ptrdiff_t loc_prev,
   431         ptrdiff_t loc_next
   432 ) {
   433     assert(begin != NULL);
   434     assert(loc_next >= 0);
   436     // swap all links
   437     void *prev = NULL;
   438     void *cur = *begin;
   439     while (cur != NULL) {
   440         void *next = ll_next(cur);
   442         ll_next(cur) = prev;
   443         if (loc_prev >= 0) {
   444             ll_prev(cur) = next;
   445         }
   447         prev = cur;
   448         cur = next;
   449     }
   451     // update begin and end
   452     if (end != NULL) {
   453         *end = *begin;
   454     }
   455     *begin = prev;
   456 }
   458 // HIGH LEVEL LINKED LIST IMPLEMENTATION
   460 typedef struct cx_linked_list_node cx_linked_list_node;
   461 struct cx_linked_list_node {
   462     cx_linked_list_node *prev;
   463     cx_linked_list_node *next;
   464     char payload[];
   465 };
   467 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
   468 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
   469 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
   471 typedef struct {
   472     struct cx_list_s base;
   473     cx_linked_list_node *begin;
   474     cx_linked_list_node *end;
   475     bool follow_ptr;
   476 } cx_linked_list;
   478 static cx_linked_list_node *cx_ll_node_at(
   479         cx_linked_list const *list,
   480         size_t index
   481 ) {
   482     if (index >= list->base.size) {
   483         return NULL;
   484     } else if (index > list->base.size / 2) {
   485         return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
   486     } else {
   487         return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
   488     }
   489 }
   491 static int cx_ll_insert_at(
   492         struct cx_list_s *list,
   493         cx_linked_list_node *node,
   494         void const *elem
   495 ) {
   497     // create the new new_node
   498     cx_linked_list_node *new_node = cxMalloc(list->allocator,
   499                                              sizeof(cx_linked_list_node) + list->itemsize);
   501     // sortir if failed
   502     if (new_node == NULL) return 1;
   504     // initialize new new_node
   505     new_node->prev = new_node->next = NULL;
   506     memcpy(new_node->payload, elem, list->itemsize);
   508     // insert
   509     cx_linked_list *ll = (cx_linked_list *) list;
   510     cx_linked_list_insert_chain(
   511             (void **) &ll->begin, (void **) &ll->end,
   512             CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
   513             node, new_node, new_node
   514     );
   516     // increase the size and return
   517     list->size++;
   518     return 0;
   519 }
   521 static size_t cx_ll_insert_array(
   522         struct cx_list_s *list,
   523         size_t index,
   524         void const *array,
   525         size_t n
   526 ) {
   527     // out-of bounds and corner case check
   528     if (index > list->size || n == 0) return 0;
   530     // find position efficiently
   531     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
   533     // perform first insert
   534     if (0 != cx_ll_insert_at(list, node, array)) {
   535         return 1;
   536     }
   538     // is there more?
   539     if (n == 1) return 1;
   541     // we now know exactly where we are
   542     node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
   544     // we can add the remaining nodes and immedately advance to the inserted node
   545     char const *source = array;
   546     for (size_t i = 1; i < n; i++) {
   547         source += list->itemsize;
   548         if (0 != cx_ll_insert_at(list, node, source)) {
   549             return i;
   550         }
   551         node = node->next;
   552     }
   553     return n;
   554 }
   556 static int cx_ll_insert(
   557         struct cx_list_s *list,
   558         size_t index,
   559         void const *elem
   560 ) {
   561     return cx_ll_insert_array(list, index, elem, 1) != 1;
   562 }
   564 static int cx_ll_add(
   565         struct cx_list_s *list,
   566         void const *elem
   567 ) {
   568     return cx_ll_insert(list, list->size, elem);
   569 }
   571 static size_t cx_ll_add_array(
   572         struct cx_list_s *list,
   573         void const *array,
   574         size_t n
   575 ) {
   576     return cx_ll_insert_array(list, list->size, array, n);
   577 }
   579 static int cx_pll_insert(
   580         struct cx_list_s *list,
   581         size_t index,
   582         void const *elem
   583 ) {
   584     return cx_ll_insert(list, index, &elem);
   585 }
   587 static int cx_pll_add(
   588         struct cx_list_s *list,
   589         void const *elem
   590 ) {
   591     return cx_ll_insert(list, list->size, &elem);
   592 }
   594 static int cx_ll_remove(
   595         struct cx_list_s *list,
   596         size_t index
   597 ) {
   598     cx_linked_list *ll = (cx_linked_list *) list;
   599     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   601     // out-of-bounds check
   602     if (node == NULL) return 1;
   604     // remove
   605     cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   606                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   608     // adjust size
   609     list->size--;
   611     // free and return
   612     cxFree(list->allocator, node);
   614     return 0;
   615 }
   617 static void *cx_ll_at(
   618         struct cx_list_s const *list,
   619         size_t index
   620 ) {
   621     cx_linked_list *ll = (cx_linked_list *) list;
   622     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   623     return node == NULL ? NULL : node->payload;
   624 }
   626 static void *cx_pll_at(
   627         struct cx_list_s const *list,
   628         size_t index
   629 ) {
   630     cx_linked_list *ll = (cx_linked_list *) list;
   631     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   632     return node == NULL ? NULL : *(void **) node->payload;
   633 }
   635 static size_t cx_ll_find(
   636         struct cx_list_s const *list,
   637         void const *elem
   638 ) {
   639     cx_linked_list *ll = (cx_linked_list *) list;
   640     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   641                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   642                                ll->follow_ptr, list->cmpfunc, elem);
   643 }
   645 static void cx_ll_sort(struct cx_list_s *list) {
   646     cx_linked_list *ll = (cx_linked_list *) list;
   647     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   648                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   649                         ll->follow_ptr, list->cmpfunc);
   650 }
   652 static void cx_ll_reverse(struct cx_list_s *list) {
   653     cx_linked_list *ll = (cx_linked_list *) list;
   654     cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
   655 }
   657 static int cx_ll_compare(
   658         struct cx_list_s const *list,
   659         struct cx_list_s const *other
   660 ) {
   661     cx_linked_list *left = (cx_linked_list *) list;
   662     cx_linked_list *right = (cx_linked_list *) other;
   663     return cx_linked_list_compare(left->begin, right->begin,
   664                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   665                                   left->follow_ptr, right->follow_ptr, list->cmpfunc);
   666 }
   668 static bool cx_ll_iter_valid(void const *it) {
   669     struct cx_iterator_s const *iter = it;
   670     return iter->elem_handle != NULL;
   671 }
   673 static void cx_ll_iter_next(void *it) {
   674     struct cx_iterator_base_s *itbase = it;
   675     if (itbase->remove) {
   676         itbase->remove = false;
   677         struct cx_mut_iterator_s *iter = it;
   678         cx_linked_list *ll = iter->src_handle;
   679         cx_linked_list_node *node = iter->elem_handle;
   680         iter->elem_handle = node->next;
   681         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   682                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   683         ll->base.size--;
   684         cxFree(ll->base.allocator, node);
   685     } else {
   686         struct cx_iterator_s *iter = it;
   687         iter->index++;
   688         cx_linked_list_node *node = iter->elem_handle;
   689         iter->elem_handle = node->next;
   690     }
   691 }
   693 static void *cx_ll_iter_current(void const *it) {
   694     struct cx_iterator_s const *iter = it;
   695     cx_linked_list_node *node = iter->elem_handle;
   696     return node->payload;
   697 }
   699 static void *cx_pll_iter_current(void const *it) {
   700     struct cx_iterator_s const *iter = it;
   701     cx_linked_list_node *node = iter->elem_handle;
   702     return *(void **) node->payload;
   703 }
   705 static bool cx_ll_iter_flag_rm(void *it) {
   706     struct cx_iterator_base_s *iter = it;
   707     if (iter->mutating) {
   708         iter->remove = true;
   709         return true;
   710     } else {
   711         return false;
   712     }
   713 }
   715 static CxIterator cx_ll_iterator(
   716         struct cx_list_s const *list,
   717         size_t index
   718 ) {
   719     CxIterator iter;
   720     iter.index = index;
   721     iter.src_handle = list;
   722     iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
   723     iter.base.valid = cx_ll_iter_valid;
   724     iter.base.current = cx_ll_iter_current;
   725     iter.base.next = cx_ll_iter_next;
   726     iter.base.flag_removal = cx_ll_iter_flag_rm;
   727     iter.base.mutating = false;
   728     iter.base.remove = false;
   729     return iter;
   730 }
   732 static CxIterator cx_pll_iterator(
   733         struct cx_list_s const *list,
   734         size_t index
   735 ) {
   736     CxIterator iter = cx_ll_iterator(list, index);
   737     iter.base.current = cx_pll_iter_current;
   738     return iter;
   739 }
   741 static CxMutIterator cx_ll_mut_iterator(
   742         struct cx_list_s *list,
   743         size_t index
   744 ) {
   745     CxIterator it = cx_ll_iterator(list, index);
   746     it.base.mutating = true;
   748     // we know the iterators share the same memory layout
   749     CxMutIterator iter;
   750     memcpy(&iter, &it, sizeof(CxMutIterator));
   751     return iter;
   752 }
   754 static CxMutIterator cx_pll_mut_iterator(
   755         struct cx_list_s *list,
   756         size_t index
   757 ) {
   758     CxMutIterator iter = cx_ll_mut_iterator(list, index);
   759     iter.base.current = cx_pll_iter_current;
   760     return iter;
   761 }
   763 static int cx_ll_insert_iter(
   764         CxMutIterator *iter,
   765         void const *elem,
   766         int prepend
   767 ) {
   768     struct cx_list_s *list = iter->src_handle;
   769     cx_linked_list_node *node = iter->elem_handle;
   770     if (node != NULL) {
   771         assert(prepend >= 0 && prepend <= 1);
   772         cx_linked_list_node *choice[2] = {node, node->prev};
   773         int result = cx_ll_insert_at(list, choice[prepend], elem);
   774         iter->index += prepend * (0 == result);
   775         return result;
   776     } else {
   777         int result = cx_ll_insert(list, list->size, elem);
   778         iter->index = list->size;
   779         return result;
   780     }
   781 }
   783 static int cx_pll_insert_iter(
   784         CxMutIterator *iter,
   785         void const *elem,
   786         int prepend
   787 ) {
   788     return cx_ll_insert_iter(iter, &elem, prepend);
   789 }
   791 static void cx_ll_destructor(CxList *list) {
   792     cx_linked_list *ll = (cx_linked_list *) list;
   794     cx_linked_list_node *node = ll->begin;
   795     while (node) {
   796         void *next = node->next;
   797         cxFree(list->allocator, node);
   798         node = next;
   799     }
   800     // do not free the list pointer, this is just a destructor!
   801 }
   803 static cx_list_class cx_linked_list_class = {
   804         cx_ll_destructor,
   805         cx_ll_add,
   806         cx_ll_add_array,
   807         cx_ll_insert,
   808         cx_ll_insert_array,
   809         cx_ll_insert_iter,
   810         cx_ll_remove,
   811         cx_ll_at,
   812         cx_ll_find,
   813         cx_ll_sort,
   814         cx_ll_compare,
   815         cx_ll_reverse,
   816         cx_ll_iterator,
   817         cx_ll_mut_iterator,
   818 };
   820 static cx_list_class cx_pointer_linked_list_class = {
   821         cx_ll_destructor,
   822         cx_pll_add,
   823         cx_ll_add_array,
   824         cx_pll_insert,
   825         cx_ll_insert_array,
   826         cx_pll_insert_iter,
   827         cx_ll_remove,
   828         cx_pll_at,
   829         cx_ll_find,
   830         cx_ll_sort,
   831         cx_ll_compare,
   832         cx_ll_reverse,
   833         cx_pll_iterator,
   834         cx_pll_mut_iterator,
   835 };
   837 CxList *cxLinkedListCreate(
   838         CxAllocator const *allocator,
   839         CxListComparator comparator,
   840         size_t item_size
   841 ) {
   842     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   843     if (list == NULL) return NULL;
   845     list->follow_ptr = false;
   846     list->base.cl = &cx_linked_list_class;
   847     list->base.allocator = allocator;
   848     list->base.cmpfunc = comparator;
   849     list->base.itemsize = item_size;
   850     list->base.capacity = SIZE_MAX;
   852     return (CxList *) list;
   853 }
   855 CxList *cxPointerLinkedListCreate(
   856         CxAllocator const *allocator,
   857         CxListComparator comparator
   858 ) {
   859     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   860     if (list == NULL) return NULL;
   862     list->follow_ptr = true;
   863     list->base.cl = &cx_pointer_linked_list_class;
   864     list->base.allocator = allocator;
   865     list->base.cmpfunc = comparator;
   866     list->base.itemsize = sizeof(void *);
   867     list->base.capacity = SIZE_MAX;
   869     return (CxList *) list;
   870 }

mercurial