Thu, 18 Dec 2025 17:50:15 +0100
update ucx
| 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 | |
| 1016 | 34 | int cx_list_compare_wrapper(const void *l, const void *r, void *c) { |
| 35 | CxList *list = c; | |
| 36 | const void *left; | |
| 37 | const void *right; | |
| 38 | if (cxCollectionStoresPointers(list)) { | |
| 39 | left = *(void**)l; | |
| 40 | right = *(void**)r; | |
| 41 | // for historic reasons, we are handling the NULL case here | |
| 42 | // because every UCX compare function does not support NULL arguments | |
| 43 | if (left == NULL) { | |
| 44 | if (right == NULL) return 0; | |
| 45 | return -1; | |
| 46 | } else if (right == NULL) { | |
| 47 | return 1; | |
| 48 | } | |
| 49 | } else { | |
| 50 | left = l; | |
| 51 | right = r; | |
| 845 | 52 | } |
| 1016 | 53 | return cx_invoke_compare_func(list, left, right); |
| 174 | 54 | } |
| 55 | ||
| 1016 | 56 | #define cx_list_compare_wrapper(l, r, c) cx_list_compare_wrapper(l, r, (void*)c) |
| 174 | 57 | |
| 58 | // <editor-fold desc="empty list implementation"> | |
| 59 | ||
| 440 | 60 | static void cx_emptyl_noop(cx_attr_unused CxList *list) { |
| 174 | 61 | // this is a noop, but MUST be implemented |
| 62 | } | |
| 63 | ||
| 64 | static void *cx_emptyl_at( | |
| 440 | 65 | cx_attr_unused const struct cx_list_s *list, |
| 66 | cx_attr_unused size_t index | |
| 174 | 67 | ) { |
| 68 | return NULL; | |
| 69 | } | |
| 70 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
71 | static size_t cx_emptyl_find_remove( |
| 440 | 72 | cx_attr_unused struct cx_list_s *list, |
| 73 | cx_attr_unused const void *elem, | |
| 74 | cx_attr_unused bool remove | |
| 174 | 75 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
76 | return 0; |
| 174 | 77 | } |
| 78 | ||
| 440 | 79 | static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) { |
| 174 | 80 | return false; |
| 81 | } | |
| 82 | ||
| 83 | static CxIterator cx_emptyl_iterator( | |
| 324 | 84 | const struct cx_list_s *list, |
| 174 | 85 | size_t index, |
| 440 | 86 | cx_attr_unused bool backwards |
| 174 | 87 | ) { |
| 88 | CxIterator iter = {0}; | |
| 870 | 89 | iter.src_handle = (void*) list; |
| 174 | 90 | iter.index = index; |
| 91 | iter.base.valid = cx_emptyl_iter_valid; | |
| 92 | return iter; | |
| 93 | } | |
| 94 | ||
| 95 | static cx_list_class cx_empty_list_class = { | |
| 96 | cx_emptyl_noop, | |
| 97 | NULL, | |
| 98 | NULL, | |
| 99 | NULL, | |
| 100 | NULL, | |
| 324 | 101 | NULL, |
| 845 | 102 | NULL, |
| 174 | 103 | cx_emptyl_noop, |
| 104 | NULL, | |
| 105 | cx_emptyl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
106 | cx_emptyl_find_remove, |
| 174 | 107 | cx_emptyl_noop, |
| 324 | 108 | NULL, |
| 174 | 109 | cx_emptyl_noop, |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
110 | NULL, |
| 174 | 111 | cx_emptyl_iterator, |
| 112 | }; | |
| 113 | ||
| 114 | 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
|
115 | { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
116 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
117 | 0, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
118 | 0, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
119 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
120 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
121 | NULL, |
| 1016 | 122 | NULL, |
| 123 | NULL, | |
| 124 | NULL, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
125 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
126 | true, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
127 | }, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
128 | &cx_empty_list_class, |
| 174 | 129 | }; |
| 130 | ||
| 131 | CxList *const cxEmptyList = &cx_empty_list; | |
| 132 | ||
| 133 | // </editor-fold> | |
| 134 | ||
| 324 | 135 | size_t cx_list_default_insert_array( |
| 136 | struct cx_list_s *list, | |
| 137 | size_t index, | |
| 138 | const void *data, | |
| 139 | size_t n | |
| 140 | ) { | |
| 141 | const char *src = data; | |
| 142 | size_t i = 0; | |
| 143 | for (; i < n; i++) { | |
| 1016 | 144 | if (NULL == list->cl->insert_element(list, index + i, src) |
| 845 | 145 | ) { |
| 146 | return i; // LCOV_EXCL_LINE | |
| 147 | } | |
| 870 | 148 | if (src != NULL) { |
| 149 | src += list->collection.elem_size; | |
| 150 | } | |
| 324 | 151 | } |
| 152 | return i; | |
| 153 | } | |
| 154 | ||
| 845 | 155 | static size_t cx_list_default_insert_sorted_impl( |
| 324 | 156 | struct cx_list_s *list, |
| 157 | const void *sorted_data, | |
| 845 | 158 | size_t n, |
| 159 | bool allow_duplicates | |
| 324 | 160 | ) { |
| 161 | // corner case | |
| 162 | if (n == 0) return 0; | |
| 163 | ||
| 164 | size_t elem_size = list->collection.elem_size; | |
| 165 | const char *src = sorted_data; | |
| 166 | ||
| 167 | // track indices and number of inserted items | |
| 845 | 168 | size_t di = 0, si = 0, processed = 0; |
| 324 | 169 | |
| 170 | // search the list for insertion points | |
| 845 | 171 | while (di < list->collection.size) { |
| 1016 | 172 | const void *list_elm = list->cl->at(list, di); |
| 324 | 173 | |
| 845 | 174 | // compare the current list element with the first source element |
| 175 | // if less, skip the list elements | |
| 176 | // if equal, skip the list elements and optionally the source elements | |
| 177 | { | |
| 1016 | 178 | int d = cx_list_compare_wrapper(list_elm, src, list); |
| 845 | 179 | if (d <= 0) { |
| 180 | if (!allow_duplicates && d == 0) { | |
| 181 | src += elem_size; | |
| 182 | si++; | |
| 183 | processed++; // we also count duplicates for the return value | |
| 1016 | 184 | while (si < n && cx_list_compare_wrapper(list_elm, src, list) == 0) { |
| 845 | 185 | src += elem_size; |
| 186 | si++; | |
| 187 | processed++; | |
| 188 | } | |
| 189 | if (processed == n) { | |
| 190 | return processed; | |
| 191 | } | |
| 192 | } | |
| 193 | di++; | |
| 194 | continue; | |
| 195 | } | |
| 324 | 196 | } |
| 197 | ||
| 845 | 198 | // determine the number of consecutive elements that can be inserted |
| 199 | size_t ins = 1, skip = 0; | |
| 324 | 200 | const char *next = src; |
| 201 | while (++si < n) { | |
| 845 | 202 | if (!allow_duplicates) { |
| 203 | // skip duplicates within the source | |
| 1016 | 204 | if (cx_list_compare_wrapper(next, next + elem_size, list) == 0) { |
| 845 | 205 | next += elem_size; |
| 206 | skip++; | |
| 207 | continue; | |
| 208 | } else { | |
| 209 | if (skip > 0) { | |
| 210 | // if we had to skip something, we must wait for the next run | |
| 211 | next += elem_size; | |
| 212 | break; | |
| 213 | } | |
| 214 | } | |
| 215 | } | |
| 324 | 216 | next += elem_size; |
| 217 | // once we become larger than the list elem, break | |
| 1016 | 218 | if (cx_list_compare_wrapper(list_elm, next, list) <= 0) { |
| 324 | 219 | break; |
| 220 | } | |
| 221 | // otherwise, we can insert one more | |
| 222 | ins++; | |
| 223 | } | |
| 224 | ||
| 225 | // insert the elements at location si | |
| 226 | if (ins == 1) { | |
| 1016 | 227 | if (NULL == list->cl->insert_element(list, di, src)) { |
| 845 | 228 | return processed; // LCOV_EXCL_LINE |
| 229 | } | |
| 324 | 230 | } else { |
| 1016 | 231 | size_t r = list->cl->insert_array(list, di, src, ins); |
| 845 | 232 | if (r < ins) { |
| 233 | return processed + r; // LCOV_EXCL_LINE | |
| 234 | } | |
| 324 | 235 | } |
| 845 | 236 | processed += ins + skip; |
| 324 | 237 | di += ins; |
| 238 | ||
| 239 | // everything inserted? | |
| 845 | 240 | if (processed == n) { |
| 241 | return processed; | |
| 242 | } | |
| 324 | 243 | src = next; |
| 244 | } | |
| 245 | ||
| 246 | // insert remaining items | |
| 247 | if (si < n) { | |
| 845 | 248 | if (allow_duplicates) { |
| 1016 | 249 | processed += list->cl->insert_array(list, di, src, n - si); |
| 845 | 250 | } else { |
| 1016 | 251 | const void *last = di == 0 ? NULL : list->cl->at(list, di - 1); |
| 845 | 252 | for (; si < n; si++) { |
| 253 | // skip duplicates within the source | |
| 1016 | 254 | if (last == NULL || cx_list_compare_wrapper(last, src, list) != 0) { |
| 255 | if (NULL == list->cl->insert_element(list, di, src)) { | |
| 845 | 256 | return processed; // LCOV_EXCL_LINE |
| 257 | } | |
| 258 | last = src; | |
| 259 | di++; | |
| 260 | } | |
| 261 | processed++; | |
| 262 | src += elem_size; | |
| 263 | } | |
| 264 | } | |
| 324 | 265 | } |
| 266 | ||
| 845 | 267 | return processed; |
| 268 | } | |
| 269 | ||
| 270 | size_t cx_list_default_insert_sorted( | |
| 271 | struct cx_list_s *list, | |
| 272 | const void *sorted_data, | |
| 273 | size_t n | |
| 274 | ) { | |
| 275 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, true); | |
| 276 | } | |
| 277 | ||
| 278 | size_t cx_list_default_insert_unique( | |
| 279 | struct cx_list_s *list, | |
| 280 | const void *sorted_data, | |
| 281 | size_t n | |
| 282 | ) { | |
| 283 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, false); | |
| 324 | 284 | } |
| 285 | ||
| 1016 | 286 | // TODO: remove this hack once we have a solution for qsort() / qsort_s() |
| 287 | static _Thread_local CxList *cx_hack_for_qsort_list; | |
| 288 | static int cx_hack_cmp_for_qsort(const void *l, const void *r) { | |
| 289 | return cx_list_compare_wrapper(l, r, cx_hack_for_qsort_list); | |
| 290 | } | |
| 291 | ||
| 324 | 292 | void cx_list_default_sort(struct cx_list_s *list) { |
| 293 | size_t elem_size = list->collection.elem_size; | |
| 294 | size_t list_size = list->collection.size; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
295 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 845 | 296 | if (tmp == NULL) abort(); // LCOV_EXCL_LINE |
| 324 | 297 | |
| 298 | // copy elements from source array | |
| 299 | char *loc = tmp; | |
| 300 | for (size_t i = 0; i < list_size; i++) { | |
| 1016 | 301 | void *src = list->cl->at(list, i); |
| 324 | 302 | memcpy(loc, src, elem_size); |
| 303 | loc += elem_size; | |
| 304 | } | |
| 305 | ||
| 306 | // qsort | |
| 1016 | 307 | // TODO: qsort_s() is not as nice as we thought |
| 308 | cx_hack_for_qsort_list = list; | |
| 309 | qsort(tmp, list_size, elem_size, cx_hack_cmp_for_qsort); | |
| 324 | 310 | |
| 311 | // copy elements back | |
| 312 | loc = tmp; | |
| 313 | for (size_t i = 0; i < list_size; i++) { | |
| 1016 | 314 | void *dest = list->cl->at(list, i); |
| 324 | 315 | memcpy(dest, loc, elem_size); |
| 316 | loc += elem_size; | |
| 317 | } | |
| 318 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
319 | cxFreeDefault(tmp); |
| 324 | 320 | } |
| 321 | ||
| 322 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
| 323 | if (i == j) return 0; | |
| 324 | if (i >= list->collection.size) return 1; | |
| 325 | if (j >= list->collection.size) return 1; | |
| 326 | ||
| 327 | size_t elem_size = list->collection.elem_size; | |
| 328 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
329 | void *tmp = cxMallocDefault(elem_size); |
| 845 | 330 | if (tmp == NULL) return 1; // LCOV_EXCL_LINE |
| 324 | 331 | |
| 1016 | 332 | void *ip = list->cl->at(list, i); |
| 333 | void *jp = list->cl->at(list, j); | |
| 324 | 334 | |
| 335 | memcpy(tmp, ip, elem_size); | |
| 336 | memcpy(ip, jp, elem_size); | |
| 337 | memcpy(jp, tmp, elem_size); | |
| 338 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
339 | cxFreeDefault(tmp); |
| 324 | 340 | |
| 341 | return 0; | |
| 342 | } | |
| 343 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
344 | 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
|
345 | 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
|
346 | 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
|
347 | 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
|
348 | 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
|
349 | ) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
350 | 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
|
351 | list->collection.allocator = allocator; |
| 1016 | 352 | list->collection.size = 0; |
| 353 | list->collection.sorted = false; // should be set by the implementation | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
354 | 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
|
355 | list->collection.elem_size = elem_size; |
| 1016 | 356 | list->collection.simple_cmp = NULL; |
| 357 | list->collection.advanced_cmp = cx_acmp_memcmp; | |
| 358 | list->collection.cmp_data = &list->collection.elem_size; | |
| 359 | list->collection.store_pointer = false; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
360 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
361 | list->collection.elem_size = sizeof(void *); |
| 1016 | 362 | list->collection.simple_cmp = cx_cmp_ptr; |
| 363 | list->collection.advanced_cmp = NULL; | |
| 364 | list->collection.cmp_data = NULL; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
365 | 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
|
366 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
367 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
368 | |
| 174 | 369 | int cxListCompare( |
| 324 | 370 | const CxList *list, |
| 371 | const CxList *other | |
| 174 | 372 | ) { |
| 1016 | 373 | // check if we cannot use the list internal function |
| 324 | 374 | bool cannot_optimize = false; |
| 375 | ||
| 376 | // if one is storing pointers but the other is not | |
| 377 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
| 378 | ||
| 1016 | 379 | // check if the lists are incompatible or this list does not implement compare |
| 380 | cx_compare_func list_cmp = (cx_compare_func) list->cl->compare; | |
| 381 | cx_compare_func other_cmp = (cx_compare_func) other->cl->compare; | |
| 382 | cannot_optimize |= list_cmp != other_cmp; | |
| 383 | cannot_optimize |= list_cmp == NULL; | |
| 174 | 384 | |
| 324 | 385 | if (cannot_optimize) { |
| 174 | 386 | // lists are definitely different - cannot use internal compare function |
| 324 | 387 | if (list->collection.size == other->collection.size) { |
| 1016 | 388 | CxIterator left = cxListIterator(list); |
| 389 | CxIterator right = cxListIterator(other); | |
| 324 | 390 | for (size_t i = 0; i < list->collection.size; i++) { |
| 174 | 391 | void *leftValue = cxIteratorCurrent(left); |
| 392 | void *rightValue = cxIteratorCurrent(right); | |
| 1016 | 393 | // values are already unwrapped, invoke immediately |
| 394 | int d = cx_invoke_compare_func(list, leftValue, rightValue); | |
| 174 | 395 | if (d != 0) { |
| 396 | return d; | |
| 397 | } | |
| 398 | cxIteratorNext(left); | |
| 399 | cxIteratorNext(right); | |
| 400 | } | |
| 401 | return 0; | |
| 402 | } else { | |
| 324 | 403 | return list->collection.size < other->collection.size ? -1 : 1; |
| 174 | 404 | } |
| 405 | } else { | |
| 406 | // lists are compatible | |
| 407 | return list->cl->compare(list, other); | |
| 408 | } | |
| 409 | } | |
| 410 | ||
| 870 | 411 | size_t cxListSize(const CxList *list) { |
| 412 | return list->collection.size; | |
| 413 | } | |
| 414 | ||
| 415 | int cxListAdd(CxList *list, const void *elem) { | |
| 416 | list->collection.sorted = false; | |
| 1016 | 417 | return list->cl->insert_element(list, list->collection.size, cx_ref(list, elem)) == NULL; |
| 870 | 418 | } |
| 419 | ||
| 420 | size_t cxListAddArray(CxList *list, const void *array, size_t n) { | |
| 421 | list->collection.sorted = false; | |
| 422 | return list->cl->insert_array(list, list->collection.size, array, n); | |
| 423 | } | |
| 424 | ||
| 425 | int cxListInsert(CxList *list, size_t index, const void *elem) { | |
| 426 | list->collection.sorted = false; | |
| 1016 | 427 | return list->cl->insert_element(list, index, cx_ref(list, elem)) == NULL; |
| 870 | 428 | } |
| 429 | ||
| 430 | void *cxListEmplaceAt(CxList *list, size_t index) { | |
| 431 | list->collection.sorted = false; | |
| 432 | return list->cl->insert_element(list, index, NULL); | |
| 433 | } | |
| 434 | ||
| 435 | void *cxListEmplace(CxList *list) { | |
| 436 | list->collection.sorted = false; | |
| 437 | return list->cl->insert_element(list, list->collection.size, NULL); | |
| 438 | } | |
| 439 | ||
| 440 | static bool cx_list_emplace_iterator_valid(const void *it) { | |
| 441 | const CxIterator *iter = it; | |
| 442 | return iter->index < iter->elem_count; | |
| 443 | } | |
| 444 | ||
| 445 | CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) { | |
| 446 | list->collection.sorted = false; | |
| 447 | size_t c = list->cl->insert_array(list, index, NULL, n); | |
| 448 | CxIterator iter = list->cl->iterator(list, index, false); | |
| 449 | // tweak the fields of this iterator | |
| 450 | iter.elem_count = c; | |
| 451 | iter.index = 0; | |
| 452 | // replace the valid function to abort iteration when c is reached | |
| 453 | iter.base.valid = cx_list_emplace_iterator_valid; | |
| 454 | return iter; | |
| 455 | } | |
| 456 | ||
| 457 | CxIterator cxListEmplaceArray(CxList *list, size_t n) { | |
| 458 | return cxListEmplaceArrayAt(list, list->collection.size, n); | |
| 459 | } | |
| 460 | ||
| 461 | int cxListInsertSorted(CxList *list, const void *elem) { | |
| 462 | assert(cxCollectionSorted(list)); | |
| 463 | list->collection.sorted = true; | |
| 1016 | 464 | return list->cl->insert_sorted(list, cx_ref(list, elem), 1) == 0; |
| 870 | 465 | } |
| 466 | ||
| 467 | int cxListInsertUnique(CxList *list, const void *elem) { | |
| 468 | if (cxCollectionSorted(list)) { | |
| 469 | list->collection.sorted = true; | |
| 1016 | 470 | return list->cl->insert_unique(list, cx_ref(list, elem), 1) == 0; |
| 870 | 471 | } else { |
| 472 | if (cxListContains(list, elem)) { | |
| 473 | return 0; | |
| 474 | } else { | |
| 475 | return cxListAdd(list, elem); | |
| 476 | } | |
| 477 | } | |
| 478 | } | |
| 479 | ||
| 480 | size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) { | |
| 481 | list->collection.sorted = false; | |
| 482 | return list->cl->insert_array(list, index, array, n); | |
| 174 | 483 | } |
| 484 | ||
| 870 | 485 | size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) { |
| 486 | assert(cxCollectionSorted(list)); | |
| 487 | list->collection.sorted = true; | |
| 488 | return list->cl->insert_sorted(list, array, n); | |
| 489 | } | |
| 490 | ||
| 491 | size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) { | |
| 492 | if (cxCollectionSorted(list)) { | |
| 493 | list->collection.sorted = true; | |
| 494 | return list->cl->insert_unique(list, array, n); | |
| 495 | } else { | |
| 496 | const char *source = array; | |
| 497 | for (size_t i = 0 ; i < n; i++) { | |
| 498 | // note: this also checks elements added in a previous iteration | |
| 1016 | 499 | const void *data = cx_deref(list, source); |
| 870 | 500 | if (!cxListContains(list, data)) { |
| 501 | if (cxListAdd(list, data)) { | |
| 502 | return i; // LCOV_EXCL_LINE | |
| 503 | } | |
| 504 | } | |
| 505 | source += list->collection.elem_size; | |
| 506 | } | |
| 507 | return n; | |
| 508 | } | |
| 509 | } | |
| 510 | ||
| 511 | int cxListInsertAfter(CxIterator *iter, const void *elem) { | |
| 1016 | 512 | CxList* list = iter->src_handle; |
| 870 | 513 | list->collection.sorted = false; |
| 1016 | 514 | return list->cl->insert_iter(iter, cx_ref(list, elem), 0); |
| 870 | 515 | } |
| 516 | ||
| 517 | int cxListInsertBefore(CxIterator *iter, const void *elem) { | |
| 1016 | 518 | CxList* list = iter->src_handle; |
| 870 | 519 | list->collection.sorted = false; |
| 1016 | 520 | return list->cl->insert_iter(iter, cx_ref(list, elem), 1); |
| 870 | 521 | } |
| 522 | ||
| 523 | int cxListRemove(CxList *list, size_t index) { | |
| 524 | return list->cl->remove(list, index, 1, NULL) == 0; | |
| 174 | 525 | } |
| 440 | 526 | |
| 870 | 527 | int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) { |
| 528 | return list->cl->remove(list, index, 1, targetbuf) == 0; | |
| 529 | } | |
| 530 | ||
| 531 | int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) { | |
| 532 | return list->cl->remove(list, 0, 1, targetbuf) == 0; | |
| 533 | } | |
| 534 | ||
| 535 | int cxListRemoveAndGetLast(CxList *list, void *targetbuf) { | |
| 536 | // note: index may wrap - member function will catch that | |
| 537 | return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; | |
| 538 | } | |
| 539 | ||
| 540 | size_t cxListRemoveArray(CxList *list, size_t index, size_t num) { | |
| 541 | return list->cl->remove(list, index, num, NULL); | |
| 542 | } | |
| 543 | ||
| 544 | size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) { | |
| 545 | return list->cl->remove(list, index, num, targetbuf); | |
| 440 | 546 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
547 | |
| 870 | 548 | void cxListClear(CxList *list) { |
| 549 | list->cl->clear(list); | |
| 550 | list->collection.sorted = true; // empty lists are always sorted | |
| 551 | } | |
| 552 | ||
| 553 | int cxListSwap(CxList *list, size_t i, size_t j) { | |
| 554 | list->collection.sorted = false; | |
| 555 | return list->cl->swap(list, i, j); | |
| 556 | } | |
| 557 | ||
| 558 | void *cxListAt(const CxList *list, size_t index) { | |
| 1016 | 559 | void *result = list->cl->at(list, index); |
| 560 | if (result == NULL) return NULL; | |
| 561 | return cx_deref(list, result); | |
| 870 | 562 | } |
| 563 | ||
| 564 | void *cxListFirst(const CxList *list) { | |
| 1016 | 565 | return cxListAt(list, 0); |
| 870 | 566 | } |
| 567 | ||
| 568 | void *cxListLast(const CxList *list) { | |
| 1016 | 569 | return cxListAt(list, list->collection.size - 1); |
| 870 | 570 | } |
| 571 | ||
| 572 | 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
|
573 | if (index >= list->collection.size) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
574 | return 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
575 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
576 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
577 | if (list->collection.store_pointer) { |
| 1016 | 578 | void **target = list->cl->at(list, index); |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
579 | *target = (void *)elem; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
580 | } else { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
581 | void *target = list->cl->at(list, index); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
582 | memcpy(target, elem, list->collection.elem_size); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
583 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
584 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
585 | return 0; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
586 | } |
| 870 | 587 | |
| 1016 | 588 | static void *cx_pl_iter_current(const void *it) { |
| 589 | const struct cx_iterator_s *iter = it; | |
| 590 | void **ptr = iter->base.current_impl(it); | |
| 591 | return ptr == NULL ? NULL : *ptr; | |
| 592 | } | |
| 593 | ||
| 594 | CX_INLINE CxIterator cx_pl_iter_wrap(const CxList *list, CxIterator iter) { | |
| 595 | if (cxCollectionStoresPointers(list)) { | |
| 596 | iter.base.current_impl = iter.base.current; | |
| 597 | iter.base.current = cx_pl_iter_current; | |
| 598 | return iter; | |
| 599 | } else { | |
| 600 | return iter; | |
| 601 | } | |
| 602 | } | |
| 603 | ||
| 870 | 604 | CxIterator cxListIteratorAt(const CxList *list, size_t index) { |
| 605 | if (list == NULL) list = cxEmptyList; | |
| 1016 | 606 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, false)); |
| 870 | 607 | } |
| 608 | ||
| 609 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { | |
| 610 | if (list == NULL) list = cxEmptyList; | |
| 1016 | 611 | return cx_pl_iter_wrap(list, list->cl->iterator(list, index, true)); |
| 870 | 612 | } |
| 613 | ||
| 614 | CxIterator cxListIterator(const CxList *list) { | |
| 615 | if (list == NULL) list = cxEmptyList; | |
| 1016 | 616 | return cx_pl_iter_wrap(list, list->cl->iterator(list, 0, false)); |
| 870 | 617 | } |
| 618 | ||
| 619 | CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 620 | if (list == NULL) list = cxEmptyList; | |
| 1016 | 621 | return cx_pl_iter_wrap(list, list->cl->iterator(list, list->collection.size - 1, true)); |
| 870 | 622 | } |
| 623 | ||
| 624 | size_t cxListFind(const CxList *list, const void *elem) { | |
| 1016 | 625 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false); |
| 870 | 626 | } |
| 627 | ||
| 628 | bool cxListContains(const CxList* list, const void* elem) { | |
| 1016 | 629 | return list->cl->find_remove((CxList*)list, cx_ref(list, elem), false) < list->collection.size; |
| 870 | 630 | } |
| 631 | ||
| 632 | bool cxListIndexValid(const CxList *list, size_t index) { | |
| 633 | return index < list->collection.size; | |
| 634 | } | |
| 635 | ||
| 636 | size_t cxListFindRemove(CxList *list, const void *elem) { | |
| 1016 | 637 | return list->cl->find_remove(list, cx_ref(list, elem), true); |
| 870 | 638 | } |
| 639 | ||
| 640 | void cxListSort(CxList *list) { | |
| 641 | if (list->collection.sorted) return; | |
| 642 | list->cl->sort(list); | |
| 643 | list->collection.sorted = true; | |
| 644 | } | |
| 645 | ||
| 646 | void cxListReverse(CxList *list) { | |
| 647 | // still sorted, but not according to the cmp_func | |
| 648 | list->collection.sorted = false; | |
| 649 | list->cl->reverse(list); | |
| 650 | } | |
| 651 | ||
| 652 | void cxListFree(CxList *list) { | |
| 653 | if (list == NULL) return; | |
| 654 | list->cl->deallocate(list); | |
| 655 | } | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
656 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
657 | 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
|
658 | 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
|
659 | 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
|
660 | list->collection.simple_destructor = NULL; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
661 | list->collection.advanced_destructor = NULL; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
662 | if (n == 1) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
663 | cxListRemove(list, list->collection.size - 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
664 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
665 | cxListRemoveArray(list,list->collection.size - n, n); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
666 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
667 | list->collection.simple_destructor = destr_bak; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
668 | list->collection.advanced_destructor = destr2_bak; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
669 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
670 | |
| 1016 | 671 | static void* cx_list_shallow_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
672 | size_t elem_size = *(size_t*)data; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
673 | 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
|
674 | 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
|
675 | return dst; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
676 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
677 | |
| 1016 | 678 | #define use_shallow_clone_func(list) cx_list_shallow_clone_func, NULL, (void*)&((list)->collection.elem_size) |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
679 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
680 | 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
|
681 | const CxAllocator *clone_allocator, void *data) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
682 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
683 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
684 | // remember the original size |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
685 | size_t orig_size = dst->collection.size; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
686 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
687 | // 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
|
688 | 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
|
689 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
690 | // get an iterator over the source elements |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
691 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
692 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
693 | // now clone the elements |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
694 | size_t cloned = empl_iter.elem_count; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
695 | 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
|
696 | void *src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
697 | void **dest_memory = cxIteratorCurrent(empl_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
698 | void *target = cxCollectionStoresPointers(dst) ? NULL : dest_memory; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
699 | 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
|
700 | if (dest_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
701 | cloned = i; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
702 | break; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
703 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
704 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
705 | *dest_memory = dest_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
706 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
707 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
708 | cxIteratorNext(empl_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
709 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
710 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
711 | // 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
|
712 | // (disable the destructors!) |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
713 | if (cloned < src->collection.size) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
714 | cx_list_pop_uninitialized_elements(dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
715 | dst->collection.size - cloned - orig_size); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
716 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
717 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
718 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
719 | // 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
|
720 | if (orig_size == 0 && src->collection.sorted) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
721 | dst->collection.sorted = true; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
722 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
723 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
724 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
725 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
726 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
727 | int cxListDifference(CxList *dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
728 | const CxList *minuend, const CxList *subtrahend, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
729 | 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
|
730 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
731 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
732 | // optimize for sorted collections |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
733 | if (cxCollectionSorted(minuend) && cxCollectionSorted(subtrahend)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
734 | bool dst_was_empty = cxCollectionSize(dst) == 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
735 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
736 | CxIterator min_iter = cxListIterator(minuend); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
737 | CxIterator sub_iter = cxListIterator(subtrahend); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
738 | while (cxIteratorValid(min_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
739 | void *min_elem = cxIteratorCurrent(min_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
740 | void *sub_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
741 | int d; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
742 | if (cxIteratorValid(sub_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
743 | sub_elem = cxIteratorCurrent(sub_iter); |
| 1016 | 744 | d = cx_list_compare_wrapper(sub_elem, min_elem, subtrahend); |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
745 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
746 | // no more elements in the subtrahend, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
747 | // 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
|
748 | d = 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
749 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
750 | if (d == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
751 | // is contained, so skip it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
752 | cxIteratorNext(min_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
753 | } else if (d < 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
754 | // subtrahend is smaller than minuend, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
755 | // check the next element |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
756 | cxIteratorNext(sub_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
757 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
758 | // subtrahend is larger than the dst element, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
759 | // clone the minuend and advance |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
760 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
761 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
762 | 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
|
763 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
764 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
765 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
766 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
767 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
768 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
769 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
770 | cxIteratorNext(min_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
771 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
772 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
773 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
774 | // 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
|
775 | dst->collection.sorted = dst_was_empty; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
776 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
777 | CxIterator min_iter = cxListIterator(minuend); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
778 | cx_foreach(void *, elem, min_iter) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
779 | if (cxListContains(subtrahend, elem)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
780 | continue; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
781 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
782 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
783 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
784 | 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
|
785 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
786 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
787 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
788 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
789 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
790 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
791 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
792 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
793 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
794 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
795 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
796 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
797 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
798 | int cxListIntersection(CxList *dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
799 | const CxList *src, const CxList *other, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
800 | 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
|
801 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
802 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
803 | // optimize for sorted collections |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
804 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
805 | bool dst_was_empty = cxCollectionSize(dst) == 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
806 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
807 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
808 | CxIterator other_iter = cxListIterator(other); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
809 | while (cxIteratorValid(src_iter) && cxIteratorValid(other_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
810 | void *src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
811 | void *other_elem = cxIteratorCurrent(other_iter); |
| 1016 | 812 | int d = cx_list_compare_wrapper(src_elem, other_elem, src); |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
813 | if (d == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
814 | // is contained, clone it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
815 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
816 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
817 | 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
|
818 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
819 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
820 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
821 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
822 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
823 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
824 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
825 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
826 | } else if (d < 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
827 | // 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
|
828 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
829 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
830 | // 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
|
831 | cxIteratorNext(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
832 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
833 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
834 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
835 | // 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
|
836 | dst->collection.sorted = dst_was_empty; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
837 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
838 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
839 | cx_foreach(void *, elem, src_iter) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
840 | if (!cxListContains(other, elem)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
841 | continue; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
842 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
843 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
844 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
845 | 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
|
846 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
847 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
848 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
849 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
850 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
851 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
852 | } |
|
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 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
855 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
856 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
857 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
858 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
859 | int cxListUnion(CxList *dst, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
860 | const CxList *src, const CxList *other, |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
861 | 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
|
862 | if (clone_allocator == NULL) clone_allocator = cxDefaultAllocator; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
863 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
864 | // optimize for sorted collections |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
865 | if (cxCollectionSorted(src) && cxCollectionSorted(other)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
866 | bool dst_was_empty = cxCollectionSize(dst) == 0; |
|
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 | CxIterator src_iter = cxListIterator(src); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
869 | CxIterator other_iter = cxListIterator(other); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
870 | while (cxIteratorValid(src_iter) || cxIteratorValid(other_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
871 | void *src_elem = NULL, *other_elem = NULL; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
872 | int d; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
873 | if (!cxIteratorValid(src_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
874 | other_elem = cxIteratorCurrent(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
875 | d = 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
876 | } else if (!cxIteratorValid(other_iter)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
877 | src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
878 | d = -1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
879 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
880 | src_elem = cxIteratorCurrent(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
881 | other_elem = cxIteratorCurrent(other_iter); |
| 1016 | 882 | d = cx_list_compare_wrapper(src_elem, other_elem, src); |
|
943
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 | void *clone_from; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
885 | if (d < 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
886 | // source element is smaller clone it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
887 | clone_from = src_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
888 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
889 | } else if (d == 0) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
890 | // 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
|
891 | clone_from = src_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
892 | cxIteratorNext(src_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
893 | cxIteratorNext(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
894 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
895 | // the other element is smaller, clone it |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
896 | clone_from = other_elem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
897 | cxIteratorNext(other_iter); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
898 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
899 | void **dst_mem = cxListEmplace(dst); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
900 | void *target = cxCollectionStoresPointers(dst) ? NULL : dst_mem; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
901 | 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
|
902 | if (dst_ptr == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
903 | cx_list_pop_uninitialized_elements(dst, 1); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
904 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
905 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
906 | if (cxCollectionStoresPointers(dst)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
907 | *dst_mem = dst_ptr; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
908 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
909 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
910 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
911 | // 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
|
912 | dst->collection.sorted = dst_was_empty; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
913 | } else { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
914 | 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
|
915 | return 1; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
916 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
917 | CxIterator other_iter = cxListIterator(other); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
918 | cx_foreach(void *, elem, other_iter) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
919 | if (cxListContains(src, elem)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
920 | continue; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
921 | } |
|
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, 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 | } |
|
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 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
936 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
937 | |
| 1016 | 938 | int cxListCloneShallow(CxList *dst, const CxList *src) { |
| 939 | return cxListClone(dst, src, use_shallow_clone_func(src)); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
940 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
941 | |
| 1016 | 942 | int cxListDifferenceShallow(CxList *dst, const CxList *minuend, const CxList *subtrahend) { |
| 943 | return cxListDifference(dst, minuend, subtrahend, use_shallow_clone_func(minuend)); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
944 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
945 | |
| 1016 | 946 | int cxListIntersectionShallow(CxList *dst, const CxList *src, const CxList *other) { |
| 947 | return cxListIntersection(dst, src, other, use_shallow_clone_func(src)); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
948 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
949 | |
| 1016 | 950 | int cxListUnionShallow(CxList *dst, const CxList *src, const CxList *other) { |
| 951 | return cxListUnion(dst, src, other, use_shallow_clone_func(src)); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
952 | } |
|
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 | int cxListReserve(CxList *list, size_t capacity) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
955 | if (list->cl->change_capacity == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
956 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
957 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
958 | if (capacity <= cxCollectionSize(list)) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
959 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
960 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
961 | return list->cl->change_capacity(list, capacity); |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
962 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
963 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
964 | int cxListShrink(CxList *list) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
965 | if (list->cl->change_capacity == NULL) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
966 | return 0; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
967 | } |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
968 | 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
|
969 | } |