src/linked_list.c

Sat, 04 Dec 2021 17:38:23 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 04 Dec 2021 17:38:23 +0100
changeset 475
31bf97fdbf71
parent 474
9c1fccda16bc
child 476
60ff4561dc04
permissions
-rw-r--r--

add cx_linked_list_first() + cx_linked_list_prepend()

removes concatenating behavior of cx_linked_list_add()

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

mercurial