src/linked_list.c

Sat, 09 Apr 2022 16:37:43 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 09 Apr 2022 16:37:43 +0200
changeset 508
8aea65ae1eaf
parent 503
a89857072ace
child 509
0d3c6075f82c
permissions
-rw-r--r--

#168 - add attributes and const

     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 <stdint.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) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data))
    42 void *cx_linked_list_at(
    43         void const *start,
    44         size_t start_index,
    45         ptrdiff_t loc_advance,
    46         size_t index
    47 ) {
    48     assert(start != NULL);
    49     assert(loc_advance >= 0);
    50     size_t i = start_index;
    51     void const *cur = start;
    52     while (i != index && cur != NULL) {
    53         cur = ll_advance(cur);
    54         i < index ? i++ : i--;
    55     }
    56     return (void *) cur;
    57 }
    59 size_t cx_linked_list_find(
    60         void const *start,
    61         ptrdiff_t loc_advance,
    62         ptrdiff_t loc_data,
    63         bool follow_ptr,
    64         CxListComparator cmp_func,
    65         void const *elem
    66 ) {
    67     assert(start != NULL);
    68     assert(loc_advance >= 0);
    69     assert(loc_data >= 0);
    70     assert(cmp_func);
    72     void const *node = start;
    73     size_t index = 0;
    74     do {
    75         void *current = ll_data(node);
    76         if (cmp_func(current, elem) == 0) {
    77             return index;
    78         }
    79         node = ll_advance(node);
    80         index++;
    81     } while (node != NULL);
    82     return index;
    83 }
    85 void *cx_linked_list_first(
    86         void const *node,
    87         ptrdiff_t loc_prev
    88 ) {
    89     return cx_linked_list_last(node, loc_prev);
    90 }
    92 void *cx_linked_list_last(
    93         void const *node,
    94         ptrdiff_t loc_next
    95 ) {
    96     assert(node != NULL);
    97     assert(loc_next >= 0);
    99     void const *cur = node;
   100     void const *last;
   101     do {
   102         last = cur;
   103     } while ((cur = ll_next(cur)) != NULL);
   105     return (void *) last;
   106 }
   108 void *cx_linked_list_prev(
   109         void const *begin,
   110         ptrdiff_t loc_next,
   111         void const *node
   112 ) {
   113     assert(begin != NULL);
   114     assert(node != NULL);
   115     assert(loc_next >= 0);
   116     if (begin == node) return NULL;
   117     void const *cur = begin;
   118     void const *next;
   119     while (1) {
   120         next = ll_next(cur);
   121         if (next == node) return (void *) cur;
   122         cur = next;
   123     }
   124 }
   126 void cx_linked_list_link(
   127         void *left,
   128         void *right,
   129         ptrdiff_t loc_prev,
   130         ptrdiff_t loc_next
   131 ) {
   132     assert(loc_next >= 0);
   133     ll_next(left) = right;
   134     if (loc_prev >= 0) {
   135         ll_prev(right) = left;
   136     }
   137 }
   139 void cx_linked_list_unlink(
   140         void *left,
   141         void *right,
   142         ptrdiff_t loc_prev,
   143         ptrdiff_t loc_next
   144 ) {
   145     assert (loc_next >= 0);
   146     assert(ll_next(left) == right);
   147     ll_next(left) = NULL;
   148     if (loc_prev >= 0) {
   149         assert(ll_prev(right) == left);
   150         ll_prev(right) = NULL;
   151     }
   152 }
   154 void cx_linked_list_add(
   155         void **begin,
   156         void **end,
   157         ptrdiff_t loc_prev,
   158         ptrdiff_t loc_next,
   159         void *new_node
   160 ) {
   161     void *last;
   162     if (end == NULL) {
   163         assert(begin != NULL);
   164         last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
   165     } else {
   166         last = *end;
   167     }
   168     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
   169 }
   171 void cx_linked_list_prepend(
   172         void **begin,
   173         void **end,
   174         ptrdiff_t loc_prev,
   175         ptrdiff_t loc_next,
   176         void *new_node
   177 ) {
   178     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
   179 }
   181 void cx_linked_list_insert(
   182         void **begin,
   183         void **end,
   184         ptrdiff_t loc_prev,
   185         ptrdiff_t loc_next,
   186         void *node,
   187         void *new_node
   188 ) {
   189     cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
   190 }
   192 void cx_linked_list_insert_chain(
   193         void **begin,
   194         void **end,
   195         ptrdiff_t loc_prev,
   196         ptrdiff_t loc_next,
   197         void *node,
   198         void *insert_begin,
   199         void *insert_end
   200 ) {
   201     // find the end of the chain, if not specified
   202     if (insert_end == NULL) {
   203         insert_end = cx_linked_list_last(insert_begin, loc_next);
   204     }
   206     // determine the successor
   207     void *successor;
   208     if (node == NULL) {
   209         assert(begin != NULL || (end != NULL && loc_prev >= 0));
   210         if (begin != NULL) {
   211             successor = *begin;
   212             *begin = insert_begin;
   213         } else {
   214             successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
   215         }
   216     } else {
   217         successor = ll_next(node);
   218         cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
   219     }
   221     if (successor == NULL) {
   222         // the list ends with the new chain
   223         if (end != NULL) {
   224             *end = insert_end;
   225         }
   226     } else {
   227         cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
   228     }
   229 }
   231 void cx_linked_list_remove(
   232         void **begin,
   233         void **end,
   234         ptrdiff_t loc_prev,
   235         ptrdiff_t loc_next,
   236         void *node
   237 ) {
   238     assert(node != NULL);
   239     assert(loc_next >= 0);
   240     assert(loc_prev >= 0 || begin != NULL);
   242     // find adjacent nodes
   243     void *next = ll_next(node);
   244     void *prev;
   245     if (loc_prev >= 0) {
   246         prev = ll_prev(node);
   247     } else {
   248         prev = cx_linked_list_prev(*begin, loc_next, node);
   249     }
   251     // update next pointer of prev node, or set begin
   252     if (prev == NULL) {
   253         if (begin != NULL) {
   254             *begin = next;
   255         }
   256     } else {
   257         ll_next(prev) = next;
   258     }
   260     // update prev pointer of next node, or set end
   261     if (next == NULL) {
   262         if (end != NULL) {
   263             *end = prev;
   264         }
   265     } else if (loc_prev >= 0) {
   266         ll_prev(next) = prev;
   267     }
   268 }
   270 size_t cx_linked_list_size(
   271         void const *node,
   272         ptrdiff_t loc_next
   273 ) {
   274     assert(loc_next >= 0);
   275     size_t size = 0;
   276     while (node != NULL) {
   277         node = ll_next(node);
   278         size++;
   279     }
   280     return size;
   281 }
   283 static void *cx_linked_list_sort_merge(
   284         ptrdiff_t loc_prev,
   285         ptrdiff_t loc_next,
   286         ptrdiff_t loc_data,
   287         bool follow_ptr,
   288         size_t length,
   289         void *ls,
   290         void *le,
   291         void *re,
   292         CxListComparator cmp_func
   293 ) {
   294     const size_t sbo_len = 1024;
   295     void *sbo[sbo_len];
   296     void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
   297     if (sorted == NULL) abort();
   298     void *rc, *lc;
   300     lc = ls;
   301     rc = le;
   302     size_t n = 0;
   303     while (lc && lc != le && rc != re) {
   304         if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
   305             sorted[n] = lc;
   306             lc = ll_next(lc);
   307         } else {
   308             sorted[n] = rc;
   309             rc = ll_next(rc);
   310         }
   311         n++;
   312     }
   313     while (lc && lc != le) {
   314         sorted[n] = lc;
   315         lc = ll_next(lc);
   316         n++;
   317     }
   318     while (rc && rc != re) {
   319         sorted[n] = rc;
   320         rc = ll_next(rc);
   321         n++;
   322     }
   324     // Update pointer
   325     if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
   326     for (size_t i = 0; i < length - 1; i++) {
   327         cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
   328     }
   329     ll_next(sorted[length - 1]) = NULL;
   331     void *ret = sorted[0];
   332     if (sorted != sbo) {
   333         free(sorted);
   334     }
   335     return ret;
   336 }
   338 void cx_linked_list_sort( /* NOLINT(misc-no-recursion) - purposely recursive function */
   339         void **begin,
   340         void **end,
   341         ptrdiff_t loc_prev,
   342         ptrdiff_t loc_next,
   343         ptrdiff_t loc_data,
   344         bool follow_ptr,
   345         CxListComparator cmp_func
   346 ) {
   347     assert(begin != NULL);
   348     assert(loc_next >= 0);
   349     assert(loc_data >= 0);
   350     assert(cmp_func);
   352     void *lc, *ls, *le, *re;
   354     // set start node
   355     ls = *begin;
   357     // check how many elements are already sorted
   358     lc = ls;
   359     size_t ln = 1;
   360     while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
   361         lc = ll_next(lc);
   362         ln++;
   363     }
   364     le = ll_next(lc);
   366     // if first unsorted node is NULL, the list is already completely sorted
   367     if (le != NULL) {
   368         void *rc;
   369         size_t rn = 1;
   370         rc = le;
   371         // skip already sorted elements
   372         while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
   373             rc = ll_next(rc);
   374             rn++;
   375         }
   376         re = ll_next(rc);
   378         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   379         void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
   380                                                  ln + rn, ls, le, re, cmp_func);
   382         // Something left? Sort it!
   383         size_t remainder_length = cx_linked_list_size(re, loc_next);
   384         if (remainder_length > 0) {
   385             void *remainder = re;
   386             cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func);
   388             // merge sorted list with (also sorted) remainder
   389             *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
   390                                                ln + rn + remainder_length,
   391                                                sorted, remainder, NULL, cmp_func);
   392         } else {
   393             // no remainder - we've got our sorted list
   394             *begin = sorted;
   395         }
   396         if (end) *end = cx_linked_list_last(sorted, loc_next);
   397     }
   398 }
   400 int cx_linked_list_compare(
   401         void const *begin_left,
   402         void const *begin_right,
   403         ptrdiff_t loc_advance,
   404         ptrdiff_t loc_data,
   405         bool follow_ptr,
   406         CxListComparator cmp_func
   407 ) {
   408     void const *left = begin_left, *right = begin_right;
   410     while (left != NULL && right != NULL) {
   411         int result = cmp_func(ll_data(left), ll_data(right));
   412         if (result != 0) return result;
   413         left = ll_advance(left);
   414         right = ll_advance(right);
   415     }
   417     if (left != NULL) { return 1; }
   418     else if (right != NULL) { return -1; }
   419     else { return 0; }
   420 }
   422 void cx_linked_list_reverse(
   423         void **begin,
   424         void **end,
   425         ptrdiff_t loc_prev,
   426         ptrdiff_t loc_next
   427 ) {
   428     assert(begin != NULL);
   429     assert(loc_next >= 0);
   431     // swap all links
   432     void *prev = NULL;
   433     void *cur = *begin;
   434     while (cur != NULL) {
   435         void *next = ll_next(cur);
   437         ll_next(cur) = prev;
   438         if (loc_prev >= 0) {
   439             ll_prev(cur) = next;
   440         }
   442         prev = cur;
   443         cur = next;
   444     }
   446     // update begin and end
   447     if (end != NULL) {
   448         *end = *begin;
   449     }
   450     *begin = prev;
   451 }
   453 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
   455 typedef struct cx_linked_list_node cx_linked_list_node;
   456 struct cx_linked_list_node {
   457     cx_linked_list_node *prev;
   458     cx_linked_list_node *next;
   459     char payload[];
   460 };
   462 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
   463 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
   464 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
   466 typedef struct {
   467     struct cx_list_s base;
   468     cx_linked_list_node *begin;
   469     cx_linked_list_node *end;
   470 } cx_linked_list;
   472 static cx_linked_list_node *cx_ll_node_at(
   473         cx_linked_list const *list,
   474         size_t index
   475 ) {
   476     if (index >= list->base.size) {
   477         return NULL;
   478     } else if (index > list->base.size / 2) {
   479         return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
   480     } else {
   481         return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
   482     }
   483 }
   485 static int cx_ll_insert_at(
   486         struct cx_list_s *list,
   487         cx_linked_list_node *node,
   488         void const *elem
   489 ) {
   491     // create the new new_node
   492     cx_linked_list_node *new_node = cxMalloc(list->allocator,
   493                                              sizeof(cx_linked_list_node) + list->itemsize);
   495     // sortir if failed
   496     if (new_node == NULL) return 1;
   498     // initialize new new_node
   499     new_node->prev = new_node->next = NULL;
   500     memcpy(new_node->payload, elem, list->itemsize);
   502     // insert
   503     cx_linked_list *ll = (cx_linked_list *) list;
   504     cx_linked_list_insert_chain(
   505             (void **) &ll->begin, (void **) &ll->end,
   506             CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
   507             node, new_node, new_node
   508     );
   510     // increase the size and return
   511     list->size++;
   512     return 0;
   513 }
   515 static int cx_ll_insert(
   516         struct cx_list_s *list,
   517         size_t index,
   518         void const *elem
   519 ) {
   520     // out-of bounds check
   521     if (index > list->size) return 1;
   523     // find position efficiently
   524     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
   526     // perform insert
   527     return cx_ll_insert_at(list, node, elem);
   528 }
   530 static int cx_ll_add(
   531         struct cx_list_s *list,
   532         void const *elem
   533 ) {
   534     return cx_ll_insert(list, list->size, elem);
   535 }
   537 static int cx_pll_insert(
   538         struct cx_list_s *list,
   539         size_t index,
   540         void const *elem
   541 ) {
   542     return cx_ll_insert(list, index, &elem);
   543 }
   545 static int cx_pll_add(
   546         struct cx_list_s *list,
   547         void const *elem
   548 ) {
   549     return cx_ll_insert(list, list->size, &elem);
   550 }
   552 static int cx_ll_remove(
   553         struct cx_list_s *list,
   554         size_t index
   555 ) {
   556     cx_linked_list *ll = (cx_linked_list *) list;
   557     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   559     // out-of-bounds check
   560     if (node == NULL) return 1;
   562     // remove
   563     cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   564                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   566     // adjust size
   567     list->size--;
   569     // free and return
   570     cxFree(list->allocator, node);
   572     return 0;
   573 }
   575 static void *cx_ll_at(
   576         struct cx_list_s const *list,
   577         size_t index
   578 ) {
   579     cx_linked_list *ll = (cx_linked_list *) list;
   580     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   581     return node == NULL ? NULL : node->payload;
   582 }
   584 static void *cx_pll_at(
   585         struct cx_list_s const *list,
   586         size_t index
   587 ) {
   588     cx_linked_list *ll = (cx_linked_list *) list;
   589     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   590     return node == NULL ? NULL : *(void **) node->payload;
   591 }
   593 static size_t cx_ll_find(
   594         struct cx_list_s const *list,
   595         void const *elem
   596 ) {
   597     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   598                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   599                                false, list->cmpfunc, elem);
   600 }
   602 static size_t cx_pll_find(
   603         struct cx_list_s const *list,
   604         void const *elem
   605 ) {
   606     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   607                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   608                                true, list->cmpfunc, elem);
   609 }
   611 static void cx_ll_sort(struct cx_list_s *list) {
   612     cx_linked_list *ll = (cx_linked_list *) list;
   613     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   614                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   615                         false, list->cmpfunc);
   616 }
   618 static void cx_pll_sort(struct cx_list_s *list) {
   619     cx_linked_list *ll = (cx_linked_list *) list;
   620     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   621                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   622                         true, list->cmpfunc);
   623 }
   625 static void cx_ll_reverse(struct cx_list_s *list) {
   626     cx_linked_list *ll = (cx_linked_list *) list;
   627     cx_linked_list_reverse((void **) &ll->begin, (void **) ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
   628 }
   630 static int cx_ll_compare(
   631         struct cx_list_s const *list,
   632         struct cx_list_s const *other
   633 ) {
   634     cx_linked_list *left = (cx_linked_list *) list;
   635     cx_linked_list *right = (cx_linked_list *) other;
   636     return cx_linked_list_compare(left->begin, right->begin,
   637                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   638                                   false, list->cmpfunc);
   639 }
   641 static int cx_pll_compare(
   642         struct cx_list_s const *list,
   643         struct cx_list_s const *other
   644 ) {
   645     cx_linked_list *left = (cx_linked_list *) list;
   646     cx_linked_list *right = (cx_linked_list *) other;
   647     return cx_linked_list_compare(left->begin, right->begin,
   648                                   CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   649                                   true, list->cmpfunc);
   650 }
   652 static bool cx_ll_iter_valid(CxIterator const *iter) {
   653     return iter->elem_handle != NULL;
   654 }
   656 static void cx_ll_iter_next(CxIterator *iter) {
   657     if (iter->remove) {
   658         iter->remove = false;
   659         cx_linked_list *ll = iter->src_handle;
   660         cx_linked_list_node *node = iter->elem_handle;
   661         iter->elem_handle = node->next;
   662         cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   663                               CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   664         ll->base.size--;
   665         cxFree(ll->base.allocator, node);
   666     } else {
   667         iter->index++;
   668         cx_linked_list_node *node = iter->elem_handle;
   669         iter->elem_handle = node->next;
   670     }
   671 }
   673 static void *cx_ll_iter_current(CxIterator const *iter) {
   674     cx_linked_list_node *node = iter->elem_handle;
   675     return node->payload;
   676 }
   678 static void *cx_pll_iter_current(CxIterator const *iter) {
   679     cx_linked_list_node *node = iter->elem_handle;
   680     return *(void **) node->payload;
   681 }
   683 static CxIterator cx_ll_iterator(
   684         struct cx_list_s *list,
   685         size_t index
   686 ) {
   687     CxIterator iter;
   688     iter.index = index;
   689     iter.src_handle = list;
   690     iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
   691     iter.valid = cx_ll_iter_valid;
   692     iter.current = cx_ll_iter_current;
   693     iter.next = cx_ll_iter_next;
   694     iter.remove = false;
   695     return iter;
   696 }
   698 static CxIterator cx_pll_iterator(
   699         struct cx_list_s *list,
   700         size_t index
   701 ) {
   702     CxIterator iter = cx_ll_iterator(list, index);
   703     iter.current = cx_pll_iter_current;
   704     return iter;
   705 }
   707 static int cx_ll_insert_iter(
   708         CxIterator *iter,
   709         void const *elem,
   710         int prepend
   711 ) {
   712     struct cx_list_s *list = iter->src_handle;
   713     cx_linked_list_node *node = iter->elem_handle;
   714     if (node != NULL) {
   715         assert(prepend >= 0 && prepend <= 1);
   716         cx_linked_list_node *choice[2] = {node, node->prev};
   717         int result = cx_ll_insert_at(list, choice[prepend], elem);
   718         iter->index += prepend * (0 == result);
   719         return result;
   720     } else {
   721         int result = cx_ll_insert(list, list->size, elem);
   722         iter->index = list->size;
   723         return result;
   724     }
   725 }
   727 static int cx_pll_insert_iter(
   728         CxIterator *iter,
   729         void const *elem,
   730         int prepend
   731 ) {
   732     return cx_ll_insert_iter(iter, &elem, prepend);
   733 }
   735 static cx_list_class cx_linked_list_class = {
   736         cx_ll_add,
   737         cx_ll_insert,
   738         cx_ll_insert_iter,
   739         cx_ll_remove,
   740         cx_ll_at,
   741         cx_ll_find,
   742         cx_ll_sort,
   743         cx_ll_compare,
   744         cx_ll_reverse,
   745         cx_ll_iterator
   746 };
   748 static cx_list_class cx_pointer_linked_list_class = {
   749         cx_pll_add,
   750         cx_pll_insert,
   751         cx_pll_insert_iter,
   752         cx_ll_remove,
   753         cx_pll_at,
   754         cx_pll_find,
   755         cx_pll_sort,
   756         cx_pll_compare,
   757         cx_ll_reverse,
   758         cx_pll_iterator,
   759 };
   761 static CxList *cx_ll_default_destructor(CxList *list) {
   762     cx_linked_list *ll = (cx_linked_list *) list;
   764     cx_linked_list_node *node = ll->begin;
   765     while (node) {
   766         void *next = node->next;
   767         cxFree(list->allocator, node);
   768         node = next;
   769     }
   771     cxFree(list->allocator, list);
   772     return NULL;
   773 }
   775 CxList *cxLinkedListCreate(
   776         CxAllocator const *allocator,
   777         CxListComparator comparator,
   778         size_t item_size
   779 ) {
   780     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   781     if (list == NULL)
   782         return NULL;
   784     list->base.cl = &cx_linked_list_class;
   785     list->base.allocator = allocator;
   786     list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
   787     list->base.cmpfunc = comparator;
   788     list->base.itemsize = item_size;
   789     list->base.capacity = SIZE_MAX;
   791     return (CxList *) list;
   792 }
   794 CxList *cxPointerLinkedListCreate(
   795         CxAllocator const *allocator,
   796         CxListComparator comparator
   797 ) {
   798     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   799     if (list == NULL)
   800         return NULL;
   802     list->base.cl = &cx_pointer_linked_list_class;
   803     list->base.allocator = allocator;
   804     list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
   805     list->base.cmpfunc = comparator;
   806     list->base.itemsize = sizeof(void *);
   807     list->base.capacity = SIZE_MAX;
   809     return (CxList *) list;
   810 }
   812 CxList *cxLinkedListFromArray(
   813         CxAllocator const *allocator,
   814         CxListComparator comparator,
   815         size_t item_size,
   816         size_t num_items,
   817         void const *array
   818 ) {
   819     CxList *list = cxLinkedListCreate(allocator, comparator, item_size);
   820     if (list == NULL) return NULL;
   821     for (size_t i = 0; i < num_items; i++) {
   822         if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
   823             return cx_ll_default_destructor(list);
   824         }
   825     }
   826     return list;
   827 }

mercurial