Sun, 14 Feb 2021 11:30:47 +0100
add test config and let the test binary return nonzero on test failures
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 | |
400
8cd274352419
changes off_t to ptrdiff_t
Mike Becker <universe@uap-core.de>
parents:
399
diff
changeset
|
37 | 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
|
38 | if (end != NULL) { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | return *end; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | } else { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | if (begin == NULL || *begin == NULL) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | return NULL; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | void *cur = *begin; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | void *last; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | do { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | last = cur; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | } 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
|
49 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | return last; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
54 | 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
|
55 | // TODO: how do we report error messages? |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | if (loc_next < 0 || (begin == NULL && end == NULL)) { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
57 | return 1; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | } |
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 | 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
|
61 | if (last == NULL) { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | if (begin == NULL) { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | return 1; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
64 | } else { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
65 | *begin = new_node; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | return 0; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | void **next = CX_LL_PTR(last, loc_next); |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
71 | *next = new_node; |
408
dfdd571550f8
fixes cx_linked_list_add not recalculating end
Mike Becker <universe@uap-core.de>
parents:
407
diff
changeset
|
72 | if (end != NULL) { |
dfdd571550f8
fixes cx_linked_list_add not recalculating end
Mike Becker <universe@uap-core.de>
parents:
407
diff
changeset
|
73 | *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
|
74 | } |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | if (loc_prev >= 0) { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
76 | 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
|
77 | *prev = last; |
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 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | return 0; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | |
402
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
85 | struct cx_linked_list_node { |
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
86 | void *prev; |
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
87 | void *next; |
403
8fa43b732980
use C99 flexible array to mark the node's payload
Mike Becker <universe@uap-core.de>
parents:
402
diff
changeset
|
88 | char payload[]; |
402
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
89 | }; |
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
90 | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | typedef struct { |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
92 | void *begin; |
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
93 | void *end; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
94 | ptrdiff_t loc_prev; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
95 | ptrdiff_t loc_next; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | } cx_linked_list; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | int cx_ll_add(cx_list *list, void *elem) { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
99 | cx_linked_list *listdata = (cx_linked_list *) list->listdata; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | CxAllocator allocator = list->allocator; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | |
402
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
102 | struct cx_linked_list_node *node = cxMalloc(allocator, |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
103 | 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
|
104 | if (node == NULL) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | return 1; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | |
402
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
107 | node->next = NULL; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
108 | 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
|
109 | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | int ret = cx_linked_list_add( |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
111 | &listdata->begin, |
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
112 | &listdata->end, |
402
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
113 | offsetof(struct cx_linked_list_node, prev), |
a34b93911956
use named fields to access node memory
Mike Becker <universe@uap-core.de>
parents:
401
diff
changeset
|
114 | offsetof(struct cx_linked_list_node, next), |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | node |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | ); |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | 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
|
118 | list->size++; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | return 0; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | } else { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | return ret; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | int cx_ll_insert(cx_list *list, size_t index, void *elem) { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
126 | cx_linked_list *listdata = (cx_linked_list *) list->listdata; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | // TODO: implement using low level API |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | return 1; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | } |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
130 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | void *cx_ll_remove(cx_list *list, size_t index) { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
132 | cx_linked_list *listdata = (cx_linked_list *) list->listdata; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
133 | // TODO: implement using low level API |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
134 | return NULL; |
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 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | size_t cx_ll_find(cx_list *list, void *elem) { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
138 | cx_linked_list *listdata = (cx_linked_list *) list->listdata; |
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 0; |
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 | |
404 | 143 | void *cx_ll_last(cx_list *list) { |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
144 | cx_linked_list *listdata = (cx_linked_list *) list->listdata; |
404 | 145 | struct cx_linked_list_node *last = cx_linked_list_last( |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
146 | NULL, &listdata->end, offsetof(struct cx_linked_list_node, next)); |
404 | 147 | return &last->payload; |
148 | } | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
149 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
150 | cx_list_class cx_linked_list_class = { |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | cx_ll_add, |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
152 | cx_ll_insert, |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
153 | cx_ll_remove, |
404 | 154 | cx_ll_find, |
155 | cx_ll_last | |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
156 | }; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
158 | CxList cxLinkedListCreate(CxAllocator allocator, CxListComparator comparator, size_t item_size) { |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
159 | CxLinkedListDesc desc; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
160 | desc.item_size = item_size; |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
161 | desc.begin = NULL; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
162 | desc.loc_prev = offsetof(struct cx_linked_list_node, prev); |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
163 | desc.loc_next = offsetof(struct cx_linked_list_node, next); |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
164 | |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
165 | return cxLinkedListWrap(allocator, comparator, desc); |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
166 | } |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
167 | |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
168 | CxList cxLinkedListWrap(CxAllocator allocator, CxListComparator comparator, CxLinkedListDesc desc) { |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
169 | CxList list = cxMalloc(allocator, sizeof(list) + sizeof(cx_linked_list)); |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
170 | if (list == NULL) |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
171 | return NULL; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
172 | |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | list->cl = &cx_linked_list_class; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | list->data.allocator = allocator; |
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
175 | list->data.cmpfunc = comparator; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
176 | list->data.itemsize = desc.item_size; |
401
e6a8f7fb0c45
copy list items when they are added to the list
Mike Becker <universe@uap-core.de>
parents:
400
diff
changeset
|
177 | list->data.capacity = SIZE_MAX; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
178 | |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
179 | cx_linked_list *ll = (cx_linked_list *) list->data.listdata; |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
180 | ll->begin = desc.begin; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
181 | ll->loc_prev = desc.loc_prev; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
182 | ll->loc_next = desc.loc_next; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
183 | cxLinkedListRecalculateSize(list); |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
184 | |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
185 | return list; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
186 | } |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
187 | |
409
5d167af0eadb
adds cxLinkedListDestroy prototype
Mike Becker <universe@uap-core.de>
parents:
408
diff
changeset
|
188 | void cxLinkedListDestroy(CxList list) { |
5d167af0eadb
adds cxLinkedListDestroy prototype
Mike Becker <universe@uap-core.de>
parents:
408
diff
changeset
|
189 | |
5d167af0eadb
adds cxLinkedListDestroy prototype
Mike Becker <universe@uap-core.de>
parents:
408
diff
changeset
|
190 | } |
5d167af0eadb
adds cxLinkedListDestroy prototype
Mike Becker <universe@uap-core.de>
parents:
408
diff
changeset
|
191 | |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
192 | size_t cxLinkedListRecalculateSize(CxList list) { |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
193 | cx_linked_list *ll = (cx_linked_list *) list->data.listdata; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
194 | |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
195 | if (ll->begin == NULL) { |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
196 | list->data.size = 0; |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
197 | ll->end = NULL; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
198 | return 0; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
199 | } else { |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
200 | void *cur = ll->begin; |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
201 | void *last; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
202 | size_t size; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
203 | do { |
407
b447539ec255
simplifies linked list descriptor
Mike Becker <universe@uap-core.de>
parents:
406
diff
changeset
|
204 | last = cur; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
205 | size++; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
206 | } 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
|
207 | ll->end = last; |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
208 | list->data.size = size; |
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
209 | return size; |
398
8d506ed6c1c0
adds first draft for linked list implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
210 | } |
406
9cbea761fbf7
adds cxLinkedListWrap and cxLinkedListRecalculateSize
Mike Becker <universe@uap-core.de>
parents:
404
diff
changeset
|
211 | } |