src/linked_list.c

Sun, 03 Oct 2021 12:04:27 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Oct 2021 12:04:27 +0200
changeset 451
db06dda7ac4d
parent 448
37c5d2fdb36b
child 453
bb144d08cd44
permissions
-rw-r--r--

make cx_linked_list_class static

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@438 37 void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) {
universe@438 38 size_t i = start_index;
universe@446 39 void *cur = start;
universe@438 40 while (i != index && cur != NULL) {
universe@438 41 cur = *CX_LL_PTR(cur, loc_advance);
universe@438 42 i < index ? i++ : i--;
universe@438 43 }
universe@438 44 return cur;
universe@438 45 }
universe@438 46
universe@400 47 void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) {
universe@398 48 if (end != NULL) {
universe@398 49 return *end;
universe@398 50 } else {
universe@398 51 if (begin == NULL || *begin == NULL)
universe@398 52 return NULL;
universe@398 53
universe@398 54 void *cur = *begin;
universe@398 55 void *last;
universe@398 56 do {
universe@398 57 last = cur;
universe@398 58 } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL);
universe@398 59
universe@398 60 return last;
universe@398 61 }
universe@398 62 }
universe@398 63
universe@406 64 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
universe@398 65 void *last = cx_linked_list_last(begin, end, loc_next);
universe@398 66 if (last == NULL) {
universe@398 67 if (begin == NULL) {
universe@428 68 // no current list and no begin ptr to write to - we don't find something to append to
universe@398 69 return 1;
universe@398 70 } else {
universe@428 71 // start fresh list
universe@406 72 *begin = new_node;
universe@398 73 }
universe@428 74 } else {
universe@428 75 // if there is a last node, update its next pointer
universe@428 76 void **next = CX_LL_PTR(last, loc_next);
universe@428 77 *next = new_node;
universe@398 78 }
universe@398 79
universe@428 80 // if there is an end pointer, update it
universe@408 81 if (end != NULL) {
universe@408 82 *end = cx_linked_list_last(&new_node, NULL, loc_next);
universe@408 83 }
universe@428 84
universe@428 85 // if the nodes use a prev pointer, update it
universe@398 86 if (loc_prev >= 0) {
universe@406 87 void **prev = CX_LL_PTR(new_node, loc_prev);
universe@398 88 *prev = last;
universe@398 89 }
universe@398 90
universe@398 91 return 0;
universe@398 92 }
universe@398 93
universe@398 94 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
universe@398 95
universe@447 96 typedef struct cx_linked_list_node cx_linked_list_node;
universe@447 97 struct cx_linked_list_node {
universe@447 98 cx_linked_list_node *prev;
universe@447 99 cx_linked_list_node *next;
universe@403 100 char payload[];
universe@447 101 };
universe@402 102
universe@446 103 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
universe@446 104 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
universe@439 105
universe@398 106 typedef struct {
universe@435 107 cx_list_s base;
universe@446 108 cx_linked_list_node *begin;
universe@446 109 cx_linked_list_node *end;
universe@398 110 } cx_linked_list;
universe@398 111
universe@448 112 static cx_linked_list_node *cx_ll_node_at(cx_linked_list *list, size_t index) {
universe@447 113 if (index >= list->base.size) {
universe@447 114 return NULL;
universe@447 115 } else if (index > list->base.size / 2) {
universe@447 116 return cx_linked_list_at(list->end, list->base.size, CX_LL_LOC_PREV, index);
universe@447 117 } else {
universe@447 118 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
universe@447 119 }
universe@447 120 }
universe@447 121
universe@438 122 static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) {
universe@448 123 // out-of bounds check
universe@448 124 if (index > list->size) return 1;
universe@448 125
universe@448 126 // cast a linked list pointer
universe@437 127 cx_linked_list *ll = (cx_linked_list *) list;
universe@448 128
universe@448 129 // create the new node
universe@448 130 cx_linked_list_node *node = cxMalloc(list->allocator,
universe@448 131 sizeof(cx_linked_list_node) + list->itemsize);
universe@448 132
universe@448 133 // sortir if failed
universe@448 134 if (node == NULL) return 1;
universe@448 135
universe@448 136 // copy payload to the new node
universe@448 137 memcpy(node->payload, elem, list->itemsize);
universe@448 138
universe@448 139 // check if this is the first node
universe@448 140 if (ll->begin == NULL) {
universe@448 141 node->prev = node->next = NULL;
universe@448 142 ll->begin = ll->end = node;
universe@448 143 } else {
universe@448 144 // check if this shall be the new end node
universe@448 145 if (index == list->size) {
universe@448 146 ll->end->next = node;
universe@448 147 node->prev = ll->end;
universe@448 148 node->next = NULL;
universe@448 149 ll->end = node;
universe@448 150 }
universe@448 151 // check if this shall be the new start node
universe@448 152 else if (index == 0) {
universe@448 153 ll->begin->prev = node;
universe@448 154 node->next = ll->begin;
universe@448 155 node->prev = NULL;
universe@448 156 ll->begin = node;
universe@448 157 } else {
universe@448 158 // find the node at the current index
universe@448 159 cx_linked_list_node *cur = cx_ll_node_at(ll, index);
universe@448 160
universe@448 161 // insert before that node
universe@448 162 // (we know all ptr are non-null because we handled all other cases before)
universe@448 163 node->next = cur;
universe@448 164 node->prev = cur->prev;
universe@448 165 cur->prev = node;
universe@448 166 node->prev->next = node;
universe@448 167 }
universe@448 168 }
universe@448 169
universe@448 170 // increase the size and return
universe@448 171 list->size++;
universe@448 172 return 0;
universe@448 173 }
universe@448 174
universe@448 175 static int cx_ll_add(cx_list_s *list, void *elem) {
universe@448 176 return cx_ll_insert(list, list->size, elem);
universe@398 177 }
universe@398 178
universe@438 179 static int cx_ll_remove(cx_list_s *list, size_t index) {
universe@447 180 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 181 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@447 182
universe@447 183 // out-of-bounds check
universe@447 184 if (node == NULL) return 1;
universe@447 185
universe@447 186 // change left side connection
universe@447 187 if (node->prev == NULL) {
universe@447 188 ll->begin = node->next;
universe@447 189 } else {
universe@447 190 node->prev->next = node->next;
universe@438 191 }
universe@447 192
universe@447 193 // change right side connection
universe@447 194 if (node->next == NULL) {
universe@447 195 ll->end = node->prev;
universe@447 196 } else {
universe@447 197 node->next->prev = node->prev;
universe@447 198 }
universe@447 199
universe@447 200 // adjust size
universe@447 201 list->size--;
universe@447 202
universe@447 203 // free and return
universe@447 204 cxFree(list->allocator, node);
universe@447 205
universe@438 206 return 0;
universe@398 207 }
universe@398 208
universe@439 209 static void *cx_ll_at(cx_list_s *list, size_t index) {
universe@439 210 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 211 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@440 212 return node == NULL ? NULL : &node->payload;
universe@439 213 }
universe@439 214
universe@438 215 static size_t cx_ll_find(cx_list_s *list, void *elem) {
universe@437 216 CxListComparator cmp = list->cmpfunc;
universe@437 217 cx_linked_list *ll = (cx_linked_list *) list;
universe@437 218
universe@437 219 size_t index;
universe@446 220 cx_linked_list_node *node = ll->begin;
universe@446 221 for (index = 0; index < list->size; index++) {
universe@446 222 void *current = node->payload;
universe@437 223 if (cmp(current, elem) == 0) {
universe@437 224 return index;
universe@437 225 }
universe@437 226 node = node->next;
universe@437 227 }
universe@437 228 return index;
universe@398 229 }
universe@398 230
universe@438 231 static void *cx_ll_last(cx_list_s *list) {
universe@439 232 cx_linked_list *ll = (cx_linked_list *) list;
universe@446 233 cx_linked_list_node *last = cx_linked_list_last(NULL, (void **) &ll->end, CX_LL_LOC_NEXT);
universe@404 234 return &last->payload;
universe@404 235 }
universe@398 236
universe@451 237 static cx_list_class cx_linked_list_class = {
universe@398 238 cx_ll_add,
universe@398 239 cx_ll_insert,
universe@398 240 cx_ll_remove,
universe@439 241 cx_ll_at,
universe@404 242 cx_ll_find,
universe@404 243 cx_ll_last
universe@398 244 };
universe@398 245
universe@406 246 CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) {
universe@446 247 cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list));
universe@398 248 if (list == NULL)
universe@398 249 return NULL;
universe@398 250
universe@435 251 list->base.cl = &cx_linked_list_class;
universe@435 252 list->base.allocator = allocator;
universe@435 253 list->base.cmpfunc = comparator;
universe@435 254 list->base.itemsize = item_size;
universe@435 255 list->base.capacity = SIZE_MAX;
universe@441 256 list->base.size = 0;
universe@406 257
universe@435 258 list->begin = NULL;
universe@441 259 list->end = NULL;
universe@406 260
universe@441 261 return (CxList) list;
universe@406 262 }
universe@406 263
universe@409 264 void cxLinkedListDestroy(CxList list) {
universe@446 265 cx_linked_list *ll = (cx_linked_list *) list;
universe@436 266
universe@446 267 cx_linked_list_node *node = ll->begin;
universe@436 268 while (node) {
universe@446 269 void *next = node->next;
universe@436 270 cxFree(list->allocator, node);
universe@436 271 node = next;
universe@436 272 }
universe@436 273
universe@435 274 cxFree(list->allocator, list);
universe@409 275 }
universe@409 276
universe@406 277 size_t cxLinkedListRecalculateSize(CxList list) {
universe@435 278 cx_linked_list *ll = (cx_linked_list *) list;
universe@406 279
universe@406 280 if (ll->begin == NULL) {
universe@435 281 ll->base.size = 0;
universe@407 282 ll->end = NULL;
universe@406 283 return 0;
universe@406 284 } else {
universe@446 285 cx_linked_list_node *cur = ll->begin;
universe@446 286 cx_linked_list_node *last;
universe@435 287 size_t size = 0;
universe@406 288 do {
universe@407 289 last = cur;
universe@406 290 size++;
universe@446 291 } while ((cur = cur->next) != NULL);
universe@407 292 ll->end = last;
universe@435 293 ll->base.size = size;
universe@406 294 return size;
universe@398 295 }
universe@406 296 }

mercurial