src/ucx/list.c

changeset 490
d218607f5a7e
parent 438
22eca559aded
child 504
c094afcdfb27
equal deleted inserted replaced
489:921f83a8943f 490:d218607f5a7e
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29 #include "cx/list.h" 29 #include "cx/list.h"
30 30
31 #include <string.h>
32
33 // <editor-fold desc="Store Pointers Functionality">
34
35 static _Thread_local cx_compare_func cx_pl_cmpfunc_impl;
36
37 static int cx_pl_cmpfunc(
38 void const *l,
39 void const *r
40 ) {
41 void *const *lptr = l;
42 void *const *rptr = r;
43 void const *left = lptr == NULL ? NULL : *lptr;
44 void const *right = rptr == NULL ? NULL : *rptr;
45 return cx_pl_cmpfunc_impl(left, right);
46 }
47
48 static void cx_pl_hack_cmpfunc(struct cx_list_s const *list) {
49 // cast away const - this is the hacky thing
50 struct cx_list_s *l = (struct cx_list_s *) list;
51 cx_pl_cmpfunc_impl = l->cmpfunc;
52 l->cmpfunc = cx_pl_cmpfunc;
53 }
54
55 static void cx_pl_unhack_cmpfunc(struct cx_list_s const *list) {
56 // cast away const - this is the hacky thing
57 struct cx_list_s *l = (struct cx_list_s *) list;
58 l->cmpfunc = cx_pl_cmpfunc_impl;
59 }
60
61 static void cx_pl_destructor(struct cx_list_s *list) {
62 list->climpl->destructor(list);
63 }
64
65 static int cx_pl_insert_element(
66 struct cx_list_s *list,
67 size_t index,
68 void const *element
69 ) {
70 return list->climpl->insert_element(list, index, &element);
71 }
72
73 static size_t cx_pl_insert_array(
74 struct cx_list_s *list,
75 size_t index,
76 void const *array,
77 size_t n
78 ) {
79 return list->climpl->insert_array(list, index, array, n);
80 }
81
82 static int cx_pl_insert_iter(
83 struct cx_mut_iterator_s *iter,
84 void const *elem,
85 int prepend
86 ) {
87 struct cx_list_s *list = iter->src_handle;
88 return list->climpl->insert_iter(iter, &elem, prepend);
89 }
90
91 static int cx_pl_remove(
92 struct cx_list_s *list,
93 size_t index
94 ) {
95 return list->climpl->remove(list, index);
96 }
97
98 static void cx_pl_clear(struct cx_list_s *list) {
99 list->climpl->clear(list);
100 }
101
102 static int cx_pl_swap(
103 struct cx_list_s *list,
104 size_t i,
105 size_t j
106 ) {
107 return list->climpl->swap(list, i, j);
108 }
109
110 static void *cx_pl_at(
111 struct cx_list_s const *list,
112 size_t index
113 ) {
114 void **ptr = list->climpl->at(list, index);
115 return ptr == NULL ? NULL : *ptr;
116 }
117
118 static ssize_t cx_pl_find(
119 struct cx_list_s const *list,
120 void const *elem
121 ) {
122 cx_pl_hack_cmpfunc(list);
123 ssize_t ret = list->climpl->find(list, &elem);
124 cx_pl_unhack_cmpfunc(list);
125 return ret;
126 }
127
128 static void cx_pl_sort(struct cx_list_s *list) {
129 cx_pl_hack_cmpfunc(list);
130 list->climpl->sort(list);
131 cx_pl_unhack_cmpfunc(list);
132 }
133
134 static int cx_pl_compare(
135 struct cx_list_s const *list,
136 struct cx_list_s const *other
137 ) {
138 cx_pl_hack_cmpfunc(list);
139 int ret = list->climpl->compare(list, other);
140 cx_pl_unhack_cmpfunc(list);
141 return ret;
142 }
143
144 static void cx_pl_reverse(struct cx_list_s *list) {
145 list->climpl->reverse(list);
146 }
147
148 static void *cx_pl_iter_current(void const *it) {
149 struct cx_iterator_s const *iter = it;
150 void **ptr = iter->base.current_impl(it);
151 return ptr == NULL ? NULL : *ptr;
152 }
153
154 static struct cx_iterator_s cx_pl_iterator(
155 struct cx_list_s const *list,
156 size_t index,
157 bool backwards
158 ) {
159 struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards);
160 iter.base.current_impl = iter.base.current;
161 iter.base.current = cx_pl_iter_current;
162 return iter;
163 }
164
165 static cx_list_class cx_pointer_list_class = {
166 cx_pl_destructor,
167 cx_pl_insert_element,
168 cx_pl_insert_array,
169 cx_pl_insert_iter,
170 cx_pl_remove,
171 cx_pl_clear,
172 cx_pl_swap,
173 cx_pl_at,
174 cx_pl_find,
175 cx_pl_sort,
176 cx_pl_compare,
177 cx_pl_reverse,
178 cx_pl_iterator,
179 };
180
181 void cxListStoreObjects(CxList *list) {
182 list->store_pointer = false;
183 if (list->climpl != NULL) {
184 list->cl = list->climpl;
185 list->climpl = NULL;
186 }
187 }
188
189 void cxListStorePointers(CxList *list) {
190 list->item_size = sizeof(void *);
191 list->store_pointer = true;
192 list->climpl = list->cl;
193 list->cl = &cx_pointer_list_class;
194 }
195
196 // </editor-fold>
197
31 void cxListDestroy(CxList *list) { 198 void cxListDestroy(CxList *list) {
32 switch (list->content_destructor_type) { 199 if (list->simple_destructor) {
33 case CX_DESTRUCTOR_SIMPLE: { 200 CxIterator iter = cxListIterator(list);
34 CxIterator iter = cxListBegin(list); 201 cx_foreach(void*, elem, iter) {
35 cx_foreach(void*, elem, iter) { 202 // already correctly resolved pointer - immediately invoke dtor
36 list->simple_destructor(elem); 203 list->simple_destructor(elem);
37 }
38 break;
39 } 204 }
40 case CX_DESTRUCTOR_ADVANCED: { 205 }
41 CxIterator iter = cxListBegin(list); 206 if (list->advanced_destructor) {
42 cx_foreach(void*, elem, iter) { 207 CxIterator iter = cxListIterator(list);
43 list->advanced_destructor.func(list->advanced_destructor.data, elem); 208 cx_foreach(void*, elem, iter) {
44 } 209 // already correctly resolved pointer - immediately invoke dtor
45 break; 210 list->advanced_destructor(list->destructor_data, elem);
46 } 211 }
47 case CX_DESTRUCTOR_NONE:
48 break; // nothing
49 } 212 }
50 213
51 list->cl->destructor(list); 214 list->cl->destructor(list);
52 cxFree(list->allocator, list); 215 cxFree(list->allocator, list);
53 } 216 }
54 217
55 int cxListCompare( 218 int cxListCompare(
56 CxList const *list, 219 CxList const *list,
57 CxList const *other 220 CxList const *other
58 ) { 221 ) {
59 if (list->cl->compare == other->cl->compare) { 222 if ((list->store_pointer ^ other->store_pointer) ||
60 // same compare function, lists are compatible 223 ((list->climpl == NULL) ^ (other->climpl != NULL)) ||
61 return list->cl->compare(list, other); 224 ((list->climpl != NULL ? list->climpl->compare : list->cl->compare) !=
62 } else { 225 (other->climpl != NULL ? other->climpl->compare : other->cl->compare))) {
63 // different compare functions, use iterator 226 // lists are definitely different - cannot use internal compare function
64 if (list->size == other->size) { 227 if (list->size == other->size) {
65 CxIterator left = cxListBegin(list); 228 CxIterator left = cxListIterator(list);
66 CxIterator right = cxListBegin(other); 229 CxIterator right = cxListIterator(other);
67 for (size_t i = 0; i < list->size; i++) { 230 for (size_t i = 0; i < list->size; i++) {
68 void *leftValue = cxIteratorCurrent(left); 231 void *leftValue = cxIteratorCurrent(left);
69 void *rightValue = cxIteratorCurrent(right); 232 void *rightValue = cxIteratorCurrent(right);
70 int d = list->cmpfunc(leftValue, rightValue); 233 int d = list->cmpfunc(leftValue, rightValue);
71 if (d != 0) { 234 if (d != 0) {
76 } 239 }
77 return 0; 240 return 0;
78 } else { 241 } else {
79 return list->size < other->size ? -1 : 1; 242 return list->size < other->size ? -1 : 1;
80 } 243 }
81 } 244 } else {
82 } 245 // lists are compatible
246 return list->cl->compare(list, other);
247 }
248 }
249
250 CxMutIterator cxListMutIteratorAt(
251 CxList *list,
252 size_t index
253 ) {
254 CxIterator it = list->cl->iterator(list, index, false);
255 it.base.mutating = true;
256
257 // we know the iterators share the same memory layout
258 CxMutIterator iter;
259 memcpy(&iter, &it, sizeof(CxMutIterator));
260 return iter;
261 }
262
263 CxMutIterator cxListMutBackwardsIteratorAt(
264 CxList *list,
265 size_t index
266 ) {
267 CxIterator it = list->cl->iterator(list, index, true);
268 it.base.mutating = true;
269
270 // we know the iterators share the same memory layout
271 CxMutIterator iter;
272 memcpy(&iter, &it, sizeof(CxMutIterator));
273 return iter;
274 }

mercurial