Mon, 20 Dec 2021 11:17:06 +0100
change contract of cx_linked_list_remove()
also use cx_linked_list_remove() in high level API
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@468 | 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@473 | 39 | assert(start != NULL); |
universe@473 | 40 | assert(loc_advance >= 0); |
universe@438 | 41 | size_t i = start_index; |
universe@446 | 42 | void *cur = start; |
universe@438 | 43 | while (i != index && cur != NULL) { |
universe@468 | 44 | cur = CX_LL_PTR(cur, loc_advance); |
universe@438 | 45 | i < index ? i++ : i--; |
universe@438 | 46 | } |
universe@438 | 47 | return cur; |
universe@438 | 48 | } |
universe@438 | 49 | |
universe@475 | 50 | void *cx_linked_list_first(void *node, ptrdiff_t loc_prev) { |
universe@475 | 51 | return cx_linked_list_last(node, loc_prev); |
universe@475 | 52 | } |
universe@475 | 53 | |
universe@475 | 54 | void *cx_linked_list_last(void *node, ptrdiff_t loc_next) { |
universe@473 | 55 | assert(loc_next >= 0); |
universe@475 | 56 | if (node == NULL) |
universe@456 | 57 | return NULL; |
universe@398 | 58 | |
universe@475 | 59 | void *cur = node; |
universe@456 | 60 | void *last; |
universe@456 | 61 | do { |
universe@456 | 62 | last = cur; |
universe@468 | 63 | } while ((cur = CX_LL_PTR(cur, loc_next)) != NULL); |
universe@398 | 64 | |
universe@456 | 65 | return last; |
universe@398 | 66 | } |
universe@398 | 67 | |
universe@473 | 68 | void *cx_linked_list_prev(void *begin, ptrdiff_t loc_next, void *node) { |
universe@473 | 69 | assert(begin != NULL); |
universe@473 | 70 | assert(loc_next >= 0); |
universe@473 | 71 | if (begin == node) return NULL; |
universe@473 | 72 | void *cur = begin; |
universe@473 | 73 | void *next; |
universe@473 | 74 | while (1) { |
universe@473 | 75 | next = CX_LL_PTR(cur, loc_next); |
universe@473 | 76 | if (next == node) return cur; |
universe@473 | 77 | cur = next; |
universe@473 | 78 | } |
universe@473 | 79 | } |
universe@473 | 80 | |
universe@453 | 81 | void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { |
universe@473 | 82 | assert(loc_next >= 0); |
universe@475 | 83 | assert(CX_LL_PTR(new_node, loc_next) == NULL); |
universe@456 | 84 | void *last; |
universe@456 | 85 | if (end == NULL) { |
universe@456 | 86 | assert(begin != NULL); |
universe@456 | 87 | last = cx_linked_list_last(*begin, loc_next); |
universe@456 | 88 | } else { |
universe@456 | 89 | last = *end; |
universe@456 | 90 | } |
universe@398 | 91 | if (last == NULL) { |
universe@475 | 92 | if (begin != NULL) { |
universe@475 | 93 | *begin = new_node; |
universe@475 | 94 | } |
universe@428 | 95 | } else { |
universe@428 | 96 | // if there is a last node, update its next pointer |
universe@468 | 97 | CX_LL_PTR(last, loc_next) = new_node; |
universe@398 | 98 | } |
universe@398 | 99 | |
universe@428 | 100 | // if there is an end pointer, update it |
universe@408 | 101 | if (end != NULL) { |
universe@475 | 102 | *end = new_node; |
universe@408 | 103 | } |
universe@428 | 104 | |
universe@428 | 105 | // if the nodes use a prev pointer, update it |
universe@398 | 106 | if (loc_prev >= 0) { |
universe@475 | 107 | assert(CX_LL_PTR(new_node, loc_prev) == NULL); |
universe@468 | 108 | CX_LL_PTR(new_node, loc_prev) = last; |
universe@398 | 109 | } |
universe@398 | 110 | } |
universe@398 | 111 | |
universe@475 | 112 | void cx_linked_list_prepend(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { |
universe@475 | 113 | assert(loc_next >= 0); |
universe@475 | 114 | assert(CX_LL_PTR(new_node, loc_next) == NULL); |
universe@475 | 115 | void *first; |
universe@475 | 116 | if (begin == NULL) { |
universe@475 | 117 | assert(end != NULL); |
universe@475 | 118 | assert(loc_prev >= 0); |
universe@475 | 119 | first = cx_linked_list_first(*end, loc_prev); |
universe@475 | 120 | } else { |
universe@475 | 121 | first = *begin; |
universe@475 | 122 | } |
universe@475 | 123 | if (first == NULL) { |
universe@475 | 124 | if (end != NULL) { |
universe@475 | 125 | *end = new_node; |
universe@475 | 126 | } |
universe@475 | 127 | } else { |
universe@475 | 128 | CX_LL_PTR(new_node, loc_next) = first; |
universe@475 | 129 | if (loc_prev >= 0) { |
universe@475 | 130 | CX_LL_PTR(first, loc_prev) = new_node; |
universe@475 | 131 | } |
universe@475 | 132 | } |
universe@475 | 133 | |
universe@475 | 134 | if (begin != NULL) { |
universe@475 | 135 | assert(loc_prev < 0 || CX_LL_PTR(new_node, loc_prev) == NULL); |
universe@475 | 136 | *begin = new_node; |
universe@475 | 137 | } |
universe@475 | 138 | } |
universe@475 | 139 | |
universe@476 | 140 | void cx_linked_list_remove(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *node) { |
universe@473 | 141 | assert(loc_next >= 0); |
universe@473 | 142 | assert(loc_prev >= 0 || begin != NULL); |
universe@473 | 143 | |
universe@473 | 144 | // find adjacent nodes |
universe@473 | 145 | void *next = CX_LL_PTR(node, loc_next); |
universe@473 | 146 | void *prev; |
universe@473 | 147 | if (loc_prev >= 0) { |
universe@473 | 148 | prev = CX_LL_PTR(node, loc_prev); |
universe@473 | 149 | } else { |
universe@473 | 150 | prev = cx_linked_list_prev(*begin, loc_next, node); |
universe@473 | 151 | } |
universe@473 | 152 | |
universe@476 | 153 | // update next pointer of prev node, or set begin |
universe@476 | 154 | if (prev == NULL) { |
universe@476 | 155 | if (begin != NULL) { |
universe@476 | 156 | *begin = next; |
universe@476 | 157 | } |
universe@476 | 158 | } else { |
universe@473 | 159 | CX_LL_PTR(prev, loc_next) = next; |
universe@473 | 160 | } |
universe@476 | 161 | |
universe@476 | 162 | // update prev pointer of next node, or set end |
universe@476 | 163 | if (next == NULL) { |
universe@476 | 164 | if (end != NULL) { |
universe@476 | 165 | *end = prev; |
universe@476 | 166 | } |
universe@476 | 167 | } else if (loc_prev >= 0) { |
universe@473 | 168 | CX_LL_PTR(next, loc_prev) = prev; |
universe@473 | 169 | } |
universe@473 | 170 | } |
universe@473 | 171 | |
universe@468 | 172 | size_t cx_linked_list_size(void *node, ptrdiff_t loc_next) { |
universe@473 | 173 | assert(loc_next >= 0); |
universe@468 | 174 | size_t size = 0; |
universe@468 | 175 | while (node != NULL) { |
universe@468 | 176 | node = CX_LL_PTR(node, loc_next); |
universe@468 | 177 | size++; |
universe@468 | 178 | } |
universe@468 | 179 | return size; |
universe@468 | 180 | } |
universe@468 | 181 | |
universe@468 | 182 | #define ll_prev(node) CX_LL_PTR(node, loc_prev) |
universe@468 | 183 | #define ll_next(node) CX_LL_PTR(node, loc_next) |
universe@468 | 184 | #define ll_data(node) (follow_ptr?CX_LL_PTR(node, loc_data):(((char*)node)+loc_data)) |
universe@468 | 185 | |
universe@468 | 186 | static void *cx_linked_list_sort_merge(ptrdiff_t loc_prev, ptrdiff_t loc_next, |
universe@468 | 187 | ptrdiff_t loc_data, int follow_ptr, |
universe@468 | 188 | size_t length, void *ls, void *le, void *re, |
universe@468 | 189 | CxListComparator cmp_func) { |
universe@468 | 190 | const size_t sbo_len = 1024; |
universe@468 | 191 | void *sbo[sbo_len]; |
universe@468 | 192 | void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo; |
universe@468 | 193 | void *rc, *lc; |
universe@468 | 194 | |
universe@468 | 195 | lc = ls; |
universe@468 | 196 | rc = le; |
universe@468 | 197 | size_t n = 0; |
universe@468 | 198 | while (lc && lc != le && rc != re) { |
universe@468 | 199 | if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) { |
universe@468 | 200 | sorted[n] = lc; |
universe@468 | 201 | lc = ll_next(lc); |
universe@468 | 202 | } else { |
universe@468 | 203 | sorted[n] = rc; |
universe@468 | 204 | rc = ll_next(rc); |
universe@468 | 205 | } |
universe@468 | 206 | n++; |
universe@468 | 207 | } |
universe@468 | 208 | while (lc && lc != le) { |
universe@468 | 209 | sorted[n] = lc; |
universe@468 | 210 | lc = ll_next(lc); |
universe@468 | 211 | n++; |
universe@468 | 212 | } |
universe@468 | 213 | while (rc && rc != re) { |
universe@468 | 214 | sorted[n] = rc; |
universe@468 | 215 | rc = ll_next(rc); |
universe@468 | 216 | n++; |
universe@468 | 217 | } |
universe@468 | 218 | |
universe@468 | 219 | // Update pointer |
universe@468 | 220 | if (loc_prev >= 0) ll_prev(sorted[0]) = NULL; |
universe@468 | 221 | for (size_t i = 0; i < length - 1; i++) { |
universe@468 | 222 | ll_next(sorted[i]) = sorted[i + 1]; |
universe@468 | 223 | if (loc_prev >= 0) ll_prev(sorted[i + 1]) = sorted[i]; |
universe@468 | 224 | } |
universe@468 | 225 | ll_next(sorted[length - 1]) = NULL; |
universe@468 | 226 | |
universe@468 | 227 | void *ret = sorted[0]; |
universe@468 | 228 | if (sorted != sbo) { |
universe@468 | 229 | free(sorted); |
universe@468 | 230 | } |
universe@468 | 231 | return ret; |
universe@468 | 232 | } |
universe@468 | 233 | |
universe@476 | 234 | void cx_linked_list_sort( /* NOLINT(misc-no-recursion) - purposely recursive function */ |
universe@476 | 235 | void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, |
universe@476 | 236 | ptrdiff_t loc_data, int follow_ptr, CxListComparator cmp_func) { |
universe@468 | 237 | assert(begin != NULL); |
universe@473 | 238 | assert(loc_next >= 0); |
universe@473 | 239 | assert(loc_data >= 0); |
universe@473 | 240 | assert(cmp_func); |
universe@468 | 241 | |
universe@468 | 242 | void *lc, *ls, *le, *re; |
universe@468 | 243 | |
universe@468 | 244 | // set start node |
universe@468 | 245 | ls = *begin; |
universe@468 | 246 | |
universe@468 | 247 | // check how many elements are already sorted |
universe@468 | 248 | lc = ls; |
universe@468 | 249 | size_t ln = 1; |
universe@468 | 250 | while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) { |
universe@468 | 251 | lc = ll_next(lc); |
universe@468 | 252 | ln++; |
universe@468 | 253 | } |
universe@468 | 254 | le = ll_next(lc); |
universe@468 | 255 | |
universe@468 | 256 | // if first unsorted node is NULL, the list is already completely sorted |
universe@468 | 257 | if (le != NULL) { |
universe@468 | 258 | void *rc; |
universe@468 | 259 | size_t rn = 1; |
universe@468 | 260 | rc = le; |
universe@468 | 261 | // skip already sorted elements |
universe@468 | 262 | while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) { |
universe@468 | 263 | rc = ll_next(rc); |
universe@468 | 264 | rn++; |
universe@468 | 265 | } |
universe@468 | 266 | re = ll_next(rc); |
universe@468 | 267 | |
universe@468 | 268 | // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them |
universe@468 | 269 | void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, |
universe@468 | 270 | ln + rn, ls, le, re, cmp_func); |
universe@468 | 271 | |
universe@468 | 272 | // Something left? Sort it! |
universe@468 | 273 | size_t remainder_length = cx_linked_list_size(re, loc_next); |
universe@468 | 274 | if (remainder_length > 0) { |
universe@468 | 275 | void *remainder = re; |
universe@468 | 276 | cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func); |
universe@468 | 277 | |
universe@468 | 278 | // merge sorted list with (also sorted) remainder |
universe@468 | 279 | *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr, |
universe@468 | 280 | ln + rn + remainder_length, |
universe@468 | 281 | sorted, remainder, NULL, cmp_func); |
universe@468 | 282 | } else { |
universe@468 | 283 | // no remainder - we've got our sorted list |
universe@468 | 284 | *begin = sorted; |
universe@468 | 285 | } |
universe@468 | 286 | if (end) *end = cx_linked_list_last(sorted, loc_next); |
universe@468 | 287 | } |
universe@468 | 288 | } |
universe@468 | 289 | |
universe@468 | 290 | #undef ll_next |
universe@468 | 291 | #undef ll_data |
universe@468 | 292 | |
universe@473 | 293 | void cx_linked_list_reverse(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next) { |
universe@473 | 294 | assert(begin != NULL); |
universe@473 | 295 | assert(loc_next >= 0); |
universe@473 | 296 | |
universe@473 | 297 | // swap all links |
universe@473 | 298 | void *prev = NULL; |
universe@473 | 299 | void *cur = *begin; |
universe@473 | 300 | while (cur != NULL) { |
universe@473 | 301 | void *next = CX_LL_PTR(cur, loc_next); |
universe@473 | 302 | |
universe@473 | 303 | CX_LL_PTR(cur, loc_next) = prev; |
universe@473 | 304 | if (loc_prev >= 0) { |
universe@473 | 305 | CX_LL_PTR(cur, loc_prev) = next; |
universe@473 | 306 | } |
universe@473 | 307 | |
universe@473 | 308 | prev = cur; |
universe@473 | 309 | cur = next; |
universe@473 | 310 | } |
universe@473 | 311 | |
universe@473 | 312 | // update begin and end |
universe@473 | 313 | if (end != NULL) { |
universe@473 | 314 | *end = *begin; |
universe@473 | 315 | } |
universe@473 | 316 | *begin = prev; |
universe@473 | 317 | } |
universe@473 | 318 | |
universe@398 | 319 | /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
universe@398 | 320 | |
universe@447 | 321 | typedef struct cx_linked_list_node cx_linked_list_node; |
universe@447 | 322 | struct cx_linked_list_node { |
universe@447 | 323 | cx_linked_list_node *prev; |
universe@447 | 324 | cx_linked_list_node *next; |
universe@403 | 325 | char payload[]; |
universe@447 | 326 | }; |
universe@402 | 327 | |
universe@446 | 328 | #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev) |
universe@446 | 329 | #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next) |
universe@469 | 330 | #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload) |
universe@439 | 331 | |
universe@398 | 332 | typedef struct { |
universe@435 | 333 | cx_list_s base; |
universe@446 | 334 | cx_linked_list_node *begin; |
universe@446 | 335 | cx_linked_list_node *end; |
universe@398 | 336 | } cx_linked_list; |
universe@398 | 337 | |
universe@448 | 338 | static cx_linked_list_node *cx_ll_node_at(cx_linked_list *list, size_t index) { |
universe@447 | 339 | if (index >= list->base.size) { |
universe@447 | 340 | return NULL; |
universe@447 | 341 | } else if (index > list->base.size / 2) { |
universe@468 | 342 | return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index); |
universe@447 | 343 | } else { |
universe@447 | 344 | return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index); |
universe@447 | 345 | } |
universe@447 | 346 | } |
universe@447 | 347 | |
universe@438 | 348 | static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) { |
universe@448 | 349 | // out-of bounds check |
universe@448 | 350 | if (index > list->size) return 1; |
universe@448 | 351 | |
universe@448 | 352 | // cast a linked list pointer |
universe@437 | 353 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@448 | 354 | |
universe@448 | 355 | // create the new node |
universe@448 | 356 | cx_linked_list_node *node = cxMalloc(list->allocator, |
universe@448 | 357 | sizeof(cx_linked_list_node) + list->itemsize); |
universe@448 | 358 | |
universe@448 | 359 | // sortir if failed |
universe@448 | 360 | if (node == NULL) return 1; |
universe@448 | 361 | |
universe@448 | 362 | // copy payload to the new node |
universe@448 | 363 | memcpy(node->payload, elem, list->itemsize); |
universe@448 | 364 | |
universe@448 | 365 | // check if this is the first node |
universe@448 | 366 | if (ll->begin == NULL) { |
universe@448 | 367 | node->prev = node->next = NULL; |
universe@448 | 368 | ll->begin = ll->end = node; |
universe@448 | 369 | } else { |
universe@448 | 370 | // check if this shall be the new end node |
universe@448 | 371 | if (index == list->size) { |
universe@448 | 372 | ll->end->next = node; |
universe@448 | 373 | node->prev = ll->end; |
universe@448 | 374 | node->next = NULL; |
universe@448 | 375 | ll->end = node; |
universe@448 | 376 | } |
universe@468 | 377 | // check if this shall be the new start node |
universe@448 | 378 | else if (index == 0) { |
universe@448 | 379 | ll->begin->prev = node; |
universe@448 | 380 | node->next = ll->begin; |
universe@448 | 381 | node->prev = NULL; |
universe@448 | 382 | ll->begin = node; |
universe@448 | 383 | } else { |
universe@448 | 384 | // find the node at the current index |
universe@448 | 385 | cx_linked_list_node *cur = cx_ll_node_at(ll, index); |
universe@448 | 386 | |
universe@448 | 387 | // insert before that node |
universe@448 | 388 | // (we know all ptr are non-null because we handled all other cases before) |
universe@448 | 389 | node->next = cur; |
universe@448 | 390 | node->prev = cur->prev; |
universe@448 | 391 | cur->prev = node; |
universe@448 | 392 | node->prev->next = node; |
universe@448 | 393 | } |
universe@448 | 394 | } |
universe@448 | 395 | |
universe@448 | 396 | // increase the size and return |
universe@448 | 397 | list->size++; |
universe@448 | 398 | return 0; |
universe@448 | 399 | } |
universe@448 | 400 | |
universe@448 | 401 | static int cx_ll_add(cx_list_s *list, void *elem) { |
universe@448 | 402 | return cx_ll_insert(list, list->size, elem); |
universe@398 | 403 | } |
universe@398 | 404 | |
universe@466 | 405 | static int cx_pll_insert(cx_list_s *list, size_t index, void *elem) { |
universe@466 | 406 | return cx_ll_insert(list, index, &elem); |
universe@466 | 407 | } |
universe@466 | 408 | |
universe@466 | 409 | static int cx_pll_add(cx_list_s *list, void *elem) { |
universe@466 | 410 | return cx_ll_insert(list, list->size, &elem); |
universe@466 | 411 | } |
universe@466 | 412 | |
universe@438 | 413 | static int cx_ll_remove(cx_list_s *list, size_t index) { |
universe@447 | 414 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 415 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@447 | 416 | |
universe@447 | 417 | // out-of-bounds check |
universe@447 | 418 | if (node == NULL) return 1; |
universe@447 | 419 | |
universe@476 | 420 | // remove |
universe@476 | 421 | cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end, |
universe@476 | 422 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node); |
universe@447 | 423 | |
universe@447 | 424 | // adjust size |
universe@447 | 425 | list->size--; |
universe@447 | 426 | |
universe@447 | 427 | // free and return |
universe@447 | 428 | cxFree(list->allocator, node); |
universe@447 | 429 | |
universe@438 | 430 | return 0; |
universe@398 | 431 | } |
universe@398 | 432 | |
universe@439 | 433 | static void *cx_ll_at(cx_list_s *list, size_t index) { |
universe@439 | 434 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@447 | 435 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@466 | 436 | return node == NULL ? NULL : node->payload; |
universe@466 | 437 | } |
universe@466 | 438 | |
universe@466 | 439 | static void *cx_pll_at(cx_list_s *list, size_t index) { |
universe@466 | 440 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@466 | 441 | cx_linked_list_node *node = cx_ll_node_at(ll, index); |
universe@468 | 442 | return node == NULL ? NULL : *(void **) node->payload; |
universe@439 | 443 | } |
universe@439 | 444 | |
universe@438 | 445 | static size_t cx_ll_find(cx_list_s *list, void *elem) { |
universe@437 | 446 | CxListComparator cmp = list->cmpfunc; |
universe@437 | 447 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@437 | 448 | |
universe@437 | 449 | size_t index; |
universe@446 | 450 | cx_linked_list_node *node = ll->begin; |
universe@446 | 451 | for (index = 0; index < list->size; index++) { |
universe@446 | 452 | void *current = node->payload; |
universe@437 | 453 | if (cmp(current, elem) == 0) { |
universe@437 | 454 | return index; |
universe@437 | 455 | } |
universe@437 | 456 | node = node->next; |
universe@437 | 457 | } |
universe@437 | 458 | return index; |
universe@398 | 459 | } |
universe@398 | 460 | |
universe@466 | 461 | static size_t cx_pll_find(cx_list_s *list, void *elem) { |
universe@466 | 462 | CxListComparator cmp = list->cmpfunc; |
universe@466 | 463 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@466 | 464 | |
universe@466 | 465 | size_t index; |
universe@466 | 466 | cx_linked_list_node *node = ll->begin; |
universe@466 | 467 | for (index = 0; index < list->size; index++) { |
universe@468 | 468 | void *current = *(void **) node->payload; |
universe@466 | 469 | if (cmp(current, elem) == 0) { |
universe@466 | 470 | return index; |
universe@466 | 471 | } |
universe@466 | 472 | node = node->next; |
universe@466 | 473 | } |
universe@466 | 474 | return index; |
universe@466 | 475 | } |
universe@466 | 476 | |
universe@469 | 477 | static void cx_ll_sort(cx_list_s *list) { |
universe@469 | 478 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@469 | 479 | cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end, |
universe@469 | 480 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@469 | 481 | 0, list->cmpfunc); |
universe@469 | 482 | } |
universe@469 | 483 | |
universe@469 | 484 | static void cx_pll_sort(cx_list_s *list) { |
universe@469 | 485 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@469 | 486 | cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end, |
universe@469 | 487 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA, |
universe@469 | 488 | 1, list->cmpfunc); |
universe@469 | 489 | } |
universe@469 | 490 | |
universe@451 | 491 | static cx_list_class cx_linked_list_class = { |
universe@398 | 492 | cx_ll_add, |
universe@398 | 493 | cx_ll_insert, |
universe@398 | 494 | cx_ll_remove, |
universe@439 | 495 | cx_ll_at, |
universe@404 | 496 | cx_ll_find, |
universe@469 | 497 | cx_ll_sort |
universe@398 | 498 | }; |
universe@398 | 499 | |
universe@466 | 500 | static cx_list_class cx_pointer_linked_list_class = { |
universe@466 | 501 | cx_pll_add, |
universe@466 | 502 | cx_pll_insert, |
universe@466 | 503 | cx_ll_remove, |
universe@466 | 504 | cx_pll_at, |
universe@466 | 505 | cx_pll_find, |
universe@469 | 506 | cx_pll_sort |
universe@466 | 507 | }; |
universe@466 | 508 | |
universe@406 | 509 | CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { |
universe@446 | 510 | cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); |
universe@398 | 511 | if (list == NULL) |
universe@398 | 512 | return NULL; |
universe@398 | 513 | |
universe@435 | 514 | list->base.cl = &cx_linked_list_class; |
universe@435 | 515 | list->base.allocator = allocator; |
universe@435 | 516 | list->base.cmpfunc = comparator; |
universe@435 | 517 | list->base.itemsize = item_size; |
universe@435 | 518 | list->base.capacity = SIZE_MAX; |
universe@441 | 519 | list->base.size = 0; |
universe@406 | 520 | |
universe@435 | 521 | list->begin = NULL; |
universe@441 | 522 | list->end = NULL; |
universe@406 | 523 | |
universe@441 | 524 | return (CxList) list; |
universe@406 | 525 | } |
universe@406 | 526 | |
universe@466 | 527 | CxList cxPointerLinkedListCreate(CxAllocator allocator, CxListComparator comparator) { |
universe@466 | 528 | cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); |
universe@466 | 529 | if (list == NULL) |
universe@466 | 530 | return NULL; |
universe@466 | 531 | |
universe@466 | 532 | list->base.cl = &cx_pointer_linked_list_class; |
universe@466 | 533 | list->base.allocator = allocator; |
universe@466 | 534 | list->base.cmpfunc = comparator; |
universe@468 | 535 | list->base.itemsize = sizeof(void *); |
universe@466 | 536 | list->base.capacity = SIZE_MAX; |
universe@466 | 537 | list->base.size = 0; |
universe@466 | 538 | |
universe@466 | 539 | list->begin = NULL; |
universe@466 | 540 | list->end = NULL; |
universe@466 | 541 | |
universe@466 | 542 | return (CxList) list; |
universe@466 | 543 | } |
universe@466 | 544 | |
universe@409 | 545 | void cxLinkedListDestroy(CxList list) { |
universe@446 | 546 | cx_linked_list *ll = (cx_linked_list *) list; |
universe@436 | 547 | |
universe@446 | 548 | cx_linked_list_node *node = ll->begin; |
universe@436 | 549 | while (node) { |
universe@446 | 550 | void *next = node->next; |
universe@436 | 551 | cxFree(list->allocator, node); |
universe@436 | 552 | node = next; |
universe@436 | 553 | } |
universe@436 | 554 | |
universe@435 | 555 | cxFree(list->allocator, list); |
universe@409 | 556 | } |