src/linked_list.c

Sun, 26 Sep 2021 12:01:25 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Sep 2021 12:01:25 +0200
changeset 416
a79b2388db5e
parent 412
af766caea48d
child 423
4cea6e50175b
permissions
-rw-r--r--

remove unnecessary check

     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             return 1;
    59         } else {
    60             *begin = new_node;
    61             return 0;
    62         }
    63     }
    65     void **next = CX_LL_PTR(last, loc_next);
    66     *next = new_node;
    67     if (end != NULL) {
    68         *end = cx_linked_list_last(&new_node, NULL, loc_next);
    69     }
    70     if (loc_prev >= 0) {
    71         void **prev = CX_LL_PTR(new_node, loc_prev);
    72         *prev = last;
    73     }
    75     return 0;
    76 }
    78 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
    80 struct cx_linked_list_node {
    81     void *prev;
    82     void *next;
    83     char payload[];
    84 };
    86 typedef struct {
    87     void *begin;
    88     void *end;
    89     ptrdiff_t loc_prev;
    90     ptrdiff_t loc_next;
    91 } cx_linked_list;
    93 int cx_ll_add(cx_list *list, void *elem) {
    94     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
    95     CxAllocator allocator = list->allocator;
    97     struct cx_linked_list_node *node = cxMalloc(allocator,
    98                                                 sizeof(struct cx_linked_list_node) + list->itemsize);
    99     if (node == NULL)
   100         return 1;
   102     node->next = NULL;
   103     memcpy(node->payload, elem, list->itemsize);
   105     int ret = cx_linked_list_add(
   106             &listdata->begin,
   107             &listdata->end,
   108             offsetof(struct cx_linked_list_node, prev),
   109             offsetof(struct cx_linked_list_node, next),
   110             node
   111     );
   112     if (ret == 0) {
   113         list->size++;
   114         return 0;
   115     } else {
   116         return ret;
   117     }
   118 }
   120 int cx_ll_insert(cx_list *list, size_t index, void *elem) {
   121     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   122     // TODO: implement using low level API
   123     return 1;
   124 }
   126 void *cx_ll_remove(cx_list *list, size_t index) {
   127     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   128     // TODO: implement using low level API
   129     return NULL;
   130 }
   132 size_t cx_ll_find(cx_list *list, void *elem) {
   133     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   134     // TODO: implement using low level API
   135     return 0;
   136 }
   138 void *cx_ll_last(cx_list *list) {
   139     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   140     struct cx_linked_list_node *last = cx_linked_list_last(
   141             NULL, &listdata->end, offsetof(struct cx_linked_list_node, next));
   142     return &last->payload;
   143 }
   145 cx_list_class cx_linked_list_class = {
   146         cx_ll_add,
   147         cx_ll_insert,
   148         cx_ll_remove,
   149         cx_ll_find,
   150         cx_ll_last
   151 };
   153 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
   154     CxList list = cxMalloc(allocator, sizeof(cx_list_s) + sizeof(cx_linked_list));
   155     if (list == NULL)
   156         return NULL;
   158     list->cl = &cx_linked_list_class;
   159     list->data.allocator = allocator;
   160     list->data.cmpfunc = comparator;
   161     list->data.itemsize = item_size;
   162     list->data.capacity = SIZE_MAX;
   164     cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
   165     ll->begin = NULL;
   166     ll->loc_prev = offsetof(struct cx_linked_list_node, prev);
   167     ll->loc_next = offsetof(struct cx_linked_list_node, next);
   168     cxLinkedListRecalculateSize(list);
   170     return list;
   171 }
   173 void cxLinkedListDestroy(CxList list) {
   175 }
   177 size_t cxLinkedListRecalculateSize(CxList list) {
   178     cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
   180     if (ll->begin == NULL) {
   181         list->data.size = 0;
   182         ll->end = NULL;
   183         return 0;
   184     } else {
   185         void *cur = ll->begin;
   186         void *last;
   187         size_t size;
   188         do {
   189             last = cur;
   190             size++;
   191         } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
   192         ll->end = last;
   193         list->data.size = size;
   194         return size;
   195     }
   196 }

mercurial