Tue, 05 Oct 2021 11:19:32 +0200
remove convenience macros
Users should write their own wrappers s.t. the type
information does not have to be repeated on every
call site.
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@438 | 175 | static int cx_ll_remove(cx_list_s *list, size_t index) { |
universe@447 | 176 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 177 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@447 | 178 | |
universe@447 | 179 | // out-of-bounds check |
universe@447 | 180 | if (node == NULL) return 1; |
universe@447 | 181 | |
universe@447 | 182 | // change left side connection |
universe@447 | 183 | if (node->prev == NULL) { |
universe@447 | 184 | ll->begin = node->next; |
universe@447 | 185 | } else { |
universe@447 | 186 | node->prev->next = node->next; |
universe@438 | 187 | } |
universe@447 | 188 | |
universe@447 | 189 | // change right side connection |
universe@447 | 190 | if (node->next == NULL) { |
universe@447 | 191 | ll->end = node->prev; |
universe@447 | 192 | } else { |
universe@447 | 193 | node->next->prev = node->prev; |
universe@447 | 194 | } |
universe@447 | 195 | |
universe@447 | 196 | // adjust size |
universe@447 | 197 | list->size--; |
universe@447 | 198 | |
universe@447 | 199 | // free and return |
universe@447 | 200 | cxFree(list->allocator, node); |
universe@447 | 201 | |
universe@438 | 202 | return 0; |
universe@398 | 203 | } |
universe@398 | 204 | |
universe@439 | 205 | static void *cx_ll_at(cx_list_s *list, size_t index) { |
universe@439 | 206 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 207 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@440 | 208 | return node == NULL ? NULL : &node->payload; |
universe@439 | 209 | } |
universe@439 | 210 | |
universe@438 | 211 | static size_t cx_ll_find(cx_list_s *list, void *elem) { |
universe@437 | 212 | CxListComparator cmp = list->cmpfunc; |
universe@437 | 213 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@437 | 214 | |
universe@437 | 215 | size_t index; |
universe@446 | 216 | cx_linked_list_node *node = ll->begin; |
universe@446 | 217 | for (index = 0; index < list->size; index++) { |
universe@446 | 218 | void *current = node->payload; |
universe@437 | 219 | if (cmp(current, elem) == 0) { |
universe@437 | 220 | return index; |
universe@437 | 221 | } |
universe@437 | 222 | node = node->next; |
universe@437 | 223 | } |
universe@437 | 224 | return index; |
universe@398 | 225 | } |
universe@398 | 226 | |
universe@438 | 227 | static void *cx_ll_last(cx_list_s *list) { |
universe@439 | 228 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@456 | 229 | cx_linked_list_node *last = ll->end; |
universe@456 | 230 | return last == NULL ? NULL : &last->payload; |
universe@404 | 231 | } |
universe@398 | 232 | |
universe@451 | 233 | static cx_list_class cx_linked_list_class = { |
universe@398 | 234 | cx_ll_add, |
universe@398 | 235 | cx_ll_insert, |
universe@398 | 236 | cx_ll_remove, |
universe@439 | 237 | cx_ll_at, |
universe@404 | 238 | cx_ll_find, |
universe@404 | 239 | cx_ll_last |
universe@398 | 240 | }; |
universe@398 | 241 | |
universe@406 | 242 | CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { |
universe@446 | 243 | cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); |
universe@398 | 244 | if (list == NULL) |
universe@398 | 245 | return NULL; |
universe@398 | 246 | |
universe@435 | 247 | list->base.cl = &cx_linked_list_class; |
universe@435 | 248 | list->base.allocator = allocator; |
universe@435 | 249 | list->base.cmpfunc = comparator; |
universe@435 | 250 | list->base.itemsize = item_size; |
universe@435 | 251 | list->base.capacity = SIZE_MAX; |
universe@441 | 252 | list->base.size = 0; |
universe@406 | 253 | |
universe@435 | 254 | list->begin = NULL; |
universe@441 | 255 | list->end = NULL; |
universe@406 | 256 | |
universe@441 | 257 | return (CxList) list; |
universe@406 | 258 | } |
universe@406 | 259 | |
universe@409 | 260 | void cxLinkedListDestroy(CxList list) { |
universe@446 | 261 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@436 | 262 | |
universe@446 | 263 | cx_linked_list_node *node = ll->begin; |
universe@436 | 264 | while (node) { |
universe@446 | 265 | void *next = node->next; |
universe@436 | 266 | cxFree(list->allocator, node); |
universe@436 | 267 | node = next; |
universe@436 | 268 | } |
universe@436 | 269 | |
universe@435 | 270 | cxFree(list->allocator, list); |
universe@409 | 271 | } |
universe@409 | 272 | |
universe@406 | 273 | size_t cxLinkedListRecalculateSize(CxList list) { |
universe@435 | 274 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@406 | 275 | |
universe@406 | 276 | if (ll->begin == NULL) { |
universe@435 | 277 | ll->base.size = 0; |
universe@407 | 278 | ll->end = NULL; |
universe@406 | 279 | return 0; |
universe@406 | 280 | } else { |
universe@446 | 281 | cx_linked_list_node *cur = ll->begin; |
universe@446 | 282 | cx_linked_list_node *last; |
universe@435 | 283 | size_t size = 0; |
universe@406 | 284 | do { |
universe@407 | 285 | last = cur; |
universe@406 | 286 | size++; |
universe@446 | 287 | } while ((cur = cur->next) != NULL); |
universe@407 | 288 | ll->end = last; |
universe@435 | 289 | ll->base.size = size; |
universe@406 | 290 | return size; |
universe@398 | 291 | } |
universe@406 | 292 | } |