Tue, 28 Sep 2021 18:05:52 +0200
remove error handling from tests
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #include "cx/linked_list.h" |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
30 | #include <stdint.h> |
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
31 | #include <string.h> |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | /* LOW LEVEL LINKED LIST FUNCTIONS */ |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off)) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | |
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
37 | void *cx_linked_list_at(void *start, size_t start_index, ptrdiff_t loc_advance, size_t index) { |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
38 | size_t i = start_index; |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
39 | void* cur = start; |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
40 | while (i != index && cur != NULL) { |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
41 | cur = *CX_LL_PTR(cur, loc_advance); |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
42 | i < index ? i++ : i--; |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
43 | } |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
44 | return cur; |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
45 | } |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
46 | |
400
8cd274352419
changes off_t to ptrdiff_t
Mike Becker <universe@uap-core.de>
parents:
399
diff
changeset
|
47 | void *cx_linked_list_last(void **begin, void **end, ptrdiff_t loc_next) { |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | if (end != NULL) { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | return *end; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | } else { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | if (begin == NULL || *begin == NULL) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | return NULL; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
54 | void *cur = *begin; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | void *last; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | do { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | last = cur; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | } while ((cur = *CX_LL_PTR(cur, loc_next)) != NULL); |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | return last; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
64 | int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | void *last = cx_linked_list_last(begin, end, loc_next); |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | if (last == NULL) { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | if (begin == NULL) { |
428
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
68 | // no current list and no begin ptr to write to - we don't find something to append to |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | return 1; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | } else { |
428
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
71 | // start fresh list |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
72 | *begin = new_node; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | } |
428
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
74 | } else { |
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
75 | // if there is a last node, update its next pointer |
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
76 | void **next = CX_LL_PTR(last, loc_next); |
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
77 | *next = new_node; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | |
428
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
80 | // if there is an end pointer, update it |
408
dfdd571550f8
fixes cx_linked_list_add not recalculating end
Mike Becker <universe@uap-core.de>
parents:
407
diff
changeset
|
81 | if (end != NULL) { |
dfdd571550f8
fixes cx_linked_list_add not recalculating end
Mike Becker <universe@uap-core.de>
parents:
407
diff
changeset
|
82 | *end = cx_linked_list_last(&new_node, NULL, loc_next); |
dfdd571550f8
fixes cx_linked_list_add not recalculating end
Mike Becker <universe@uap-core.de>
parents:
407
diff
changeset
|
83 | } |
428
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
84 | |
da66264af8ad
fix special cases for linked_list_add
Mike Becker <universe@uap-core.de>
parents:
423
diff
changeset
|
85 | // if the nodes use a prev pointer, update it |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | if (loc_prev >= 0) { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
87 | void **prev = CX_LL_PTR(new_node, loc_prev); |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | *prev = last; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | return 0; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | |
402
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
96 | struct cx_linked_list_node { |
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
97 | void *prev; |
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
98 | void *next; |
403
8fa43b732980
use C99 flexible array to mark the node's payload
Mike Becker <universe@uap-core.de>
parents:
402
diff
changeset
|
99 | char payload[]; |
402
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
100 | }; |
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
101 | |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
102 | #define CX_LL_LOC_PREV offsetof(struct cx_linked_list_node, prev) |
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
103 | #define CX_LL_LOC_NEXT offsetof(struct cx_linked_list_node, next) |
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
104 | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | typedef struct { |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
106 | cx_list_s base; |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
107 | void *begin; |
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
108 | void *end; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
109 | ptrdiff_t loc_prev; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
110 | ptrdiff_t loc_next; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | } cx_linked_list; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | |
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
113 | static int cx_ll_add(cx_list_s *list, void *elem) { |
437
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
114 | cx_linked_list *ll = (cx_linked_list *) list; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
116 | struct cx_linked_list_node *node = cxMalloc(list->allocator, |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
117 | sizeof(struct cx_linked_list_node) + list->itemsize); |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | if (node == NULL) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | return 1; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
121 | node->next = node->prev = NULL; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
122 | memcpy(node->payload, elem, list->itemsize); |
399
8902fcd1e057
adds missing assignments for new list node
Mike Becker <universe@uap-core.de>
parents:
398
diff
changeset
|
123 | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | int ret = cx_linked_list_add( |
437
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
125 | &ll->begin, &ll->end, |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
126 | CX_LL_LOC_PREV, CX_LL_LOC_NEXT, |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | node |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | ); |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | if (ret == 0) { |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
130 | list->size++; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | return 0; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | } else { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
133 | return ret; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
134 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
135 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | |
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
137 | static int cx_ll_insert(cx_list_s *list, size_t index, void *elem) { |
437
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
138 | cx_linked_list *ll = (cx_linked_list *) list; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
139 | // TODO: implement using low level API |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
140 | return 1; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
142 | |
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
143 | static int cx_ll_remove(cx_list_s *list, size_t index) { |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
144 | if (index >= list->size) { |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
145 | return 1; |
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
146 | } |
437
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
147 | cx_linked_list *ll = (cx_linked_list *) list; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
148 | // TODO: implement using low level API |
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
149 | return 0; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
150 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
152 | static void *cx_ll_at(cx_list_s *list, size_t index) { |
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
153 | cx_linked_list *ll = (cx_linked_list *) list; |
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
154 | struct cx_linked_list_node *node; |
440
003aa0a78e1e
fix mixed up cases in cx_ll_at()
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
155 | if (index >= list->size) { |
003aa0a78e1e
fix mixed up cases in cx_ll_at()
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
156 | node = NULL; |
003aa0a78e1e
fix mixed up cases in cx_ll_at()
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
157 | } else if (index > list->size / 2) { |
003aa0a78e1e
fix mixed up cases in cx_ll_at()
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
158 | node = cx_linked_list_at(ll->end, list->size, CX_LL_LOC_PREV, index); |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
159 | } else { |
440
003aa0a78e1e
fix mixed up cases in cx_ll_at()
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
160 | node = cx_linked_list_at(ll->begin, 0, CX_LL_LOC_NEXT, index); |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
161 | } |
440
003aa0a78e1e
fix mixed up cases in cx_ll_at()
Mike Becker <universe@uap-core.de>
parents:
439
diff
changeset
|
162 | return node == NULL ? NULL : &node->payload; |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
163 | } |
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
164 | |
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
165 | static size_t cx_ll_find(cx_list_s *list, void *elem) { |
437
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
166 | CxListComparator cmp = list->cmpfunc; |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
167 | cx_linked_list *ll = (cx_linked_list *) list; |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
168 | |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
169 | size_t index; |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
170 | struct cx_linked_list_node* node = ll->begin; |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
171 | for (index = 0 ; index < list->size ; index++) { |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
172 | void* current = node->payload; |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
173 | if (cmp(current, elem) == 0) { |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
174 | return index; |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
175 | } |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
176 | node = node->next; |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
177 | } |
9d4971ea0625
implement linked list find
Mike Becker <universe@uap-core.de>
parents:
436
diff
changeset
|
178 | return index; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
179 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | |
438
cd3069757010
add function cx_linked_list_at()
Mike Becker <universe@uap-core.de>
parents:
437
diff
changeset
|
181 | static void *cx_ll_last(cx_list_s *list) { |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
182 | cx_linked_list *ll = (cx_linked_list *) list; |
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
183 | struct cx_linked_list_node *last = cx_linked_list_last(NULL, &ll->end, CX_LL_LOC_NEXT); |
404 | 184 | return &last->payload; |
185 | } | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | cx_list_class cx_linked_list_class = { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | cx_ll_add, |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | cx_ll_insert, |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
190 | cx_ll_remove, |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
191 | cx_ll_at, |
404 | 192 | cx_ll_find, |
193 | cx_ll_last | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
194 | }; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
196 | CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
197 | cx_linked_list* list = cxMalloc(allocator, sizeof(cx_linked_list)); |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
198 | if (list == NULL) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | return NULL; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
200 | |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
201 | list->base.cl = &cx_linked_list_class; |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
202 | list->base.allocator = allocator; |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
203 | list->base.cmpfunc = comparator; |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
204 | list->base.itemsize = item_size; |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
205 | list->base.capacity = SIZE_MAX; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
206 | |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
207 | list->begin = NULL; |
439
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
208 | list->loc_prev = CX_LL_LOC_PREV; |
9a5adedd6de6
add high-level function cxListAt()
Mike Becker <universe@uap-core.de>
parents:
438
diff
changeset
|
209 | list->loc_next = CX_LL_LOC_NEXT; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
210 | |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
211 | CxList result = (CxList) list; |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
212 | cxLinkedListRecalculateSize(result); |
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
213 | return result; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
214 | } |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
215 | |
409
5d167af0eadb
adds cxLinkedListDestroy prototype
Mike Becker <universe@uap-core.de>
parents:
408
diff
changeset
|
216 | void cxLinkedListDestroy(CxList list) { |
436
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
217 | cx_linked_list* ll = (cx_linked_list*) list; |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
218 | |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
219 | struct cx_linked_list_node* node = ll->begin; |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
220 | while (node) { |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
221 | void* next = node->next; |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
222 | cxFree(list->allocator, node); |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
223 | node = next; |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
224 | } |
ca9ce2297c29
add node destruction in cxLinkedListDestroy()
Mike Becker <universe@uap-core.de>
parents:
435
diff
changeset
|
225 | |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
226 | cxFree(list->allocator, list); |
409
5d167af0eadb
adds cxLinkedListDestroy prototype
Mike Becker <universe@uap-core.de>
parents:
408
diff
changeset
|
227 | } |
5d167af0eadb
adds cxLinkedListDestroy prototype
Mike Becker <universe@uap-core.de>
parents:
408
diff
changeset
|
228 | |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
229 | size_t cxLinkedListRecalculateSize(CxList list) { |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
230 | cx_linked_list *ll = (cx_linked_list *) list; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
231 | |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
232 | if (ll->begin == NULL) { |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
233 | ll->base.size = 0; |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
234 | ll->end = NULL; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
235 | return 0; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
236 | } else { |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
237 | void *cur = ll->begin; |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
238 | void *last; |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
239 | size_t size = 0; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
240 | do { |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
241 | last = cur; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
242 | size++; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
243 | } while ((cur = *CX_LL_PTR(cur, ll->loc_next)) != NULL); |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
244 | ll->end = last; |
435
0fe204d50f54
change inheritance model for lists
Mike Becker <universe@uap-core.de>
parents:
428
diff
changeset
|
245 | ll->base.size = size; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
246 | return size; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
247 | } |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
248 | } |