Fri, 17 Oct 2025 16:02:26 +0200
fix gtk3 build
| 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> | |
| 32 | ||
| 33 | // <editor-fold desc="Store Pointers Functionality"> | |
| 34 | ||
| 35 | static _Thread_local cx_compare_func cx_pl_cmpfunc_impl; | |
| 36 | ||
| 37 | static int cx_pl_cmpfunc( | |
| 324 | 38 | const void *l, |
| 39 | const void *r | |
| 174 | 40 | ) { |
| 845 | 41 | // l and r are guaranteed to be non-NULL pointing to the list's memory |
| 174 | 42 | void *const *lptr = l; |
| 43 | void *const *rptr = r; | |
| 845 | 44 | const void *left = *lptr; |
| 45 | const void *right = *rptr; | |
| 46 | if (left == NULL) { | |
| 47 | // NULL is smaller than any value except NULL | |
| 48 | return right == NULL ? 0 : -1; | |
| 49 | } else if (right == NULL) { | |
| 50 | // any value is larger than NULL | |
| 51 | return 1; | |
| 52 | } | |
| 174 | 53 | return cx_pl_cmpfunc_impl(left, right); |
| 54 | } | |
| 55 | ||
| 324 | 56 | static void cx_pl_hack_cmpfunc(const struct cx_list_s *list) { |
| 174 | 57 | // cast away const - this is the hacky thing |
| 324 | 58 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 174 | 59 | cx_pl_cmpfunc_impl = l->cmpfunc; |
| 60 | l->cmpfunc = cx_pl_cmpfunc; | |
| 61 | } | |
| 62 | ||
| 324 | 63 | static void cx_pl_unhack_cmpfunc(const struct cx_list_s *list) { |
| 174 | 64 | // cast away const - this is the hacky thing |
| 324 | 65 | struct cx_collection_s *l = (struct cx_collection_s*) &list->collection; |
| 174 | 66 | l->cmpfunc = cx_pl_cmpfunc_impl; |
| 67 | } | |
| 68 | ||
| 69 | static void cx_pl_destructor(struct cx_list_s *list) { | |
| 440 | 70 | list->climpl->deallocate(list); |
| 174 | 71 | } |
| 72 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
73 | static void *cx_pl_insert_element( |
| 174 | 74 | struct cx_list_s *list, |
| 75 | size_t index, | |
| 324 | 76 | const void *element |
| 174 | 77 | ) { |
| 78 | return list->climpl->insert_element(list, index, &element); | |
| 79 | } | |
| 80 | ||
| 81 | static size_t cx_pl_insert_array( | |
| 82 | struct cx_list_s *list, | |
| 83 | size_t index, | |
| 324 | 84 | const void *array, |
| 174 | 85 | size_t n |
| 86 | ) { | |
| 87 | return list->climpl->insert_array(list, index, array, n); | |
| 88 | } | |
| 89 | ||
| 324 | 90 | static size_t cx_pl_insert_sorted( |
| 91 | struct cx_list_s *list, | |
| 92 | const void *array, | |
| 93 | size_t n | |
| 94 | ) { | |
| 95 | cx_pl_hack_cmpfunc(list); | |
| 96 | size_t result = list->climpl->insert_sorted(list, array, n); | |
| 97 | cx_pl_unhack_cmpfunc(list); | |
| 98 | return result; | |
| 99 | } | |
| 100 | ||
| 845 | 101 | static size_t cx_pl_insert_unique( |
| 102 | struct cx_list_s *list, | |
| 103 | const void *array, | |
| 104 | size_t n | |
| 105 | ) { | |
| 106 | cx_pl_hack_cmpfunc(list); | |
| 107 | size_t result = list->climpl->insert_unique(list, array, n); | |
| 108 | cx_pl_unhack_cmpfunc(list); | |
| 109 | return result; | |
| 110 | } | |
| 111 | ||
| 174 | 112 | static int cx_pl_insert_iter( |
| 324 | 113 | struct cx_iterator_s *iter, |
| 114 | const void *elem, | |
| 174 | 115 | int prepend |
| 116 | ) { | |
| 324 | 117 | struct cx_list_s *list = iter->src_handle.m; |
| 174 | 118 | return list->climpl->insert_iter(iter, &elem, prepend); |
| 119 | } | |
| 120 | ||
| 440 | 121 | static size_t cx_pl_remove( |
| 174 | 122 | struct cx_list_s *list, |
| 440 | 123 | size_t index, |
| 124 | size_t num, | |
| 125 | void *targetbuf | |
| 174 | 126 | ) { |
| 440 | 127 | return list->climpl->remove(list, index, num, targetbuf); |
| 174 | 128 | } |
| 129 | ||
| 130 | static void cx_pl_clear(struct cx_list_s *list) { | |
| 131 | list->climpl->clear(list); | |
| 132 | } | |
| 133 | ||
| 134 | static int cx_pl_swap( | |
| 135 | struct cx_list_s *list, | |
| 136 | size_t i, | |
| 137 | size_t j | |
| 138 | ) { | |
| 139 | return list->climpl->swap(list, i, j); | |
| 140 | } | |
| 141 | ||
| 142 | static void *cx_pl_at( | |
| 324 | 143 | const struct cx_list_s *list, |
| 174 | 144 | size_t index |
| 145 | ) { | |
| 146 | void **ptr = list->climpl->at(list, index); | |
| 147 | return ptr == NULL ? NULL : *ptr; | |
| 148 | } | |
| 149 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
150 | static size_t cx_pl_find_remove( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
151 | struct cx_list_s *list, |
| 324 | 152 | const void *elem, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
153 | bool remove |
| 174 | 154 | ) { |
| 155 | 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
|
156 | size_t ret = list->climpl->find_remove(list, &elem, remove); |
| 174 | 157 | cx_pl_unhack_cmpfunc(list); |
| 158 | return ret; | |
| 159 | } | |
| 160 | ||
| 161 | static void cx_pl_sort(struct cx_list_s *list) { | |
| 162 | cx_pl_hack_cmpfunc(list); | |
| 163 | list->climpl->sort(list); | |
| 164 | cx_pl_unhack_cmpfunc(list); | |
| 165 | } | |
| 166 | ||
| 167 | static int cx_pl_compare( | |
| 324 | 168 | const struct cx_list_s *list, |
| 169 | const struct cx_list_s *other | |
| 174 | 170 | ) { |
| 171 | cx_pl_hack_cmpfunc(list); | |
| 172 | int ret = list->climpl->compare(list, other); | |
| 173 | cx_pl_unhack_cmpfunc(list); | |
| 174 | return ret; | |
| 175 | } | |
| 176 | ||
| 177 | static void cx_pl_reverse(struct cx_list_s *list) { | |
| 178 | list->climpl->reverse(list); | |
| 179 | } | |
| 180 | ||
| 324 | 181 | static void *cx_pl_iter_current(const void *it) { |
| 182 | const struct cx_iterator_s *iter = it; | |
| 174 | 183 | void **ptr = iter->base.current_impl(it); |
| 184 | return ptr == NULL ? NULL : *ptr; | |
| 185 | } | |
| 186 | ||
| 187 | static struct cx_iterator_s cx_pl_iterator( | |
| 324 | 188 | const struct cx_list_s *list, |
| 174 | 189 | size_t index, |
| 190 | bool backwards | |
| 191 | ) { | |
| 192 | struct cx_iterator_s iter = list->climpl->iterator(list, index, backwards); | |
| 193 | iter.base.current_impl = iter.base.current; | |
| 194 | iter.base.current = cx_pl_iter_current; | |
| 195 | return iter; | |
| 196 | } | |
| 197 | ||
| 198 | static cx_list_class cx_pointer_list_class = { | |
| 199 | cx_pl_destructor, | |
| 200 | cx_pl_insert_element, | |
| 201 | cx_pl_insert_array, | |
| 324 | 202 | cx_pl_insert_sorted, |
| 845 | 203 | cx_pl_insert_unique, |
| 174 | 204 | cx_pl_insert_iter, |
| 205 | cx_pl_remove, | |
| 206 | cx_pl_clear, | |
| 207 | cx_pl_swap, | |
| 208 | cx_pl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
209 | cx_pl_find_remove, |
| 174 | 210 | cx_pl_sort, |
| 211 | cx_pl_compare, | |
| 212 | cx_pl_reverse, | |
| 213 | cx_pl_iterator, | |
| 214 | }; | |
| 215 | // </editor-fold> | |
| 216 | ||
| 217 | // <editor-fold desc="empty list implementation"> | |
| 218 | ||
| 440 | 219 | static void cx_emptyl_noop(cx_attr_unused CxList *list) { |
| 174 | 220 | // this is a noop, but MUST be implemented |
| 221 | } | |
| 222 | ||
| 223 | static void *cx_emptyl_at( | |
| 440 | 224 | cx_attr_unused const struct cx_list_s *list, |
| 225 | cx_attr_unused size_t index | |
| 174 | 226 | ) { |
| 227 | return NULL; | |
| 228 | } | |
| 229 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
230 | static size_t cx_emptyl_find_remove( |
| 440 | 231 | cx_attr_unused struct cx_list_s *list, |
| 232 | cx_attr_unused const void *elem, | |
| 233 | cx_attr_unused bool remove | |
| 174 | 234 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
235 | return 0; |
| 174 | 236 | } |
| 237 | ||
| 440 | 238 | static bool cx_emptyl_iter_valid(cx_attr_unused const void *iter) { |
| 174 | 239 | return false; |
| 240 | } | |
| 241 | ||
| 242 | static CxIterator cx_emptyl_iterator( | |
| 324 | 243 | const struct cx_list_s *list, |
| 174 | 244 | size_t index, |
| 440 | 245 | cx_attr_unused bool backwards |
| 174 | 246 | ) { |
| 247 | CxIterator iter = {0}; | |
| 324 | 248 | iter.src_handle.c = list; |
| 174 | 249 | iter.index = index; |
| 250 | iter.base.valid = cx_emptyl_iter_valid; | |
| 251 | return iter; | |
| 252 | } | |
| 253 | ||
| 254 | static cx_list_class cx_empty_list_class = { | |
| 255 | cx_emptyl_noop, | |
| 256 | NULL, | |
| 257 | NULL, | |
| 258 | NULL, | |
| 259 | NULL, | |
| 324 | 260 | NULL, |
| 845 | 261 | NULL, |
| 174 | 262 | cx_emptyl_noop, |
| 263 | NULL, | |
| 264 | cx_emptyl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
265 | cx_emptyl_find_remove, |
| 174 | 266 | cx_emptyl_noop, |
| 324 | 267 | NULL, |
| 174 | 268 | cx_emptyl_noop, |
| 269 | cx_emptyl_iterator, | |
| 270 | }; | |
| 271 | ||
| 272 | 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
|
273 | { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
274 | NULL, |
|
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 | 0, |
|
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 | NULL, |
|
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 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
282 | true, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
283 | }, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
284 | &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
|
285 | NULL |
| 174 | 286 | }; |
| 287 | ||
| 288 | CxList *const cxEmptyList = &cx_empty_list; | |
| 289 | ||
| 290 | // </editor-fold> | |
| 291 | ||
| 324 | 292 | #define invoke_list_func(name, list, ...) \ |
| 293 | ((list)->climpl == NULL ? (list)->cl->name : (list)->climpl->name) \ | |
| 294 | (list, __VA_ARGS__) | |
| 295 | ||
| 296 | size_t cx_list_default_insert_array( | |
| 297 | struct cx_list_s *list, | |
| 298 | size_t index, | |
| 299 | const void *data, | |
| 300 | size_t n | |
| 301 | ) { | |
| 302 | size_t elem_size = list->collection.elem_size; | |
| 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( |
| 440 | 307 | insert_element, list, index + i, |
| 845 | 308 | src + i * elem_size) |
| 309 | ) { | |
| 310 | return i; // LCOV_EXCL_LINE | |
| 311 | } | |
| 324 | 312 | } |
| 313 | return i; | |
| 314 | } | |
| 315 | ||
| 845 | 316 | static size_t cx_list_default_insert_sorted_impl( |
| 324 | 317 | struct cx_list_s *list, |
| 318 | const void *sorted_data, | |
| 845 | 319 | size_t n, |
| 320 | bool allow_duplicates | |
| 324 | 321 | ) { |
| 322 | // corner case | |
| 323 | if (n == 0) return 0; | |
| 324 | ||
| 325 | size_t elem_size = list->collection.elem_size; | |
| 326 | cx_compare_func cmp = list->collection.cmpfunc; | |
| 327 | const char *src = sorted_data; | |
| 328 | ||
| 329 | // track indices and number of inserted items | |
| 845 | 330 | size_t di = 0, si = 0, processed = 0; |
| 324 | 331 | |
| 332 | // search the list for insertion points | |
| 845 | 333 | while (di < list->collection.size) { |
| 324 | 334 | const void *list_elm = invoke_list_func(at, list, di); |
| 335 | ||
| 845 | 336 | // compare the current list element with the first source element |
| 337 | // if less, skip the list elements | |
| 338 | // if equal, skip the list elements and optionally the source elements | |
| 339 | { | |
| 340 | int d = cmp(list_elm, src); | |
| 341 | if (d <= 0) { | |
| 342 | if (!allow_duplicates && d == 0) { | |
| 343 | src += elem_size; | |
| 344 | si++; | |
| 345 | processed++; // we also count duplicates for the return value | |
| 346 | while (si < n && cmp(list_elm, src) == 0) { | |
| 347 | src += elem_size; | |
| 348 | si++; | |
| 349 | processed++; | |
| 350 | } | |
| 351 | if (processed == n) { | |
| 352 | return processed; | |
| 353 | } | |
| 354 | } | |
| 355 | di++; | |
| 356 | continue; | |
| 357 | } | |
| 324 | 358 | } |
| 359 | ||
| 845 | 360 | // determine the number of consecutive elements that can be inserted |
| 361 | size_t ins = 1, skip = 0; | |
| 324 | 362 | const char *next = src; |
| 363 | while (++si < n) { | |
| 845 | 364 | if (!allow_duplicates) { |
| 365 | // skip duplicates within the source | |
| 366 | if (cmp(next, next + elem_size) == 0) { | |
| 367 | next += elem_size; | |
| 368 | skip++; | |
| 369 | continue; | |
| 370 | } else { | |
| 371 | if (skip > 0) { | |
| 372 | // if we had to skip something, we must wait for the next run | |
| 373 | next += elem_size; | |
| 374 | break; | |
| 375 | } | |
| 376 | } | |
| 377 | } | |
| 324 | 378 | next += elem_size; |
| 379 | // once we become larger than the list elem, break | |
| 380 | if (cmp(list_elm, next) <= 0) { | |
| 381 | break; | |
| 382 | } | |
| 383 | // otherwise, we can insert one more | |
| 384 | ins++; | |
| 385 | } | |
| 386 | ||
| 387 | // insert the elements at location si | |
| 388 | if (ins == 1) { | |
| 845 | 389 | if (NULL == invoke_list_func(insert_element, list, di, src)) { |
| 390 | return processed; // LCOV_EXCL_LINE | |
| 391 | } | |
| 324 | 392 | } else { |
| 393 | size_t r = invoke_list_func(insert_array, list, di, src, ins); | |
| 845 | 394 | if (r < ins) { |
| 395 | return processed + r; // LCOV_EXCL_LINE | |
| 396 | } | |
| 324 | 397 | } |
| 845 | 398 | processed += ins + skip; |
| 324 | 399 | di += ins; |
| 400 | ||
| 401 | // everything inserted? | |
| 845 | 402 | if (processed == n) { |
| 403 | return processed; | |
| 404 | } | |
| 324 | 405 | src = next; |
| 406 | } | |
| 407 | ||
| 408 | // insert remaining items | |
| 409 | if (si < n) { | |
| 845 | 410 | if (allow_duplicates) { |
| 411 | processed += invoke_list_func(insert_array, list, di, src, n - si); | |
| 412 | } else { | |
| 413 | const void *last = di == 0 ? NULL : invoke_list_func(at, list, di - 1); | |
| 414 | for (; si < n; si++) { | |
| 415 | // skip duplicates within the source | |
| 416 | if (last == NULL || cmp(last, src) != 0) { | |
| 417 | if (NULL == invoke_list_func(insert_element, list, di, src)) { | |
| 418 | return processed; // LCOV_EXCL_LINE | |
| 419 | } | |
| 420 | last = src; | |
| 421 | di++; | |
| 422 | } | |
| 423 | processed++; | |
| 424 | src += elem_size; | |
| 425 | } | |
| 426 | } | |
| 324 | 427 | } |
| 428 | ||
| 845 | 429 | return processed; |
| 430 | } | |
| 431 | ||
| 432 | size_t cx_list_default_insert_sorted( | |
| 433 | struct cx_list_s *list, | |
| 434 | const void *sorted_data, | |
| 435 | size_t n | |
| 436 | ) { | |
| 437 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, true); | |
| 438 | } | |
| 439 | ||
| 440 | size_t cx_list_default_insert_unique( | |
| 441 | struct cx_list_s *list, | |
| 442 | const void *sorted_data, | |
| 443 | size_t n | |
| 444 | ) { | |
| 445 | return cx_list_default_insert_sorted_impl(list, sorted_data, n, false); | |
| 324 | 446 | } |
| 447 | ||
| 448 | void cx_list_default_sort(struct cx_list_s *list) { | |
| 449 | size_t elem_size = list->collection.elem_size; | |
| 450 | size_t list_size = list->collection.size; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
451 | void *tmp = cxMallocDefault(elem_size * list_size); |
| 845 | 452 | if (tmp == NULL) abort(); // LCOV_EXCL_LINE |
| 324 | 453 | |
| 454 | // copy elements from source array | |
| 455 | char *loc = tmp; | |
| 456 | for (size_t i = 0; i < list_size; i++) { | |
| 457 | void *src = invoke_list_func(at, list, i); | |
| 458 | memcpy(loc, src, elem_size); | |
| 459 | loc += elem_size; | |
| 460 | } | |
| 461 | ||
| 462 | // qsort | |
| 463 | qsort(tmp, list_size, elem_size, | |
| 464 | list->collection.cmpfunc); | |
| 465 | ||
| 466 | // copy elements back | |
| 467 | loc = tmp; | |
| 468 | for (size_t i = 0; i < list_size; i++) { | |
| 469 | void *dest = invoke_list_func(at, list, i); | |
| 470 | memcpy(dest, loc, elem_size); | |
| 471 | loc += elem_size; | |
| 472 | } | |
| 473 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
474 | cxFreeDefault(tmp); |
| 324 | 475 | } |
| 476 | ||
| 477 | int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) { | |
| 478 | if (i == j) return 0; | |
| 479 | if (i >= list->collection.size) return 1; | |
| 480 | if (j >= list->collection.size) return 1; | |
| 481 | ||
| 482 | size_t elem_size = list->collection.elem_size; | |
| 483 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
484 | void *tmp = cxMallocDefault(elem_size); |
| 845 | 485 | if (tmp == NULL) return 1; // LCOV_EXCL_LINE |
| 324 | 486 | |
| 487 | void *ip = invoke_list_func(at, list, i); | |
| 488 | void *jp = invoke_list_func(at, list, j); | |
| 489 | ||
| 490 | memcpy(tmp, ip, elem_size); | |
| 491 | memcpy(ip, jp, elem_size); | |
| 492 | memcpy(jp, tmp, elem_size); | |
| 493 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
494 | cxFreeDefault(tmp); |
| 324 | 495 | |
| 496 | return 0; | |
| 497 | } | |
| 498 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
499 | 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
|
500 | 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
|
501 | 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
|
502 | 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
|
503 | 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
|
504 | 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
|
505 | ) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
506 | 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
|
507 | 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
|
508 | 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
|
509 | 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
|
510 | 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
|
511 | } else { |
|
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 = 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
|
513 | 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
|
514 | 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
|
515 | } |
|
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.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
|
517 | 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
|
518 | 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
|
519 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
520 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
521 | |
| 174 | 522 | int cxListCompare( |
| 324 | 523 | const CxList *list, |
| 524 | const CxList *other | |
| 174 | 525 | ) { |
| 324 | 526 | bool cannot_optimize = false; |
| 527 | ||
| 528 | // if one is storing pointers but the other is not | |
| 529 | cannot_optimize |= list->collection.store_pointer ^ other->collection.store_pointer; | |
| 530 | ||
| 531 | // if one class is wrapped but the other is not | |
| 532 | cannot_optimize |= (list->climpl == NULL) ^ (other->climpl == NULL); | |
| 174 | 533 | |
| 324 | 534 | // if the compare functions do not match or both are NULL |
| 535 | if (!cannot_optimize) { | |
| 536 | cx_compare_func list_cmp = (cx_compare_func) (list->climpl != NULL ? | |
| 537 | list->climpl->compare : list->cl->compare); | |
| 538 | cx_compare_func other_cmp = (cx_compare_func) (other->climpl != NULL ? | |
| 539 | other->climpl->compare : other->cl->compare); | |
| 540 | cannot_optimize |= list_cmp != other_cmp; | |
| 541 | cannot_optimize |= list_cmp == NULL; | |
| 542 | } | |
| 174 | 543 | |
| 324 | 544 | if (cannot_optimize) { |
| 174 | 545 | // lists are definitely different - cannot use internal compare function |
| 324 | 546 | if (list->collection.size == other->collection.size) { |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
547 | CxIterator left = list->cl->iterator(list, 0, false); |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
548 | CxIterator right = other->cl->iterator(other, 0, false); |
| 324 | 549 | for (size_t i = 0; i < list->collection.size; i++) { |
| 174 | 550 | void *leftValue = cxIteratorCurrent(left); |
| 551 | void *rightValue = cxIteratorCurrent(right); | |
| 324 | 552 | int d = list->collection.cmpfunc(leftValue, rightValue); |
| 174 | 553 | if (d != 0) { |
| 554 | return d; | |
| 555 | } | |
| 556 | cxIteratorNext(left); | |
| 557 | cxIteratorNext(right); | |
| 558 | } | |
| 559 | return 0; | |
| 560 | } else { | |
| 324 | 561 | return list->collection.size < other->collection.size ? -1 : 1; |
| 174 | 562 | } |
| 563 | } else { | |
| 564 | // lists are compatible | |
| 565 | return list->cl->compare(list, other); | |
| 566 | } | |
| 567 | } | |
| 568 | ||
| 324 | 569 | CxIterator cxListMutIteratorAt( |
| 174 | 570 | CxList *list, |
| 571 | size_t index | |
| 572 | ) { | |
| 845 | 573 | if (list == NULL) list = cxEmptyList; |
| 174 | 574 | CxIterator it = list->cl->iterator(list, index, false); |
| 575 | it.base.mutating = true; | |
| 324 | 576 | return it; |
| 174 | 577 | } |
| 578 | ||
| 324 | 579 | CxIterator cxListMutBackwardsIteratorAt( |
| 174 | 580 | CxList *list, |
| 581 | size_t index | |
| 582 | ) { | |
| 845 | 583 | if (list == NULL) list = cxEmptyList; |
| 174 | 584 | CxIterator it = list->cl->iterator(list, index, true); |
| 585 | it.base.mutating = true; | |
| 324 | 586 | return it; |
| 174 | 587 | } |
| 440 | 588 | |
| 589 | void cxListFree(CxList *list) { | |
| 590 | if (list == NULL) return; | |
| 591 | list->cl->deallocate(list); | |
| 592 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
593 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
594 | int cxListSet( |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
595 | CxList *list, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
596 | size_t index, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
597 | const void *elem |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
598 | ) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
599 | if (index >= list->collection.size) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
600 | return 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
601 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
602 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
603 | if (list->collection.store_pointer) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
604 | // For pointer collections, always use climpl |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
605 | void **target = list->climpl->at(list, index); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
606 | *target = (void *)elem; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
607 | } else { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
608 | void *target = list->cl->at(list, index); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
609 | memcpy(target, elem, list->collection.elem_size); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
610 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
611 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
612 | return 0; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
613 | } |