src/linked_list.c

Mon, 20 Dec 2021 11:58:36 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Dec 2021 11:58:36 +0100
changeset 478
599770bb6314
parent 477
73a93c7a56ae
child 480
e3be53a3354f
permissions
-rw-r--r--

add more nonnull attributes

This also changes the contract for last/first in the sense that these
functions now also require a valid pointer.

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

mercurial