Mon, 27 Sep 2021 18:50:07 +0200
add high-level function cxListAt()
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@438 | 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@402 | 96 | struct cx_linked_list_node { |
universe@402 | 97 | void *prev; |
universe@402 | 98 | void *next; |
universe@403 | 99 | char payload[]; |
universe@402 | 100 | }; |
universe@402 | 101 | |
universe@439 | 102 | #define CX_LL_LOC_PREV offsetof(struct cx_linked_list_node, prev) |
universe@439 | 103 | #define CX_LL_LOC_NEXT offsetof(struct cx_linked_list_node, next) |
universe@439 | 104 | |
universe@398 | 105 | typedef struct { |
universe@435 | 106 | cx_list_s base; |
universe@401 | 107 | void *begin; |
universe@401 | 108 | void *end; |
universe@406 | 109 | ptrdiff_t loc_prev; |
universe@406 | 110 | ptrdiff_t loc_next; |
universe@398 | 111 | } cx_linked_list; |
universe@398 | 112 | |
universe@438 | 113 | static int cx_ll_add(cx_list_s *list, void *elem) { |
universe@437 | 114 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@398 | 115 | |
universe@435 | 116 | struct cx_linked_list_node *node = cxMalloc(list->allocator, |
universe@406 | 117 | sizeof(struct cx_linked_list_node) + list->itemsize); |
universe@398 | 118 | if (node == NULL) |
universe@398 | 119 | return 1; |
universe@398 | 120 | |
universe@435 | 121 | node->next = node->prev = NULL; |
universe@406 | 122 | memcpy(node->payload, elem, list->itemsize); |
universe@399 | 123 | |
universe@398 | 124 | int ret = cx_linked_list_add( |
universe@437 | 125 | &ll->begin, &ll->end, |
universe@439 | 126 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, |
universe@398 | 127 | node |
universe@398 | 128 | ); |
universe@398 | 129 | if (ret == 0) { |
universe@401 | 130 | list->size++; |
universe@398 | 131 | return 0; |
universe@398 | 132 | } else { |
universe@398 | 133 | return ret; |
universe@398 | 134 | } |
universe@398 | 135 | } |
universe@398 | 136 | |
universe@438 | 137 | static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) { |
universe@437 | 138 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@398 | 139 | // TODO: implement using low level API |
universe@398 | 140 | return 1; |
universe@398 | 141 | } |
universe@398 | 142 | |
universe@438 | 143 | static int cx_ll_remove(cx_list_s *list, size_t index) { |
universe@438 | 144 | if (index >= list->size) { |
universe@438 | 145 | return 1; |
universe@438 | 146 | } |
universe@437 | 147 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@398 | 148 | // TODO: implement using low level API |
universe@438 | 149 | return 0; |
universe@398 | 150 | } |
universe@398 | 151 | |
universe@439 | 152 | static void *cx_ll_at(cx_list_s *list, size_t index) { |
universe@439 | 153 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@439 | 154 | struct cx_linked_list_node *node; |
universe@439 | 155 | if (index > list->size / 2) { |
universe@439 | 156 | node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index); |
universe@439 | 157 | } else { |
universe@439 | 158 | node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index); |
universe@439 | 159 | } |
universe@439 | 160 | return &node->payload; |
universe@439 | 161 | } |
universe@439 | 162 | |
universe@438 | 163 | static size_t cx_ll_find(cx_list_s *list, void *elem) { |
universe@437 | 164 | CxListComparator cmp = list->cmpfunc; |
universe@437 | 165 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@437 | 166 | |
universe@437 | 167 | size_t index; |
universe@437 | 168 | struct cx_linked_list_node* node = ll->begin; |
universe@437 | 169 | for (index = 0 ; index < list->size ; index++) { |
universe@437 | 170 | void* current = node->payload; |
universe@437 | 171 | if (cmp(current, elem) == 0) { |
universe@437 | 172 | return index; |
universe@437 | 173 | } |
universe@437 | 174 | node = node->next; |
universe@437 | 175 | } |
universe@437 | 176 | return index; |
universe@398 | 177 | } |
universe@398 | 178 | |
universe@438 | 179 | static void *cx_ll_last(cx_list_s *list) { |
universe@439 | 180 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@439 | 181 | struct cx_linked_list_node *last = cx_linked_list_last(NULL, &ll->end, CX_LL_LOC_NEXT); |
universe@404 | 182 | return &last->payload; |
universe@404 | 183 | } |
universe@398 | 184 | |
universe@398 | 185 | cx_list_class cx_linked_list_class = { |
universe@398 | 186 | cx_ll_add, |
universe@398 | 187 | cx_ll_insert, |
universe@398 | 188 | cx_ll_remove, |
universe@439 | 189 | cx_ll_at, |
universe@404 | 190 | cx_ll_find, |
universe@404 | 191 | cx_ll_last |
universe@398 | 192 | }; |
universe@398 | 193 | |
universe@406 | 194 | CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { |
universe@435 | 195 | cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list)); |
universe@398 | 196 | if (list == NULL) |
universe@398 | 197 | return NULL; |
universe@398 | 198 | |
universe@435 | 199 | list->base.cl = &cx_linked_list_class; |
universe@435 | 200 | list->base.allocator = allocator; |
universe@435 | 201 | list->base.cmpfunc = comparator; |
universe@435 | 202 | list->base.itemsize = item_size; |
universe@435 | 203 | list->base.capacity = SIZE_MAX; |
universe@406 | 204 | |
universe@435 | 205 | list->begin = NULL; |
universe@439 | 206 | list->loc_prev = CX_LL_LOC_PREV; |
universe@439 | 207 | list->loc_next = CX_LL_LOC_NEXT; |
universe@406 | 208 | |
universe@435 | 209 | CxList result = (CxList) list; |
universe@435 | 210 | cxLinkedListRecalculateSize(result); |
universe@435 | 211 | return result; |
universe@406 | 212 | } |
universe@406 | 213 | |
universe@409 | 214 | void cxLinkedListDestroy(CxList list) { |
universe@436 | 215 | cx_linked_list* ll = (cx_linked_list*) list; |
universe@436 | 216 | |
universe@436 | 217 | struct cx_linked_list_node* node = ll->begin; |
universe@436 | 218 | while (node) { |
universe@436 | 219 | void* next = node->next; |
universe@436 | 220 | cxFree(list->allocator, node); |
universe@436 | 221 | node = next; |
universe@436 | 222 | } |
universe@436 | 223 | |
universe@435 | 224 | cxFree(list->allocator, list); |
universe@409 | 225 | } |
universe@409 | 226 | |
universe@406 | 227 | size_t cxLinkedListRecalculateSize(CxList list) { |
universe@435 | 228 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@406 | 229 | |
universe@406 | 230 | if (ll->begin == NULL) { |
universe@435 | 231 | ll->base.size = 0; |
universe@407 | 232 | ll->end = NULL; |
universe@406 | 233 | return 0; |
universe@406 | 234 | } else { |
universe@406 | 235 | void *cur = ll->begin; |
universe@407 | 236 | void *last; |
universe@435 | 237 | size_t size = 0; |
universe@406 | 238 | do { |
universe@407 | 239 | last = cur; |
universe@406 | 240 | size++; |
universe@406 | 241 | } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL); |
universe@407 | 242 | ll->end = last; |
universe@435 | 243 | ll->base.size = size; |
universe@406 | 244 | return size; |
universe@398 | 245 | } |
universe@406 | 246 | } |