src/linked_list.c

Fri, 12 Apr 2024 21:48:12 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Apr 2024 21:48:12 +0200
changeset 849
edb9f875b7f9
parent 829
7d4e31d295af
permissions
-rw-r--r--

improves interface of cx_sprintf() variants

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

mercurial