src/linked_list.c

Mon, 20 Dec 2021 11:26:39 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Dec 2021 11:26:39 +0100
changeset 477
73a93c7a56ae
parent 476
60ff4561dc04
child 478
599770bb6314
permissions
-rw-r--r--

add more explicit documentation to cx_linked_list_remove()

also require nonnull node argument

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

mercurial