src/linked_list.c

Mon, 27 Sep 2021 17:49:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 27 Sep 2021 17:49:23 +0200
changeset 437
9d4971ea0625
parent 436
ca9ce2297c29
child 438
cd3069757010
permissions
-rw-r--r--

implement linked list find

     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 *ll = (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             &ll->begin, &ll->end,
   113             offsetof(struct cx_linked_list_node, prev),
   114             offsetof(struct cx_linked_list_node, next),
   115             node
   116     );
   117     if (ret == 0) {
   118         list->size++;
   119         return 0;
   120     } else {
   121         return ret;
   122     }
   123 }
   125 int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
   126     cx_linked_list *ll = (cx_linked_list *) list;
   127     // TODO: implement using low level API
   128     return 1;
   129 }
   131 void *cx_ll_remove(cx_list_s *list, size_t index) {
   132     cx_linked_list *ll = (cx_linked_list *) list;
   133     // TODO: implement using low level API
   134     return NULL;
   135 }
   137 size_t cx_ll_find(cx_list_s *list, void *elem) {
   138     CxListComparator cmp = list->cmpfunc;
   139     cx_linked_list *ll = (cx_linked_list *) list;
   141     size_t index;
   142     struct cx_linked_list_node* node = ll->begin;
   143     for (index = 0 ; index < list->size ; index++) {
   144         void* current = node->payload;
   145         if (cmp(current, elem) == 0) {
   146             return index;
   147         }
   148         node = node->next;
   149     }
   150     return index;
   151 }
   153 void *cx_ll_last(cx_list_s *list) {
   154     cx_linked_list *linkedList = (cx_linked_list *) list;
   155     struct cx_linked_list_node *last = cx_linked_list_last(
   156             NULL, &linkedList->end, offsetof(struct cx_linked_list_node, next));
   157     return &last->payload;
   158 }
   160 cx_list_class cx_linked_list_class = {
   161         cx_ll_add,
   162         cx_ll_insert,
   163         cx_ll_remove,
   164         cx_ll_find,
   165         cx_ll_last
   166 };
   168 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
   169     cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list));
   170     if (list == NULL)
   171         return NULL;
   173     list->base.cl = &cx_linked_list_class;
   174     list->base.allocator = allocator;
   175     list->base.cmpfunc = comparator;
   176     list->base.itemsize = item_size;
   177     list->base.capacity = SIZE_MAX;
   179     list->begin = NULL;
   180     list->loc_prev = offsetof(struct cx_linked_list_node, prev);
   181     list->loc_next = offsetof(struct cx_linked_list_node, next);
   183     CxList result = (CxList) list;
   184     cxLinkedListRecalculateSize(result);
   185     return result;
   186 }
   188 void cxLinkedListDestroy(CxList list) {
   189     cx_linked_list* ll = (cx_linked_list*) list;
   191     struct cx_linked_list_node* node = ll->begin;
   192     while (node) {
   193         void* next = node->next;
   194         cxFree(list->allocator, node);
   195         node = next;
   196     }
   198     cxFree(list->allocator, list);
   199 }
   201 size_t cxLinkedListRecalculateSize(CxList list) {
   202     cx_linked_list *ll = (cx_linked_list *) list;
   204     if (ll->begin == NULL) {
   205         ll->base.size = 0;
   206         ll->end = NULL;
   207         return 0;
   208     } else {
   209         void *cur = ll->begin;
   210         void *last;
   211         size_t size = 0;
   212         do {
   213             last = cur;
   214             size++;
   215         } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
   216         ll->end = last;
   217         ll->base.size = size;
   218         return size;
   219     }
   220 }

mercurial