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