3 weeks ago
ucx update + fix doc attach/detach + fix ui_set with unbound values
174 | 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/list.h" | |
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( | |
324 | 38 | const void *l, |
39 | const void *r | |
174 | 40 | ) { |
41 | void *const *lptr = l; | |
42 | void *const *rptr = r; | |
324 | 43 | const void *left = lptr == NULL ? NULL : *lptr; |
44 | const void *right = rptr == NULL ? NULL : *rptr; | |
174 | 45 | return cx_pl_cmpfunc_impl(left, right); |
46 | } | |
47 | ||
324 | 48 | static void cx_pl_hack_cmpfunc(const struct cx_list_s *list) { |
174 | 49 | // cast away const - this is the hacky thing |
324 | 50 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
174 | 51 | cx_pl_cmpfunc_impl = l->cmpfunc; |
52 | l->cmpfunc = cx_pl_cmpfunc; | |
53 | } | |
54 | ||
324 | 55 | static void cx_pl_unhack_cmpfunc(const struct cx_list_s *list) { |
174 | 56 | // cast away const - this is the hacky thing |
324 | 57 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
174 | 58 | l->cmpfunc = cx_pl_cmpfunc_impl; |
59 | } | |
60 | ||
61 | static void cx_pl_destructor(struct cx_list_s *list) { | |
440 | 62 | list->climpl->deallocate(list); |
174 | 63 | } |
64 | ||
65 | static int cx_pl_insert_element( | |
66 | struct cx_list_s *list, | |
67 | size_t index, | |
324 | 68 | const void *element |
174 | 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, | |
324 | 76 | const void *array, |
174 | 77 | size_t n |
78 | ) { | |
79 | return list->climpl->insert_array(list, index, array, n); | |
80 | } | |
81 | ||
324 | 82 | static size_t cx_pl_insert_sorted( |
83 | struct cx_list_s *list, | |
84 | const void *array, | |
85 | size_t n | |
86 | ) { | |
87 | cx_pl_hack_cmpfunc(list); | |
88 | size_t result = list->climpl->insert_sorted(list, array, n); | |
89 | cx_pl_unhack_cmpfunc(list); | |
90 | return result; | |
91 | } | |
92 | ||
174 | 93 | static int cx_pl_insert_iter( |
324 | 94 | struct cx_iterator_s *iter, |
95 | const void *elem, | |
174 | 96 | int prepend |
97 | ) { | |
324 | 98 | struct cx_list_s *list = iter->src_handle.m; |
174 | 99 | return list->climpl->insert_iter(iter, &elem, prepend); |
100 | } | |
101 | ||
440 | 102 | static size_t cx_pl_remove( |
174 | 103 | struct cx_list_s *list, |
440 | 104 | size_t index, |
105 | size_t num, | |
106 | void *targetbuf | |
174 | 107 | ) { |
440 | 108 | return list->climpl->remove(list, index, num, targetbuf); |
174 | 109 | } |
110 | ||
111 | static void cx_pl_clear(struct cx_list_s *list) { | |
112 | list->climpl->clear(list); | |
113 | } | |
114 | ||
115 | static int cx_pl_swap( | |
116 | struct cx_list_s *list, | |
117 | size_t i, | |
118 | size_t j | |
119 | ) { | |
120 | return list->climpl->swap(list, i, j); | |
121 | } | |
122 | ||
123 | static void *cx_pl_at( | |
324 | 124 | const struct cx_list_s *list, |
174 | 125 | size_t index |
126 | ) { | |
127 | void **ptr = list->climpl->at(list, index); | |
128 | return ptr == NULL ? NULL : *ptr; | |
129 | } | |
130 | ||
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
131 | static size_t cx_pl_find_remove( |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
132 | struct cx_list_s *list, |
324 | 133 | const void *elem, |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
134 | bool remove |
174 | 135 | ) { |
136 | cx_pl_hack_cmpfunc(list); | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
137 | size_t ret = list->climpl->find_remove(list, &elem, remove); |
174 | 138 | cx_pl_unhack_cmpfunc(list); |
139 | return ret; | |
140 | } | |
141 | ||
142 | static void cx_pl_sort(struct cx_list_s *list) { | |
143 | cx_pl_hack_cmpfunc(list); | |
144 | list->climpl->sort(list); | |
145 | cx_pl_unhack_cmpfunc(list); | |
146 | } | |
147 | ||
148 | static int cx_pl_compare( | |
324 | 149 | const struct cx_list_s *list, |
150 | const struct cx_list_s *other | |
174 | 151 | ) { |
152 | cx_pl_hack_cmpfunc(list); | |
153 | int ret = list->climpl->compare(list, other); | |
154 | cx_pl_unhack_cmpfunc(list); | |
155 | return ret; | |
156 | } | |
157 | ||
158 | static void cx_pl_reverse(struct cx_list_s *list) { | |
159 | list->climpl->reverse(list); | |
160 | } | |
161 | ||
324 | 162 | static void *cx_pl_iter_current(const void *it) { |
163 | const struct cx_iterator_s *iter = it; | |
174 | 164 | void **ptr = iter->base.current_impl(it); |
165 | return ptr == NULL ? NULL : *ptr; | |
166 | } | |
167 | ||
168 | static struct cx_iterator_s cx_pl_iterator( | |
324 | 169 | const struct cx_list_s *list, |
174 | 170 | size_t index, |
171 | bool backwards | |
172 | ) { | |
173 | struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); | |
174 | iter.base.current_impl = iter.base.current; | |
175 | iter.base.current = cx_pl_iter_current; | |
176 | return iter; | |
177 | } | |
178 | ||
179 | static cx_list_class cx_pointer_list_class = { | |
180 | cx_pl_destructor, | |
181 | cx_pl_insert_element, | |
182 | cx_pl_insert_array, | |
324 | 183 | cx_pl_insert_sorted, |
174 | 184 | cx_pl_insert_iter, |
185 | cx_pl_remove, | |
186 | cx_pl_clear, | |
187 | cx_pl_swap, | |
188 | cx_pl_at, | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
189 | cx_pl_find_remove, |
174 | 190 | cx_pl_sort, |
191 | cx_pl_compare, | |
192 | cx_pl_reverse, | |
193 | cx_pl_iterator, | |
194 | }; | |
195 | // </editor-fold> | |
196 | ||
197 | // <editor-fold desc="empty list implementation"> | |
198 | ||
440 | 199 | static void cx_emptyl_noop(cx_attr_unused CxList *list) { |
174 | 200 | // this is a noop, but MUST be implemented |
201 | } | |
202 | ||
203 | static void *cx_emptyl_at( | |
440 | 204 | cx_attr_unused const struct cx_list_s *list, |
205 | cx_attr_unused size_t index | |
174 | 206 | ) { |
207 | return NULL; | |
208 | } | |
209 | ||
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
210 | static size_t cx_emptyl_find_remove( |
440 | 211 | cx_attr_unused struct cx_list_s *list, |
212 | cx_attr_unused const void *elem, | |
213 | cx_attr_unused bool remove | |
174 | 214 | ) { |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
215 | return 0; |
174 | 216 | } |
217 | ||
440 | 218 | static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) { |
174 | 219 | return false; |
220 | } | |
221 | ||
222 | static CxIterator cx_emptyl_iterator( | |
324 | 223 | const struct cx_list_s *list, |
174 | 224 | size_t index, |
440 | 225 | cx_attr_unused bool backwards |
174 | 226 | ) { |
227 | CxIterator iter = {0}; | |
324 | 228 | iter.src_handle.c = list; |
174 | 229 | iter.index = index; |
230 | iter.base.valid = cx_emptyl_iter_valid; | |
231 | return iter; | |
232 | } | |
233 | ||
234 | static cx_list_class cx_empty_list_class = { | |
235 | cx_emptyl_noop, | |
236 | NULL, | |
237 | NULL, | |
238 | NULL, | |
239 | NULL, | |
324 | 240 | NULL, |
174 | 241 | cx_emptyl_noop, |
242 | NULL, | |
243 | cx_emptyl_at, | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
244 | cx_emptyl_find_remove, |
174 | 245 | cx_emptyl_noop, |
324 | 246 | NULL, |
174 | 247 | cx_emptyl_noop, |
248 | cx_emptyl_iterator, | |
249 | }; | |
250 | ||
251 | CxList cx_empty_list = { | |
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
252 | { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
253 | NULL, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
254 | NULL, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
255 | 0, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
256 | 0, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
257 | NULL, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
258 | NULL, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
259 | NULL, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
260 | false, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
261 | true, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
262 | }, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
263 | &cx_empty_list_class, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
264 | NULL |
174 | 265 | }; |
266 | ||
267 | CxList *const cxEmptyList = &cx_empty_list; | |
268 | ||
269 | // </editor-fold> | |
270 | ||
324 | 271 | #define invoke_list_func(name, list, ...) \ |
272 | ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \ | |
273 | (list, __VA_ARGS__) | |
274 | ||
275 | size_t cx_list_default_insert_array( | |
276 | struct cx_list_s *list, | |
277 | size_t index, | |
278 | const void *data, | |
279 | size_t n | |
280 | ) { | |
281 | size_t elem_size = list->collection.elem_size; | |
282 | const char *src = data; | |
283 | size_t i = 0; | |
284 | for (; i < n; i++) { | |
440 | 285 | if (0 != invoke_list_func( |
286 | insert_element, list, index + i, | |
287 | src + (i * elem_size))) return i; | |
324 | 288 | } |
289 | return i; | |
290 | } | |
291 | ||
292 | size_t cx_list_default_insert_sorted( | |
293 | struct cx_list_s *list, | |
294 | const void *sorted_data, | |
295 | size_t n | |
296 | ) { | |
297 | // corner case | |
298 | if (n == 0) return 0; | |
299 | ||
300 | size_t elem_size = list->collection.elem_size; | |
301 | cx_compare_func cmp = list->collection.cmpfunc; | |
302 | const char *src = sorted_data; | |
303 | ||
304 | // track indices and number of inserted items | |
305 | size_t di = 0, si = 0, inserted = 0; | |
306 | ||
307 | // search the list for insertion points | |
308 | for (; di < list->collection.size; di++) { | |
309 | const void *list_elm = invoke_list_func(at, list, di); | |
310 | ||
311 | // compare current list element with first source element | |
312 | // if less or equal, skip | |
313 | if (cmp(list_elm, src) <= 0) { | |
314 | continue; | |
315 | } | |
316 | ||
317 | // determine number of consecutive elements that can be inserted | |
318 | size_t ins = 1; | |
319 | const char *next = src; | |
320 | while (++si < n) { | |
321 | next += elem_size; | |
322 | // once we become larger than the list elem, break | |
323 | if (cmp(list_elm, next) <= 0) { | |
324 | break; | |
325 | } | |
326 | // otherwise, we can insert one more | |
327 | ins++; | |
328 | } | |
329 | ||
330 | // insert the elements at location si | |
331 | if (ins == 1) { | |
440 | 332 | if (0 != invoke_list_func( |
333 | insert_element, list, di, src)) return inserted; | |
324 | 334 | } else { |
335 | size_t r = invoke_list_func(insert_array, list, di, src, ins); | |
336 | if (r < ins) return inserted + r; | |
337 | } | |
338 | inserted += ins; | |
339 | di += ins; | |
340 | ||
341 | // everything inserted? | |
342 | if (inserted == n) return inserted; | |
343 | src = next; | |
344 | } | |
345 | ||
346 | // insert remaining items | |
347 | if (si < n) { | |
348 | inserted += invoke_list_func(insert_array, list, di, src, n - si); | |
349 | } | |
350 | ||
351 | return inserted; | |
352 | } | |
353 | ||
354 | void cx_list_default_sort(struct cx_list_s *list) { | |
355 | size_t elem_size = list->collection.elem_size; | |
356 | size_t list_size = list->collection.size; | |
357 | void *tmp = malloc(elem_size * list_size); | |
358 | if (tmp == NULL) abort(); | |
359 | ||
360 | // copy elements from source array | |
361 | char *loc = tmp; | |
362 | for (size_t i = 0; i < list_size; i++) { | |
363 | void *src = invoke_list_func(at, list, i); | |
364 | memcpy(loc, src, elem_size); | |
365 | loc += elem_size; | |
366 | } | |
367 | ||
368 | // qsort | |
369 | qsort(tmp, list_size, elem_size, | |
370 | list->collection.cmpfunc); | |
371 | ||
372 | // copy elements back | |
373 | loc = tmp; | |
374 | for (size_t i = 0; i < list_size; i++) { | |
375 | void *dest = invoke_list_func(at, list, i); | |
376 | memcpy(dest, loc, elem_size); | |
377 | loc += elem_size; | |
378 | } | |
379 | ||
380 | free(tmp); | |
381 | } | |
382 | ||
383 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
384 | if (i == j) return 0; | |
385 | if (i >= list->collection.size) return 1; | |
386 | if (j >= list->collection.size) return 1; | |
387 | ||
388 | size_t elem_size = list->collection.elem_size; | |
389 | ||
390 | void *tmp = malloc(elem_size); | |
391 | if (tmp == NULL) return 1; | |
392 | ||
393 | void *ip = invoke_list_func(at, list, i); | |
394 | void *jp = invoke_list_func(at, list, j); | |
395 | ||
396 | memcpy(tmp, ip, elem_size); | |
397 | memcpy(ip, jp, elem_size); | |
398 | memcpy(jp, tmp, elem_size); | |
399 | ||
400 | free(tmp); | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
405 | void cx_list_init( |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
406 | struct cx_list_s *list, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
407 | struct cx_list_class_s *cl, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
408 | const struct cx_allocator_s *allocator, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
409 | cx_compare_func comparator, |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
410 | size_t elem_size |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
411 | ) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
412 | list->cl = cl; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
413 | list->collection.allocator = allocator; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
414 | list->collection.cmpfunc = comparator; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
415 | if (elem_size > 0) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
416 | list->collection.elem_size = elem_size; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
417 | } else { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
418 | list->collection.elem_size = sizeof(void *); |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
419 | if (list->collection.cmpfunc == NULL) { |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
420 | list->collection.cmpfunc = cx_cmp_ptr; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
421 | } |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
422 | list->collection.store_pointer = true; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
423 | list->climpl = list->cl; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
424 | list->cl = &cx_pointer_list_class; |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
425 | } |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
426 | } |
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
427 | |
174 | 428 | int cxListCompare( |
324 | 429 | const CxList *list, |
430 | const CxList *other | |
174 | 431 | ) { |
324 | 432 | bool cannot_optimize = false; |
433 | ||
434 | // if one is storing pointers but the other is not | |
435 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
436 | ||
437 | // if one class is wrapped but the other is not | |
438 | cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL); | |
174 | 439 | |
324 | 440 | // if the compare functions do not match or both are NULL |
441 | if (!cannot_optimize) { | |
442 | cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ? | |
443 | list->climpl->compare : list->cl->compare); | |
444 | cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ? | |
445 | other->climpl->compare : other->cl->compare); | |
446 | cannot_optimize |= list_cmp != other_cmp; | |
447 | cannot_optimize |= list_cmp == NULL; | |
448 | } | |
174 | 449 | |
324 | 450 | if (cannot_optimize) { |
174 | 451 | // lists are definitely different - cannot use internal compare function |
324 | 452 | if (list->collection.size == other->collection.size) { |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
453 | CxIterator left = list->cl->iterator(list, 0, false); |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
454 | CxIterator right = other->cl->iterator(other, 0, false); |
324 | 455 | for (size_t i = 0; i < list->collection.size; i++) { |
174 | 456 | void *leftValue = cxIteratorCurrent(left); |
457 | void *rightValue = cxIteratorCurrent(right); | |
324 | 458 | int d = list->collection.cmpfunc(leftValue, rightValue); |
174 | 459 | if (d != 0) { |
460 | return d; | |
461 | } | |
462 | cxIteratorNext(left); | |
463 | cxIteratorNext(right); | |
464 | } | |
465 | return 0; | |
466 | } else { | |
324 | 467 | return list->collection.size < other->collection.size ? -1 : 1; |
174 | 468 | } |
469 | } else { | |
470 | // lists are compatible | |
471 | return list->cl->compare(list, other); | |
472 | } | |
473 | } | |
474 | ||
324 | 475 | CxIterator cxListMutIteratorAt( |
174 | 476 | CxList *list, |
477 | size_t index | |
478 | ) { | |
479 | CxIterator it = list->cl->iterator(list, index, false); | |
480 | it.base.mutating = true; | |
324 | 481 | return it; |
174 | 482 | } |
483 | ||
324 | 484 | CxIterator cxListMutBackwardsIteratorAt( |
174 | 485 | CxList *list, |
486 | size_t index | |
487 | ) { | |
488 | CxIterator it = list->cl->iterator(list, index, true); | |
489 | it.base.mutating = true; | |
324 | 490 | return it; |
174 | 491 | } |
440 | 492 | |
493 | void cxListFree(CxList *list) { | |
494 | if (list == NULL) return; | |
495 | list->cl->deallocate(list); | |
496 | } |