src/linked_list.c

Sat, 26 Nov 2022 16:58:41 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Nov 2022 16:58:41 +0100
changeset 630
ac5e7f789048
parent 629
6c81ee4f11ad
child 638
eafb45eefc51
permissions
-rw-r--r--

separate iterators and mutating iterators

Trade tons of code duplication for const-correctness.

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@509 30 #include "cx/utils.h"
universe@401 31 #include <stdint.h>
universe@401 32 #include <string.h>
universe@453 33 #include <assert.h>
universe@398 34
universe@628 35 // LOW LEVEL LINKED LIST FUNCTIONS
universe@398 36
universe@592 37 #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
universe@480 38 #define ll_prev(node) CX_LL_PTR(node, loc_prev)
universe@480 39 #define ll_next(node) CX_LL_PTR(node, loc_next)
universe@480 40 #define ll_advance(node) CX_LL_PTR(node, loc_advance)
universe@592 41 #define ll_data_f(node, follow_ptr) ((follow_ptr)?CX_LL_PTR(node, loc_data):(((char*)(node))+loc_data))
universe@552 42 #define ll_data(node) ll_data_f(node,follow_ptr)
universe@398 43
universe@481 44 void *cx_linked_list_at(
universe@508 45 void const *start,
universe@481 46 size_t start_index,
universe@481 47 ptrdiff_t loc_advance,
universe@481 48 size_t index
universe@481 49 ) {
universe@473 50 assert(start != NULL);
universe@473 51 assert(loc_advance >= 0);
universe@438 52 size_t i = start_index;
universe@508 53 void const *cur = start;
universe@438 54 while (i != index && cur != NULL) {
universe@480 55 cur = ll_advance(cur);
universe@438 56 i < index ? i++ : i--;
universe@438 57 }
universe@508 58 return (void *) cur;
universe@438 59 }
universe@438 60
universe@481 61 size_t cx_linked_list_find(
universe@489 62 void const *start,
universe@481 63 ptrdiff_t loc_advance,
universe@481 64 ptrdiff_t loc_data,
universe@487 65 bool follow_ptr,
universe@481 66 CxListComparator cmp_func,
universe@489 67 void const *elem
universe@481 68 ) {
universe@480 69 assert(start != NULL);
universe@480 70 assert(loc_advance >= 0);
universe@480 71 assert(loc_data >= 0);
universe@480 72 assert(cmp_func);
universe@480 73
universe@489 74 void const *node = start;
universe@480 75 size_t index = 0;
universe@480 76 do {
universe@480 77 void *current = ll_data(node);
universe@480 78 if (cmp_func(current, elem) == 0) {
universe@480 79 return index;
universe@480 80 }
universe@480 81 node = ll_advance(node);
universe@480 82 index++;
universe@480 83 } while (node != NULL);
universe@480 84 return index;
universe@480 85 }
universe@480 86
universe@481 87 void *cx_linked_list_first(
universe@508 88 void const *node,
universe@481 89 ptrdiff_t loc_prev
universe@481 90 ) {
universe@475 91 return cx_linked_list_last(node, loc_prev);
universe@475 92 }
universe@475 93
universe@481 94 void *cx_linked_list_last(
universe@508 95 void const *node,
universe@481 96 ptrdiff_t loc_next
universe@481 97 ) {
universe@478 98 assert(node != NULL);
universe@473 99 assert(loc_next >= 0);
universe@398 100
universe@508 101 void const *cur = node;
universe@508 102 void const *last;
universe@456 103 do {
universe@456 104 last = cur;
universe@480 105 } while ((cur = ll_next(cur)) != NULL);
universe@398 106
universe@508 107 return (void *) last;
universe@398 108 }
universe@398 109
universe@481 110 void *cx_linked_list_prev(
universe@508 111 void const *begin,
universe@481 112 ptrdiff_t loc_next,
universe@508 113 void const *node
universe@481 114 ) {
universe@473 115 assert(begin != NULL);
universe@478 116 assert(node != NULL);
universe@473 117 assert(loc_next >= 0);
universe@473 118 if (begin == node) return NULL;
universe@508 119 void const *cur = begin;
universe@508 120 void const *next;
universe@473 121 while (1) {
universe@480 122 next = ll_next(cur);
universe@508 123 if (next == node) return (void *) cur;
universe@473 124 cur = next;
universe@473 125 }
universe@473 126 }
universe@473 127
universe@481 128 void cx_linked_list_link(
universe@481 129 void *left,
universe@481 130 void *right,
universe@481 131 ptrdiff_t loc_prev,
universe@481 132 ptrdiff_t loc_next
universe@481 133 ) {
universe@473 134 assert(loc_next >= 0);
universe@481 135 ll_next(left) = right;
universe@481 136 if (loc_prev >= 0) {
universe@481 137 ll_prev(right) = left;
universe@481 138 }
universe@481 139 }
universe@481 140
universe@481 141 void cx_linked_list_unlink(
universe@481 142 void *left,
universe@481 143 void *right,
universe@481 144 ptrdiff_t loc_prev,
universe@481 145 ptrdiff_t loc_next
universe@481 146 ) {
universe@481 147 assert (loc_next >= 0);
universe@481 148 assert(ll_next(left) == right);
universe@481 149 ll_next(left) = NULL;
universe@481 150 if (loc_prev >= 0) {
universe@481 151 assert(ll_prev(right) == left);
universe@481 152 ll_prev(right) = NULL;
universe@481 153 }
universe@481 154 }
universe@481 155
universe@481 156 void cx_linked_list_add(
universe@481 157 void **begin,
universe@481 158 void **end,
universe@481 159 ptrdiff_t loc_prev,
universe@481 160 ptrdiff_t loc_next,
universe@481 161 void *new_node
universe@481 162 ) {
universe@456 163 void *last;
universe@456 164 if (end == NULL) {
universe@456 165 assert(begin != NULL);
universe@478 166 last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
universe@456 167 } else {
universe@456 168 last = *end;
universe@456 169 }
universe@481 170 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
universe@481 171 }
universe@481 172
universe@481 173 void cx_linked_list_prepend(
universe@481 174 void **begin,
universe@481 175 void **end,
universe@481 176 ptrdiff_t loc_prev,
universe@481 177 ptrdiff_t loc_next,
universe@481 178 void *new_node
universe@481 179 ) {
universe@481 180 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
universe@481 181 }
universe@481 182
universe@481 183 void cx_linked_list_insert(
universe@481 184 void **begin,
universe@481 185 void **end,
universe@481 186 ptrdiff_t loc_prev,
universe@481 187 ptrdiff_t loc_next,
universe@481 188 void *node,
universe@481 189 void *new_node
universe@481 190 ) {
universe@481 191 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
universe@481 192 }
universe@481 193
universe@481 194 void cx_linked_list_insert_chain(
universe@481 195 void **begin,
universe@481 196 void **end,
universe@481 197 ptrdiff_t loc_prev,
universe@481 198 ptrdiff_t loc_next,
universe@481 199 void *node,
universe@481 200 void *insert_begin,
universe@481 201 void *insert_end
universe@481 202 ) {
universe@481 203 // find the end of the chain, if not specified
universe@481 204 if (insert_end == NULL) {
universe@481 205 insert_end = cx_linked_list_last(insert_begin, loc_next);
universe@481 206 }
universe@481 207
universe@481 208 // determine the successor
universe@481 209 void *successor;
universe@481 210 if (node == NULL) {
universe@481 211 assert(begin != NULL || (end != NULL && loc_prev >= 0));
universe@475 212 if (begin != NULL) {
universe@481 213 successor = *begin;
universe@481 214 *begin = insert_begin;
universe@481 215 } else {
universe@481 216 successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
universe@475 217 }
universe@428 218 } else {
universe@481 219 successor = ll_next(node);
universe@481 220 cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
universe@398 221 }
universe@398 222
universe@481 223 if (successor == NULL) {
universe@481 224 // the list ends with the new chain
universe@481 225 if (end != NULL) {
universe@481 226 *end = insert_end;
universe@481 227 }
universe@481 228 } else {
universe@481 229 cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
universe@398 230 }
universe@398 231 }
universe@398 232
universe@481 233 void cx_linked_list_remove(
universe@481 234 void **begin,
universe@481 235 void **end,
universe@481 236 ptrdiff_t loc_prev,
universe@481 237 ptrdiff_t loc_next,
universe@481 238 void *node
universe@481 239 ) {
universe@477 240 assert(node != NULL);
universe@473 241 assert(loc_next >= 0);
universe@473 242 assert(loc_prev >= 0 || begin != NULL);
universe@473 243
universe@473 244 // find adjacent nodes
universe@480 245 void *next = ll_next(node);
universe@473 246 void *prev;
universe@473 247 if (loc_prev >= 0) {
universe@480 248 prev = ll_prev(node);
universe@473 249 } else {
universe@473 250 prev = cx_linked_list_prev(*begin, loc_next, node);
universe@473 251 }
universe@473 252
universe@476 253 // update next pointer of prev node, or set begin
universe@476 254 if (prev == NULL) {
universe@476 255 if (begin != NULL) {
universe@476 256 *begin = next;
universe@476 257 }
universe@476 258 } else {
universe@480 259 ll_next(prev) = next;
universe@473 260 }
universe@476 261
universe@476 262 // update prev pointer of next node, or set end
universe@476 263 if (next == NULL) {
universe@476 264 if (end != NULL) {
universe@476 265 *end = prev;
universe@476 266 }
universe@476 267 } else if (loc_prev >= 0) {
universe@480 268 ll_prev(next) = prev;
universe@473 269 }
universe@473 270 }
universe@473 271
universe@481 272 size_t cx_linked_list_size(
universe@489 273 void const *node,
universe@481 274 ptrdiff_t loc_next
universe@481 275 ) {
universe@473 276 assert(loc_next >= 0);
universe@468 277 size_t size = 0;
universe@468 278 while (node != NULL) {
universe@480 279 node = ll_next(node);
universe@468 280 size++;
universe@468 281 }
universe@468 282 return size;
universe@468 283 }
universe@468 284
universe@481 285 static void *cx_linked_list_sort_merge(
universe@481 286 ptrdiff_t loc_prev,
universe@481 287 ptrdiff_t loc_next,
universe@481 288 ptrdiff_t loc_data,
universe@487 289 bool follow_ptr,
universe@481 290 size_t length,
universe@481 291 void *ls,
universe@481 292 void *le,
universe@481 293 void *re,
universe@481 294 CxListComparator cmp_func
universe@481 295 ) {
universe@468 296 const size_t sbo_len = 1024;
universe@468 297 void *sbo[sbo_len];
universe@468 298 void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
universe@508 299 if (sorted == NULL) abort();
universe@468 300 void *rc, *lc;
universe@468 301
universe@468 302 lc = ls;
universe@468 303 rc = le;
universe@468 304 size_t n = 0;
universe@468 305 while (lc && lc != le && rc != re) {
universe@468 306 if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
universe@468 307 sorted[n] = lc;
universe@468 308 lc = ll_next(lc);
universe@468 309 } else {
universe@468 310 sorted[n] = rc;
universe@468 311 rc = ll_next(rc);
universe@468 312 }
universe@468 313 n++;
universe@468 314 }
universe@468 315 while (lc && lc != le) {
universe@468 316 sorted[n] = lc;
universe@468 317 lc = ll_next(lc);
universe@468 318 n++;
universe@468 319 }
universe@468 320 while (rc && rc != re) {
universe@468 321 sorted[n] = rc;
universe@468 322 rc = ll_next(rc);
universe@468 323 n++;
universe@468 324 }
universe@468 325
universe@468 326 // Update pointer
universe@468 327 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
universe@509 328 cx_for_n (i, length - 1) {
universe@481 329 cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
universe@468 330 }
universe@468 331 ll_next(sorted[length - 1]) = NULL;
universe@468 332
universe@468 333 void *ret = sorted[0];
universe@468 334 if (sorted != sbo) {
universe@468 335 free(sorted);
universe@468 336 }
universe@468 337 return ret;
universe@468 338 }
universe@468 339
universe@628 340 void cx_linked_list_sort( // NOLINT(misc-no-recursion) - purposely recursive function
universe@481 341 void **begin,
universe@481 342 void **end,
universe@481 343 ptrdiff_t loc_prev,
universe@481 344 ptrdiff_t loc_next,
universe@481 345 ptrdiff_t loc_data,
universe@487 346 bool follow_ptr,
universe@481 347 CxListComparator cmp_func
universe@481 348 ) {
universe@468 349 assert(begin != NULL);
universe@473 350 assert(loc_next >= 0);
universe@473 351 assert(loc_data >= 0);
universe@473 352 assert(cmp_func);
universe@468 353
universe@468 354 void *lc, *ls, *le, *re;
universe@468 355
universe@468 356 // set start node
universe@468 357 ls = *begin;
universe@468 358
universe@468 359 // check how many elements are already sorted
universe@468 360 lc = ls;
universe@468 361 size_t ln = 1;
universe@468 362 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
universe@468 363 lc = ll_next(lc);
universe@468 364 ln++;
universe@468 365 }
universe@468 366 le = ll_next(lc);
universe@468 367
universe@468 368 // if first unsorted node is NULL, the list is already completely sorted
universe@468 369 if (le != NULL) {
universe@468 370 void *rc;
universe@468 371 size_t rn = 1;
universe@468 372 rc = le;
universe@468 373 // skip already sorted elements
universe@468 374 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
universe@468 375 rc = ll_next(rc);
universe@468 376 rn++;
universe@468 377 }
universe@468 378 re = ll_next(rc);
universe@468 379
universe@468 380 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
universe@468 381 void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
universe@468 382 ln + rn, ls, le, re, cmp_func);
universe@468 383
universe@468 384 // Something left? Sort it!
universe@468 385 size_t remainder_length = cx_linked_list_size(re, loc_next);
universe@468 386 if (remainder_length > 0) {
universe@468 387 void *remainder = re;
universe@468 388 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func);
universe@468 389
universe@468 390 // merge sorted list with (also sorted) remainder
universe@468 391 *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
universe@468 392 ln + rn + remainder_length,
universe@468 393 sorted, remainder, NULL, cmp_func);
universe@468 394 } else {
universe@468 395 // no remainder - we've got our sorted list
universe@468 396 *begin = sorted;
universe@468 397 }
universe@468 398 if (end) *end = cx_linked_list_last(sorted, loc_next);
universe@468 399 }
universe@468 400 }
universe@468 401
universe@486 402 int cx_linked_list_compare(
universe@489 403 void const *begin_left,
universe@489 404 void const *begin_right,
universe@486 405 ptrdiff_t loc_advance,
universe@486 406 ptrdiff_t loc_data,
universe@552 407 bool follow_ptr_left,
universe@552 408 bool follow_ptr_right,
universe@486 409 CxListComparator cmp_func
universe@486 410 ) {
universe@489 411 void const *left = begin_left, *right = begin_right;
universe@486 412
universe@486 413 while (left != NULL && right != NULL) {
universe@552 414 void const *left_data = ll_data_f(left, follow_ptr_left);
universe@552 415 void const *right_data = ll_data_f(right, follow_ptr_right);
universe@552 416 int result = cmp_func(left_data, right_data);
universe@486 417 if (result != 0) return result;
universe@486 418 left = ll_advance(left);
universe@486 419 right = ll_advance(right);
universe@486 420 }
universe@486 421
universe@486 422 if (left != NULL) { return 1; }
universe@486 423 else if (right != NULL) { return -1; }
universe@486 424 else { return 0; }
universe@486 425 }
universe@486 426
universe@481 427 void cx_linked_list_reverse(
universe@481 428 void **begin,
universe@481 429 void **end,
universe@481 430 ptrdiff_t loc_prev,
universe@481 431 ptrdiff_t loc_next
universe@481 432 ) {
universe@473 433 assert(begin != NULL);
universe@473 434 assert(loc_next >= 0);
universe@473 435
universe@473 436 // swap all links
universe@473 437 void *prev = NULL;
universe@473 438 void *cur = *begin;
universe@473 439 while (cur != NULL) {
universe@480 440 void *next = ll_next(cur);
universe@473 441
universe@480 442 ll_next(cur) = prev;
universe@473 443 if (loc_prev >= 0) {
universe@480 444 ll_prev(cur) = next;
universe@473 445 }
universe@473 446
universe@473 447 prev = cur;
universe@473 448 cur = next;
universe@473 449 }
universe@473 450
universe@473 451 // update begin and end
universe@473 452 if (end != NULL) {
universe@473 453 *end = *begin;
universe@473 454 }
universe@473 455 *begin = prev;
universe@473 456 }
universe@473 457
universe@628 458 // HIGH LEVEL LINKED LIST IMPLEMENTATION
universe@398 459
universe@447 460 typedef struct cx_linked_list_node cx_linked_list_node;
universe@447 461 struct cx_linked_list_node {
universe@447 462 cx_linked_list_node *prev;
universe@447 463 cx_linked_list_node *next;
universe@403 464 char payload[];
universe@447 465 };
universe@402 466
universe@446 467 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
universe@446 468 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
universe@469 469 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
universe@439 470
universe@398 471 typedef struct {
universe@500 472 struct cx_list_s base;
universe@446 473 cx_linked_list_node *begin;
universe@446 474 cx_linked_list_node *end;
universe@552 475 bool follow_ptr;
universe@398 476 } cx_linked_list;
universe@398 477
universe@481 478 static cx_linked_list_node *cx_ll_node_at(
universe@489 479 cx_linked_list const *list,
universe@481 480 size_t index
universe@481 481 ) {
universe@447 482 if (index >= list->base.size) {
universe@447 483 return NULL;
universe@447 484 } else if (index > list->base.size / 2) {
universe@468 485 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
universe@447 486 } else {
universe@447 487 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
universe@447 488 }
universe@447 489 }
universe@447 490
universe@499 491 static int cx_ll_insert_at(
universe@500 492 struct cx_list_s *list,
universe@499 493 cx_linked_list_node *node,
universe@489 494 void const *elem
universe@481 495 ) {
universe@448 496
universe@481 497 // create the new new_node
universe@481 498 cx_linked_list_node *new_node = cxMalloc(list->allocator,
universe@481 499 sizeof(cx_linked_list_node) + list->itemsize);
universe@448 500
universe@448 501 // sortir if failed
universe@481 502 if (new_node == NULL) return 1;
universe@448 503
universe@481 504 // initialize new new_node
universe@481 505 new_node->prev = new_node->next = NULL;
universe@481 506 memcpy(new_node->payload, elem, list->itemsize);
universe@448 507
universe@481 508 // insert
universe@499 509 cx_linked_list *ll = (cx_linked_list *) list;
universe@481 510 cx_linked_list_insert_chain(
universe@481 511 (void **) &ll->begin, (void **) &ll->end,
universe@481 512 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
universe@481 513 node, new_node, new_node
universe@481 514 );
universe@448 515
universe@448 516 // increase the size and return
universe@448 517 list->size++;
universe@448 518 return 0;
universe@448 519 }
universe@448 520
universe@499 521 static int cx_ll_insert(
universe@500 522 struct cx_list_s *list,
universe@499 523 size_t index,
universe@499 524 void const *elem
universe@499 525 ) {
universe@499 526 // out-of bounds check
universe@499 527 if (index > list->size) return 1;
universe@499 528
universe@499 529 // find position efficiently
universe@499 530 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
universe@499 531
universe@499 532 // perform insert
universe@499 533 return cx_ll_insert_at(list, node, elem);
universe@499 534 }
universe@499 535
universe@481 536 static int cx_ll_add(
universe@500 537 struct cx_list_s *list,
universe@489 538 void const *elem
universe@481 539 ) {
universe@448 540 return cx_ll_insert(list, list->size, elem);
universe@398 541 }
universe@398 542
universe@629 543 static size_t cx_ll_add_array(
universe@629 544 struct cx_list_s *list,
universe@629 545 void const *array,
universe@629 546 size_t n
universe@629 547 ) {
universe@629 548 // TODO: redirect to cx_ll_insert_array
universe@629 549 cx_for_n (i, n) {
universe@629 550 if (cx_ll_add(list, ((char const *) array) + i * list->itemsize)) {
universe@629 551 return i;
universe@629 552 }
universe@629 553 }
universe@629 554 return n;
universe@629 555 }
universe@629 556
universe@481 557 static int cx_pll_insert(
universe@500 558 struct cx_list_s *list,
universe@481 559 size_t index,
universe@489 560 void const *elem
universe@481 561 ) {
universe@466 562 return cx_ll_insert(list, index, &elem);
universe@466 563 }
universe@466 564
universe@481 565 static int cx_pll_add(
universe@500 566 struct cx_list_s *list,
universe@489 567 void const *elem
universe@481 568 ) {
universe@466 569 return cx_ll_insert(list, list->size, &elem);
universe@466 570 }
universe@466 571
universe@481 572 static int cx_ll_remove(
universe@500 573 struct cx_list_s *list,
universe@481 574 size_t index
universe@481 575 ) {
universe@447 576 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 577 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@447 578
universe@447 579 // out-of-bounds check
universe@447 580 if (node == NULL) return 1;
universe@447 581
universe@476 582 // remove
universe@476 583 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
universe@476 584 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
universe@447 585
universe@447 586 // adjust size
universe@447 587 list->size--;
universe@447 588
universe@447 589 // free and return
universe@447 590 cxFree(list->allocator, node);
universe@447 591
universe@438 592 return 0;
universe@398 593 }
universe@398 594
universe@481 595 static void *cx_ll_at(
universe@500 596 struct cx_list_s const *list,
universe@481 597 size_t index
universe@481 598 ) {
universe@439 599 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 600 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@466 601 return node == NULL ? NULL : node->payload;
universe@466 602 }
universe@466 603
universe@481 604 static void *cx_pll_at(
universe@500 605 struct cx_list_s const *list,
universe@481 606 size_t index
universe@481 607 ) {
universe@466 608 cx_linked_list *ll = (cx_linked_list *) list;
universe@466 609 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@468 610 return node == NULL ? NULL : *(void **) node->payload;
universe@439 611 }
universe@439 612
universe@481 613 static size_t cx_ll_find(
universe@500 614 struct cx_list_s const *list,
universe@489 615 void const *elem
universe@481 616 ) {
universe@552 617 cx_linked_list *ll = (cx_linked_list *) list;
universe@480 618 return cx_linked_list_find(((cx_linked_list *) list)->begin,
universe@480 619 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
universe@552 620 ll->follow_ptr, list->cmpfunc, elem);
universe@466 621 }
universe@466 622
universe@500 623 static void cx_ll_sort(struct cx_list_s *list) {
universe@469 624 cx_linked_list *ll = (cx_linked_list *) list;
universe@469 625 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
universe@469 626 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
universe@552 627 ll->follow_ptr, list->cmpfunc);
universe@469 628 }
universe@469 629
universe@500 630 static void cx_ll_reverse(struct cx_list_s *list) {
universe@490 631 cx_linked_list *ll = (cx_linked_list *) list;
universe@521 632 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
universe@490 633 }
universe@490 634
universe@488 635 static int cx_ll_compare(
universe@500 636 struct cx_list_s const *list,
universe@500 637 struct cx_list_s const *other
universe@488 638 ) {
universe@488 639 cx_linked_list *left = (cx_linked_list *) list;
universe@488 640 cx_linked_list *right = (cx_linked_list *) other;
universe@488 641 return cx_linked_list_compare(left->begin, right->begin,
universe@488 642 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
universe@552 643 left->follow_ptr, right->follow_ptr, list->cmpfunc);
universe@488 644 }
universe@488 645
universe@630 646 static bool cx_ll_iter_valid(void const *it) {
universe@630 647 struct cx_iterator_s const *iter = it;
universe@495 648 return iter->elem_handle != NULL;
universe@494 649 }
universe@494 650
universe@630 651 static void cx_ll_iter_next(void *it) {
universe@630 652 struct cx_iterator_base_s *itbase = it;
universe@630 653 if (itbase->remove) {
universe@630 654 itbase->remove = false;
universe@630 655 struct cx_mut_iterator_s *iter = it;
universe@495 656 cx_linked_list *ll = iter->src_handle;
universe@495 657 cx_linked_list_node *node = iter->elem_handle;
universe@495 658 iter->elem_handle = node->next;
universe@495 659 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
universe@495 660 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
universe@495 661 ll->base.size--;
universe@495 662 cxFree(ll->base.allocator, node);
universe@495 663 } else {
universe@630 664 struct cx_iterator_s *iter = it;
universe@495 665 iter->index++;
universe@495 666 cx_linked_list_node *node = iter->elem_handle;
universe@495 667 iter->elem_handle = node->next;
universe@495 668 }
universe@494 669 }
universe@494 670
universe@630 671 static void *cx_ll_iter_current(void const *it) {
universe@630 672 struct cx_iterator_s const *iter = it;
universe@495 673 cx_linked_list_node *node = iter->elem_handle;
universe@494 674 return node->payload;
universe@494 675 }
universe@494 676
universe@630 677 static void *cx_pll_iter_current(void const *it) {
universe@630 678 struct cx_iterator_s const *iter = it;
universe@495 679 cx_linked_list_node *node = iter->elem_handle;
universe@494 680 return *(void **) node->payload;
universe@494 681 }
universe@494 682
universe@630 683 static bool cx_ll_iter_flag_rm(void *it) {
universe@630 684 struct cx_iterator_base_s *iter = it;
universe@630 685 if (iter->mutating) {
universe@630 686 iter->remove = true;
universe@630 687 return true;
universe@630 688 } else {
universe@630 689 return false;
universe@630 690 }
universe@630 691 }
universe@630 692
universe@494 693 static CxIterator cx_ll_iterator(
universe@630 694 struct cx_list_s const *list,
universe@494 695 size_t index
universe@494 696 ) {
universe@494 697 CxIterator iter;
universe@494 698 iter.index = index;
universe@495 699 iter.src_handle = list;
universe@495 700 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
universe@630 701 iter.base.valid = cx_ll_iter_valid;
universe@630 702 iter.base.current = cx_ll_iter_current;
universe@630 703 iter.base.next = cx_ll_iter_next;
universe@630 704 iter.base.flag_removal = cx_ll_iter_flag_rm;
universe@630 705 iter.base.mutating = false;
universe@630 706 iter.base.remove = false;
universe@494 707 return iter;
universe@494 708 }
universe@494 709
universe@494 710 static CxIterator cx_pll_iterator(
universe@630 711 struct cx_list_s const *list,
universe@630 712 size_t index
universe@630 713 ) {
universe@630 714 CxIterator iter = cx_ll_iterator(list, index);
universe@630 715 iter.base.current = cx_pll_iter_current;
universe@630 716 return iter;
universe@630 717 }
universe@630 718
universe@630 719 static CxMutIterator cx_ll_mut_iterator(
universe@500 720 struct cx_list_s *list,
universe@494 721 size_t index
universe@494 722 ) {
universe@630 723 CxIterator it = cx_ll_iterator(list, index);
universe@630 724 it.base.mutating = true;
universe@630 725
universe@630 726 // we know the iterators share the same memory layout
universe@630 727 CxMutIterator iter;
universe@630 728 memcpy(&iter, &it, sizeof(CxMutIterator));
universe@630 729 return iter;
universe@630 730 }
universe@630 731
universe@630 732 static CxMutIterator cx_pll_mut_iterator(
universe@630 733 struct cx_list_s *list,
universe@630 734 size_t index
universe@630 735 ) {
universe@630 736 CxMutIterator iter = cx_ll_mut_iterator(list, index);
universe@630 737 iter.base.current = cx_pll_iter_current;
universe@494 738 return iter;
universe@494 739 }
universe@494 740
universe@499 741 static int cx_ll_insert_iter(
universe@630 742 CxMutIterator *iter,
universe@499 743 void const *elem,
universe@499 744 int prepend
universe@499 745 ) {
universe@500 746 struct cx_list_s *list = iter->src_handle;
universe@499 747 cx_linked_list_node *node = iter->elem_handle;
universe@499 748 if (node != NULL) {
universe@499 749 assert(prepend >= 0 && prepend <= 1);
universe@499 750 cx_linked_list_node *choice[2] = {node, node->prev};
universe@499 751 int result = cx_ll_insert_at(list, choice[prepend], elem);
universe@499 752 iter->index += prepend * (0 == result);
universe@499 753 return result;
universe@499 754 } else {
universe@499 755 int result = cx_ll_insert(list, list->size, elem);
universe@499 756 iter->index = list->size;
universe@499 757 return result;
universe@499 758 }
universe@499 759 }
universe@499 760
universe@499 761 static int cx_pll_insert_iter(
universe@630 762 CxMutIterator *iter,
universe@499 763 void const *elem,
universe@499 764 int prepend
universe@499 765 ) {
universe@499 766 return cx_ll_insert_iter(iter, &elem, prepend);
universe@499 767 }
universe@499 768
universe@524 769 static void cx_ll_destructor(CxList *list) {
universe@524 770 cx_linked_list *ll = (cx_linked_list *) list;
universe@524 771
universe@524 772 cx_linked_list_node *node = ll->begin;
universe@524 773 while (node) {
universe@524 774 void *next = node->next;
universe@524 775 cxFree(list->allocator, node);
universe@524 776 node = next;
universe@524 777 }
universe@524 778 // do not free the list pointer, this is just a destructor!
universe@524 779 }
universe@524 780
universe@451 781 static cx_list_class cx_linked_list_class = {
universe@524 782 cx_ll_destructor,
universe@398 783 cx_ll_add,
universe@629 784 cx_ll_add_array,
universe@398 785 cx_ll_insert,
universe@499 786 cx_ll_insert_iter,
universe@398 787 cx_ll_remove,
universe@439 788 cx_ll_at,
universe@404 789 cx_ll_find,
universe@488 790 cx_ll_sort,
universe@490 791 cx_ll_compare,
universe@494 792 cx_ll_reverse,
universe@630 793 cx_ll_iterator,
universe@630 794 cx_ll_mut_iterator,
universe@398 795 };
universe@398 796
universe@466 797 static cx_list_class cx_pointer_linked_list_class = {
universe@524 798 cx_ll_destructor,
universe@466 799 cx_pll_add,
universe@629 800 cx_ll_add_array,
universe@466 801 cx_pll_insert,
universe@499 802 cx_pll_insert_iter,
universe@466 803 cx_ll_remove,
universe@466 804 cx_pll_at,
universe@552 805 cx_ll_find,
universe@552 806 cx_ll_sort,
universe@552 807 cx_ll_compare,
universe@494 808 cx_ll_reverse,
universe@494 809 cx_pll_iterator,
universe@630 810 cx_pll_mut_iterator,
universe@466 811 };
universe@466 812
universe@500 813 CxList *cxLinkedListCreate(
universe@508 814 CxAllocator const *allocator,
universe@481 815 CxListComparator comparator,
universe@481 816 size_t item_size
universe@481 817 ) {
universe@503 818 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
universe@528 819 if (list == NULL) return NULL;
universe@398 820
universe@552 821 list->follow_ptr = false;
universe@435 822 list->base.cl = &cx_linked_list_class;
universe@435 823 list->base.allocator = allocator;
universe@435 824 list->base.cmpfunc = comparator;
universe@435 825 list->base.itemsize = item_size;
universe@435 826 list->base.capacity = SIZE_MAX;
universe@406 827
universe@500 828 return (CxList *) list;
universe@406 829 }
universe@406 830
universe@500 831 CxList *cxPointerLinkedListCreate(
universe@508 832 CxAllocator const *allocator,
universe@481 833 CxListComparator comparator
universe@481 834 ) {
universe@503 835 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
universe@528 836 if (list == NULL) return NULL;
universe@466 837
universe@552 838 list->follow_ptr = true;
universe@466 839 list->base.cl = &cx_pointer_linked_list_class;
universe@466 840 list->base.allocator = allocator;
universe@466 841 list->base.cmpfunc = comparator;
universe@468 842 list->base.itemsize = sizeof(void *);
universe@466 843 list->base.capacity = SIZE_MAX;
universe@466 844
universe@500 845 return (CxList *) list;
universe@466 846 }

mercurial