src/linked_list.c

Tue, 05 Oct 2021 13:04:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 05 Oct 2021 13:04:20 +0200
changeset 467
95e42a963520
parent 466
28bc3e10ac28
child 468
75ae1dccd101
permissions
-rw-r--r--

remove unused cxLinkedListRecalculateSize()

It is not clear what this function was ever supposed to do.

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

mercurial