Tue, 21 Oct 2025 16:20:51 +0200
update ucx
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | #include "cx/list.h" | |
| 30 | ||
| 31 | #include <string.h> | |
| 870 | 32 | #include <assert.h> |
| 174 | 33 | |
| 34 | // <editor-fold desc="Store Pointers Functionality"> | |
| 35 | ||
| 36 | static _Thread_local cx_compare_func cx_pl_cmpfunc_impl; | |
| 37 | ||
| 38 | static int cx_pl_cmpfunc( | |
| 324 | 39 | const void *l, |
| 40 | const void *r | |
| 174 | 41 | ) { |
| 845 | 42 | // l and r are guaranteed to be non-NULL pointing to the list's memory |
| 174 | 43 | void *const *lptr = l; |
| 44 | void *const *rptr = r; | |
| 845 | 45 | const void *left = *lptr; |
| 46 | const void *right = *rptr; | |
| 47 | if (left == NULL) { | |
| 48 | // NULL is smaller than any value except NULL | |
| 49 | return right == NULL ? 0 : -1; | |
| 50 | } else if (right == NULL) { | |
| 51 | // any value is larger than NULL | |
| 52 | return 1; | |
| 53 | } | |
| 174 | 54 | return cx_pl_cmpfunc_impl(left, right); |
| 55 | } | |
| 56 | ||
| 324 | 57 | static void cx_pl_hack_cmpfunc(const struct cx_list_s *list) { |
| 174 | 58 | // cast away const - this is the hacky thing |
| 324 | 59 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 174 | 60 | cx_pl_cmpfunc_impl = l->cmpfunc; |
| 61 | l->cmpfunc = cx_pl_cmpfunc; | |
| 62 | } | |
| 63 | ||
| 324 | 64 | static void cx_pl_unhack_cmpfunc(const struct cx_list_s *list) { |
| 174 | 65 | // cast away const - this is the hacky thing |
| 324 | 66 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 174 | 67 | l->cmpfunc = cx_pl_cmpfunc_impl; |
| 68 | } | |
| 69 | ||
| 70 | static void cx_pl_destructor(struct cx_list_s *list) { | |
| 440 | 71 | list->climpl->deallocate(list); |
| 174 | 72 | } |
| 73 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
74 | static void *cx_pl_insert_element( |
| 174 | 75 | struct cx_list_s *list, |
| 76 | size_t index, | |
| 324 | 77 | const void *element |
| 174 | 78 | ) { |
| 79 | return list->climpl->insert_element(list, index, &element); | |
| 80 | } | |
| 81 | ||
| 82 | static size_t cx_pl_insert_array( | |
| 83 | struct cx_list_s *list, | |
| 84 | size_t index, | |
| 324 | 85 | const void *array, |
| 174 | 86 | size_t n |
| 87 | ) { | |
| 88 | return list->climpl->insert_array(list, index, array, n); | |
| 89 | } | |
| 90 | ||
| 324 | 91 | static size_t cx_pl_insert_sorted( |
| 92 | struct cx_list_s *list, | |
| 93 | const void *array, | |
| 94 | size_t n | |
| 95 | ) { | |
| 96 | cx_pl_hack_cmpfunc(list); | |
| 97 | size_t result = list->climpl->insert_sorted(list, array, n); | |
| 98 | cx_pl_unhack_cmpfunc(list); | |
| 99 | return result; | |
| 100 | } | |
| 101 | ||
| 845 | 102 | static size_t cx_pl_insert_unique( |
| 103 | struct cx_list_s *list, | |
| 104 | const void *array, | |
| 105 | size_t n | |
| 106 | ) { | |
| 107 | cx_pl_hack_cmpfunc(list); | |
| 108 | size_t result = list->climpl->insert_unique(list, array, n); | |
| 109 | cx_pl_unhack_cmpfunc(list); | |
| 110 | return result; | |
| 111 | } | |
| 112 | ||
| 174 | 113 | static int cx_pl_insert_iter( |
| 324 | 114 | struct cx_iterator_s *iter, |
| 115 | const void *elem, | |
| 174 | 116 | int prepend |
| 117 | ) { | |
| 870 | 118 | struct cx_list_s *list = iter->src_handle; |
| 174 | 119 | return list->climpl->insert_iter(iter, &elem, prepend); |
| 120 | } | |
| 121 | ||
| 440 | 122 | static size_t cx_pl_remove( |
| 174 | 123 | struct cx_list_s *list, |
| 440 | 124 | size_t index, |
| 125 | size_t num, | |
| 126 | void *targetbuf | |
| 174 | 127 | ) { |
| 440 | 128 | return list->climpl->remove(list, index, num, targetbuf); |
| 174 | 129 | } |
| 130 | ||
| 131 | static void cx_pl_clear(struct cx_list_s *list) { | |
| 132 | list->climpl->clear(list); | |
| 133 | } | |
| 134 | ||
| 135 | static int cx_pl_swap( | |
| 136 | struct cx_list_s *list, | |
| 137 | size_t i, | |
| 138 | size_t j | |
| 139 | ) { | |
| 140 | return list->climpl->swap(list, i, j); | |
| 141 | } | |
| 142 | ||
| 143 | static void *cx_pl_at( | |
| 324 | 144 | const struct cx_list_s *list, |
| 174 | 145 | size_t index |
| 146 | ) { | |
| 147 | void **ptr = list->climpl->at(list, index); | |
| 148 | return ptr == NULL ? NULL : *ptr; | |
| 149 | } | |
| 150 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
151 | static size_t cx_pl_find_remove( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
152 | struct cx_list_s *list, |
| 324 | 153 | const void *elem, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
154 | bool remove |
| 174 | 155 | ) { |
| 156 | cx_pl_hack_cmpfunc(list); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
157 | size_t ret = list->climpl->find_remove(list, &elem, remove); |
| 174 | 158 | cx_pl_unhack_cmpfunc(list); |
| 159 | return ret; | |
| 160 | } | |
| 161 | ||
| 162 | static void cx_pl_sort(struct cx_list_s *list) { | |
| 163 | cx_pl_hack_cmpfunc(list); | |
| 164 | list->climpl->sort(list); | |
| 165 | cx_pl_unhack_cmpfunc(list); | |
| 166 | } | |
| 167 | ||
| 168 | static int cx_pl_compare( | |
| 324 | 169 | const struct cx_list_s *list, |
| 170 | const struct cx_list_s *other | |
| 174 | 171 | ) { |
| 172 | cx_pl_hack_cmpfunc(list); | |
| 173 | int ret = list->climpl->compare(list, other); | |
| 174 | cx_pl_unhack_cmpfunc(list); | |
| 175 | return ret; | |
| 176 | } | |
| 177 | ||
| 178 | static void cx_pl_reverse(struct cx_list_s *list) { | |
| 179 | list->climpl->reverse(list); | |
| 180 | } | |
| 181 | ||
| 324 | 182 | static void *cx_pl_iter_current(const void *it) { |
| 183 | const struct cx_iterator_s *iter = it; | |
| 174 | 184 | void **ptr = iter->base.current_impl(it); |
| 185 | return ptr == NULL ? NULL : *ptr; | |
| 186 | } | |
| 187 | ||
| 188 | static struct cx_iterator_s cx_pl_iterator( | |
| 324 | 189 | const struct cx_list_s *list, |
| 174 | 190 | size_t index, |
| 191 | bool backwards | |
| 192 | ) { | |
| 193 | struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); | |
| 194 | iter.base.current_impl = iter.base.current; | |
| 195 | iter.base.current = cx_pl_iter_current; | |
| 196 | return iter; | |
| 197 | } | |
| 198 | ||
| 199 | static cx_list_class cx_pointer_list_class = { | |
| 200 | cx_pl_destructor, | |
| 201 | cx_pl_insert_element, | |
| 202 | cx_pl_insert_array, | |
| 324 | 203 | cx_pl_insert_sorted, |
| 845 | 204 | cx_pl_insert_unique, |
| 174 | 205 | cx_pl_insert_iter, |
| 206 | cx_pl_remove, | |
| 207 | cx_pl_clear, | |
| 208 | cx_pl_swap, | |
| 209 | cx_pl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
210 | cx_pl_find_remove, |
| 174 | 211 | cx_pl_sort, |
| 212 | cx_pl_compare, | |
| 213 | cx_pl_reverse, | |
| 214 | cx_pl_iterator, | |
| 215 | }; | |
| 216 | // </editor-fold> | |
| 217 | ||
| 218 | // <editor-fold desc="empty list implementation"> | |
| 219 | ||
| 440 | 220 | static void cx_emptyl_noop(cx_attr_unused CxList *list) { |
| 174 | 221 | // this is a noop, but MUST be implemented |
| 222 | } | |
| 223 | ||
| 224 | static void *cx_emptyl_at( | |
| 440 | 225 | cx_attr_unused const struct cx_list_s *list, |
| 226 | cx_attr_unused size_t index | |
| 174 | 227 | ) { |
| 228 | return NULL; | |
| 229 | } | |
| 230 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
231 | static size_t cx_emptyl_find_remove( |
| 440 | 232 | cx_attr_unused struct cx_list_s *list, |
| 233 | cx_attr_unused const void *elem, | |
| 234 | cx_attr_unused bool remove | |
| 174 | 235 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
236 | return 0; |
| 174 | 237 | } |
| 238 | ||
| 440 | 239 | static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) { |
| 174 | 240 | return false; |
| 241 | } | |
| 242 | ||
| 243 | static CxIterator cx_emptyl_iterator( | |
| 324 | 244 | const struct cx_list_s *list, |
| 174 | 245 | size_t index, |
| 440 | 246 | cx_attr_unused bool backwards |
| 174 | 247 | ) { |
| 248 | CxIterator iter = {0}; | |
| 870 | 249 | iter.src_handle = (void*) list; |
| 174 | 250 | iter.index = index; |
| 251 | iter.base.valid = cx_emptyl_iter_valid; | |
| 252 | return iter; | |
| 253 | } | |
| 254 | ||
| 255 | static cx_list_class cx_empty_list_class = { | |
| 256 | cx_emptyl_noop, | |
| 257 | NULL, | |
| 258 | NULL, | |
| 259 | NULL, | |
| 260 | NULL, | |
| 324 | 261 | NULL, |
| 845 | 262 | NULL, |
| 174 | 263 | cx_emptyl_noop, |
| 264 | NULL, | |
| 265 | cx_emptyl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
266 | cx_emptyl_find_remove, |
| 174 | 267 | cx_emptyl_noop, |
| 324 | 268 | NULL, |
| 174 | 269 | cx_emptyl_noop, |
| 270 | cx_emptyl_iterator, | |
| 271 | }; | |
| 272 | ||
| 273 | 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
|
274 | { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
275 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
276 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
277 | 0, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
278 | 0, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
279 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
280 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
281 | NULL, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
282 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
283 | true, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
284 | }, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
285 | &cx_empty_list_class, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
286 | NULL |
| 174 | 287 | }; |
| 288 | ||
| 289 | CxList *const cxEmptyList = &cx_empty_list; | |
| 290 | ||
| 291 | // </editor-fold> | |
| 292 | ||
| 324 | 293 | #define invoke_list_func(name, list, ...) \ |
| 294 | ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \ | |
| 295 | (list, __VA_ARGS__) | |
| 296 | ||
| 297 | size_t cx_list_default_insert_array( | |
| 298 | struct cx_list_s *list, | |
| 299 | size_t index, | |
| 300 | const void *data, | |
| 301 | size_t n | |
| 302 | ) { | |
| 303 | const char *src = data; | |
| 304 | size_t i = 0; | |
| 305 | for (; i < n; i++) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
306 | if (NULL == invoke_list_func( |
| 870 | 307 | insert_element, list, index + i, src) |
| 845 | 308 | ) { |
| 309 | return i; // LCOV_EXCL_LINE | |
| 310 | } | |
| 870 | 311 | if (src != NULL) { |
| 312 | src += list->collection.elem_size; | |
| 313 | } | |
| 324 | 314 | } |
| 315 | return i; | |
| 316 | } | |
| 317 | ||
| 845 | 318 | static size_t cx_list_default_insert_sorted_impl( |
| 324 | 319 | struct cx_list_s *list, |
| 320 | const void *sorted_data, | |
| 845 | 321 | size_t n, |
| 322 | bool allow_duplicates | |
| 324 | 323 | ) { |
| 324 | // corner case | |
| 325 | if (n == 0) return 0; | |
| 326 | ||
| 327 | size_t elem_size = list->collection.elem_size; | |
| 328 | cx_compare_func cmp = list->collection.cmpfunc; | |
| 329 | const char *src = sorted_data; | |
| 330 | ||
| 331 | // track indices and number of inserted items | |
| 845 | 332 | size_t di = 0, si = 0, processed = 0; |
| 324 | 333 | |
| 334 | // search the list for insertion points | |
| 845 | 335 | while (di < list->collection.size) { |
| 324 | 336 | const void *list_elm = invoke_list_func(at, list, di); |
| 337 | ||
| 845 | 338 | // compare the current list element with the first source element |
| 339 | // if less, skip the list elements | |
| 340 | // if equal, skip the list elements and optionally the source elements | |
| 341 | { | |
| 342 | int d = cmp(list_elm, src); | |
| 343 | if (d <= 0) { | |
| 344 | if (!allow_duplicates && d == 0) { | |
| 345 | src += elem_size; | |
| 346 | si++; | |
| 347 | processed++; // we also count duplicates for the return value | |
| 348 | while (si < n && cmp(list_elm, src) == 0) { | |
| 349 | src += elem_size; | |
| 350 | si++; | |
| 351 | processed++; | |
| 352 | } | |
| 353 | if (processed == n) { | |
| 354 | return processed; | |
| 355 | } | |
| 356 | } | |
| 357 | di++; | |
| 358 | continue; | |
| 359 | } | |
| 324 | 360 | } |
| 361 | ||
| 845 | 362 | // determine the number of consecutive elements that can be inserted |
| 363 | size_t ins = 1, skip = 0; | |
| 324 | 364 | const char *next = src; |
| 365 | while (++si < n) { | |
| 845 | 366 | if (!allow_duplicates) { |
| 367 | // skip duplicates within the source | |
| 368 | if (cmp(next, next + elem_size) == 0) { | |
| 369 | next += elem_size; | |
| 370 | skip++; | |
| 371 | continue; | |
| 372 | } else { | |
| 373 | if (skip > 0) { | |
| 374 | // if we had to skip something, we must wait for the next run | |
| 375 | next += elem_size; | |
| 376 | break; | |
| 377 | } | |
| 378 | } | |
| 379 | } | |
| 324 | 380 | next += elem_size; |
| 381 | // once we become larger than the list elem, break | |
| 382 | if (cmp(list_elm, next) <= 0) { | |
| 383 | break; | |
| 384 | } | |
| 385 | // otherwise, we can insert one more | |
| 386 | ins++; | |
| 387 | } | |
| 388 | ||
| 389 | // insert the elements at location si | |
| 390 | if (ins == 1) { | |
| 845 | 391 | if (NULL == invoke_list_func(insert_element, list, di, src)) { |
| 392 | return processed; // LCOV_EXCL_LINE | |
| 393 | } | |
| 324 | 394 | } else { |
| 395 | size_t r = invoke_list_func(insert_array, list, di, src, ins); | |
| 845 | 396 | if (r < ins) { |
| 397 | return processed + r; // LCOV_EXCL_LINE | |
| 398 | } | |
| 324 | 399 | } |
| 845 | 400 | processed += ins + skip; |
| 324 | 401 | di += ins; |
| 402 | ||
| 403 | // everything inserted? | |
| 845 | 404 | if (processed == n) { |
| 405 | return processed; | |
| 406 | } | |
| 324 | 407 | src = next; |
| 408 | } | |
| 409 | ||
| 410 | // insert remaining items | |
| 411 | if (si < n) { | |
| 845 | 412 | if (allow_duplicates) { |
| 413 | processed += invoke_list_func(insert_array, list, di, src, n - si); | |
| 414 | } else { | |
| 415 | const void *last = di == 0 ? NULL : invoke_list_func(at, list, di - 1); | |
| 416 | for (; si < n; si++) { | |
| 417 | // skip duplicates within the source | |
| 418 | if (last == NULL || cmp(last, src) != 0) { | |
| 419 | if (NULL == invoke_list_func(insert_element, list, di, src)) { | |
| 420 | return processed; // LCOV_EXCL_LINE | |
| 421 | } | |
| 422 | last = src; | |
| 423 | di++; | |
| 424 | } | |
| 425 | processed++; | |
| 426 | src += elem_size; | |
| 427 | } | |
| 428 | } | |
| 324 | 429 | } |
| 430 | ||
| 845 | 431 | return processed; |
| 432 | } | |
| 433 | ||
| 434 | size_t cx_list_default_insert_sorted( | |
| 435 | struct cx_list_s *list, | |
| 436 | const void *sorted_data, | |
| 437 | size_t n | |
| 438 | ) { | |
| 439 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, true); | |
| 440 | } | |
| 441 | ||
| 442 | size_t cx_list_default_insert_unique( | |
| 443 | struct cx_list_s *list, | |
| 444 | const void *sorted_data, | |
| 445 | size_t n | |
| 446 | ) { | |
| 447 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, false); | |
| 324 | 448 | } |
| 449 | ||
| 450 | void cx_list_default_sort(struct cx_list_s *list) { | |
| 451 | size_t elem_size = list->collection.elem_size; | |
| 452 | size_t list_size = list->collection.size; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
453 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 845 | 454 | if (tmp == NULL) abort(); // LCOV_EXCL_LINE |
| 324 | 455 | |
| 456 | // copy elements from source array | |
| 457 | char *loc = tmp; | |
| 458 | for (size_t i = 0; i < list_size; i++) { | |
| 459 | void *src = invoke_list_func(at, list, i); | |
| 460 | memcpy(loc, src, elem_size); | |
| 461 | loc += elem_size; | |
| 462 | } | |
| 463 | ||
| 464 | // qsort | |
| 465 | qsort(tmp, list_size, elem_size, | |
| 466 | list->collection.cmpfunc); | |
| 467 | ||
| 468 | // copy elements back | |
| 469 | loc = tmp; | |
| 470 | for (size_t i = 0; i < list_size; i++) { | |
| 471 | void *dest = invoke_list_func(at, list, i); | |
| 472 | memcpy(dest, loc, elem_size); | |
| 473 | loc += elem_size; | |
| 474 | } | |
| 475 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
476 | cxFreeDefault(tmp); |
| 324 | 477 | } |
| 478 | ||
| 479 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
| 480 | if (i == j) return 0; | |
| 481 | if (i >= list->collection.size) return 1; | |
| 482 | if (j >= list->collection.size) return 1; | |
| 483 | ||
| 484 | size_t elem_size = list->collection.elem_size; | |
| 485 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
486 | void *tmp = cxMallocDefault(elem_size); |
| 845 | 487 | if (tmp == NULL) return 1; // LCOV_EXCL_LINE |
| 324 | 488 | |
| 489 | void *ip = invoke_list_func(at, list, i); | |
| 490 | void *jp = invoke_list_func(at, list, j); | |
| 491 | ||
| 492 | memcpy(tmp, ip, elem_size); | |
| 493 | memcpy(ip, jp, elem_size); | |
| 494 | memcpy(jp, tmp, elem_size); | |
| 495 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
496 | cxFreeDefault(tmp); |
| 324 | 497 | |
| 498 | return 0; | |
| 499 | } | |
| 500 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
501 | 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
|
502 | 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
|
503 | 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
|
504 | 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
|
505 | cx_compare_func comparator, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
506 | 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
|
507 | ) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
508 | 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
|
509 | list->collection.allocator = allocator; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
510 | list->collection.cmpfunc = comparator; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
511 | 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
|
512 | list->collection.elem_size = elem_size; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
513 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
514 | list->collection.elem_size = sizeof(void *); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
515 | if (list->collection.cmpfunc == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
516 | list->collection.cmpfunc = cx_cmp_ptr; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
517 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
518 | list->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
|
519 | list->climpl = list->cl; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
520 | list->cl = &cx_pointer_list_class; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
521 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
522 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
523 | |
| 174 | 524 | int cxListCompare( |
| 324 | 525 | const CxList *list, |
| 526 | const CxList *other | |
| 174 | 527 | ) { |
| 324 | 528 | bool cannot_optimize = false; |
| 529 | ||
| 530 | // if one is storing pointers but the other is not | |
| 531 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
| 532 | ||
| 533 | // if one class is wrapped but the other is not | |
| 534 | cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL); | |
| 174 | 535 | |
| 324 | 536 | // if the compare functions do not match or both are NULL |
| 537 | if (!cannot_optimize) { | |
| 538 | cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ? | |
| 539 | list->climpl->compare : list->cl->compare); | |
| 540 | cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ? | |
| 541 | other->climpl->compare : other->cl->compare); | |
| 542 | cannot_optimize |= list_cmp != other_cmp; | |
| 543 | cannot_optimize |= list_cmp == NULL; | |
| 544 | } | |
| 174 | 545 | |
| 324 | 546 | if (cannot_optimize) { |
| 174 | 547 | // lists are definitely different - cannot use internal compare function |
| 324 | 548 | if (list->collection.size == other->collection.size) { |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
549 | CxIterator left = list->cl->iterator(list, 0, false); |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
550 | CxIterator right = other->cl->iterator(other, 0, false); |
| 324 | 551 | for (size_t i = 0; i < list->collection.size; i++) { |
| 174 | 552 | void *leftValue = cxIteratorCurrent(left); |
| 553 | void *rightValue = cxIteratorCurrent(right); | |
| 324 | 554 | int d = list->collection.cmpfunc(leftValue, rightValue); |
| 174 | 555 | if (d != 0) { |
| 556 | return d; | |
| 557 | } | |
| 558 | cxIteratorNext(left); | |
| 559 | cxIteratorNext(right); | |
| 560 | } | |
| 561 | return 0; | |
| 562 | } else { | |
| 324 | 563 | return list->collection.size < other->collection.size ? -1 : 1; |
| 174 | 564 | } |
| 565 | } else { | |
| 566 | // lists are compatible | |
| 567 | return list->cl->compare(list, other); | |
| 568 | } | |
| 569 | } | |
| 570 | ||
| 870 | 571 | size_t cxListSize(const CxList *list) { |
| 572 | return list->collection.size; | |
| 573 | } | |
| 574 | ||
| 575 | int cxListAdd(CxList *list, const void *elem) { | |
| 576 | list->collection.sorted = false; | |
| 577 | return list->cl->insert_element(list, list->collection.size, elem) == NULL; | |
| 578 | } | |
| 579 | ||
| 580 | size_t cxListAddArray(CxList *list, const void *array, size_t n) { | |
| 581 | list->collection.sorted = false; | |
| 582 | return list->cl->insert_array(list, list->collection.size, array, n); | |
| 583 | } | |
| 584 | ||
| 585 | int cxListInsert(CxList *list, size_t index, const void *elem) { | |
| 586 | list->collection.sorted = false; | |
| 587 | return list->cl->insert_element(list, index, elem) == NULL; | |
| 588 | } | |
| 589 | ||
| 590 | void *cxListEmplaceAt(CxList *list, size_t index) { | |
| 591 | list->collection.sorted = false; | |
| 592 | return list->cl->insert_element(list, index, NULL); | |
| 593 | } | |
| 594 | ||
| 595 | void *cxListEmplace(CxList *list) { | |
| 596 | list->collection.sorted = false; | |
| 597 | return list->cl->insert_element(list, list->collection.size, NULL); | |
| 598 | } | |
| 599 | ||
| 600 | static bool cx_list_emplace_iterator_valid(const void *it) { | |
| 601 | const CxIterator *iter = it; | |
| 602 | return iter->index < iter->elem_count; | |
| 603 | } | |
| 604 | ||
| 605 | CxIterator cxListEmplaceArrayAt(CxList *list, size_t index, size_t n) { | |
| 606 | list->collection.sorted = false; | |
| 607 | size_t c = list->cl->insert_array(list, index, NULL, n); | |
| 608 | CxIterator iter = list->cl->iterator(list, index, false); | |
| 609 | // tweak the fields of this iterator | |
| 610 | iter.elem_count = c; | |
| 611 | iter.index = 0; | |
| 612 | // replace the valid function to abort iteration when c is reached | |
| 613 | iter.base.valid = cx_list_emplace_iterator_valid; | |
| 614 | // if we are storing pointers, we want to return the pure pointers. | |
| 615 | // therefore, we must unwrap the "current" method | |
| 616 | if (list->collection.store_pointer) { | |
| 617 | iter.base.current = iter.base.current_impl; | |
| 618 | } | |
| 619 | return iter; | |
| 620 | } | |
| 621 | ||
| 622 | CxIterator cxListEmplaceArray(CxList *list, size_t n) { | |
| 623 | return cxListEmplaceArrayAt(list, list->collection.size, n); | |
| 624 | } | |
| 625 | ||
| 626 | int cxListInsertSorted(CxList *list, const void *elem) { | |
| 627 | assert(cxCollectionSorted(list)); | |
| 628 | list->collection.sorted = true; | |
| 629 | const void *data = list->collection.store_pointer ? &elem : elem; | |
| 630 | return list->cl->insert_sorted(list, data, 1) == 0; | |
| 631 | } | |
| 632 | ||
| 633 | int cxListInsertUnique(CxList *list, const void *elem) { | |
| 634 | if (cxCollectionSorted(list)) { | |
| 635 | list->collection.sorted = true; | |
| 636 | const void *data = list->collection.store_pointer ? &elem : elem; | |
| 637 | return list->cl->insert_unique(list, data, 1) == 0; | |
| 638 | } else { | |
| 639 | if (cxListContains(list, elem)) { | |
| 640 | return 0; | |
| 641 | } else { | |
| 642 | return cxListAdd(list, elem); | |
| 643 | } | |
| 644 | } | |
| 645 | } | |
| 646 | ||
| 647 | size_t cxListInsertArray(CxList *list, size_t index, const void *array, size_t n) { | |
| 648 | list->collection.sorted = false; | |
| 649 | return list->cl->insert_array(list, index, array, n); | |
| 174 | 650 | } |
| 651 | ||
| 870 | 652 | size_t cxListInsertSortedArray(CxList *list, const void *array, size_t n) { |
| 653 | assert(cxCollectionSorted(list)); | |
| 654 | list->collection.sorted = true; | |
| 655 | return list->cl->insert_sorted(list, array, n); | |
| 656 | } | |
| 657 | ||
| 658 | size_t cxListInsertUniqueArray(CxList *list, const void *array, size_t n) { | |
| 659 | if (cxCollectionSorted(list)) { | |
| 660 | list->collection.sorted = true; | |
| 661 | return list->cl->insert_unique(list, array, n); | |
| 662 | } else { | |
| 663 | const char *source = array; | |
| 664 | for (size_t i = 0 ; i < n; i++) { | |
| 665 | // note: this also checks elements added in a previous iteration | |
| 666 | const void *data = list->collection.store_pointer ? | |
| 667 | *((const void**)source) : source; | |
| 668 | if (!cxListContains(list, data)) { | |
| 669 | if (cxListAdd(list, data)) { | |
| 670 | return i; // LCOV_EXCL_LINE | |
| 671 | } | |
| 672 | } | |
| 673 | source += list->collection.elem_size; | |
| 674 | } | |
| 675 | return n; | |
| 676 | } | |
| 677 | } | |
| 678 | ||
| 679 | int cxListInsertAfter(CxIterator *iter, const void *elem) { | |
| 680 | CxList* list = (CxList*)iter->src_handle; | |
| 681 | list->collection.sorted = false; | |
| 682 | return list->cl->insert_iter(iter, elem, 0); | |
| 683 | } | |
| 684 | ||
| 685 | int cxListInsertBefore(CxIterator *iter, const void *elem) { | |
| 686 | CxList* list = (CxList*)iter->src_handle; | |
| 687 | list->collection.sorted = false; | |
| 688 | return list->cl->insert_iter(iter, elem, 1); | |
| 689 | } | |
| 690 | ||
| 691 | int cxListRemove(CxList *list, size_t index) { | |
| 692 | return list->cl->remove(list, index, 1, NULL) == 0; | |
| 174 | 693 | } |
| 440 | 694 | |
| 870 | 695 | int cxListRemoveAndGet(CxList *list, size_t index, void *targetbuf) { |
| 696 | return list->cl->remove(list, index, 1, targetbuf) == 0; | |
| 697 | } | |
| 698 | ||
| 699 | int cxListRemoveAndGetFirst(CxList *list, void *targetbuf) { | |
| 700 | return list->cl->remove(list, 0, 1, targetbuf) == 0; | |
| 701 | } | |
| 702 | ||
| 703 | int cxListRemoveAndGetLast(CxList *list, void *targetbuf) { | |
| 704 | // note: index may wrap - member function will catch that | |
| 705 | return list->cl->remove(list, list->collection.size - 1, 1, targetbuf) == 0; | |
| 706 | } | |
| 707 | ||
| 708 | size_t cxListRemoveArray(CxList *list, size_t index, size_t num) { | |
| 709 | return list->cl->remove(list, index, num, NULL); | |
| 710 | } | |
| 711 | ||
| 712 | size_t cxListRemoveArrayAndGet(CxList *list, size_t index, size_t num, void *targetbuf) { | |
| 713 | return list->cl->remove(list, index, num, targetbuf); | |
| 440 | 714 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
715 | |
| 870 | 716 | void cxListClear(CxList *list) { |
| 717 | list->cl->clear(list); | |
| 718 | list->collection.sorted = true; // empty lists are always sorted | |
| 719 | } | |
| 720 | ||
| 721 | int cxListSwap(CxList *list, size_t i, size_t j) { | |
| 722 | list->collection.sorted = false; | |
| 723 | return list->cl->swap(list, i, j); | |
| 724 | } | |
| 725 | ||
| 726 | void *cxListAt(const CxList *list, size_t index) { | |
| 727 | return list->cl->at(list, index); | |
| 728 | } | |
| 729 | ||
| 730 | void *cxListFirst(const CxList *list) { | |
| 731 | return list->cl->at(list, 0); | |
| 732 | } | |
| 733 | ||
| 734 | void *cxListLast(const CxList *list) { | |
| 735 | return list->cl->at(list, list->collection.size - 1); | |
| 736 | } | |
| 737 | ||
| 738 | 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
|
739 | if (index >= list->collection.size) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
740 | return 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
741 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
742 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
743 | if (list->collection.store_pointer) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
744 | // For pointer collections, always use climpl |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
745 | void **target = list->climpl->at(list, index); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
746 | *target = (void *)elem; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
747 | } else { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
748 | void *target = list->cl->at(list, index); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
749 | memcpy(target, elem, list->collection.elem_size); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
750 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
751 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
752 | return 0; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
753 | } |
| 870 | 754 | |
| 755 | CxIterator cxListIteratorAt(const CxList *list, size_t index) { | |
| 756 | if (list == NULL) list = cxEmptyList; | |
| 757 | return list->cl->iterator(list, index, false); | |
| 758 | } | |
| 759 | ||
| 760 | CxIterator cxListBackwardsIteratorAt(const CxList *list, size_t index) { | |
| 761 | if (list == NULL) list = cxEmptyList; | |
| 762 | return list->cl->iterator(list, index, true); | |
| 763 | } | |
| 764 | ||
| 765 | CxIterator cxListIterator(const CxList *list) { | |
| 766 | if (list == NULL) list = cxEmptyList; | |
| 767 | return list->cl->iterator(list, 0, false); | |
| 768 | } | |
| 769 | ||
| 770 | CxIterator cxListBackwardsIterator(const CxList *list) { | |
| 771 | if (list == NULL) list = cxEmptyList; | |
| 772 | return list->cl->iterator(list, list->collection.size - 1, true); | |
| 773 | } | |
| 774 | ||
| 775 | size_t cxListFind(const CxList *list, const void *elem) { | |
| 776 | return list->cl->find_remove((CxList*)list, elem, false); | |
| 777 | } | |
| 778 | ||
| 779 | bool cxListContains(const CxList* list, const void* elem) { | |
| 780 | return list->cl->find_remove((CxList*)list, elem, false) < list->collection.size; | |
| 781 | } | |
| 782 | ||
| 783 | bool cxListIndexValid(const CxList *list, size_t index) { | |
| 784 | return index < list->collection.size; | |
| 785 | } | |
| 786 | ||
| 787 | size_t cxListFindRemove(CxList *list, const void *elem) { | |
| 788 | return list->cl->find_remove(list, elem, true); | |
| 789 | } | |
| 790 | ||
| 791 | void cxListSort(CxList *list) { | |
| 792 | if (list->collection.sorted) return; | |
| 793 | list->cl->sort(list); | |
| 794 | list->collection.sorted = true; | |
| 795 | } | |
| 796 | ||
| 797 | void cxListReverse(CxList *list) { | |
| 798 | // still sorted, but not according to the cmp_func | |
| 799 | list->collection.sorted = false; | |
| 800 | list->cl->reverse(list); | |
| 801 | } | |
| 802 | ||
| 803 | void cxListFree(CxList *list) { | |
| 804 | if (list == NULL) return; | |
| 805 | list->cl->deallocate(list); | |
| 806 | } |