src/linked_list.c

Sun, 14 Feb 2021 15:37:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 14 Feb 2021 15:37:12 +0100
changeset 413
0f4aa9fc75d9
parent 412
af766caea48d
child 416
a79b2388db5e
permissions
-rw-r--r--

perform array and list tests in the same test binary (use the same assertions, later)

     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     // TODO: how do we report error messages?
    56     if (loc_next < 0 || (begin == NULL && end == NULL)) {
    57         return 1;
    58     }
    60     void *last = cx_linked_list_last(begin, end, loc_next);
    61     if (last == NULL) {
    62         if (begin == NULL) {
    63             return 1;
    64         } else {
    65             *begin = new_node;
    66             return 0;
    67         }
    68     }
    70     void **next = CX_LL_PTR(last, loc_next);
    71     *next = new_node;
    72     if (end != NULL) {
    73         *end = cx_linked_list_last(&new_node, NULL, loc_next);
    74     }
    75     if (loc_prev >= 0) {
    76         void **prev = CX_LL_PTR(new_node, loc_prev);
    77         *prev = last;
    78     }
    80     return 0;
    81 }
    83 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
    85 struct cx_linked_list_node {
    86     void *prev;
    87     void *next;
    88     char payload[];
    89 };
    91 typedef struct {
    92     void *begin;
    93     void *end;
    94     ptrdiff_t loc_prev;
    95     ptrdiff_t loc_next;
    96 } cx_linked_list;
    98 int cx_ll_add(cx_list *list, void *elem) {
    99     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   100     CxAllocator allocator = list->allocator;
   102     struct cx_linked_list_node *node = cxMalloc(allocator,
   103                                                 sizeof(struct cx_linked_list_node) + list->itemsize);
   104     if (node == NULL)
   105         return 1;
   107     node->next = NULL;
   108     memcpy(node->payload, elem, list->itemsize);
   110     int ret = cx_linked_list_add(
   111             &listdata->begin,
   112             &listdata->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 *list, size_t index, void *elem) {
   126     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   127     // TODO: implement using low level API
   128     return 1;
   129 }
   131 void *cx_ll_remove(cx_list *list, size_t index) {
   132     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   133     // TODO: implement using low level API
   134     return NULL;
   135 }
   137 size_t cx_ll_find(cx_list *list, void *elem) {
   138     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   139     // TODO: implement using low level API
   140     return 0;
   141 }
   143 void *cx_ll_last(cx_list *list) {
   144     cx_linked_list *listdata = (cx_linked_list *) list->listdata;
   145     struct cx_linked_list_node *last = cx_linked_list_last(
   146             NULL, &listdata->end, offsetof(struct cx_linked_list_node, next));
   147     return &last->payload;
   148 }
   150 cx_list_class cx_linked_list_class = {
   151         cx_ll_add,
   152         cx_ll_insert,
   153         cx_ll_remove,
   154         cx_ll_find,
   155         cx_ll_last
   156 };
   158 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
   159     CxList list = cxMalloc(allocator, sizeof(cx_list_s) + sizeof(cx_linked_list));
   160     if (list == NULL)
   161         return NULL;
   163     list->cl = &cx_linked_list_class;
   164     list->data.allocator = allocator;
   165     list->data.cmpfunc = comparator;
   166     list->data.itemsize = item_size;
   167     list->data.capacity = SIZE_MAX;
   169     cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
   170     ll->begin = NULL;
   171     ll->loc_prev = offsetof(struct cx_linked_list_node, prev);
   172     ll->loc_next = offsetof(struct cx_linked_list_node, next);
   173     cxLinkedListRecalculateSize(list);
   175     return list;
   176 }
   178 void cxLinkedListDestroy(CxList list) {
   180 }
   182 size_t cxLinkedListRecalculateSize(CxList list) {
   183     cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
   185     if (ll->begin == NULL) {
   186         list->data.size = 0;
   187         ll->end = NULL;
   188         return 0;
   189     } else {
   190         void *cur = ll->begin;
   191         void *last;
   192         size_t size;
   193         do {
   194             last = cur;
   195             size++;
   196         } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
   197         ll->end = last;
   198         list->data.size = size;
   199         return size;
   200     }
   201 }

mercurial