src/linked_list.c

Thu, 23 Dec 2021 15:20:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Dec 2021 15:20:50 +0100
changeset 481
eef025d82a34
parent 480
e3be53a3354f
child 486
d7ca126eab7f
permissions
-rw-r--r--

add several new linked list functions

* cx_linked_list_insert()
* cx_linked_list_insert_chain()
* cx_linked_list_link()
* cx_linked_list_unlink()

Also uses the most general function wherever possible.

     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 *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 *cur = start;
    52     while (i != index && cur != NULL) {
    53         cur = ll_advance(cur);
    54         i < index ? i++ : i--;
    55     }
    56     return cur;
    57 }
    59 size_t cx_linked_list_find(
    60         void *start,
    61         ptrdiff_t loc_advance,
    62         ptrdiff_t loc_data,
    63         int follow_ptr,
    64         CxListComparator cmp_func,
    65         void *elem
    66 ) {
    67     assert(start != NULL);
    68     assert(loc_advance >= 0);
    69     assert(loc_data >= 0);
    70     assert(cmp_func);
    72     void *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 *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 *node,
    94         ptrdiff_t loc_next
    95 ) {
    96     assert(node != NULL);
    97     assert(loc_next >= 0);
    99     void *cur = node;
   100     void *last;
   101     do {
   102         last = cur;
   103     } while ((cur = ll_next(cur)) != NULL);
   105     return last;
   106 }
   108 void *cx_linked_list_prev(
   109         void *begin,
   110         ptrdiff_t loc_next,
   111         void *node
   112 ) {
   113     assert(begin != NULL);
   114     assert(node != NULL);
   115     assert(loc_next >= 0);
   116     if (begin == node) return NULL;
   117     void *cur = begin;
   118     void *next;
   119     while (1) {
   120         next = ll_next(cur);
   121         if (next == node) return 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 *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         int 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     void *rc, *lc;
   299     lc = ls;
   300     rc = le;
   301     size_t n = 0;
   302     while (lc && lc != le && rc != re) {
   303         if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
   304             sorted[n] = lc;
   305             lc = ll_next(lc);
   306         } else {
   307             sorted[n] = rc;
   308             rc = ll_next(rc);
   309         }
   310         n++;
   311     }
   312     while (lc && lc != le) {
   313         sorted[n] = lc;
   314         lc = ll_next(lc);
   315         n++;
   316     }
   317     while (rc && rc != re) {
   318         sorted[n] = rc;
   319         rc = ll_next(rc);
   320         n++;
   321     }
   323     // Update pointer
   324     if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
   325     for (size_t i = 0; i < length - 1; i++) {
   326         cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
   327     }
   328     ll_next(sorted[length - 1]) = NULL;
   330     void *ret = sorted[0];
   331     if (sorted != sbo) {
   332         free(sorted);
   333     }
   334     return ret;
   335 }
   337 void cx_linked_list_sort( /* NOLINT(misc-no-recursion) - purposely recursive function */
   338         void **begin,
   339         void **end,
   340         ptrdiff_t loc_prev,
   341         ptrdiff_t loc_next,
   342         ptrdiff_t loc_data,
   343         int follow_ptr,
   344         CxListComparator cmp_func
   345 ) {
   346     assert(begin != NULL);
   347     assert(loc_next >= 0);
   348     assert(loc_data >= 0);
   349     assert(cmp_func);
   351     void *lc, *ls, *le, *re;
   353     // set start node
   354     ls = *begin;
   356     // check how many elements are already sorted
   357     lc = ls;
   358     size_t ln = 1;
   359     while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
   360         lc = ll_next(lc);
   361         ln++;
   362     }
   363     le = ll_next(lc);
   365     // if first unsorted node is NULL, the list is already completely sorted
   366     if (le != NULL) {
   367         void *rc;
   368         size_t rn = 1;
   369         rc = le;
   370         // skip already sorted elements
   371         while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
   372             rc = ll_next(rc);
   373             rn++;
   374         }
   375         re = ll_next(rc);
   377         // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
   378         void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
   379                                                  ln + rn, ls, le, re, cmp_func);
   381         // Something left? Sort it!
   382         size_t remainder_length = cx_linked_list_size(re, loc_next);
   383         if (remainder_length > 0) {
   384             void *remainder = re;
   385             cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func);
   387             // merge sorted list with (also sorted) remainder
   388             *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
   389                                                ln + rn + remainder_length,
   390                                                sorted, remainder, NULL, cmp_func);
   391         } else {
   392             // no remainder - we've got our sorted list
   393             *begin = sorted;
   394         }
   395         if (end) *end = cx_linked_list_last(sorted, loc_next);
   396     }
   397 }
   399 void cx_linked_list_reverse(
   400         void **begin,
   401         void **end,
   402         ptrdiff_t loc_prev,
   403         ptrdiff_t loc_next
   404 ) {
   405     assert(begin != NULL);
   406     assert(loc_next >= 0);
   408     // swap all links
   409     void *prev = NULL;
   410     void *cur = *begin;
   411     while (cur != NULL) {
   412         void *next = ll_next(cur);
   414         ll_next(cur) = prev;
   415         if (loc_prev >= 0) {
   416             ll_prev(cur) = next;
   417         }
   419         prev = cur;
   420         cur = next;
   421     }
   423     // update begin and end
   424     if (end != NULL) {
   425         *end = *begin;
   426     }
   427     *begin = prev;
   428 }
   430 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
   432 typedef struct cx_linked_list_node cx_linked_list_node;
   433 struct cx_linked_list_node {
   434     cx_linked_list_node *prev;
   435     cx_linked_list_node *next;
   436     char payload[];
   437 };
   439 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
   440 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
   441 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
   443 typedef struct {
   444     cx_list_s base;
   445     cx_linked_list_node *begin;
   446     cx_linked_list_node *end;
   447 } cx_linked_list;
   449 static cx_linked_list_node *cx_ll_node_at(
   450         cx_linked_list *list,
   451         size_t index
   452 ) {
   453     if (index >= list->base.size) {
   454         return NULL;
   455     } else if (index > list->base.size / 2) {
   456         return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
   457     } else {
   458         return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
   459     }
   460 }
   462 static int cx_ll_insert(
   463         cx_list_s *list,
   464         size_t index,
   465         void *elem
   466 ) {
   467     // out-of bounds check
   468     if (index > list->size) return 1;
   470     // cast a linked list pointer
   471     cx_linked_list *ll = (cx_linked_list *) list;
   473     // create the new new_node
   474     cx_linked_list_node *new_node = cxMalloc(list->allocator,
   475                                              sizeof(cx_linked_list_node) + list->itemsize);
   477     // sortir if failed
   478     if (new_node == NULL) return 1;
   480     // initialize new new_node
   481     new_node->prev = new_node->next = NULL;
   482     memcpy(new_node->payload, elem, list->itemsize);
   484     // find position efficiently
   485     cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at(ll, index - 1);
   487     // insert
   488     cx_linked_list_insert_chain(
   489             (void **) &ll->begin, (void **) &ll->end,
   490             CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
   491             node, new_node, new_node
   492     );
   494     // increase the size and return
   495     list->size++;
   496     return 0;
   497 }
   499 static int cx_ll_add(
   500         cx_list_s *list,
   501         void *elem
   502 ) {
   503     return cx_ll_insert(list, list->size, elem);
   504 }
   506 static int cx_pll_insert(
   507         cx_list_s *list,
   508         size_t index,
   509         void *elem
   510 ) {
   511     return cx_ll_insert(list, index, &elem);
   512 }
   514 static int cx_pll_add(
   515         cx_list_s *list,
   516         void *elem
   517 ) {
   518     return cx_ll_insert(list, list->size, &elem);
   519 }
   521 static int cx_ll_remove(
   522         cx_list_s *list,
   523         size_t index
   524 ) {
   525     cx_linked_list *ll = (cx_linked_list *) list;
   526     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   528     // out-of-bounds check
   529     if (node == NULL) return 1;
   531     // remove
   532     cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
   533                           CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
   535     // adjust size
   536     list->size--;
   538     // free and return
   539     cxFree(list->allocator, node);
   541     return 0;
   542 }
   544 static void *cx_ll_at(
   545         cx_list_s *list,
   546         size_t index
   547 ) {
   548     cx_linked_list *ll = (cx_linked_list *) list;
   549     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   550     return node == NULL ? NULL : node->payload;
   551 }
   553 static void *cx_pll_at(
   554         cx_list_s *list,
   555         size_t index
   556 ) {
   557     cx_linked_list *ll = (cx_linked_list *) list;
   558     cx_linked_list_node *node = cx_ll_node_at(ll, index);
   559     return node == NULL ? NULL : *(void **) node->payload;
   560 }
   562 static size_t cx_ll_find(
   563         cx_list_s *list,
   564         void *elem
   565 ) {
   566     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   567                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   568                                0, list->cmpfunc, elem);
   569 }
   571 static size_t cx_pll_find(
   572         cx_list_s *list,
   573         void *elem
   574 ) {
   575     return cx_linked_list_find(((cx_linked_list *) list)->begin,
   576                                CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   577                                1, list->cmpfunc, elem);
   578 }
   580 static void cx_ll_sort(cx_list_s *list) {
   581     cx_linked_list *ll = (cx_linked_list *) list;
   582     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   583                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   584                         0, list->cmpfunc);
   585 }
   587 static void cx_pll_sort(cx_list_s *list) {
   588     cx_linked_list *ll = (cx_linked_list *) list;
   589     cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
   590                         CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
   591                         1, list->cmpfunc);
   592 }
   594 static cx_list_class cx_linked_list_class = {
   595         cx_ll_add,
   596         cx_ll_insert,
   597         cx_ll_remove,
   598         cx_ll_at,
   599         cx_ll_find,
   600         cx_ll_sort
   601 };
   603 static cx_list_class cx_pointer_linked_list_class = {
   604         cx_pll_add,
   605         cx_pll_insert,
   606         cx_ll_remove,
   607         cx_pll_at,
   608         cx_pll_find,
   609         cx_pll_sort
   610 };
   612 CxList cxLinkedListCreate(
   613         CxAllocator allocator,
   614         CxListComparator comparator,
   615         size_t item_size
   616 ) {
   617     cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
   618     if (list == NULL)
   619         return NULL;
   621     list->base.cl = &cx_linked_list_class;
   622     list->base.allocator = allocator;
   623     list->base.cmpfunc = comparator;
   624     list->base.itemsize = item_size;
   625     list->base.capacity = SIZE_MAX;
   626     list->base.size = 0;
   628     list->begin = NULL;
   629     list->end = NULL;
   631     return (CxList) list;
   632 }
   634 CxList cxPointerLinkedListCreate(
   635         CxAllocator allocator,
   636         CxListComparator comparator
   637 ) {
   638     cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
   639     if (list == NULL)
   640         return NULL;
   642     list->base.cl = &cx_pointer_linked_list_class;
   643     list->base.allocator = allocator;
   644     list->base.cmpfunc = comparator;
   645     list->base.itemsize = sizeof(void *);
   646     list->base.capacity = SIZE_MAX;
   647     list->base.size = 0;
   649     list->begin = NULL;
   650     list->end = NULL;
   652     return (CxList) list;
   653 }
   655 void cxLinkedListDestroy(CxList list) {
   656     cx_linked_list *ll = (cx_linked_list *) list;
   658     cx_linked_list_node *node = ll->begin;
   659     while (node) {
   660         void *next = node->next;
   661         cxFree(list->allocator, node);
   662         node = next;
   663     }
   665     cxFree(list->allocator, list);
   666 }

mercurial