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