src/linked_list.c

Mon, 20 Dec 2021 13:01:38 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Dec 2021 13:01:38 +0100
changeset 480
e3be53a3354f
parent 478
599770bb6314
child 481
eef025d82a34
permissions
-rw-r--r--

add cx_linked_list_find()

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

mercurial