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