src/linked_list.c

Mon, 23 Jan 2023 20:22:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 23 Jan 2023 20:22:11 +0100
changeset 638
eafb45eefc51
parent 630
ac5e7f789048
child 639
309e8b08c60e
permissions
-rw-r--r--

add cxListInsertArray() - fixes #224

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@638 521 static size_t cx_ll_insert_array(
universe@638 522 struct cx_list_s *list,
universe@638 523 size_t index,
universe@638 524 void const *array,
universe@638 525 size_t n
universe@638 526 ) {
universe@638 527 // out-of bounds and corner case check
universe@638 528 if (index > list->size || n == 0) return 0;
universe@638 529
universe@638 530 // find position efficiently
universe@638 531 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
universe@638 532
universe@638 533 // perform first insert
universe@638 534 if (0 != cx_ll_insert_at(list, node, array)) {
universe@638 535 return 1;
universe@638 536 }
universe@638 537
universe@638 538 // is there more?
universe@638 539 if (n == 1) return 1;
universe@638 540
universe@638 541 // we now know exactly where we are
universe@638 542 node = node == NULL ? ((cx_linked_list *) list)->begin : node->next;
universe@638 543
universe@638 544 // we can add the remaining nodes and immedately advance to the inserted node
universe@638 545 char const *source = array;
universe@638 546 for (size_t i = 1; i < n; i++) {
universe@638 547 source += list->itemsize;
universe@638 548 if (0 != cx_ll_insert_at(list, node, source)) {
universe@638 549 return i;
universe@638 550 }
universe@638 551 node = node->next;
universe@638 552 }
universe@638 553 return n;
universe@638 554 }
universe@638 555
universe@499 556 static int cx_ll_insert(
universe@500 557 struct cx_list_s *list,
universe@499 558 size_t index,
universe@499 559 void const *elem
universe@499 560 ) {
universe@638 561 return cx_ll_insert_array(list, index, elem, 1) != 1;
universe@499 562 }
universe@499 563
universe@481 564 static int cx_ll_add(
universe@500 565 struct cx_list_s *list,
universe@489 566 void const *elem
universe@481 567 ) {
universe@448 568 return cx_ll_insert(list, list->size, elem);
universe@398 569 }
universe@398 570
universe@629 571 static size_t cx_ll_add_array(
universe@629 572 struct cx_list_s *list,
universe@629 573 void const *array,
universe@629 574 size_t n
universe@629 575 ) {
universe@638 576 return cx_ll_insert_array(list, list->size, array, n);
universe@629 577 }
universe@629 578
universe@481 579 static int cx_pll_insert(
universe@500 580 struct cx_list_s *list,
universe@481 581 size_t index,
universe@489 582 void const *elem
universe@481 583 ) {
universe@466 584 return cx_ll_insert(list, index, &elem);
universe@466 585 }
universe@466 586
universe@481 587 static int cx_pll_add(
universe@500 588 struct cx_list_s *list,
universe@489 589 void const *elem
universe@481 590 ) {
universe@466 591 return cx_ll_insert(list, list->size, &elem);
universe@466 592 }
universe@466 593
universe@481 594 static int cx_ll_remove(
universe@500 595 struct cx_list_s *list,
universe@481 596 size_t index
universe@481 597 ) {
universe@447 598 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 599 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@447 600
universe@447 601 // out-of-bounds check
universe@447 602 if (node == NULL) return 1;
universe@447 603
universe@476 604 // remove
universe@476 605 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
universe@476 606 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
universe@447 607
universe@447 608 // adjust size
universe@447 609 list->size--;
universe@447 610
universe@447 611 // free and return
universe@447 612 cxFree(list->allocator, node);
universe@447 613
universe@438 614 return 0;
universe@398 615 }
universe@398 616
universe@481 617 static void *cx_ll_at(
universe@500 618 struct cx_list_s const *list,
universe@481 619 size_t index
universe@481 620 ) {
universe@439 621 cx_linked_list *ll = (cx_linked_list *) list;
universe@447 622 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@466 623 return node == NULL ? NULL : node->payload;
universe@466 624 }
universe@466 625
universe@481 626 static void *cx_pll_at(
universe@500 627 struct cx_list_s const *list,
universe@481 628 size_t index
universe@481 629 ) {
universe@466 630 cx_linked_list *ll = (cx_linked_list *) list;
universe@466 631 cx_linked_list_node *node = cx_ll_node_at(ll, index);
universe@468 632 return node == NULL ? NULL : *(void **) node->payload;
universe@439 633 }
universe@439 634
universe@481 635 static size_t cx_ll_find(
universe@500 636 struct cx_list_s const *list,
universe@489 637 void const *elem
universe@481 638 ) {
universe@552 639 cx_linked_list *ll = (cx_linked_list *) list;
universe@480 640 return cx_linked_list_find(((cx_linked_list *) list)->begin,
universe@480 641 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
universe@552 642 ll->follow_ptr, list->cmpfunc, elem);
universe@466 643 }
universe@466 644
universe@500 645 static void cx_ll_sort(struct cx_list_s *list) {
universe@469 646 cx_linked_list *ll = (cx_linked_list *) list;
universe@469 647 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
universe@469 648 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
universe@552 649 ll->follow_ptr, list->cmpfunc);
universe@469 650 }
universe@469 651
universe@500 652 static void cx_ll_reverse(struct cx_list_s *list) {
universe@490 653 cx_linked_list *ll = (cx_linked_list *) list;
universe@521 654 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
universe@490 655 }
universe@490 656
universe@488 657 static int cx_ll_compare(
universe@500 658 struct cx_list_s const *list,
universe@500 659 struct cx_list_s const *other
universe@488 660 ) {
universe@488 661 cx_linked_list *left = (cx_linked_list *) list;
universe@488 662 cx_linked_list *right = (cx_linked_list *) other;
universe@488 663 return cx_linked_list_compare(left->begin, right->begin,
universe@488 664 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
universe@552 665 left->follow_ptr, right->follow_ptr, list->cmpfunc);
universe@488 666 }
universe@488 667
universe@630 668 static bool cx_ll_iter_valid(void const *it) {
universe@630 669 struct cx_iterator_s const *iter = it;
universe@495 670 return iter->elem_handle != NULL;
universe@494 671 }
universe@494 672
universe@630 673 static void cx_ll_iter_next(void *it) {
universe@630 674 struct cx_iterator_base_s *itbase = it;
universe@630 675 if (itbase->remove) {
universe@630 676 itbase->remove = false;
universe@630 677 struct cx_mut_iterator_s *iter = it;
universe@495 678 cx_linked_list *ll = iter->src_handle;
universe@495 679 cx_linked_list_node *node = iter->elem_handle;
universe@495 680 iter->elem_handle = node->next;
universe@495 681 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
universe@495 682 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
universe@495 683 ll->base.size--;
universe@495 684 cxFree(ll->base.allocator, node);
universe@495 685 } else {
universe@630 686 struct cx_iterator_s *iter = it;
universe@495 687 iter->index++;
universe@495 688 cx_linked_list_node *node = iter->elem_handle;
universe@495 689 iter->elem_handle = node->next;
universe@495 690 }
universe@494 691 }
universe@494 692
universe@630 693 static void *cx_ll_iter_current(void const *it) {
universe@630 694 struct cx_iterator_s const *iter = it;
universe@495 695 cx_linked_list_node *node = iter->elem_handle;
universe@494 696 return node->payload;
universe@494 697 }
universe@494 698
universe@630 699 static void *cx_pll_iter_current(void const *it) {
universe@630 700 struct cx_iterator_s const *iter = it;
universe@495 701 cx_linked_list_node *node = iter->elem_handle;
universe@494 702 return *(void **) node->payload;
universe@494 703 }
universe@494 704
universe@630 705 static bool cx_ll_iter_flag_rm(void *it) {
universe@630 706 struct cx_iterator_base_s *iter = it;
universe@630 707 if (iter->mutating) {
universe@630 708 iter->remove = true;
universe@630 709 return true;
universe@630 710 } else {
universe@630 711 return false;
universe@630 712 }
universe@630 713 }
universe@630 714
universe@494 715 static CxIterator cx_ll_iterator(
universe@630 716 struct cx_list_s const *list,
universe@494 717 size_t index
universe@494 718 ) {
universe@494 719 CxIterator iter;
universe@494 720 iter.index = index;
universe@495 721 iter.src_handle = list;
universe@495 722 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
universe@630 723 iter.base.valid = cx_ll_iter_valid;
universe@630 724 iter.base.current = cx_ll_iter_current;
universe@630 725 iter.base.next = cx_ll_iter_next;
universe@630 726 iter.base.flag_removal = cx_ll_iter_flag_rm;
universe@630 727 iter.base.mutating = false;
universe@630 728 iter.base.remove = false;
universe@494 729 return iter;
universe@494 730 }
universe@494 731
universe@494 732 static CxIterator cx_pll_iterator(
universe@630 733 struct cx_list_s const *list,
universe@630 734 size_t index
universe@630 735 ) {
universe@630 736 CxIterator iter = cx_ll_iterator(list, index);
universe@630 737 iter.base.current = cx_pll_iter_current;
universe@630 738 return iter;
universe@630 739 }
universe@630 740
universe@630 741 static CxMutIterator cx_ll_mut_iterator(
universe@500 742 struct cx_list_s *list,
universe@494 743 size_t index
universe@494 744 ) {
universe@630 745 CxIterator it = cx_ll_iterator(list, index);
universe@630 746 it.base.mutating = true;
universe@630 747
universe@630 748 // we know the iterators share the same memory layout
universe@630 749 CxMutIterator iter;
universe@630 750 memcpy(&iter, &it, sizeof(CxMutIterator));
universe@630 751 return iter;
universe@630 752 }
universe@630 753
universe@630 754 static CxMutIterator cx_pll_mut_iterator(
universe@630 755 struct cx_list_s *list,
universe@630 756 size_t index
universe@630 757 ) {
universe@630 758 CxMutIterator iter = cx_ll_mut_iterator(list, index);
universe@630 759 iter.base.current = cx_pll_iter_current;
universe@494 760 return iter;
universe@494 761 }
universe@494 762
universe@499 763 static int cx_ll_insert_iter(
universe@630 764 CxMutIterator *iter,
universe@499 765 void const *elem,
universe@499 766 int prepend
universe@499 767 ) {
universe@500 768 struct cx_list_s *list = iter->src_handle;
universe@499 769 cx_linked_list_node *node = iter->elem_handle;
universe@499 770 if (node != NULL) {
universe@499 771 assert(prepend >= 0 && prepend <= 1);
universe@499 772 cx_linked_list_node *choice[2] = {node, node->prev};
universe@499 773 int result = cx_ll_insert_at(list, choice[prepend], elem);
universe@499 774 iter->index += prepend * (0 == result);
universe@499 775 return result;
universe@499 776 } else {
universe@499 777 int result = cx_ll_insert(list, list->size, elem);
universe@499 778 iter->index = list->size;
universe@499 779 return result;
universe@499 780 }
universe@499 781 }
universe@499 782
universe@499 783 static int cx_pll_insert_iter(
universe@630 784 CxMutIterator *iter,
universe@499 785 void const *elem,
universe@499 786 int prepend
universe@499 787 ) {
universe@499 788 return cx_ll_insert_iter(iter, &elem, prepend);
universe@499 789 }
universe@499 790
universe@524 791 static void cx_ll_destructor(CxList *list) {
universe@524 792 cx_linked_list *ll = (cx_linked_list *) list;
universe@524 793
universe@524 794 cx_linked_list_node *node = ll->begin;
universe@524 795 while (node) {
universe@524 796 void *next = node->next;
universe@524 797 cxFree(list->allocator, node);
universe@524 798 node = next;
universe@524 799 }
universe@524 800 // do not free the list pointer, this is just a destructor!
universe@524 801 }
universe@524 802
universe@451 803 static cx_list_class cx_linked_list_class = {
universe@524 804 cx_ll_destructor,
universe@398 805 cx_ll_add,
universe@629 806 cx_ll_add_array,
universe@398 807 cx_ll_insert,
universe@638 808 cx_ll_insert_array,
universe@499 809 cx_ll_insert_iter,
universe@398 810 cx_ll_remove,
universe@439 811 cx_ll_at,
universe@404 812 cx_ll_find,
universe@488 813 cx_ll_sort,
universe@490 814 cx_ll_compare,
universe@494 815 cx_ll_reverse,
universe@630 816 cx_ll_iterator,
universe@630 817 cx_ll_mut_iterator,
universe@398 818 };
universe@398 819
universe@466 820 static cx_list_class cx_pointer_linked_list_class = {
universe@524 821 cx_ll_destructor,
universe@466 822 cx_pll_add,
universe@629 823 cx_ll_add_array,
universe@466 824 cx_pll_insert,
universe@638 825 cx_ll_insert_array,
universe@499 826 cx_pll_insert_iter,
universe@466 827 cx_ll_remove,
universe@466 828 cx_pll_at,
universe@552 829 cx_ll_find,
universe@552 830 cx_ll_sort,
universe@552 831 cx_ll_compare,
universe@494 832 cx_ll_reverse,
universe@494 833 cx_pll_iterator,
universe@630 834 cx_pll_mut_iterator,
universe@466 835 };
universe@466 836
universe@500 837 CxList *cxLinkedListCreate(
universe@508 838 CxAllocator const *allocator,
universe@481 839 CxListComparator comparator,
universe@481 840 size_t item_size
universe@481 841 ) {
universe@503 842 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
universe@528 843 if (list == NULL) return NULL;
universe@398 844
universe@552 845 list->follow_ptr = false;
universe@435 846 list->base.cl = &cx_linked_list_class;
universe@435 847 list->base.allocator = allocator;
universe@435 848 list->base.cmpfunc = comparator;
universe@435 849 list->base.itemsize = item_size;
universe@435 850 list->base.capacity = SIZE_MAX;
universe@406 851
universe@500 852 return (CxList *) list;
universe@406 853 }
universe@406 854
universe@500 855 CxList *cxPointerLinkedListCreate(
universe@508 856 CxAllocator const *allocator,
universe@481 857 CxListComparator comparator
universe@481 858 ) {
universe@503 859 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
universe@528 860 if (list == NULL) return NULL;
universe@466 861
universe@552 862 list->follow_ptr = true;
universe@466 863 list->base.cl = &cx_pointer_linked_list_class;
universe@466 864 list->base.allocator = allocator;
universe@466 865 list->base.cmpfunc = comparator;
universe@468 866 list->base.itemsize = sizeof(void *);
universe@466 867 list->base.capacity = SIZE_MAX;
universe@466 868
universe@500 869 return (CxList *) list;
universe@466 870 }

mercurial