src/linked_list.c

Wed, 23 Nov 2022 22:40:55 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 23 Nov 2022 22:40:55 +0100
changeset 629
6c81ee4f11ad
parent 628
1e2be40f0cb5
child 630
ac5e7f789048
permissions
-rw-r--r--

#224 add cxListAddArray()

This also replaces cxLinkedListFromArray().

     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 int cx_ll_insert(
   522         struct cx_list_s *list,
   523         size_t index,
   524         void const *elem
   525 ) {
   526     // out-of bounds check
   527     if (index > list->size) return 1;
   529     // find position efficiently
   530     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
   532     // perform insert
   533     return cx_ll_insert_at(list, node, elem);
   534 }
   536 static int cx_ll_add(
   537         struct cx_list_s *list,
   538         void const *elem
   539 ) {
   540     return cx_ll_insert(list, list->size, elem);
   541 }
   543 static size_t cx_ll_add_array(
   544         struct cx_list_s *list,
   545         void const *array,
   546         size_t n
   547 ) {
   548     // TODO: redirect to cx_ll_insert_array
   549     cx_for_n (i, n) {
   550         if (cx_ll_add(list, ((char const *) array) + i * list->itemsize)) {
   551             return i;
   552         }
   553     }
   554     return n;
   555 }
   557 static int cx_pll_insert(
   558         struct cx_list_s *list,
   559         size_t index,
   560         void const *elem
   561 ) {
   562     return cx_ll_insert(list, index, &elem);
   563 }
   565 static int cx_pll_add(
   566         struct cx_list_s *list,
   567         void const *elem
   568 ) {
   569     return cx_ll_insert(list, list->size, &elem);
   570 }
   572 static int cx_ll_remove(
   573         struct cx_list_s *list,
   574         size_t index
   575 ) {
   576     cx_linked_list *ll = (cx_linked_list *) list;
   577     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   579     // out-of-bounds check
   580     if (node == NULL) return 1;
   582     // remove
   583     cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   584                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   586     // adjust size
   587     list->size--;
   589     // free and return
   590     cxFree(list->allocator, node);
   592     return 0;
   593 }
   595 static void *cx_ll_at(
   596         struct cx_list_s const *list,
   597         size_t index
   598 ) {
   599     cx_linked_list *ll = (cx_linked_list *) list;
   600     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   601     return node == NULL ? NULL : node->payload;
   602 }
   604 static void *cx_pll_at(
   605         struct cx_list_s const *list,
   606         size_t index
   607 ) {
   608     cx_linked_list *ll = (cx_linked_list *) list;
   609     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   610     return node == NULL ? NULL : *(void **) node->payload;
   611 }
   613 static size_t cx_ll_find(
   614         struct cx_list_s const *list,
   615         void const *elem
   616 ) {
   617     cx_linked_list *ll = (cx_linked_list *) list;
   618     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   619                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   620                                ll->follow_ptr, list->cmpfunc, elem);
   621 }
   623 static void cx_ll_sort(struct cx_list_s *list) {
   624     cx_linked_list *ll = (cx_linked_list *) list;
   625     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   626                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   627                         ll->follow_ptr, list->cmpfunc);
   628 }
   630 static void cx_ll_reverse(struct cx_list_s *list) {
   631     cx_linked_list *ll = (cx_linked_list *) list;
   632     cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
   633 }
   635 static int cx_ll_compare(
   636         struct cx_list_s const *list,
   637         struct cx_list_s const *other
   638 ) {
   639     cx_linked_list *left = (cx_linked_list *) list;
   640     cx_linked_list *right = (cx_linked_list *) other;
   641     return cx_linked_list_compare(left->begin, right->begin,
   642                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   643                                   left->follow_ptr, right->follow_ptr, list->cmpfunc);
   644 }
   646 static bool cx_ll_iter_valid(CxIterator const *iter) {
   647     return iter->elem_handle != NULL;
   648 }
   650 static void cx_ll_iter_next(CxIterator *iter) {
   651     if (iter->remove) {
   652         iter->remove = false;
   653         cx_linked_list *ll = iter->src_handle;
   654         cx_linked_list_node *node = iter->elem_handle;
   655         iter->elem_handle = node->next;
   656         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   657                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   658         ll->base.size--;
   659         cxFree(ll->base.allocator, node);
   660     } else {
   661         iter->index++;
   662         cx_linked_list_node *node = iter->elem_handle;
   663         iter->elem_handle = node->next;
   664     }
   665 }
   667 static void *cx_ll_iter_current(CxIterator const *iter) {
   668     cx_linked_list_node *node = iter->elem_handle;
   669     return node->payload;
   670 }
   672 static void *cx_pll_iter_current(CxIterator const *iter) {
   673     cx_linked_list_node *node = iter->elem_handle;
   674     return *(void **) node->payload;
   675 }
   677 static CxIterator cx_ll_iterator(
   678         struct cx_list_s *list,
   679         size_t index
   680 ) {
   681     CxIterator iter;
   682     iter.index = index;
   683     iter.src_handle = list;
   684     iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
   685     iter.valid = cx_ll_iter_valid;
   686     iter.current = cx_ll_iter_current;
   687     iter.next = cx_ll_iter_next;
   688     iter.remove = false;
   689     return iter;
   690 }
   692 static CxIterator cx_pll_iterator(
   693         struct cx_list_s *list,
   694         size_t index
   695 ) {
   696     CxIterator iter = cx_ll_iterator(list, index);
   697     iter.current = cx_pll_iter_current;
   698     return iter;
   699 }
   701 static int cx_ll_insert_iter(
   702         CxIterator *iter,
   703         void const *elem,
   704         int prepend
   705 ) {
   706     struct cx_list_s *list = iter->src_handle;
   707     cx_linked_list_node *node = iter->elem_handle;
   708     if (node != NULL) {
   709         assert(prepend >= 0 && prepend <= 1);
   710         cx_linked_list_node *choice[2] = {node, node->prev};
   711         int result = cx_ll_insert_at(list, choice[prepend], elem);
   712         iter->index += prepend * (0 == result);
   713         return result;
   714     } else {
   715         int result = cx_ll_insert(list, list->size, elem);
   716         iter->index = list->size;
   717         return result;
   718     }
   719 }
   721 static int cx_pll_insert_iter(
   722         CxIterator *iter,
   723         void const *elem,
   724         int prepend
   725 ) {
   726     return cx_ll_insert_iter(iter, &elem, prepend);
   727 }
   729 static void cx_ll_destructor(CxList *list) {
   730     cx_linked_list *ll = (cx_linked_list *) list;
   732     cx_linked_list_node *node = ll->begin;
   733     while (node) {
   734         void *next = node->next;
   735         cxFree(list->allocator, node);
   736         node = next;
   737     }
   738     // do not free the list pointer, this is just a destructor!
   739 }
   741 static cx_list_class cx_linked_list_class = {
   742         cx_ll_destructor,
   743         cx_ll_add,
   744         cx_ll_add_array,
   745         cx_ll_insert,
   746         cx_ll_insert_iter,
   747         cx_ll_remove,
   748         cx_ll_at,
   749         cx_ll_find,
   750         cx_ll_sort,
   751         cx_ll_compare,
   752         cx_ll_reverse,
   753         cx_ll_iterator
   754 };
   756 static cx_list_class cx_pointer_linked_list_class = {
   757         cx_ll_destructor,
   758         cx_pll_add,
   759         cx_ll_add_array,
   760         cx_pll_insert,
   761         cx_pll_insert_iter,
   762         cx_ll_remove,
   763         cx_pll_at,
   764         cx_ll_find,
   765         cx_ll_sort,
   766         cx_ll_compare,
   767         cx_ll_reverse,
   768         cx_pll_iterator,
   769 };
   771 CxList *cxLinkedListCreate(
   772         CxAllocator const *allocator,
   773         CxListComparator comparator,
   774         size_t item_size
   775 ) {
   776     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   777     if (list == NULL) return NULL;
   779     list->follow_ptr = false;
   780     list->base.cl = &cx_linked_list_class;
   781     list->base.allocator = allocator;
   782     list->base.cmpfunc = comparator;
   783     list->base.itemsize = item_size;
   784     list->base.capacity = SIZE_MAX;
   786     return (CxList *) list;
   787 }
   789 CxList *cxPointerLinkedListCreate(
   790         CxAllocator const *allocator,
   791         CxListComparator comparator
   792 ) {
   793     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   794     if (list == NULL) return NULL;
   796     list->follow_ptr = true;
   797     list->base.cl = &cx_pointer_linked_list_class;
   798     list->base.allocator = allocator;
   799     list->base.cmpfunc = comparator;
   800     list->base.itemsize = sizeof(void *);
   801     list->base.capacity = SIZE_MAX;
   803     return (CxList *) list;
   804 }

mercurial