src/linked_list.c

Sun, 07 Feb 2021 20:37:20 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 07 Feb 2021 20:37:20 +0100
changeset 401
e6a8f7fb0c45
parent 400
8cd274352419
child 402
a34b93911956
permissions
-rw-r--r--

copy list items when they are added to the list

     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_next, ptrdiff_t loc_prev, void *newnode) {
    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 = newnode;
    66             return 0;
    67         }
    68     }
    70     void **next = CX_LL_PTR(last, loc_next);
    71     *next = newnode;
    72     if (loc_prev >= 0) {
    73         void **prev = CX_LL_PTR(newnode, loc_prev);
    74         *prev = last;
    75     }
    77     return 0;
    78 }
    80 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
    82 typedef struct {
    83     void *begin;
    84     void *end;
    85 } cx_linked_list;
    87 int cx_ll_add(cx_list *list, void *elem) {
    88     cx_linked_list *listdata = list->listdata;
    89     CxAllocator allocator = list->allocator;
    91     /*
    92      * Memory layout:
    93      * next : sizeof(void*)
    94      * prev : sizeof(void*)
    95      * data : itemsize
    96      */
    97     void *node = cxMalloc(allocator,2*sizeof(void*)+list->itemsize);
    98     if (node == NULL)
    99         return 1;
   101     memset(node, 0, sizeof(void*));
   102     memcpy(node+2, elem, list->itemsize);
   104     int ret = cx_linked_list_add(
   105             &listdata->begin,
   106             &listdata->end,
   107             0,
   108             sizeof(void*),
   109             node
   110     );
   111     if (ret == 0) {
   112         list->size++;
   113         return 0;
   114     } else {
   115         return ret;
   116     }
   117 }
   119 int cx_ll_insert(cx_list *list, size_t index, void *elem) {
   120     cx_linked_list *listdata = list->listdata;
   121     // TODO: implement using low level API
   122     return 1;
   123 }
   125 void *cx_ll_remove(cx_list *list, size_t index) {
   126     cx_linked_list *listdata = list->listdata;
   127     // TODO: implement using low level API
   128     return NULL;
   129 }
   131 size_t cx_ll_find(cx_list *list, void *elem) {
   132     cx_linked_list *listdata = list->listdata;
   133     // TODO: implement using low level API
   134     return 0;
   135 }
   138 cx_list_class cx_linked_list_class = {
   139         cx_ll_add,
   140         cx_ll_insert,
   141         cx_ll_remove,
   142         cx_ll_find
   143 };
   145 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t itemsize) {
   146     CxList list = cxMalloc(allocator, sizeof(list));
   147     if (list == NULL)
   148         return NULL;
   150     list->cl = &cx_linked_list_class;
   151     list->data.allocator = allocator;
   152     list->data.cmpfunc = comparator;
   153     list->data.size = 0;
   154     list->data.itemsize = itemsize;
   155     list->data.capacity = SIZE_MAX;
   156     list->data.listdata = cxCalloc(allocator, 1, sizeof(cx_linked_list));
   157     if (list->data.listdata == NULL) {
   158         cxFree(allocator, list);
   159         return NULL;
   160     }
   161 }

mercurial