Sun, 07 Dec 2025 19:20:48 +0100
implement radiobutton (Client)
| 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> | |
| 870 | 32 | #include <assert.h> |
| 174 | 33 | |
| 34 | // <editor-fold desc="Store Pointers Functionality"> | |
| 35 | ||
| 36 | static _Thread_local cx_compare_func cx_pl_cmpfunc_impl; | |
| 37 | ||
| 38 | static int cx_pl_cmpfunc( | |
| 324 | 39 | const void *l, |
| 40 | const void *r | |
| 174 | 41 | ) { |
| 845 | 42 | // l and r are guaranteed to be non-NULL pointing to the list's memory |
| 174 | 43 | void *const *lptr = l; |
| 44 | void *const *rptr = r; | |
| 845 | 45 | const void *left = *lptr; |
| 46 | const void *right = *rptr; | |
| 47 | if (left == NULL) { | |
| 48 | // NULL is smaller than any value except NULL | |
| 49 | return right == NULL ? 0 : -1; | |
| 50 | } else if (right == NULL) { | |
| 51 | // any value is larger than NULL | |
| 52 | return 1; | |
| 53 | } | |
| 174 | 54 | return cx_pl_cmpfunc_impl(left, right); |
| 55 | } | |
| 56 | ||
| 324 | 57 | static void cx_pl_hack_cmpfunc(const struct cx_list_s *list) { |
| 174 | 58 | // cast away const - this is the hacky thing |
| 324 | 59 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 174 | 60 | cx_pl_cmpfunc_impl = l->cmpfunc; |
| 61 | l->cmpfunc = cx_pl_cmpfunc; | |
| 62 | } | |
| 63 | ||
| 324 | 64 | static void cx_pl_unhack_cmpfunc(const struct cx_list_s *list) { |
| 174 | 65 | // cast away const - this is the hacky thing |
| 324 | 66 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 174 | 67 | l->cmpfunc = cx_pl_cmpfunc_impl; |
| 68 | } | |
| 69 | ||
| 70 | static void cx_pl_destructor(struct cx_list_s *list) { | |
| 440 | 71 | list->climpl->deallocate(list); |
| 174 | 72 | } |
| 73 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
74 | static void *cx_pl_insert_element( |
| 174 | 75 | struct cx_list_s *list, |
| 76 | size_t index, | |
| 324 | 77 | const void *element |
| 174 | 78 | ) { |
| 79 | return list->climpl->insert_element(list, index, &element); | |
| 80 | } | |
| 81 | ||
| 82 | static size_t cx_pl_insert_array( | |
| 83 | struct cx_list_s *list, | |
| 84 | size_t index, | |
| 324 | 85 | const void *array, |
| 174 | 86 | size_t n |
| 87 | ) { | |
| 88 | return list->climpl->insert_array(list, index, array, n); | |
| 89 | } | |
| 90 | ||
| 324 | 91 | static size_t cx_pl_insert_sorted( |
| 92 | struct cx_list_s *list, | |
| 93 | const void *array, | |
| 94 | size_t n | |
| 95 | ) { | |
| 96 | cx_pl_hack_cmpfunc(list); | |
| 97 | size_t result = list->climpl->insert_sorted(list, array, n); | |
| 98 | cx_pl_unhack_cmpfunc(list); | |
| 99 | return result; | |
| 100 | } | |
| 101 | ||
| 845 | 102 | static size_t cx_pl_insert_unique( |
| 103 | struct cx_list_s *list, | |
| 104 | const void *array, | |
| 105 | size_t n | |
| 106 | ) { | |
| 107 | cx_pl_hack_cmpfunc(list); | |
| 108 | size_t result = list->climpl->insert_unique(list, array, n); | |
| 109 | cx_pl_unhack_cmpfunc(list); | |
| 110 | return result; | |
| 111 | } | |
| 112 | ||
| 174 | 113 | static int cx_pl_insert_iter( |
| 324 | 114 | struct cx_iterator_s *iter, |
| 115 | const void *elem, | |
| 174 | 116 | int prepend |
| 117 | ) { | |
| 870 | 118 | struct cx_list_s *list = iter->src_handle; |
| 174 | 119 | return list->climpl->insert_iter(iter, &elem, prepend); |
| 120 | } | |
| 121 | ||
| 440 | 122 | static size_t cx_pl_remove( |
| 174 | 123 | struct cx_list_s *list, |
| 440 | 124 | size_t index, |
| 125 | size_t num, | |
| 126 | void *targetbuf | |
| 174 | 127 | ) { |
| 440 | 128 | return list->climpl->remove(list, index, num, targetbuf); |
| 174 | 129 | } |
| 130 | ||
| 131 | static void cx_pl_clear(struct cx_list_s *list) { | |
| 132 | list->climpl->clear(list); | |
| 133 | } | |
| 134 | ||
| 135 | static int cx_pl_swap( | |
| 136 | struct cx_list_s *list, | |
| 137 | size_t i, | |
| 138 | size_t j | |
| 139 | ) { | |
| 140 | return list->climpl->swap(list, i, j); | |
| 141 | } | |
| 142 | ||
| 143 | static void *cx_pl_at( | |
| 324 | 144 | const struct cx_list_s *list, |
| 174 | 145 | size_t index |
| 146 | ) { | |
| 147 | void **ptr = list->climpl->at(list, index); | |
| 148 | return ptr == NULL ? NULL : *ptr; | |
| 149 | } | |
| 150 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
151 | static size_t cx_pl_find_remove( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
152 | struct cx_list_s *list, |
| 324 | 153 | const void *elem, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
154 | bool remove |
| 174 | 155 | ) { |
| 156 | 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
|
157 | size_t ret = list->climpl->find_remove(list, &elem, remove); |
| 174 | 158 | cx_pl_unhack_cmpfunc(list); |
| 159 | return ret; | |
| 160 | } | |
| 161 | ||
| 162 | static void cx_pl_sort(struct cx_list_s *list) { | |
| 163 | cx_pl_hack_cmpfunc(list); | |
| 164 | list->climpl->sort(list); | |
| 165 | cx_pl_unhack_cmpfunc(list); | |
| 166 | } | |
| 167 | ||
| 168 | static int cx_pl_compare( | |
| 324 | 169 | const struct cx_list_s *list, |
| 170 | const struct cx_list_s *other | |
| 174 | 171 | ) { |
| 172 | cx_pl_hack_cmpfunc(list); | |
| 173 | int ret = list->climpl->compare(list, other); | |
| 174 | cx_pl_unhack_cmpfunc(list); | |
| 175 | return ret; | |
| 176 | } | |
| 177 | ||
| 178 | static void cx_pl_reverse(struct cx_list_s *list) { | |
| 179 | list->climpl->reverse(list); | |
| 180 | } | |
| 181 | ||
| 324 | 182 | static void *cx_pl_iter_current(const void *it) { |
| 183 | const struct cx_iterator_s *iter = it; | |
| 174 | 184 | void **ptr = iter->base.current_impl(it); |
| 185 | return ptr == NULL ? NULL : *ptr; | |
| 186 | } | |
| 187 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
188 | static int cx_pl_change_capacity(struct cx_list_s *list, size_t cap) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
189 | if (list->climpl->change_capacity == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
190 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
191 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
192 | return list->climpl->change_capacity(list, cap); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
193 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
194 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
195 | |
| 174 | 196 | static struct cx_iterator_s cx_pl_iterator( |
| 324 | 197 | const struct cx_list_s *list, |
| 174 | 198 | size_t index, |
| 199 | bool backwards | |
| 200 | ) { | |
| 201 | struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); | |
| 202 | iter.base.current_impl = iter.base.current; | |
| 203 | iter.base.current = cx_pl_iter_current; | |
| 204 | return iter; | |
| 205 | } | |
| 206 | ||
| 207 | static cx_list_class cx_pointer_list_class = { | |
| 208 | cx_pl_destructor, | |
| 209 | cx_pl_insert_element, | |
| 210 | cx_pl_insert_array, | |
| 324 | 211 | cx_pl_insert_sorted, |
| 845 | 212 | cx_pl_insert_unique, |
| 174 | 213 | cx_pl_insert_iter, |
| 214 | cx_pl_remove, | |
| 215 | cx_pl_clear, | |
| 216 | cx_pl_swap, | |
| 217 | cx_pl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
218 | cx_pl_find_remove, |
| 174 | 219 | cx_pl_sort, |
| 220 | cx_pl_compare, | |
| 221 | cx_pl_reverse, | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
222 | cx_pl_change_capacity, |
| 174 | 223 | cx_pl_iterator, |
| 224 | }; | |
| 225 | // </editor-fold> | |
| 226 | ||
| 227 | // <editor-fold desc="empty list implementation"> | |
| 228 | ||
| 440 | 229 | static void cx_emptyl_noop(cx_attr_unused CxList *list) { |
| 174 | 230 | // this is a noop, but MUST be implemented |
| 231 | } | |
| 232 | ||
| 233 | static void *cx_emptyl_at( | |
| 440 | 234 | cx_attr_unused const struct cx_list_s *list, |
| 235 | cx_attr_unused size_t index | |
| 174 | 236 | ) { |
| 237 | return NULL; | |
| 238 | } | |
| 239 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
240 | static size_t cx_emptyl_find_remove( |
| 440 | 241 | cx_attr_unused struct cx_list_s *list, |
| 242 | cx_attr_unused const void *elem, | |
| 243 | cx_attr_unused bool remove | |
| 174 | 244 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
245 | return 0; |
| 174 | 246 | } |
| 247 | ||
| 440 | 248 | static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) { |
| 174 | 249 | return false; |
| 250 | } | |
| 251 | ||
| 252 | static CxIterator cx_emptyl_iterator( | |
| 324 | 253 | const struct cx_list_s *list, |
| 174 | 254 | size_t index, |
| 440 | 255 | cx_attr_unused bool backwards |
| 174 | 256 | ) { |
| 257 | CxIterator iter = {0}; | |
| 870 | 258 | iter.src_handle = (void*) list; |
| 174 | 259 | iter.index = index; |
| 260 | iter.base.valid = cx_emptyl_iter_valid; | |
| 261 | return iter; | |
| 262 | } | |
| 263 | ||
| 264 | static cx_list_class cx_empty_list_class = { | |
| 265 | cx_emptyl_noop, | |
| 266 | NULL, | |
| 267 | NULL, | |
| 268 | NULL, | |
| 269 | NULL, | |
| 324 | 270 | NULL, |
| 845 | 271 | NULL, |
| 174 | 272 | cx_emptyl_noop, |
| 273 | NULL, | |
| 274 | cx_emptyl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
275 | cx_emptyl_find_remove, |
| 174 | 276 | cx_emptyl_noop, |
| 324 | 277 | NULL, |
| 174 | 278 | cx_emptyl_noop, |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
279 | NULL, |
| 174 | 280 | cx_emptyl_iterator, |
| 281 | }; | |
| 282 | ||
| 283 | 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
|
284 | { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
285 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
286 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
287 | 0, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
288 | 0, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
289 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
290 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
291 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
292 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
293 | true, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
294 | }, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
295 | &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
|
296 | NULL |
| 174 | 297 | }; |
| 298 | ||
| 299 | CxList *const cxEmptyList = &cx_empty_list; | |
| 300 | ||
| 301 | // </editor-fold> | |
| 302 | ||
| 324 | 303 | #define invoke_list_func(name, list, ...) \ |
| 304 | ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \ | |
| 305 | (list, __VA_ARGS__) | |
| 306 | ||
| 307 | size_t cx_list_default_insert_array( | |
| 308 | struct cx_list_s *list, | |
| 309 | size_t index, | |
| 310 | const void *data, | |
| 311 | size_t n | |
| 312 | ) { | |
| 313 | const char *src = data; | |
| 314 | size_t i = 0; | |
| 315 | for (; i < n; i++) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
316 | if (NULL == invoke_list_func( |
| 870 | 317 | insert_element, list, index + i, src) |
| 845 | 318 | ) { |
| 319 | return i; // LCOV_EXCL_LINE | |
| 320 | } | |
| 870 | 321 | if (src != NULL) { |
| 322 | src += list->collection.elem_size; | |
| 323 | } | |
| 324 | 324 | } |
| 325 | return i; | |
| 326 | } | |
| 327 | ||
| 845 | 328 | static size_t cx_list_default_insert_sorted_impl( |
| 324 | 329 | struct cx_list_s *list, |
| 330 | const void *sorted_data, | |
| 845 | 331 | size_t n, |
| 332 | bool allow_duplicates | |
| 324 | 333 | ) { |
| 334 | // corner case | |
| 335 | if (n == 0) return 0; | |
| 336 | ||
| 337 | size_t elem_size = list->collection.elem_size; | |
| 338 | cx_compare_func cmp = list->collection.cmpfunc; | |
| 339 | const char *src = sorted_data; | |
| 340 | ||
| 341 | // track indices and number of inserted items | |
| 845 | 342 | size_t di = 0, si = 0, processed = 0; |
| 324 | 343 | |
| 344 | // search the list for insertion points | |
| 845 | 345 | while (di < list->collection.size) { |
| 324 | 346 | const void *list_elm = invoke_list_func(at, list, di); |
| 347 | ||
| 845 | 348 | // compare the current list element with the first source element |
| 349 | // if less, skip the list elements | |
| 350 | // if equal, skip the list elements and optionally the source elements | |
| 351 | { | |
| 352 | int d = cmp(list_elm, src); | |
| 353 | if (d <= 0) { | |
| 354 | if (!allow_duplicates && d == 0) { | |
| 355 | src += elem_size; | |
| 356 | si++; | |
| 357 | processed++; // we also count duplicates for the return value | |
| 358 | while (si < n && cmp(list_elm, src) == 0) { | |
| 359 | src += elem_size; | |
| 360 | si++; | |
| 361 | processed++; | |
| 362 | } | |
| 363 | if (processed == n) { | |
| 364 | return processed; | |
| 365 | } | |
| 366 | } | |
| 367 | di++; | |
| 368 | continue; | |
| 369 | } | |
| 324 | 370 | } |
| 371 | ||
| 845 | 372 | // determine the number of consecutive elements that can be inserted |
| 373 | size_t ins = 1, skip = 0; | |
| 324 | 374 | const char *next = src; |
| 375 | while (++si < n) { | |
| 845 | 376 | if (!allow_duplicates) { |
| 377 | // skip duplicates within the source | |
| 378 | if (cmp(next, next + elem_size) == 0) { | |
| 379 | next += elem_size; | |
| 380 | skip++; | |
| 381 | continue; | |
| 382 | } else { | |
| 383 | if (skip > 0) { | |
| 384 | // if we had to skip something, we must wait for the next run | |
| 385 | next += elem_size; | |
| 386 | break; | |
| 387 | } | |
| 388 | } | |
| 389 | } | |
| 324 | 390 | next += elem_size; |
| 391 | // once we become larger than the list elem, break | |
| 392 | if (cmp(list_elm, next) <= 0) { | |
| 393 | break; | |
| 394 | } | |
| 395 | // otherwise, we can insert one more | |
| 396 | ins++; | |
| 397 | } | |
| 398 | ||
| 399 | // insert the elements at location si | |
| 400 | if (ins == 1) { | |
| 845 | 401 | if (NULL == invoke_list_func(insert_element, list, di, src)) { |
| 402 | return processed; // LCOV_EXCL_LINE | |
| 403 | } | |
| 324 | 404 | } else { |
| 405 | size_t r = invoke_list_func(insert_array, list, di, src, ins); | |
| 845 | 406 | if (r < ins) { |
| 407 | return processed + r; // LCOV_EXCL_LINE | |
| 408 | } | |
| 324 | 409 | } |
| 845 | 410 | processed += ins + skip; |
| 324 | 411 | di += ins; |
| 412 | ||
| 413 | // everything inserted? | |
| 845 | 414 | if (processed == n) { |
| 415 | return processed; | |
| 416 | } | |
| 324 | 417 | src = next; |
| 418 | } | |
| 419 | ||
| 420 | // insert remaining items | |
| 421 | if (si < n) { | |
| 845 | 422 | if (allow_duplicates) { |
| 423 | processed += invoke_list_func(insert_array, list, di, src, n - si); | |
| 424 | } else { | |
| 425 | const void *last = di == 0 ? NULL : invoke_list_func(at, list, di - 1); | |
| 426 | for (; si < n; si++) { | |
| 427 | // skip duplicates within the source | |
| 428 | if (last == NULL || cmp(last, src) != 0) { | |
| 429 | if (NULL == invoke_list_func(insert_element, list, di, src)) { | |
| 430 | return processed; // LCOV_EXCL_LINE | |
| 431 | } | |
| 432 | last = src; | |
| 433 | di++; | |
| 434 | } | |
| 435 | processed++; | |
| 436 | src += elem_size; | |
| 437 | } | |
| 438 | } | |
| 324 | 439 | } |
| 440 | ||
| 845 | 441 | return processed; |
| 442 | } | |
| 443 | ||
| 444 | size_t cx_list_default_insert_sorted( | |
| 445 | struct cx_list_s *list, | |
| 446 | const void *sorted_data, | |
| 447 | size_t n | |
| 448 | ) { | |
| 449 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, true); | |
| 450 | } | |
| 451 | ||
| 452 | size_t cx_list_default_insert_unique( | |
| 453 | struct cx_list_s *list, | |
| 454 | const void *sorted_data, | |
| 455 | size_t n | |
| 456 | ) { | |
| 457 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, false); | |
| 324 | 458 | } |
| 459 | ||
| 460 | void cx_list_default_sort(struct cx_list_s *list) { | |
| 461 | size_t elem_size = list->collection.elem_size; | |
| 462 | size_t list_size = list->collection.size; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
463 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 845 | 464 | if (tmp == NULL) abort(); // LCOV_EXCL_LINE |
| 324 | 465 | |
| 466 | // copy elements from source array | |
| 467 | char *loc = tmp; | |
| 468 | for (size_t i = 0; i < list_size; i++) { | |
| 469 | void *src = invoke_list_func(at, list, i); | |
| 470 | memcpy(loc, src, elem_size); | |
| 471 | loc += elem_size; | |
| 472 | } | |
| 473 | ||
| 474 | // qsort | |
| 475 | qsort(tmp, list_size, elem_size, | |
| 476 | list->collection.cmpfunc); | |
| 477 | ||
| 478 | // copy elements back | |
| 479 | loc = tmp; | |
| 480 | for (size_t i = 0; i < list_size; i++) { | |
| 481 | void *dest = invoke_list_func(at, list, i); | |
| 482 | memcpy(dest, loc, elem_size); | |
| 483 | loc += elem_size; | |
| 484 | } | |
| 485 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
486 | cxFreeDefault(tmp); |
| 324 | 487 | } |
| 488 | ||
| 489 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
| 490 | if (i == j) return 0; | |
| 491 | if (i >= list->collection.size) return 1; | |
| 492 | if (j >= list->collection.size) return 1; | |
| 493 | ||
| 494 | size_t elem_size = list->collection.elem_size; | |
| 495 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
496 | void *tmp = cxMallocDefault(elem_size); |
| 845 | 497 | if (tmp == NULL) return 1; // LCOV_EXCL_LINE |
| 324 | 498 | |
| 499 | void *ip = invoke_list_func(at, list, i); | |
| 500 | void *jp = invoke_list_func(at, list, j); | |
| 501 | ||
| 502 | memcpy(tmp, ip, elem_size); | |
| 503 | memcpy(ip, jp, elem_size); | |
| 504 | memcpy(jp, tmp, elem_size); | |
| 505 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
506 | cxFreeDefault(tmp); |
| 324 | 507 | |
| 508 | return 0; | |
| 509 | } | |
| 510 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
511 | 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
|
512 | 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
|
513 | 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
|
514 | 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
|
515 | 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
|
516 | 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
|
517 | ) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
518 | 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
|
519 | 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
|
520 | 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
|
521 | 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
|
522 | 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
|
523 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
524 | 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
|
525 | 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
|
526 | 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
|
527 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
528 | 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
|
529 | 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
|
530 | 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
|
531 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
532 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
533 | |
| 174 | 534 | int cxListCompare( |
| 324 | 535 | const CxList *list, |
| 536 | const CxList *other | |
| 174 | 537 | ) { |
| 324 | 538 | bool cannot_optimize = false; |
| 539 | ||
| 540 | // if one is storing pointers but the other is not | |
| 541 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
| 542 | ||
| 543 | // if one class is wrapped but the other is not | |
| 544 | cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL); | |
| 174 | 545 | |
| 324 | 546 | // if the compare functions do not match or both are NULL |
| 547 | if (!cannot_optimize) { | |
| 548 | cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ? | |
| 549 | list->climpl->compare : list->cl->compare); | |
| 550 | cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ? | |
| 551 | other->climpl->compare : other->cl->compare); | |
| 552 | cannot_optimize |= list_cmp != other_cmp; | |
| 553 | cannot_optimize |= list_cmp == NULL; | |
| 554 | } | |
| 174 | 555 | |
| 324 | 556 | if (cannot_optimize) { |
| 174 | 557 | // lists are definitely different - cannot use internal compare function |
| 324 | 558 | if (list->collection.size == other->collection.size) { |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
559 | CxIterator left = list->cl->iterator(list, 0, false); |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
560 | CxIterator right = other->cl->iterator(other, 0, false); |
| 324 | 561 | for (size_t i = 0; i < list->collection.size; i++) { |
| 174 | 562 | void *leftValue = cxIteratorCurrent(left); |
| 563 | void *rightValue = cxIteratorCurrent(right); | |
| 324 | 564 | int d = list->collection.cmpfunc(leftValue, rightValue); |
| 174 | 565 | if (d != 0) { |
| 566 | return d; | |
| 567 | } | |
| 568 | cxIteratorNext(left); | |
| 569 | cxIteratorNext(right); | |
| 570 | } | |
| 571 | return 0; | |
| 572 | } else { | |
| 324 | 573 | return list->collection.size < other->collection.size ? -1 : 1; |
| 174 | 574 | } |
| 575 | } else { | |
| 576 | // lists are compatible | |
| 577 | return list->cl->compare(list, other); | |
| 578 | } | |
| 579 | } | |
| 580 | ||
| 870 | 581 | size_t cxListSize(const CxList *list) { |
| 582 | return list->collection.size; | |
| 583 | } | |
| 584 | ||
| 585 | int cxListAdd(CxList *list, const void *elem) { | |
| 586 | list->collection.sorted = false; | |
| 587 | return list->cl->insert_element(list, list->collection.size, elem) == NULL; | |
| 588 | } | |
| 589 | ||
| 590 | size_t cxListAddArray(CxList *list, const void *array, size_t n) { | |
| 591 | list->collection.sorted = false; | |
| 592 | return list->cl->insert_array(list, list->collection.size, array, n); | |
| 593 | } | |
| 594 | ||
| 595 | int cxListInsert(CxList *list, size_t index, const void *elem) { | |
| 596 | list->collection.sorted = false; | |
| 597 | return list->cl->insert_element(list, index, elem) == NULL; | |
| 598 | } | |
| 599 | ||
| 600 | void *cxListEmplaceAt(CxList *list, size_t index) { | |
| 601 | list->collection.sorted = false; | |
| 602 | return list->cl->insert_element(list, index, NULL); | |
| 603 | } | |
| 604 | ||
| 605 | void *cxListEmplace(CxList *list) { | |
| 606 | list->collection.sorted = false; | |
| 607 | return list->cl->insert_element(list, list->collection.size, NULL); | |
| 608 | } | |
| 609 | ||
| 610 | static bool cx_list_emplace_iterator_valid(const void *it) { | |
| 611 | const CxIterator *iter = it; | |
| 612 | return iter->index < iter->elem_count; | |
| 613 | } | |
| 614 | ||
| 615 | CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) { | |
| 616 | list->collection.sorted = false; | |
| 617 | size_t c = list->cl->insert_array(list, index, NULL, n); | |
| 618 | CxIterator iter = list->cl->iterator(list, index, false); | |
| 619 | // tweak the fields of this iterator | |
| 620 | iter.elem_count = c; | |
| 621 | iter.index = 0; | |
| 622 | // replace the valid function to abort iteration when c is reached | |
| 623 | iter.base.valid = cx_list_emplace_iterator_valid; | |
| 624 | // if we are storing pointers, we want to return the pure pointers. | |
| 625 | // therefore, we must unwrap the "current" method | |
| 626 | if (list->collection.store_pointer) { | |
| 627 | iter.base.current = iter.base.current_impl; | |
| 628 | } | |
| 629 | return iter; | |
| 630 | } | |
| 631 | ||
| 632 | CxIterator cxListEmplaceArray(CxList *list, size_t n) { | |
| 633 | return cxListEmplaceArrayAt(list, list->collection.size, n); | |
| 634 | } | |
| 635 | ||
| 636 | int cxListInsertSorted(CxList *list, const void *elem) { | |
| 637 | assert(cxCollectionSorted(list)); | |
| 638 | list->collection.sorted = true; | |
| 639 | const void *data = list->collection.store_pointer ? &elem : elem; | |
| 640 | return list->cl->insert_sorted(list, data, 1) == 0; | |
| 641 | } | |
| 642 | ||
| 643 | int cxListInsertUnique(CxList *list, const void *elem) { | |
| 644 | if (cxCollectionSorted(list)) { | |
| 645 | list->collection.sorted = true; | |
| 646 | const void *data = list->collection.store_pointer ? &elem : elem; | |
| 647 | return list->cl->insert_unique(list, data, 1) == 0; | |
| 648 | } else { | |
| 649 | if (cxListContains(list, elem)) { | |
| 650 | return 0; | |
| 651 | } else { | |
| 652 | return cxListAdd(list, elem); | |
| 653 | } | |
| 654 | } | |
| 655 | } | |
| 656 | ||
| 657 | size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) { | |
| 658 | list->collection.sorted = false; | |
| 659 | return list->cl->insert_array(list, index, array, n); | |
| 174 | 660 | } |
| 661 | ||
| 870 | 662 | size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) { |
| 663 | assert(cxCollectionSorted(list)); | |
| 664 | list->collection.sorted = true; | |
| 665 | return list->cl->insert_sorted(list, array, n); | |
| 666 | } | |
| 667 | ||
| 668 | size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) { | |
| 669 | if (cxCollectionSorted(list)) { | |
| 670 | list->collection.sorted = true; | |
| 671 | return list->cl->insert_unique(list, array, n); | |
| 672 | } else { | |
| 673 | const char *source = array; | |
| 674 | for (size_t i = 0 ; i < n; i++) { | |
| 675 | // note: this also checks elements added in a previous iteration | |
| 676 | const void *data = list->collection.store_pointer ? | |
| 677 | *((const void**)source) : source; | |
| 678 | if (!cxListContains(list, data)) { | |
| 679 | if (cxListAdd(list, data)) { | |
| 680 | return i; // LCOV_EXCL_LINE | |
| 681 | } | |
| 682 | } | |
| 683 | source += list->collection.elem_size; | |
| 684 | } | |
| 685 | return n; | |
| 686 | } | |
| 687 | } | |
| 688 | ||
| 689 | int cxListInsertAfter(CxIterator *iter, const void *elem) { | |
| 690 | CxList* list = (CxList*)iter->src_handle; | |
| 691 | list->collection.sorted = false; | |
| 692 | return list->cl->insert_iter(iter, elem, 0); | |
| 693 | } | |
| 694 | ||
| 695 | int cxListInsertBefore(CxIterator *iter, const void *elem) { | |
| 696 | CxList* list = (CxList*)iter->src_handle; | |
| 697 | list->collection.sorted = false; | |
| 698 | return list->cl->insert_iter(iter, elem, 1); | |
| 699 | } | |
| 700 | ||
| 701 | int cxListRemove(CxList *list, size_t index) { | |
| 702 | return list->cl->remove(list, index, 1, NULL) == 0; | |
| 174 | 703 | } |
| 440 | 704 | |
| 870 | 705 | int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) { |
| 706 | return list->cl->remove(list, index, 1, targetbuf) == 0; | |
| 707 | } | |
| 708 | ||
| 709 | int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) { | |
| 710 | return list->cl->remove(list, 0, 1, targetbuf) == 0; | |
| 711 | } | |
| 712 | ||
| 713 | int cxListRemoveAndGetLast(CxList *list, void *targetbuf) { | |
| 714 | // note: index may wrap - member function will catch that | |
| 715 | return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; | |
| 716 | } | |
| 717 | ||
| 718 | size_t cxListRemoveArray(CxList *list, size_t index, size_t num) { | |
| 719 | return list->cl->remove(list, index, num, NULL); | |
| 720 | } | |
| 721 | ||
| 722 | size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) { | |
| 723 | return list->cl->remove(list, index, num, targetbuf); | |
| 440 | 724 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
725 | |
| 870 | 726 | void cxListClear(CxList *list) { |
| 727 | list->cl->clear(list); | |
| 728 | list->collection.sorted = true; // empty lists are always sorted | |
| 729 | } | |
| 730 | ||
| 731 | int cxListSwap(CxList *list, size_t i, size_t j) { | |
| 732 | list->collection.sorted = false; | |
| 733 | return list->cl->swap(list, i, j); | |
| 734 | } | |
| 735 | ||
| 736 | void *cxListAt(const CxList *list, size_t index) { | |
| 737 | return list->cl->at(list, index); | |
| 738 | } | |
| 739 | ||
| 740 | void *cxListFirst(const CxList *list) { | |
| 741 | return list->cl->at(list, 0); | |
| 742 | } | |
| 743 | ||
| 744 | void *cxListLast(const CxList *list) { | |
| 745 | return list->cl->at(list, list->collection.size - 1); | |
| 746 | } | |
| 747 | ||
| 748 | int cxListSet(CxList *list, size_t index, const void *elem) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
749 | if (index >= list->collection.size) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
750 | return 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
751 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
752 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
753 | if (list->collection.store_pointer) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
754 | // For pointer collections, always use climpl |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
755 | void **target = list->climpl->at(list, index); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
756 | *target = (void *)elem; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
757 | } else { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
758 | void *target = list->cl->at(list, index); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
759 | memcpy(target, elem, list->collection.elem_size); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
760 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
761 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
762 | return 0; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
763 | } |
| 870 | 764 | |
| 765 | CxIterator cxListIteratorAt(const CxList *list, size_t index) { | |
| 766 | if (list == NULL) list = cxEmptyList; | |
| 767 | return list->cl->iterator(list, index, false); | |
| 768 | } | |
| 769 | ||
| 770 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { | |
| 771 | if (list == NULL) list = cxEmptyList; | |
| 772 | return list->cl->iterator(list, index, true); | |
| 773 | } | |
| 774 | ||
| 775 | CxIterator cxListIterator(const CxList *list) { | |
| 776 | if (list == NULL) list = cxEmptyList; | |
| 777 | return list->cl->iterator(list, 0, false); | |
| 778 | } | |
| 779 | ||
| 780 | CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 781 | if (list == NULL) list = cxEmptyList; | |
| 782 | return list->cl->iterator(list, list->collection.size - 1, true); | |
| 783 | } | |
| 784 | ||
| 785 | size_t cxListFind(const CxList *list, const void *elem) { | |
| 786 | return list->cl->find_remove((CxList*)list, elem, false); | |
| 787 | } | |
| 788 | ||
| 789 | bool cxListContains(const CxList* list, const void* elem) { | |
| 790 | return list->cl->find_remove((CxList*)list, elem, false) < list->collection.size; | |
| 791 | } | |
| 792 | ||
| 793 | bool cxListIndexValid(const CxList *list, size_t index) { | |
| 794 | return index < list->collection.size; | |
| 795 | } | |
| 796 | ||
| 797 | size_t cxListFindRemove(CxList *list, const void *elem) { | |
| 798 | return list->cl->find_remove(list, elem, true); | |
| 799 | } | |
| 800 | ||
| 801 | void cxListSort(CxList *list) { | |
| 802 | if (list->collection.sorted) return; | |
| 803 | list->cl->sort(list); | |
| 804 | list->collection.sorted = true; | |
| 805 | } | |
| 806 | ||
| 807 | void cxListReverse(CxList *list) { | |
| 808 | // still sorted, but not according to the cmp_func | |
| 809 | list->collection.sorted = false; | |
| 810 | list->cl->reverse(list); | |
| 811 | } | |
| 812 | ||
| 813 | void cxListFree(CxList *list) { | |
| 814 | if (list == NULL) return; | |
| 815 | list->cl->deallocate(list); | |
| 816 | } | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
817 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
818 | static void cx_list_pop_uninitialized_elements(CxList *list, size_t n) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
819 | cx_destructor_func destr_bak = list->collection.simple_destructor; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
820 | cx_destructor_func2 destr2_bak = list->collection.advanced_destructor; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
821 | list->collection.simple_destructor = NULL; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
822 | list->collection.advanced_destructor = NULL; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
823 | if (n == 1) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
824 | cxListRemove(list, list->collection.size - 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
825 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
826 | cxListRemoveArray(list,list->collection.size - n, n); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
827 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
828 | list->collection.simple_destructor = destr_bak; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
829 | list->collection.advanced_destructor = destr2_bak; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
830 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
831 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
832 | static void* cx_list_simple_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
833 | size_t elem_size = *(size_t*)data; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
834 | if (dst == NULL) dst = cxMalloc(al, elem_size); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
835 | if (dst != NULL) memcpy(dst, src, elem_size); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
836 | return dst; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
837 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
838 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
839 | #define use_simple_clone_func(list) cx_list_simple_clone_func, NULL, (void*)&((list)->collection.elem_size) |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
840 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
841 | int cxListClone(CxList *dst, const CxList *src, cx_clone_func clone_func, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
842 | const CxAllocator *clone_allocator, void *data) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
843 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
844 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
845 | // remember the original size |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
846 | size_t orig_size = dst->collection.size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
847 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
848 | // first, try to allocate the memory in the new list |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
849 | CxIterator empl_iter = cxListEmplaceArray(dst, src->collection.size); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
850 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
851 | // get an iterator over the source elements |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
852 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
853 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
854 | // now clone the elements |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
855 | size_t cloned = empl_iter.elem_count; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
856 | for (size_t i = 0 ; i < empl_iter.elem_count; i++) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
857 | void *src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
858 | void **dest_memory = cxIteratorCurrent(empl_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
859 | void *target = cxCollectionStoresPointers(dst) ? NULL : dest_memory; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
860 | void *dest_ptr = clone_func(target, src_elem, clone_allocator, data); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
861 | if (dest_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
862 | cloned = i; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
863 | break; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
864 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
865 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
866 | *dest_memory = dest_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
867 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
868 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
869 | cxIteratorNext(empl_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
870 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
871 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
872 | // if we could not clone everything, free the allocated memory |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
873 | // (disable the destructors!) |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
874 | if (cloned < src->collection.size) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
875 | cx_list_pop_uninitialized_elements(dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
876 | dst->collection.size - cloned - orig_size); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
877 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
878 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
879 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
880 | // set the sorted flag when we know it's sorted |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
881 | if (orig_size == 0 && src->collection.sorted) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
882 | dst->collection.sorted = true; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
883 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
884 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
885 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
886 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
887 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
888 | int cxListDifference(CxList *dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
889 | const CxList *minuend, const CxList *subtrahend, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
890 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
891 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
892 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
893 | // optimize for sorted collections |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
894 | if (cxCollectionSorted(minuend) && cxCollectionSorted(subtrahend)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
895 | bool dst_was_empty = cxCollectionSize(dst) == 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
896 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
897 | CxIterator min_iter = cxListIterator(minuend); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
898 | CxIterator sub_iter = cxListIterator(subtrahend); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
899 | while (cxIteratorValid(min_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
900 | void *min_elem = cxIteratorCurrent(min_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
901 | void *sub_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
902 | int d; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
903 | if (cxIteratorValid(sub_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
904 | sub_elem = cxIteratorCurrent(sub_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
905 | cx_compare_func cmp = subtrahend->collection.cmpfunc; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
906 | d = cmp(sub_elem, min_elem); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
907 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
908 | // no more elements in the subtrahend, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
909 | // i.e., the min_elem is larger than any elem of the subtrahend |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
910 | d = 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
911 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
912 | if (d == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
913 | // is contained, so skip it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
914 | cxIteratorNext(min_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
915 | } else if (d < 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
916 | // subtrahend is smaller than minuend, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
917 | // check the next element |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
918 | cxIteratorNext(sub_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
919 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
920 | // subtrahend is larger than the dst element, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
921 | // clone the minuend and advance |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
922 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
923 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
924 | void* dst_ptr = clone_func(target, min_elem, clone_allocator, data); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
925 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
926 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
927 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
928 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
929 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
930 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
931 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
932 | cxIteratorNext(min_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
933 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
934 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
935 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
936 | // if dst was empty, it is now guaranteed to be sorted |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
937 | dst->collection.sorted = dst_was_empty; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
938 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
939 | CxIterator min_iter = cxListIterator(minuend); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
940 | cx_foreach(void *, elem, min_iter) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
941 | if (cxListContains(subtrahend, elem)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
942 | continue; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
943 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
944 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
945 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
946 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
947 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
948 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
949 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
950 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
951 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
952 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
953 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
954 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
955 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
956 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
957 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
958 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
959 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
960 | int cxListIntersection(CxList *dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
961 | const CxList *src, const CxList *other, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
962 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
963 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
964 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
965 | // optimize for sorted collections |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
966 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
967 | bool dst_was_empty = cxCollectionSize(dst) == 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
968 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
969 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
970 | CxIterator other_iter = cxListIterator(other); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
971 | while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
972 | void *src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
973 | void *other_elem = cxIteratorCurrent(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
974 | int d = src->collection.cmpfunc(src_elem, other_elem); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
975 | if (d == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
976 | // is contained, clone it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
977 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
978 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
979 | void* dst_ptr = clone_func(target, src_elem, clone_allocator, data); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
980 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
981 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
982 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
983 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
984 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
985 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
986 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
987 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
988 | } else if (d < 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
989 | // the other element is larger, skip the source element |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
990 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
991 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
992 | // the source element is larger, try to find it in the other list |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
993 | cxIteratorNext(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
994 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
995 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
996 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
997 | // if dst was empty, it is now guaranteed to be sorted |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
998 | dst->collection.sorted = dst_was_empty; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
999 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1000 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1001 | cx_foreach(void *, elem, src_iter) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1002 | if (!cxListContains(other, elem)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1003 | continue; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1004 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1005 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1006 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1007 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1008 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1009 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1010 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1011 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1012 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1013 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1014 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1015 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1016 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1017 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1018 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1019 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1020 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1021 | int cxListUnion(CxList *dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1022 | const CxList *src, const CxList *other, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1023 | cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1024 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1025 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1026 | // optimize for sorted collections |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1027 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1028 | bool dst_was_empty = cxCollectionSize(dst) == 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1029 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1030 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1031 | CxIterator other_iter = cxListIterator(other); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1032 | while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1033 | void *src_elem = NULL, *other_elem = NULL; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1034 | int d; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1035 | if (!cxIteratorValid(src_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1036 | other_elem = cxIteratorCurrent(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1037 | d = 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1038 | } else if (!cxIteratorValid(other_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1039 | src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1040 | d = -1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1041 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1042 | src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1043 | other_elem = cxIteratorCurrent(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1044 | d = src->collection.cmpfunc(src_elem, other_elem); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1045 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1046 | void *clone_from; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1047 | if (d < 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1048 | // source element is smaller clone it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1049 | clone_from = src_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1050 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1051 | } else if (d == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1052 | // both elements are equal, clone from the source, skip other |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1053 | clone_from = src_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1054 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1055 | cxIteratorNext(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1056 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1057 | // the other element is smaller, clone it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1058 | clone_from = other_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1059 | cxIteratorNext(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1060 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1061 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1062 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1063 | void* dst_ptr = clone_func(target, clone_from, clone_allocator, data); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1064 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1065 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1066 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1067 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1068 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1069 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1070 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1071 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1072 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1073 | // if dst was empty, it is now guaranteed to be sorted |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1074 | dst->collection.sorted = dst_was_empty; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1075 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1076 | if (cxListClone(dst, src, clone_func, clone_allocator, data)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1077 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1078 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1079 | CxIterator other_iter = cxListIterator(other); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1080 | cx_foreach(void *, elem, other_iter) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1081 | if (cxListContains(src, elem)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1082 | continue; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1083 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1084 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1085 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1086 | void* dst_ptr = clone_func(target, elem, clone_allocator, data); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1087 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1088 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1089 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1090 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1091 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1092 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1093 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1094 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1095 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1096 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1097 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1098 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1099 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1100 | int cxListCloneSimple(CxList *dst, const CxList *src) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1101 | return cxListClone(dst, src, use_simple_clone_func(src)); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1102 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1103 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1104 | int cxListDifferenceSimple(CxList *dst, const CxList *minuend, const CxList *subtrahend) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1105 | return cxListDifference(dst, minuend, subtrahend, use_simple_clone_func(minuend)); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1106 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1107 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1108 | int cxListIntersectionSimple(CxList *dst, const CxList *src, const CxList *other) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1109 | return cxListIntersection(dst, src, other, use_simple_clone_func(src)); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1110 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1111 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1112 | int cxListUnionSimple(CxList *dst, const CxList *src, const CxList *other) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1113 | return cxListUnion(dst, src, other, use_simple_clone_func(src)); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1114 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1115 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1116 | int cxListReserve(CxList *list, size_t capacity) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1117 | if (list->cl->change_capacity == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1118 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1119 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1120 | if (capacity <= cxCollectionSize(list)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1121 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1122 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1123 | return list->cl->change_capacity(list, capacity); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1124 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1125 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1126 | int cxListShrink(CxList *list) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1127 | if (list->cl->change_capacity == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1128 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1129 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1130 | return list->cl->change_capacity(list, cxCollectionSize(list)); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1131 | } |