src/linked_list.c

Mon, 27 Sep 2021 17:00:19 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Sep 2021 17:00:19 +0200
changeset 436
ca9ce2297c29
parent 435
0fe204d50f54
child 437
9d4971ea0625
permissions
-rw-r--r--

add node destruction in cxLinkedListDestroy()

     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>
    33 /* LOW LEVEL LINKED LIST FUNCTIONS */
    35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
    37 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
    38     if (end != NULL) {
    39         return *end;
    40     } else {
    41         if (begin == NULL || *begin == NULL)
    42             return NULL;
    44         void *cur = *begin;
    45         void *last;
    46         do {
    47             last = cur;
    48         } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
    50         return last;
    51     }
    52 }
    54 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
    55     void *last = cx_linked_list_last(begin, end, loc_next);
    56     if (last == NULL) {
    57         if (begin == NULL) {
    58             // no current list and no begin ptr to write to - we don't find something to append to
    59             return 1;
    60         } else {
    61             // start fresh list
    62             *begin = new_node;
    63         }
    64     } else {
    65         // if there is a last node, update its next pointer
    66         void **next = CX_LL_PTR(last, loc_next);
    67         *next = new_node;
    68     }
    70     // if there is an end pointer, update it
    71     if (end != NULL) {
    72         *end = cx_linked_list_last(&new_node, NULL, loc_next);
    73     }
    75     // if the nodes use a prev pointer, update it
    76     if (loc_prev >= 0) {
    77         void **prev = CX_LL_PTR(new_node, loc_prev);
    78         *prev = last;
    79     }
    81     return 0;
    82 }
    84 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
    86 struct cx_linked_list_node {
    87     void *prev;
    88     void *next;
    89     char payload[];
    90 };
    92 typedef struct {
    93     cx_list_s base;
    94     void *begin;
    95     void *end;
    96     ptrdiff_t loc_prev;
    97     ptrdiff_t loc_next;
    98 } cx_linked_list;
   100 int cx_ll_add(cx_list_s *list, void *elem) {
   101     cx_linked_list *linkedlist = (cx_linked_list *) list;
   103     struct cx_linked_list_node *node = cxMalloc(list->allocator,
   104                                                 sizeof(struct cx_linked_list_node) + list->itemsize);
   105     if (node == NULL)
   106         return 1;
   108     node->next = node->prev = NULL;
   109     memcpy(node->payload, elem, list->itemsize);
   111     int ret = cx_linked_list_add(
   112             &linkedlist->begin,
   113             &linkedlist->end,
   114             offsetof(struct cx_linked_list_node, prev),
   115             offsetof(struct cx_linked_list_node, next),
   116             node
   117     );
   118     if (ret == 0) {
   119         list->size++;
   120         return 0;
   121     } else {
   122         return ret;
   123     }
   124 }
   126 int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
   127     cx_linked_list *linkedList = (cx_linked_list *) list;
   128     // TODO: implement using low level API
   129     return 1;
   130 }
   132 void *cx_ll_remove(cx_list_s *list, size_t index) {
   133     cx_linked_list *linkedList = (cx_linked_list *) list;
   134     // TODO: implement using low level API
   135     return NULL;
   136 }
   138 size_t cx_ll_find(cx_list_s *list, void *elem) {
   139     cx_linked_list *linkedList = (cx_linked_list *) list;
   140     // TODO: implement using low level API
   141     return 0;
   142 }
   144 void *cx_ll_last(cx_list_s *list) {
   145     cx_linked_list *linkedList = (cx_linked_list *) list;
   146     struct cx_linked_list_node *last = cx_linked_list_last(
   147             NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next));
   148     return &last->payload;
   149 }
   151 cx_list_class cx_linked_list_class = {
   152         cx_ll_add,
   153         cx_ll_insert,
   154         cx_ll_remove,
   155         cx_ll_find,
   156         cx_ll_last
   157 };
   159 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
   160     cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list));
   161     if (list == NULL)
   162         return NULL;
   164     list->base.cl = &cx_linked_list_class;
   165     list->base.allocator = allocator;
   166     list->base.cmpfunc = comparator;
   167     list->base.itemsize = item_size;
   168     list->base.capacity = SIZE_MAX;
   170     list->begin = NULL;
   171     list->loc_prev = offsetof(struct cx_linked_list_node, prev);
   172     list->loc_next = offsetof(struct cx_linked_list_node, next);
   174     CxList result = (CxList) list;
   175     cxLinkedListRecalculateSize(result);
   176     return result;
   177 }
   179 void cxLinkedListDestroy(CxList list) {
   180     cx_linked_list* ll = (cx_linked_list*) list;
   182     struct cx_linked_list_node* node = ll->begin;
   183     while (node) {
   184         void* next = node->next;
   185         cxFree(list->allocator, node);
   186         node = next;
   187     }
   189     cxFree(list->allocator, list);
   190 }
   192 size_t cxLinkedListRecalculateSize(CxList list) {
   193     cx_linked_list *ll = (cx_linked_list *) list;
   195     if (ll->begin == NULL) {
   196         ll->base.size = 0;
   197         ll->end = NULL;
   198         return 0;
   199     } else {
   200         void *cur = ll->begin;
   201         void *last;
   202         size_t size = 0;
   203         do {
   204             last = cur;
   205             size++;
   206         } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
   207         ll->end = last;
   208         ll->base.size = size;
   209         return size;
   210     }
   211 }

mercurial