src/ucx/linked_list.c

changeset 415
d938228c382e
child 438
22eca559aded
equal deleted inserted replaced
414:99a34860c105 415:d938228c382e
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "cx/linked_list.h"
30 #include "cx/utils.h"
31 #include <stdint.h>
32 #include <string.h>
33 #include <assert.h>
34
35 /* LOW LEVEL LINKED LIST FUNCTIONS */
36
37 #define CX_LL_PTR(cur, off) (*(void**)(((char*)(cur))+(off)))
38 #define ll_prev(node) CX_LL_PTR(node, loc_prev)
39 #define ll_next(node) CX_LL_PTR(node, loc_next)
40 #define ll_advance(node) CX_LL_PTR(node, loc_advance)
41 #define ll_data_f(node, follow_ptr) ((follow_ptr)?CX_LL_PTR(node, loc_data):(((char*)(node))+loc_data))
42 #define ll_data(node) ll_data_f(node,follow_ptr)
43
44 void *cx_linked_list_at(
45 void const *start,
46 size_t start_index,
47 ptrdiff_t loc_advance,
48 size_t index
49 ) {
50 assert(start != NULL);
51 assert(loc_advance >= 0);
52 size_t i = start_index;
53 void const *cur = start;
54 while (i != index && cur != NULL) {
55 cur = ll_advance(cur);
56 i < index ? i++ : i--;
57 }
58 return (void *) cur;
59 }
60
61 size_t cx_linked_list_find(
62 void const *start,
63 ptrdiff_t loc_advance,
64 ptrdiff_t loc_data,
65 bool follow_ptr,
66 CxListComparator cmp_func,
67 void const *elem
68 ) {
69 assert(start != NULL);
70 assert(loc_advance >= 0);
71 assert(loc_data >= 0);
72 assert(cmp_func);
73
74 void const *node = start;
75 size_t index = 0;
76 do {
77 void *current = ll_data(node);
78 if (cmp_func(current, elem) == 0) {
79 return index;
80 }
81 node = ll_advance(node);
82 index++;
83 } while (node != NULL);
84 return index;
85 }
86
87 void *cx_linked_list_first(
88 void const *node,
89 ptrdiff_t loc_prev
90 ) {
91 return cx_linked_list_last(node, loc_prev);
92 }
93
94 void *cx_linked_list_last(
95 void const *node,
96 ptrdiff_t loc_next
97 ) {
98 assert(node != NULL);
99 assert(loc_next >= 0);
100
101 void const *cur = node;
102 void const *last;
103 do {
104 last = cur;
105 } while ((cur = ll_next(cur)) != NULL);
106
107 return (void *) last;
108 }
109
110 void *cx_linked_list_prev(
111 void const *begin,
112 ptrdiff_t loc_next,
113 void const *node
114 ) {
115 assert(begin != NULL);
116 assert(node != NULL);
117 assert(loc_next >= 0);
118 if (begin == node) return NULL;
119 void const *cur = begin;
120 void const *next;
121 while (1) {
122 next = ll_next(cur);
123 if (next == node) return (void *) cur;
124 cur = next;
125 }
126 }
127
128 void cx_linked_list_link(
129 void *left,
130 void *right,
131 ptrdiff_t loc_prev,
132 ptrdiff_t loc_next
133 ) {
134 assert(loc_next >= 0);
135 ll_next(left) = right;
136 if (loc_prev >= 0) {
137 ll_prev(right) = left;
138 }
139 }
140
141 void cx_linked_list_unlink(
142 void *left,
143 void *right,
144 ptrdiff_t loc_prev,
145 ptrdiff_t loc_next
146 ) {
147 assert (loc_next >= 0);
148 assert(ll_next(left) == right);
149 ll_next(left) = NULL;
150 if (loc_prev >= 0) {
151 assert(ll_prev(right) == left);
152 ll_prev(right) = NULL;
153 }
154 }
155
156 void cx_linked_list_add(
157 void **begin,
158 void **end,
159 ptrdiff_t loc_prev,
160 ptrdiff_t loc_next,
161 void *new_node
162 ) {
163 void *last;
164 if (end == NULL) {
165 assert(begin != NULL);
166 last = *begin == NULL ? NULL : cx_linked_list_last(*begin, loc_next);
167 } else {
168 last = *end;
169 }
170 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, last, new_node, new_node);
171 }
172
173 void cx_linked_list_prepend(
174 void **begin,
175 void **end,
176 ptrdiff_t loc_prev,
177 ptrdiff_t loc_next,
178 void *new_node
179 ) {
180 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, NULL, new_node, new_node);
181 }
182
183 void cx_linked_list_insert(
184 void **begin,
185 void **end,
186 ptrdiff_t loc_prev,
187 ptrdiff_t loc_next,
188 void *node,
189 void *new_node
190 ) {
191 cx_linked_list_insert_chain(begin, end, loc_prev, loc_next, node, new_node, new_node);
192 }
193
194 void cx_linked_list_insert_chain(
195 void **begin,
196 void **end,
197 ptrdiff_t loc_prev,
198 ptrdiff_t loc_next,
199 void *node,
200 void *insert_begin,
201 void *insert_end
202 ) {
203 // find the end of the chain, if not specified
204 if (insert_end == NULL) {
205 insert_end = cx_linked_list_last(insert_begin, loc_next);
206 }
207
208 // determine the successor
209 void *successor;
210 if (node == NULL) {
211 assert(begin != NULL || (end != NULL && loc_prev >= 0));
212 if (begin != NULL) {
213 successor = *begin;
214 *begin = insert_begin;
215 } else {
216 successor = *end == NULL ? NULL : cx_linked_list_first(*end, loc_prev);
217 }
218 } else {
219 successor = ll_next(node);
220 cx_linked_list_link(node, insert_begin, loc_prev, loc_next);
221 }
222
223 if (successor == NULL) {
224 // the list ends with the new chain
225 if (end != NULL) {
226 *end = insert_end;
227 }
228 } else {
229 cx_linked_list_link(insert_end, successor, loc_prev, loc_next);
230 }
231 }
232
233 void cx_linked_list_remove(
234 void **begin,
235 void **end,
236 ptrdiff_t loc_prev,
237 ptrdiff_t loc_next,
238 void *node
239 ) {
240 assert(node != NULL);
241 assert(loc_next >= 0);
242 assert(loc_prev >= 0 || begin != NULL);
243
244 // find adjacent nodes
245 void *next = ll_next(node);
246 void *prev;
247 if (loc_prev >= 0) {
248 prev = ll_prev(node);
249 } else {
250 prev = cx_linked_list_prev(*begin, loc_next, node);
251 }
252
253 // update next pointer of prev node, or set begin
254 if (prev == NULL) {
255 if (begin != NULL) {
256 *begin = next;
257 }
258 } else {
259 ll_next(prev) = next;
260 }
261
262 // update prev pointer of next node, or set end
263 if (next == NULL) {
264 if (end != NULL) {
265 *end = prev;
266 }
267 } else if (loc_prev >= 0) {
268 ll_prev(next) = prev;
269 }
270 }
271
272 size_t cx_linked_list_size(
273 void const *node,
274 ptrdiff_t loc_next
275 ) {
276 assert(loc_next >= 0);
277 size_t size = 0;
278 while (node != NULL) {
279 node = ll_next(node);
280 size++;
281 }
282 return size;
283 }
284
285 static void *cx_linked_list_sort_merge(
286 ptrdiff_t loc_prev,
287 ptrdiff_t loc_next,
288 ptrdiff_t loc_data,
289 bool follow_ptr,
290 size_t length,
291 void *ls,
292 void *le,
293 void *re,
294 CxListComparator cmp_func
295 ) {
296 const size_t sbo_len = 1024;
297 void *sbo[sbo_len];
298 void **sorted = (length >= sbo_len) ? malloc(sizeof(void *) * length) : sbo;
299 if (sorted == NULL) abort();
300 void *rc, *lc;
301
302 lc = ls;
303 rc = le;
304 size_t n = 0;
305 while (lc && lc != le && rc != re) {
306 if (cmp_func(ll_data(lc), ll_data(rc)) <= 0) {
307 sorted[n] = lc;
308 lc = ll_next(lc);
309 } else {
310 sorted[n] = rc;
311 rc = ll_next(rc);
312 }
313 n++;
314 }
315 while (lc && lc != le) {
316 sorted[n] = lc;
317 lc = ll_next(lc);
318 n++;
319 }
320 while (rc && rc != re) {
321 sorted[n] = rc;
322 rc = ll_next(rc);
323 n++;
324 }
325
326 // Update pointer
327 if (loc_prev >= 0) ll_prev(sorted[0]) = NULL;
328 cx_for_n (i, length - 1) {
329 cx_linked_list_link(sorted[i], sorted[i + 1], loc_prev, loc_next);
330 }
331 ll_next(sorted[length - 1]) = NULL;
332
333 void *ret = sorted[0];
334 if (sorted != sbo) {
335 free(sorted);
336 }
337 return ret;
338 }
339
340 void cx_linked_list_sort( /* NOLINT(misc-no-recursion) - purposely recursive function */
341 void **begin,
342 void **end,
343 ptrdiff_t loc_prev,
344 ptrdiff_t loc_next,
345 ptrdiff_t loc_data,
346 bool follow_ptr,
347 CxListComparator cmp_func
348 ) {
349 assert(begin != NULL);
350 assert(loc_next >= 0);
351 assert(loc_data >= 0);
352 assert(cmp_func);
353
354 void *lc, *ls, *le, *re;
355
356 // set start node
357 ls = *begin;
358
359 // check how many elements are already sorted
360 lc = ls;
361 size_t ln = 1;
362 while (ll_next(lc) != NULL && cmp_func(ll_data(ll_next(lc)), ll_data(lc)) > 0) {
363 lc = ll_next(lc);
364 ln++;
365 }
366 le = ll_next(lc);
367
368 // if first unsorted node is NULL, the list is already completely sorted
369 if (le != NULL) {
370 void *rc;
371 size_t rn = 1;
372 rc = le;
373 // skip already sorted elements
374 while (ll_next(rc) != NULL && cmp_func(ll_data(ll_next(rc)), ll_data(rc)) > 0) {
375 rc = ll_next(rc);
376 rn++;
377 }
378 re = ll_next(rc);
379
380 // {ls,...,le->prev} and {rs,...,re->prev} are sorted - merge them
381 void *sorted = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
382 ln + rn, ls, le, re, cmp_func);
383
384 // Something left? Sort it!
385 size_t remainder_length = cx_linked_list_size(re, loc_next);
386 if (remainder_length > 0) {
387 void *remainder = re;
388 cx_linked_list_sort(&remainder, NULL, loc_prev, loc_next, loc_data, follow_ptr, cmp_func);
389
390 // merge sorted list with (also sorted) remainder
391 *begin = cx_linked_list_sort_merge(loc_prev, loc_next, loc_data, follow_ptr,
392 ln + rn + remainder_length,
393 sorted, remainder, NULL, cmp_func);
394 } else {
395 // no remainder - we've got our sorted list
396 *begin = sorted;
397 }
398 if (end) *end = cx_linked_list_last(sorted, loc_next);
399 }
400 }
401
402 int cx_linked_list_compare(
403 void const *begin_left,
404 void const *begin_right,
405 ptrdiff_t loc_advance,
406 ptrdiff_t loc_data,
407 bool follow_ptr_left,
408 bool follow_ptr_right,
409 CxListComparator cmp_func
410 ) {
411 void const *left = begin_left, *right = begin_right;
412
413 while (left != NULL && right != NULL) {
414 void const *left_data = ll_data_f(left, follow_ptr_left);
415 void const *right_data = ll_data_f(right, follow_ptr_right);
416 int result = cmp_func(left_data, right_data);
417 if (result != 0) return result;
418 left = ll_advance(left);
419 right = ll_advance(right);
420 }
421
422 if (left != NULL) { return 1; }
423 else if (right != NULL) { return -1; }
424 else { return 0; }
425 }
426
427 void cx_linked_list_reverse(
428 void **begin,
429 void **end,
430 ptrdiff_t loc_prev,
431 ptrdiff_t loc_next
432 ) {
433 assert(begin != NULL);
434 assert(loc_next >= 0);
435
436 // swap all links
437 void *prev = NULL;
438 void *cur = *begin;
439 while (cur != NULL) {
440 void *next = ll_next(cur);
441
442 ll_next(cur) = prev;
443 if (loc_prev >= 0) {
444 ll_prev(cur) = next;
445 }
446
447 prev = cur;
448 cur = next;
449 }
450
451 // update begin and end
452 if (end != NULL) {
453 *end = *begin;
454 }
455 *begin = prev;
456 }
457
458 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
459
460 typedef struct cx_linked_list_node cx_linked_list_node;
461 struct cx_linked_list_node {
462 cx_linked_list_node *prev;
463 cx_linked_list_node *next;
464 char payload[];
465 };
466
467 #define CX_LL_LOC_PREV offsetof(cx_linked_list_node, prev)
468 #define CX_LL_LOC_NEXT offsetof(cx_linked_list_node, next)
469 #define CX_LL_LOC_DATA offsetof(cx_linked_list_node, payload)
470
471 typedef struct {
472 struct cx_list_s base;
473 cx_linked_list_node *begin;
474 cx_linked_list_node *end;
475 bool follow_ptr;
476 } cx_linked_list;
477
478 static cx_linked_list_node *cx_ll_node_at(
479 cx_linked_list const *list,
480 size_t index
481 ) {
482 if (index >= list->base.size) {
483 return NULL;
484 } else if (index > list->base.size / 2) {
485 return cx_linked_list_at(list->end, list->base.size - 1, CX_LL_LOC_PREV, index);
486 } else {
487 return cx_linked_list_at(list->begin, 0, CX_LL_LOC_NEXT, index);
488 }
489 }
490
491 static int cx_ll_insert_at(
492 struct cx_list_s *list,
493 cx_linked_list_node *node,
494 void const *elem
495 ) {
496
497 // create the new new_node
498 cx_linked_list_node *new_node = cxMalloc(list->allocator,
499 sizeof(cx_linked_list_node) + list->itemsize);
500
501 // sortir if failed
502 if (new_node == NULL) return 1;
503
504 // initialize new new_node
505 new_node->prev = new_node->next = NULL;
506 memcpy(new_node->payload, elem, list->itemsize);
507
508 // insert
509 cx_linked_list *ll = (cx_linked_list *) list;
510 cx_linked_list_insert_chain(
511 (void **) &ll->begin, (void **) &ll->end,
512 CX_LL_LOC_PREV, CX_LL_LOC_NEXT,
513 node, new_node, new_node
514 );
515
516 // increase the size and return
517 list->size++;
518 return 0;
519 }
520
521 static int cx_ll_insert(
522 struct cx_list_s *list,
523 size_t index,
524 void const *elem
525 ) {
526 // out-of bounds check
527 if (index > list->size) return 1;
528
529 // find position efficiently
530 cx_linked_list_node *node = index == 0 ? NULL : cx_ll_node_at((cx_linked_list *) list, index - 1);
531
532 // perform insert
533 return cx_ll_insert_at(list, node, elem);
534 }
535
536 static int cx_ll_add(
537 struct cx_list_s *list,
538 void const *elem
539 ) {
540 return cx_ll_insert(list, list->size, elem);
541 }
542
543 static int cx_pll_insert(
544 struct cx_list_s *list,
545 size_t index,
546 void const *elem
547 ) {
548 return cx_ll_insert(list, index, &elem);
549 }
550
551 static int cx_pll_add(
552 struct cx_list_s *list,
553 void const *elem
554 ) {
555 return cx_ll_insert(list, list->size, &elem);
556 }
557
558 static int cx_ll_remove(
559 struct cx_list_s *list,
560 size_t index
561 ) {
562 cx_linked_list *ll = (cx_linked_list *) list;
563 cx_linked_list_node *node = cx_ll_node_at(ll, index);
564
565 // out-of-bounds check
566 if (node == NULL) return 1;
567
568 // remove
569 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
570 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
571
572 // adjust size
573 list->size--;
574
575 // free and return
576 cxFree(list->allocator, node);
577
578 return 0;
579 }
580
581 static void *cx_ll_at(
582 struct cx_list_s const *list,
583 size_t index
584 ) {
585 cx_linked_list *ll = (cx_linked_list *) list;
586 cx_linked_list_node *node = cx_ll_node_at(ll, index);
587 return node == NULL ? NULL : node->payload;
588 }
589
590 static void *cx_pll_at(
591 struct cx_list_s const *list,
592 size_t index
593 ) {
594 cx_linked_list *ll = (cx_linked_list *) list;
595 cx_linked_list_node *node = cx_ll_node_at(ll, index);
596 return node == NULL ? NULL : *(void **) node->payload;
597 }
598
599 static size_t cx_ll_find(
600 struct cx_list_s const *list,
601 void const *elem
602 ) {
603 cx_linked_list *ll = (cx_linked_list *) list;
604 return cx_linked_list_find(((cx_linked_list *) list)->begin,
605 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
606 ll->follow_ptr, list->cmpfunc, elem);
607 }
608
609 static void cx_ll_sort(struct cx_list_s *list) {
610 cx_linked_list *ll = (cx_linked_list *) list;
611 cx_linked_list_sort((void **) &ll->begin, (void **) &ll->end,
612 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
613 ll->follow_ptr, list->cmpfunc);
614 }
615
616 static void cx_ll_reverse(struct cx_list_s *list) {
617 cx_linked_list *ll = (cx_linked_list *) list;
618 cx_linked_list_reverse((void **) &ll->begin, (void **) &ll->end, CX_LL_LOC_PREV, CX_LL_LOC_NEXT);
619 }
620
621 static int cx_ll_compare(
622 struct cx_list_s const *list,
623 struct cx_list_s const *other
624 ) {
625 cx_linked_list *left = (cx_linked_list *) list;
626 cx_linked_list *right = (cx_linked_list *) other;
627 return cx_linked_list_compare(left->begin, right->begin,
628 CX_LL_LOC_NEXT, CX_LL_LOC_DATA,
629 left->follow_ptr, right->follow_ptr, list->cmpfunc);
630 }
631
632 static bool cx_ll_iter_valid(CxIterator const *iter) {
633 return iter->elem_handle != NULL;
634 }
635
636 static void cx_ll_iter_next(CxIterator *iter) {
637 if (iter->remove) {
638 iter->remove = false;
639 cx_linked_list *ll = iter->src_handle;
640 cx_linked_list_node *node = iter->elem_handle;
641 iter->elem_handle = node->next;
642 cx_linked_list_remove((void **) &ll->begin, (void **) &ll->end,
643 CX_LL_LOC_PREV, CX_LL_LOC_NEXT, node);
644 ll->base.size--;
645 cxFree(ll->base.allocator, node);
646 } else {
647 iter->index++;
648 cx_linked_list_node *node = iter->elem_handle;
649 iter->elem_handle = node->next;
650 }
651 }
652
653 static void *cx_ll_iter_current(CxIterator const *iter) {
654 cx_linked_list_node *node = iter->elem_handle;
655 return node->payload;
656 }
657
658 static void *cx_pll_iter_current(CxIterator const *iter) {
659 cx_linked_list_node *node = iter->elem_handle;
660 return *(void **) node->payload;
661 }
662
663 static CxIterator cx_ll_iterator(
664 struct cx_list_s *list,
665 size_t index
666 ) {
667 CxIterator iter;
668 iter.index = index;
669 iter.src_handle = list;
670 iter.elem_handle = cx_ll_node_at((cx_linked_list const *) list, index);
671 iter.valid = cx_ll_iter_valid;
672 iter.current = cx_ll_iter_current;
673 iter.next = cx_ll_iter_next;
674 iter.remove = false;
675 return iter;
676 }
677
678 static CxIterator cx_pll_iterator(
679 struct cx_list_s *list,
680 size_t index
681 ) {
682 CxIterator iter = cx_ll_iterator(list, index);
683 iter.current = cx_pll_iter_current;
684 return iter;
685 }
686
687 static int cx_ll_insert_iter(
688 CxIterator *iter,
689 void const *elem,
690 int prepend
691 ) {
692 struct cx_list_s *list = iter->src_handle;
693 cx_linked_list_node *node = iter->elem_handle;
694 if (node != NULL) {
695 assert(prepend >= 0 && prepend <= 1);
696 cx_linked_list_node *choice[2] = {node, node->prev};
697 int result = cx_ll_insert_at(list, choice[prepend], elem);
698 iter->index += prepend * (0 == result);
699 return result;
700 } else {
701 int result = cx_ll_insert(list, list->size, elem);
702 iter->index = list->size;
703 return result;
704 }
705 }
706
707 static int cx_pll_insert_iter(
708 CxIterator *iter,
709 void const *elem,
710 int prepend
711 ) {
712 return cx_ll_insert_iter(iter, &elem, prepend);
713 }
714
715 static void cx_ll_destructor(CxList *list) {
716 cx_linked_list *ll = (cx_linked_list *) list;
717
718 cx_linked_list_node *node = ll->begin;
719 while (node) {
720 void *next = node->next;
721 cxFree(list->allocator, node);
722 node = next;
723 }
724 // do not free the list pointer, this is just a destructor!
725 }
726
727 static cx_list_class cx_linked_list_class = {
728 cx_ll_destructor,
729 cx_ll_add,
730 cx_ll_insert,
731 cx_ll_insert_iter,
732 cx_ll_remove,
733 cx_ll_at,
734 cx_ll_find,
735 cx_ll_sort,
736 cx_ll_compare,
737 cx_ll_reverse,
738 cx_ll_iterator
739 };
740
741 static cx_list_class cx_pointer_linked_list_class = {
742 cx_ll_destructor,
743 cx_pll_add,
744 cx_pll_insert,
745 cx_pll_insert_iter,
746 cx_ll_remove,
747 cx_pll_at,
748 cx_ll_find,
749 cx_ll_sort,
750 cx_ll_compare,
751 cx_ll_reverse,
752 cx_pll_iterator,
753 };
754
755 CxList *cxLinkedListCreate(
756 CxAllocator const *allocator,
757 CxListComparator comparator,
758 size_t item_size
759 ) {
760 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
761 if (list == NULL) return NULL;
762
763 list->follow_ptr = false;
764 list->base.cl = &cx_linked_list_class;
765 list->base.allocator = allocator;
766 list->base.cmpfunc = comparator;
767 list->base.itemsize = item_size;
768 list->base.capacity = SIZE_MAX;
769
770 return (CxList *) list;
771 }
772
773 CxList *cxPointerLinkedListCreate(
774 CxAllocator const *allocator,
775 CxListComparator comparator
776 ) {
777 cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
778 if (list == NULL) return NULL;
779
780 list->follow_ptr = true;
781 list->base.cl = &cx_pointer_linked_list_class;
782 list->base.allocator = allocator;
783 list->base.cmpfunc = comparator;
784 list->base.itemsize = sizeof(void *);
785 list->base.capacity = SIZE_MAX;
786
787 return (CxList *) list;
788 }
789
790 CxList *cxLinkedListFromArray(
791 CxAllocator const *allocator,
792 CxListComparator comparator,
793 size_t item_size,
794 size_t num_items,
795 void const *array
796 ) {
797 CxList *list = cxLinkedListCreate(allocator, comparator, item_size);
798 if (list == NULL) return NULL;
799 cx_for_n (i, num_items) {
800 if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
801 cx_ll_destructor(list);
802 cxFree(allocator, list);
803 return NULL;
804 }
805 }
806 return list;
807 }

mercurial