src/linked_list.c

Mon, 08 Feb 2021 00:14:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 08 Feb 2021 00:14:07 +0100
changeset 406
9cbea761fbf7
parent 404
86ebc3745e62
child 407
b447539ec255
permissions
-rw-r--r--

adds cxLinkedListWrap and cxLinkedListRecalculateSize

universe@398 1 /*
universe@398 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@398 3 *
universe@398 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@398 5 *
universe@398 6 * Redistribution and use in source and binary forms, with or without
universe@398 7 * modification, are permitted provided that the following conditions are met:
universe@398 8 *
universe@398 9 * 1. Redistributions of source code must retain the above copyright
universe@398 10 * notice, this list of conditions and the following disclaimer.
universe@398 11 *
universe@398 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@398 13 * notice, this list of conditions and the following disclaimer in the
universe@398 14 * documentation and/or other materials provided with the distribution.
universe@398 15 *
universe@398 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@398 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@398 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@398 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@398 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@398 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@398 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@398 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@398 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@398 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@398 26 * POSSIBILITY OF SUCH DAMAGE.
universe@398 27 */
universe@398 28
universe@398 29 #include "cx/linked_list.h"
universe@401 30 #include <stdint.h>
universe@401 31 #include <string.h>
universe@398 32
universe@398 33 /* LOW LEVEL LINKED LIST FUNCTIONS */
universe@398 34
universe@398 35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
universe@398 36
universe@400 37 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
universe@398 38 if (end != NULL) {
universe@398 39 return *end;
universe@398 40 } else {
universe@398 41 if (begin == NULL || *begin == NULL)
universe@398 42 return NULL;
universe@398 43
universe@398 44 void *cur = *begin;
universe@398 45 void *last;
universe@398 46 do {
universe@398 47 last = cur;
universe@398 48 } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
universe@398 49
universe@398 50 return last;
universe@398 51 }
universe@398 52 }
universe@398 53
universe@406 54 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
universe@398 55 // TODO: how do we report error messages?
universe@398 56 if (loc_next < 0 || (begin == NULL && end == NULL)) {
universe@398 57 return 1;
universe@398 58 }
universe@398 59
universe@398 60 void *last = cx_linked_list_last(begin, end, loc_next);
universe@398 61 if (last == NULL) {
universe@398 62 if (begin == NULL) {
universe@398 63 return 1;
universe@398 64 } else {
universe@406 65 *begin = new_node;
universe@398 66 return 0;
universe@398 67 }
universe@398 68 }
universe@398 69
universe@398 70 void **next = CX_LL_PTR(last, loc_next);
universe@406 71 *next = new_node;
universe@398 72 if (loc_prev >= 0) {
universe@406 73 void **prev = CX_LL_PTR(new_node, loc_prev);
universe@398 74 *prev = last;
universe@398 75 }
universe@398 76
universe@398 77 return 0;
universe@398 78 }
universe@398 79
universe@398 80 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
universe@398 81
universe@402 82 struct cx_linked_list_node {
universe@402 83 void *prev;
universe@402 84 void *next;
universe@403 85 char payload[];
universe@402 86 };
universe@402 87
universe@398 88 typedef struct {
universe@401 89 void *begin;
universe@401 90 void *end;
universe@406 91 ptrdiff_t loc_prev;
universe@406 92 ptrdiff_t loc_next;
universe@398 93 } cx_linked_list;
universe@398 94
universe@398 95 int cx_ll_add(cx_list *list, void *elem) {
universe@406 96 cx_linked_list *listdata = (cx_linked_list *) list->listdata;
universe@398 97 CxAllocator allocator = list->allocator;
universe@398 98
universe@402 99 struct cx_linked_list_node *node = cxMalloc(allocator,
universe@406 100 sizeof(struct cx_linked_list_node) + list->itemsize);
universe@398 101 if (node == NULL)
universe@398 102 return 1;
universe@398 103
universe@402 104 node->next = NULL;
universe@406 105 memcpy(node->payload, elem, list->itemsize);
universe@399 106
universe@398 107 int ret = cx_linked_list_add(
universe@401 108 &listdata->begin,
universe@401 109 &listdata->end,
universe@402 110 offsetof(struct cx_linked_list_node, prev),
universe@402 111 offsetof(struct cx_linked_list_node, next),
universe@398 112 node
universe@398 113 );
universe@398 114 if (ret == 0) {
universe@401 115 list->size++;
universe@398 116 return 0;
universe@398 117 } else {
universe@398 118 return ret;
universe@398 119 }
universe@398 120 }
universe@398 121
universe@398 122 int cx_ll_insert(cx_list *list, size_t index, void *elem) {
universe@406 123 cx_linked_list *listdata = (cx_linked_list *) list->listdata;
universe@398 124 // TODO: implement using low level API
universe@398 125 return 1;
universe@398 126 }
universe@398 127
universe@398 128 void *cx_ll_remove(cx_list *list, size_t index) {
universe@406 129 cx_linked_list *listdata = (cx_linked_list *) list->listdata;
universe@398 130 // TODO: implement using low level API
universe@398 131 return NULL;
universe@398 132 }
universe@398 133
universe@398 134 size_t cx_ll_find(cx_list *list, void *elem) {
universe@406 135 cx_linked_list *listdata = (cx_linked_list *) list->listdata;
universe@398 136 // TODO: implement using low level API
universe@398 137 return 0;
universe@398 138 }
universe@398 139
universe@404 140 void *cx_ll_last(cx_list *list) {
universe@406 141 cx_linked_list *listdata = (cx_linked_list *) list->listdata;
universe@404 142 struct cx_linked_list_node *last = cx_linked_list_last(
universe@406 143 NULL, &listdata->end, offsetof(struct cx_linked_list_node, next));
universe@404 144 return &last->payload;
universe@404 145 }
universe@398 146
universe@398 147 cx_list_class cx_linked_list_class = {
universe@398 148 cx_ll_add,
universe@398 149 cx_ll_insert,
universe@398 150 cx_ll_remove,
universe@404 151 cx_ll_find,
universe@404 152 cx_ll_last
universe@398 153 };
universe@398 154
universe@406 155 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
universe@406 156 CxLinkedListDesc desc;
universe@406 157 desc.item_size = item_size;
universe@406 158 desc.begin = desc.end = NULL;
universe@406 159 desc.loc_prev = offsetof(struct cx_linked_list_node, prev);
universe@406 160 desc.loc_next = offsetof(struct cx_linked_list_node, next);
universe@406 161
universe@406 162 return cxLinkedListWrap(allocator, comparator, desc);
universe@406 163 }
universe@406 164
universe@406 165 CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc) {
universe@406 166 CxList list = cxMalloc(allocator, sizeof(list) + sizeof(cx_linked_list));
universe@398 167 if (list == NULL)
universe@398 168 return NULL;
universe@398 169
universe@398 170 list->cl = &cx_linked_list_class;
universe@398 171 list->data.allocator = allocator;
universe@398 172 list->data.cmpfunc = comparator;
universe@406 173 list->data.itemsize = desc.item_size;
universe@401 174 list->data.capacity = SIZE_MAX;
universe@406 175
universe@406 176 cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
universe@406 177 ll->begin = desc.begin ? *desc.begin : NULL;
universe@406 178 ll->end = desc.end ? *desc.end : NULL;
universe@406 179 ll->loc_prev = desc.loc_prev;
universe@406 180 ll->loc_next = desc.loc_next;
universe@406 181 cxLinkedListRecalculateSize(list);
universe@406 182
universe@406 183 return list;
universe@406 184 }
universe@406 185
universe@406 186 size_t cxLinkedListRecalculateSize(CxList list) {
universe@406 187 cx_linked_list *ll = (cx_linked_list *) list->data.listdata;
universe@406 188
universe@406 189 if (ll->begin == NULL) {
universe@406 190 list->data.size = 0;
universe@406 191 return 0;
universe@406 192 } else {
universe@406 193 void *cur = ll->begin;
universe@406 194 size_t size;
universe@406 195 do {
universe@406 196 size++;
universe@406 197 } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL);
universe@406 198 list->data.size = size;
universe@406 199 return size;
universe@398 200 }
universe@406 201 }

mercurial