Sun, 05 Jan 2025 22:00:39 +0100
update ucx
174 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include "cx/array_list.h" | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
30 | #include "cx/compare.h" |
174 | 31 | #include <assert.h> |
32 | #include <string.h> | |
440 | 33 | #include <errno.h> |
174 | 34 | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
35 | // Default array reallocator |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
36 | |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
37 | static void *cx_array_default_realloc( |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
38 | void *array, |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
39 | size_t capacity, |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
40 | size_t elem_size, |
440 | 41 | cx_attr_unused CxArrayReallocator *alloc |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
42 | ) { |
440 | 43 | size_t n; |
44 | if (cx_szmul(capacity, elem_size, &n)) { | |
45 | errno = EOVERFLOW; | |
46 | return NULL; | |
47 | } | |
48 | return realloc(array, n); | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
49 | } |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
50 | |
440 | 51 | CxArrayReallocator cx_array_default_reallocator_impl = { |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
52 | cx_array_default_realloc, NULL, NULL, 0, 0 |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
53 | }; |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
54 | |
440 | 55 | CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl; |
56 | ||
57 | // Stack-aware array reallocator | |
58 | ||
59 | static void *cx_array_advanced_realloc( | |
60 | void *array, | |
61 | size_t capacity, | |
62 | size_t elem_size, | |
63 | cx_attr_unused CxArrayReallocator *alloc | |
64 | ) { | |
65 | // check for overflow | |
66 | size_t n; | |
67 | if (cx_szmul(capacity, elem_size, &n)) { | |
68 | errno = EOVERFLOW; | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | // retrieve the pointer to the actual allocator | |
73 | const CxAllocator *al = alloc->ptr1; | |
74 | ||
75 | // check if the array is still located on the stack | |
76 | void *newmem; | |
77 | if (array == alloc->ptr2) { | |
78 | newmem = cxMalloc(al, n); | |
79 | if (newmem != NULL && array != NULL) { | |
80 | memcpy(newmem, array, n); | |
81 | } | |
82 | } else { | |
83 | newmem = cxRealloc(al, array, n); | |
84 | } | |
85 | return newmem; | |
86 | } | |
87 | ||
88 | struct cx_array_reallocator_s cx_array_reallocator( | |
89 | const struct cx_allocator_s *allocator, | |
90 | const void *stackmem | |
91 | ) { | |
92 | if (allocator == NULL) { | |
93 | allocator = cxDefaultAllocator; | |
94 | } | |
95 | return (struct cx_array_reallocator_s) { | |
96 | cx_array_advanced_realloc, | |
97 | (void*) allocator, (void*) stackmem, | |
98 | 0, 0 | |
99 | }; | |
100 | } | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
101 | |
174 | 102 | // LOW LEVEL ARRAY LIST FUNCTIONS |
103 | ||
440 | 104 | static size_t cx_array_align_capacity( |
105 | size_t cap, | |
106 | size_t alignment, | |
107 | size_t max | |
108 | ) { | |
109 | if (cap > max - alignment) { | |
110 | return cap; | |
111 | } else { | |
112 | return cap - (cap % alignment) + alignment; | |
113 | } | |
114 | } | |
115 | ||
116 | int cx_array_reserve( | |
117 | void **array, | |
118 | void *size, | |
119 | void *capacity, | |
120 | unsigned width, | |
121 | size_t elem_size, | |
122 | size_t elem_count, | |
123 | CxArrayReallocator *reallocator | |
124 | ) { | |
125 | // assert pointers | |
126 | assert(array != NULL); | |
127 | assert(size != NULL); | |
128 | assert(capacity != NULL); | |
129 | ||
130 | // default reallocator | |
131 | if (reallocator == NULL) { | |
132 | reallocator = cx_array_default_reallocator; | |
133 | } | |
134 | ||
135 | // determine size and capacity | |
136 | size_t oldcap; | |
137 | size_t oldsize; | |
138 | size_t max_size; | |
139 | if (width == 0 || width == sizeof(size_t)) { | |
140 | oldcap = *(size_t*) capacity; | |
141 | oldsize = *(size_t*) size; | |
142 | max_size = SIZE_MAX; | |
143 | } else if (width == sizeof(uint16_t)) { | |
144 | oldcap = *(uint16_t*) capacity; | |
145 | oldsize = *(uint16_t*) size; | |
146 | max_size = UINT16_MAX; | |
147 | } else if (width == sizeof(uint8_t)) { | |
148 | oldcap = *(uint8_t*) capacity; | |
149 | oldsize = *(uint8_t*) size; | |
150 | max_size = UINT8_MAX; | |
151 | } | |
152 | #if CX_WORDSIZE == 64 | |
153 | else if (width == sizeof(uint32_t)) { | |
154 | oldcap = *(uint32_t*) capacity; | |
155 | oldsize = *(uint32_t*) size; | |
156 | max_size = UINT32_MAX; | |
157 | } | |
158 | #endif | |
159 | else { | |
160 | errno = EINVAL; | |
161 | return 1; | |
162 | } | |
163 | ||
164 | // assert that the array is allocated when it has capacity | |
165 | assert(*array != NULL || oldcap == 0); | |
166 | ||
167 | // check for overflow | |
168 | if (elem_count > max_size - oldsize) { | |
169 | errno = EOVERFLOW; | |
170 | return 1; | |
171 | } | |
172 | ||
173 | // determine new capacity | |
174 | size_t newcap = oldsize + elem_count; | |
175 | ||
176 | // reallocate if possible | |
177 | if (newcap > oldcap) { | |
178 | // calculate new capacity (next number divisible by 16) | |
179 | newcap = cx_array_align_capacity(newcap, 16, max_size); | |
180 | ||
181 | // perform reallocation | |
182 | void *newmem = reallocator->realloc( | |
183 | *array, newcap, elem_size, reallocator | |
184 | ); | |
185 | if (newmem == NULL) { | |
186 | return 1; // LCOV_EXCL_LINE | |
187 | } | |
188 | ||
189 | // store new pointer | |
190 | *array = newmem; | |
191 | ||
192 | // store new capacity | |
193 | if (width == 0 || width == sizeof(size_t)) { | |
194 | *(size_t*) capacity = newcap; | |
195 | } else if (width == sizeof(uint16_t)) { | |
196 | *(uint16_t*) capacity = (uint16_t) newcap; | |
197 | } else if (width == sizeof(uint8_t)) { | |
198 | *(uint8_t*) capacity = (uint8_t) newcap; | |
199 | } | |
200 | #if CX_WORDSIZE == 64 | |
201 | else if (width == sizeof(uint32_t)) { | |
202 | *(uint32_t*) capacity = (uint32_t) newcap; | |
203 | } | |
204 | #endif | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | int cx_array_copy( | |
174 | 211 | void **target, |
440 | 212 | void *size, |
213 | void *capacity, | |
214 | unsigned width, | |
174 | 215 | size_t index, |
324 | 216 | const void *src, |
174 | 217 | size_t elem_size, |
218 | size_t elem_count, | |
440 | 219 | CxArrayReallocator *reallocator |
174 | 220 | ) { |
221 | // assert pointers | |
222 | assert(target != NULL); | |
223 | assert(size != NULL); | |
440 | 224 | assert(capacity != NULL); |
174 | 225 | assert(src != NULL); |
226 | ||
440 | 227 | // default reallocator |
228 | if (reallocator == NULL) { | |
229 | reallocator = cx_array_default_reallocator; | |
230 | } | |
231 | ||
232 | // determine size and capacity | |
233 | size_t oldcap; | |
234 | size_t oldsize; | |
235 | size_t max_size; | |
236 | if (width == 0 || width == sizeof(size_t)) { | |
237 | oldcap = *(size_t*) capacity; | |
238 | oldsize = *(size_t*) size; | |
239 | max_size = SIZE_MAX; | |
240 | } else if (width == sizeof(uint16_t)) { | |
241 | oldcap = *(uint16_t*) capacity; | |
242 | oldsize = *(uint16_t*) size; | |
243 | max_size = UINT16_MAX; | |
244 | } else if (width == sizeof(uint8_t)) { | |
245 | oldcap = *(uint8_t*) capacity; | |
246 | oldsize = *(uint8_t*) size; | |
247 | max_size = UINT8_MAX; | |
248 | } | |
249 | #if CX_WORDSIZE == 64 | |
250 | else if (width == sizeof(uint32_t)) { | |
251 | oldcap = *(uint32_t*) capacity; | |
252 | oldsize = *(uint32_t*) size; | |
253 | max_size = UINT32_MAX; | |
254 | } | |
255 | #endif | |
256 | else { | |
257 | errno = EINVAL; | |
258 | return 1; | |
259 | } | |
260 | ||
261 | // assert that the array is allocated when it has capacity | |
262 | assert(*target != NULL || oldcap == 0); | |
263 | ||
264 | // check for overflow | |
265 | if (index > max_size || elem_count > max_size - index) { | |
266 | errno = EOVERFLOW; | |
267 | return 1; | |
268 | } | |
174 | 269 | |
270 | // check if resize is required | |
271 | size_t minsize = index + elem_count; | |
440 | 272 | size_t newsize = oldsize < minsize ? minsize : oldsize; |
174 | 273 | |
274 | // reallocate if possible | |
440 | 275 | size_t newcap = oldcap; |
276 | if (newsize > oldcap) { | |
174 | 277 | // check, if we need to repair the src pointer |
278 | uintptr_t targetaddr = (uintptr_t) *target; | |
279 | uintptr_t srcaddr = (uintptr_t) src; | |
280 | bool repairsrc = targetaddr <= srcaddr | |
440 | 281 | && srcaddr < targetaddr + oldcap * elem_size; |
174 | 282 | |
283 | // calculate new capacity (next number divisible by 16) | |
440 | 284 | newcap = cx_array_align_capacity(newsize, 16, max_size); |
285 | assert(newcap > newsize); | |
174 | 286 | |
287 | // perform reallocation | |
288 | void *newmem = reallocator->realloc( | |
440 | 289 | *target, newcap, elem_size, reallocator |
174 | 290 | ); |
291 | if (newmem == NULL) { | |
440 | 292 | return 1; |
174 | 293 | } |
294 | ||
295 | // repair src pointer, if necessary | |
296 | if (repairsrc) { | |
297 | src = ((char *) newmem) + (srcaddr - targetaddr); | |
298 | } | |
299 | ||
440 | 300 | // store new pointer |
174 | 301 | *target = newmem; |
302 | } | |
303 | ||
304 | // determine target pointer | |
305 | char *start = *target; | |
306 | start += index * elem_size; | |
307 | ||
308 | // copy elements and set new size | |
440 | 309 | // note: no overflow check here, b/c we cannot get here w/o allocation |
174 | 310 | memmove(start, src, elem_count * elem_size); |
440 | 311 | |
312 | // if any of size or capacity changed, store them back | |
313 | if (newsize != oldsize || newcap != oldcap) { | |
314 | if (width == 0 || width == sizeof(size_t)) { | |
315 | *(size_t*) capacity = newcap; | |
316 | *(size_t*) size = newsize; | |
317 | } else if (width == sizeof(uint16_t)) { | |
318 | *(uint16_t*) capacity = (uint16_t) newcap; | |
319 | *(uint16_t*) size = (uint16_t) newsize; | |
320 | } else if (width == sizeof(uint8_t)) { | |
321 | *(uint8_t*) capacity = (uint8_t) newcap; | |
322 | *(uint8_t*) size = (uint8_t) newsize; | |
323 | } | |
324 | #if CX_WORDSIZE == 64 | |
325 | else if (width == sizeof(uint32_t)) { | |
326 | *(uint32_t*) capacity = (uint32_t) newcap; | |
327 | *(uint32_t*) size = (uint32_t) newsize; | |
328 | } | |
329 | #endif | |
330 | } | |
174 | 331 | |
332 | // return successfully | |
440 | 333 | return 0; |
174 | 334 | } |
335 | ||
440 | 336 | int cx_array_insert_sorted( |
324 | 337 | void **target, |
338 | size_t *size, | |
339 | size_t *capacity, | |
340 | cx_compare_func cmp_func, | |
341 | const void *sorted_data, | |
342 | size_t elem_size, | |
343 | size_t elem_count, | |
440 | 344 | CxArrayReallocator *reallocator |
324 | 345 | ) { |
346 | // assert pointers | |
347 | assert(target != NULL); | |
348 | assert(size != NULL); | |
349 | assert(capacity != NULL); | |
350 | assert(cmp_func != NULL); | |
351 | assert(sorted_data != NULL); | |
440 | 352 | |
353 | // default reallocator | |
354 | if (reallocator == NULL) { | |
355 | reallocator = cx_array_default_reallocator; | |
356 | } | |
324 | 357 | |
358 | // corner case | |
359 | if (elem_count == 0) return 0; | |
360 | ||
440 | 361 | // overflow check |
362 | if (elem_count > SIZE_MAX - *size) { | |
363 | errno = EOVERFLOW; | |
364 | return 1; | |
365 | } | |
366 | ||
324 | 367 | // store some counts |
368 | size_t old_size = *size; | |
369 | size_t needed_capacity = old_size + elem_count; | |
370 | ||
371 | // if we need more than we have, try a reallocation | |
372 | if (needed_capacity > *capacity) { | |
440 | 373 | size_t new_capacity = cx_array_align_capacity(needed_capacity, 16, SIZE_MAX); |
324 | 374 | void *new_mem = reallocator->realloc( |
375 | *target, new_capacity, elem_size, reallocator | |
376 | ); | |
377 | if (new_mem == NULL) { | |
378 | // give it up right away, there is no contract | |
379 | // that requires us to insert as much as we can | |
440 | 380 | return 1; // LCOV_EXCL_LINE |
324 | 381 | } |
382 | *target = new_mem; | |
383 | *capacity = new_capacity; | |
384 | } | |
385 | ||
386 | // now we have guaranteed that we can insert everything | |
387 | size_t new_size = old_size + elem_count; | |
388 | *size = new_size; | |
389 | ||
390 | // declare the source and destination indices/pointers | |
391 | size_t si = 0, di = 0; | |
392 | const char *src = sorted_data; | |
393 | char *dest = *target; | |
394 | ||
395 | // find the first insertion point | |
396 | di = cx_array_binary_search_sup(dest, old_size, elem_size, src, cmp_func); | |
397 | dest += di * elem_size; | |
398 | ||
399 | // move the remaining elements in the array completely to the right | |
400 | // we will call it the "buffer" for parked elements | |
401 | size_t buf_size = old_size - di; | |
402 | size_t bi = new_size - buf_size; | |
403 | char *bptr = ((char *) *target) + bi * elem_size; | |
404 | memmove(bptr, dest, buf_size * elem_size); | |
405 | ||
406 | // while there are both source and buffered elements left, | |
407 | // copy them interleaving | |
408 | while (si < elem_count && bi < new_size) { | |
409 | // determine how many source elements can be inserted | |
410 | size_t copy_len, bytes_copied; | |
411 | copy_len = cx_array_binary_search_sup( | |
412 | src, | |
413 | elem_count - si, | |
414 | elem_size, | |
415 | bptr, | |
416 | cmp_func | |
417 | ); | |
418 | ||
419 | // copy the source elements | |
420 | bytes_copied = copy_len * elem_size; | |
421 | memcpy(dest, src, bytes_copied); | |
422 | dest += bytes_copied; | |
423 | src += bytes_copied; | |
424 | si += copy_len; | |
425 | ||
426 | // when all source elements are in place, we are done | |
427 | if (si >= elem_count) break; | |
428 | ||
429 | // determine how many buffered elements need to be restored | |
430 | copy_len = cx_array_binary_search_sup( | |
431 | bptr, | |
432 | new_size - bi, | |
433 | elem_size, | |
434 | src, | |
435 | cmp_func | |
436 | ); | |
437 | ||
438 | // restore the buffered elements | |
439 | bytes_copied = copy_len * elem_size; | |
440 | memmove(dest, bptr, bytes_copied); | |
441 | dest += bytes_copied; | |
442 | bptr += bytes_copied; | |
443 | bi += copy_len; | |
444 | } | |
445 | ||
446 | // still source elements left? simply append them | |
447 | if (si < elem_count) { | |
448 | memcpy(dest, src, elem_size * (elem_count - si)); | |
449 | } | |
450 | ||
451 | // still buffer elements left? | |
452 | // don't worry, we already moved them to the correct place | |
453 | ||
440 | 454 | return 0; |
324 | 455 | } |
456 | ||
457 | size_t cx_array_binary_search_inf( | |
458 | const void *arr, | |
459 | size_t size, | |
460 | size_t elem_size, | |
461 | const void *elem, | |
462 | cx_compare_func cmp_func | |
463 | ) { | |
464 | // special case: empty array | |
465 | if (size == 0) return 0; | |
466 | ||
467 | // declare a variable that will contain the compare results | |
468 | int result; | |
469 | ||
470 | // cast the array pointer to something we can use offsets with | |
471 | const char *array = arr; | |
472 | ||
473 | // check the first array element | |
474 | result = cmp_func(elem, array); | |
475 | if (result < 0) { | |
476 | return size; | |
477 | } else if (result == 0) { | |
478 | return 0; | |
479 | } | |
480 | ||
440 | 481 | // special case: there is only one element and that is smaller |
482 | if (size == 1) return 0; | |
483 | ||
324 | 484 | // check the last array element |
485 | result = cmp_func(elem, array + elem_size * (size - 1)); | |
486 | if (result >= 0) { | |
487 | return size - 1; | |
488 | } | |
489 | ||
490 | // the element is now guaranteed to be somewhere in the list | |
491 | // so start the binary search | |
492 | size_t left_index = 1; | |
493 | size_t right_index = size - 1; | |
494 | size_t pivot_index; | |
495 | ||
496 | while (left_index <= right_index) { | |
497 | pivot_index = left_index + (right_index - left_index) / 2; | |
498 | const char *arr_elem = array + pivot_index * elem_size; | |
499 | result = cmp_func(elem, arr_elem); | |
500 | if (result == 0) { | |
501 | // found it! | |
502 | return pivot_index; | |
503 | } else if (result < 0) { | |
504 | // element is smaller than pivot, continue search left | |
505 | right_index = pivot_index - 1; | |
506 | } else { | |
507 | // element is larger than pivot, continue search right | |
508 | left_index = pivot_index + 1; | |
509 | } | |
510 | } | |
511 | ||
512 | // report the largest upper bound | |
513 | return result < 0 ? (pivot_index - 1) : pivot_index; | |
514 | } | |
515 | ||
440 | 516 | size_t cx_array_binary_search( |
517 | const void *arr, | |
518 | size_t size, | |
519 | size_t elem_size, | |
520 | const void *elem, | |
521 | cx_compare_func cmp_func | |
522 | ) { | |
523 | size_t index = cx_array_binary_search_inf( | |
524 | arr, size, elem_size, elem, cmp_func | |
525 | ); | |
526 | if (index < size && | |
527 | cmp_func(((const char *) arr) + index * elem_size, elem) == 0) { | |
528 | return index; | |
529 | } else { | |
530 | return size; | |
531 | } | |
532 | } | |
533 | ||
534 | size_t cx_array_binary_search_sup( | |
535 | const void *arr, | |
536 | size_t size, | |
537 | size_t elem_size, | |
538 | const void *elem, | |
539 | cx_compare_func cmp_func | |
540 | ) { | |
541 | size_t inf = cx_array_binary_search_inf( | |
542 | arr, size, elem_size, elem, cmp_func | |
543 | ); | |
544 | if (inf == size) { | |
545 | // no infimum means, first element is supremum | |
546 | return 0; | |
547 | } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) { | |
548 | return inf; | |
549 | } else { | |
550 | return inf + 1; | |
551 | } | |
552 | } | |
553 | ||
174 | 554 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
555 | #define CX_ARRAY_SWAP_SBO_SIZE 128 |
174 | 556 | #endif |
440 | 557 | const unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE; |
174 | 558 | |
559 | void cx_array_swap( | |
560 | void *arr, | |
561 | size_t elem_size, | |
562 | size_t idx1, | |
563 | size_t idx2 | |
564 | ) { | |
565 | assert(arr != NULL); | |
566 | ||
567 | // short circuit | |
568 | if (idx1 == idx2) return; | |
569 | ||
570 | char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; | |
571 | void *tmp; | |
572 | ||
573 | // decide if we can use the local buffer | |
574 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { | |
575 | tmp = malloc(elem_size); | |
576 | // we don't want to enforce error handling | |
577 | if (tmp == NULL) abort(); | |
578 | } else { | |
579 | tmp = sbo_mem; | |
580 | } | |
581 | ||
582 | // calculate memory locations | |
583 | char *left = arr, *right = arr; | |
584 | left += idx1 * elem_size; | |
585 | right += idx2 * elem_size; | |
586 | ||
587 | // three-way swap | |
588 | memcpy(tmp, left, elem_size); | |
589 | memcpy(left, right, elem_size); | |
590 | memcpy(right, tmp, elem_size); | |
591 | ||
592 | // free dynamic memory, if it was needed | |
593 | if (tmp != sbo_mem) { | |
594 | free(tmp); | |
595 | } | |
596 | } | |
597 | ||
598 | // HIGH LEVEL ARRAY LIST FUNCTIONS | |
599 | ||
600 | typedef struct { | |
601 | struct cx_list_s base; | |
602 | void *data; | |
603 | size_t capacity; | |
440 | 604 | CxArrayReallocator reallocator; |
174 | 605 | } cx_array_list; |
606 | ||
607 | static void cx_arl_destructor(struct cx_list_s *list) { | |
608 | cx_array_list *arl = (cx_array_list *) list; | |
609 | ||
610 | char *ptr = arl->data; | |
611 | ||
324 | 612 | if (list->collection.simple_destructor) { |
613 | for (size_t i = 0; i < list->collection.size; i++) { | |
174 | 614 | cx_invoke_simple_destructor(list, ptr); |
324 | 615 | ptr += list->collection.elem_size; |
174 | 616 | } |
617 | } | |
324 | 618 | if (list->collection.advanced_destructor) { |
619 | for (size_t i = 0; i < list->collection.size; i++) { | |
174 | 620 | cx_invoke_advanced_destructor(list, ptr); |
324 | 621 | ptr += list->collection.elem_size; |
174 | 622 | } |
623 | } | |
624 | ||
324 | 625 | cxFree(list->collection.allocator, arl->data); |
626 | cxFree(list->collection.allocator, list); | |
174 | 627 | } |
628 | ||
629 | static size_t cx_arl_insert_array( | |
630 | struct cx_list_s *list, | |
631 | size_t index, | |
324 | 632 | const void *array, |
174 | 633 | size_t n |
634 | ) { | |
635 | // out of bounds and special case check | |
324 | 636 | if (index > list->collection.size || n == 0) return 0; |
174 | 637 | |
638 | // get a correctly typed pointer to the list | |
639 | cx_array_list *arl = (cx_array_list *) list; | |
640 | ||
641 | // do we need to move some elements? | |
324 | 642 | if (index < list->collection.size) { |
643 | const char *first_to_move = (const char *) arl->data; | |
644 | first_to_move += index * list->collection.elem_size; | |
645 | size_t elems_to_move = list->collection.size - index; | |
174 | 646 | size_t start_of_moved = index + n; |
647 | ||
440 | 648 | if (cx_array_copy( |
174 | 649 | &arl->data, |
324 | 650 | &list->collection.size, |
174 | 651 | &arl->capacity, |
440 | 652 | 0, |
174 | 653 | start_of_moved, |
654 | first_to_move, | |
324 | 655 | list->collection.elem_size, |
174 | 656 | elems_to_move, |
657 | &arl->reallocator | |
658 | )) { | |
659 | // if moving existing elems is unsuccessful, abort | |
660 | return 0; | |
661 | } | |
662 | } | |
663 | ||
664 | // note that if we had to move the elements, the following operation | |
665 | // is guaranteed to succeed, because we have the memory already allocated | |
666 | // therefore, it is impossible to leave this function with an invalid array | |
667 | ||
668 | // place the new elements | |
440 | 669 | if (cx_array_copy( |
174 | 670 | &arl->data, |
324 | 671 | &list->collection.size, |
174 | 672 | &arl->capacity, |
440 | 673 | 0, |
174 | 674 | index, |
675 | array, | |
324 | 676 | list->collection.elem_size, |
677 | n, | |
678 | &arl->reallocator | |
679 | )) { | |
680 | // array list implementation is "all or nothing" | |
681 | return 0; | |
440 | 682 | } else { |
683 | return n; | |
324 | 684 | } |
685 | } | |
686 | ||
687 | static size_t cx_arl_insert_sorted( | |
688 | struct cx_list_s *list, | |
689 | const void *sorted_data, | |
690 | size_t n | |
691 | ) { | |
692 | // get a correctly typed pointer to the list | |
693 | cx_array_list *arl = (cx_array_list *) list; | |
694 | ||
440 | 695 | if (cx_array_insert_sorted( |
324 | 696 | &arl->data, |
697 | &list->collection.size, | |
698 | &arl->capacity, | |
699 | list->collection.cmpfunc, | |
700 | sorted_data, | |
701 | list->collection.elem_size, | |
174 | 702 | n, |
703 | &arl->reallocator | |
704 | )) { | |
705 | // array list implementation is "all or nothing" | |
706 | return 0; | |
440 | 707 | } else { |
708 | return n; | |
174 | 709 | } |
710 | } | |
711 | ||
712 | static int cx_arl_insert_element( | |
713 | struct cx_list_s *list, | |
714 | size_t index, | |
324 | 715 | const void *element |
174 | 716 | ) { |
717 | return 1 != cx_arl_insert_array(list, index, element, 1); | |
718 | } | |
719 | ||
720 | static int cx_arl_insert_iter( | |
324 | 721 | struct cx_iterator_s *iter, |
722 | const void *elem, | |
174 | 723 | int prepend |
724 | ) { | |
324 | 725 | struct cx_list_s *list = iter->src_handle.m; |
726 | if (iter->index < list->collection.size) { | |
174 | 727 | int result = cx_arl_insert_element( |
728 | list, | |
729 | iter->index + 1 - prepend, | |
730 | elem | |
731 | ); | |
324 | 732 | if (result == 0) { |
733 | iter->elem_count++; | |
734 | if (prepend != 0) { | |
735 | iter->index++; | |
736 | iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size; | |
737 | } | |
174 | 738 | } |
739 | return result; | |
740 | } else { | |
324 | 741 | int result = cx_arl_insert_element(list, list->collection.size, elem); |
742 | if (result == 0) { | |
743 | iter->elem_count++; | |
744 | iter->index = list->collection.size; | |
745 | } | |
174 | 746 | return result; |
747 | } | |
748 | } | |
749 | ||
440 | 750 | static size_t cx_arl_remove( |
174 | 751 | struct cx_list_s *list, |
440 | 752 | size_t index, |
753 | size_t num, | |
754 | void *targetbuf | |
174 | 755 | ) { |
756 | cx_array_list *arl = (cx_array_list *) list; | |
757 | ||
758 | // out-of-bounds check | |
440 | 759 | size_t remove; |
324 | 760 | if (index >= list->collection.size) { |
440 | 761 | remove = 0; |
762 | } else if (index + num > list->collection.size) { | |
763 | remove = list->collection.size - index; | |
764 | } else { | |
765 | remove = num; | |
174 | 766 | } |
767 | ||
440 | 768 | // easy exit |
769 | if (remove == 0) return 0; | |
174 | 770 | |
440 | 771 | // destroy or copy contents |
772 | if (targetbuf == NULL) { | |
773 | for (size_t idx = index; idx < index + remove; idx++) { | |
774 | cx_invoke_destructor( | |
775 | list, | |
776 | ((char *) arl->data) + idx * list->collection.elem_size | |
777 | ); | |
778 | } | |
779 | } else { | |
780 | memcpy( | |
781 | targetbuf, | |
782 | ((char *) arl->data) + index * list->collection.elem_size, | |
783 | remove * list->collection.elem_size | |
784 | ); | |
174 | 785 | } |
786 | ||
440 | 787 | // short-circuit removal of last elements |
788 | if (index + remove == list->collection.size) { | |
789 | list->collection.size -= remove; | |
790 | return remove; | |
791 | } | |
792 | ||
793 | // just move the elements to the left | |
794 | cx_array_copy( | |
174 | 795 | &arl->data, |
324 | 796 | &list->collection.size, |
174 | 797 | &arl->capacity, |
440 | 798 | 0, |
174 | 799 | index, |
440 | 800 | ((char *) arl->data) + (index + remove) * list->collection.elem_size, |
324 | 801 | list->collection.elem_size, |
440 | 802 | list->collection.size - index - remove, |
174 | 803 | &arl->reallocator |
804 | ); | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
805 | |
440 | 806 | // decrease the size |
807 | list->collection.size -= remove; | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
808 | |
440 | 809 | return remove; |
174 | 810 | } |
811 | ||
812 | static void cx_arl_clear(struct cx_list_s *list) { | |
324 | 813 | if (list->collection.size == 0) return; |
174 | 814 | |
815 | cx_array_list *arl = (cx_array_list *) list; | |
816 | char *ptr = arl->data; | |
817 | ||
324 | 818 | if (list->collection.simple_destructor) { |
819 | for (size_t i = 0; i < list->collection.size; i++) { | |
174 | 820 | cx_invoke_simple_destructor(list, ptr); |
324 | 821 | ptr += list->collection.elem_size; |
174 | 822 | } |
823 | } | |
324 | 824 | if (list->collection.advanced_destructor) { |
825 | for (size_t i = 0; i < list->collection.size; i++) { | |
174 | 826 | cx_invoke_advanced_destructor(list, ptr); |
324 | 827 | ptr += list->collection.elem_size; |
174 | 828 | } |
829 | } | |
830 | ||
324 | 831 | memset(arl->data, 0, list->collection.size * list->collection.elem_size); |
832 | list->collection.size = 0; | |
174 | 833 | } |
834 | ||
835 | static int cx_arl_swap( | |
836 | struct cx_list_s *list, | |
837 | size_t i, | |
838 | size_t j | |
839 | ) { | |
324 | 840 | if (i >= list->collection.size || j >= list->collection.size) return 1; |
174 | 841 | cx_array_list *arl = (cx_array_list *) list; |
324 | 842 | cx_array_swap(arl->data, list->collection.elem_size, i, j); |
174 | 843 | return 0; |
844 | } | |
845 | ||
846 | static void *cx_arl_at( | |
324 | 847 | const struct cx_list_s *list, |
174 | 848 | size_t index |
849 | ) { | |
324 | 850 | if (index < list->collection.size) { |
851 | const cx_array_list *arl = (const cx_array_list *) list; | |
174 | 852 | char *space = arl->data; |
324 | 853 | return space + index * list->collection.elem_size; |
174 | 854 | } else { |
855 | return NULL; | |
856 | } | |
857 | } | |
858 | ||
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
859 | static ssize_t cx_arl_find_remove( |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
860 | struct cx_list_s *list, |
324 | 861 | const void *elem, |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
862 | bool remove |
174 | 863 | ) { |
324 | 864 | assert(list->collection.cmpfunc != NULL); |
865 | assert(list->collection.size < SIZE_MAX / 2); | |
866 | char *cur = ((const cx_array_list *) list)->data; | |
174 | 867 | |
324 | 868 | for (ssize_t i = 0; i < (ssize_t) list->collection.size; i++) { |
869 | if (0 == list->collection.cmpfunc(elem, cur)) { | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
870 | if (remove) { |
440 | 871 | if (1 == cx_arl_remove(list, i, 1, NULL)) { |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
872 | return i; |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
873 | } else { |
440 | 874 | // should be unreachable |
875 | return -1; // LCOV_EXCL_LINE | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
876 | } |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
877 | } else { |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
878 | return i; |
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
879 | } |
174 | 880 | } |
324 | 881 | cur += list->collection.elem_size; |
174 | 882 | } |
883 | ||
884 | return -1; | |
885 | } | |
886 | ||
887 | static void cx_arl_sort(struct cx_list_s *list) { | |
324 | 888 | assert(list->collection.cmpfunc != NULL); |
174 | 889 | qsort(((cx_array_list *) list)->data, |
324 | 890 | list->collection.size, |
891 | list->collection.elem_size, | |
892 | list->collection.cmpfunc | |
174 | 893 | ); |
894 | } | |
895 | ||
896 | static int cx_arl_compare( | |
324 | 897 | const struct cx_list_s *list, |
898 | const struct cx_list_s *other | |
174 | 899 | ) { |
324 | 900 | assert(list->collection.cmpfunc != NULL); |
901 | if (list->collection.size == other->collection.size) { | |
902 | const char *left = ((const cx_array_list *) list)->data; | |
903 | const char *right = ((const cx_array_list *) other)->data; | |
904 | for (size_t i = 0; i < list->collection.size; i++) { | |
905 | int d = list->collection.cmpfunc(left, right); | |
174 | 906 | if (d != 0) { |
907 | return d; | |
908 | } | |
324 | 909 | left += list->collection.elem_size; |
910 | right += other->collection.elem_size; | |
174 | 911 | } |
912 | return 0; | |
913 | } else { | |
324 | 914 | return list->collection.size < other->collection.size ? -1 : 1; |
174 | 915 | } |
916 | } | |
917 | ||
918 | static void cx_arl_reverse(struct cx_list_s *list) { | |
324 | 919 | if (list->collection.size < 2) return; |
920 | void *data = ((const cx_array_list *) list)->data; | |
921 | size_t half = list->collection.size / 2; | |
174 | 922 | for (size_t i = 0; i < half; i++) { |
324 | 923 | cx_array_swap(data, list->collection.elem_size, i, list->collection.size - 1 - i); |
174 | 924 | } |
925 | } | |
926 | ||
324 | 927 | static bool cx_arl_iter_valid(const void *it) { |
928 | const struct cx_iterator_s *iter = it; | |
929 | const struct cx_list_s *list = iter->src_handle.c; | |
930 | return iter->index < list->collection.size; | |
174 | 931 | } |
932 | ||
324 | 933 | static void *cx_arl_iter_current(const void *it) { |
934 | const struct cx_iterator_s *iter = it; | |
174 | 935 | return iter->elem_handle; |
936 | } | |
937 | ||
938 | static void cx_arl_iter_next(void *it) { | |
324 | 939 | struct cx_iterator_s *iter = it; |
940 | if (iter->base.remove) { | |
941 | iter->base.remove = false; | |
440 | 942 | cx_arl_remove(iter->src_handle.m, iter->index, 1, NULL); |
174 | 943 | } else { |
944 | iter->index++; | |
945 | iter->elem_handle = | |
946 | ((char *) iter->elem_handle) | |
324 | 947 | + ((const struct cx_list_s *) iter->src_handle.c)->collection.elem_size; |
174 | 948 | } |
949 | } | |
950 | ||
951 | static void cx_arl_iter_prev(void *it) { | |
324 | 952 | struct cx_iterator_s *iter = it; |
953 | const cx_array_list *list = iter->src_handle.c; | |
954 | if (iter->base.remove) { | |
955 | iter->base.remove = false; | |
440 | 956 | cx_arl_remove(iter->src_handle.m, iter->index, 1, NULL); |
174 | 957 | } |
958 | iter->index--; | |
324 | 959 | if (iter->index < list->base.collection.size) { |
174 | 960 | iter->elem_handle = ((char *) list->data) |
324 | 961 | + iter->index * list->base.collection.elem_size; |
174 | 962 | } |
963 | } | |
964 | ||
965 | ||
966 | static struct cx_iterator_s cx_arl_iterator( | |
324 | 967 | const struct cx_list_s *list, |
174 | 968 | size_t index, |
969 | bool backwards | |
970 | ) { | |
971 | struct cx_iterator_s iter; | |
972 | ||
973 | iter.index = index; | |
324 | 974 | iter.src_handle.c = list; |
174 | 975 | iter.elem_handle = cx_arl_at(list, index); |
324 | 976 | iter.elem_size = list->collection.elem_size; |
977 | iter.elem_count = list->collection.size; | |
174 | 978 | iter.base.valid = cx_arl_iter_valid; |
979 | iter.base.current = cx_arl_iter_current; | |
980 | iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next; | |
981 | iter.base.remove = false; | |
982 | iter.base.mutating = false; | |
983 | ||
984 | return iter; | |
985 | } | |
986 | ||
987 | static cx_list_class cx_array_list_class = { | |
988 | cx_arl_destructor, | |
989 | cx_arl_insert_element, | |
990 | cx_arl_insert_array, | |
324 | 991 | cx_arl_insert_sorted, |
174 | 992 | cx_arl_insert_iter, |
993 | cx_arl_remove, | |
994 | cx_arl_clear, | |
995 | cx_arl_swap, | |
996 | cx_arl_at, | |
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
997 | cx_arl_find_remove, |
174 | 998 | cx_arl_sort, |
999 | cx_arl_compare, | |
1000 | cx_arl_reverse, | |
1001 | cx_arl_iterator, | |
1002 | }; | |
1003 | ||
1004 | CxList *cxArrayListCreate( | |
324 | 1005 | const CxAllocator *allocator, |
174 | 1006 | cx_compare_func comparator, |
324 | 1007 | size_t elem_size, |
174 | 1008 | size_t initial_capacity |
1009 | ) { | |
1010 | if (allocator == NULL) { | |
1011 | allocator = cxDefaultAllocator; | |
1012 | } | |
1013 | ||
1014 | cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); | |
1015 | if (list == NULL) return NULL; | |
1016 | ||
1017 | list->base.cl = &cx_array_list_class; | |
324 | 1018 | list->base.collection.allocator = allocator; |
174 | 1019 | list->capacity = initial_capacity; |
1020 | ||
324 | 1021 | if (elem_size > 0) { |
1022 | list->base.collection.elem_size = elem_size; | |
1023 | list->base.collection.cmpfunc = comparator; | |
174 | 1024 | } else { |
324 | 1025 | elem_size = sizeof(void *); |
1026 | list->base.collection.cmpfunc = comparator == NULL ? cx_cmp_ptr : comparator; | |
174 | 1027 | cxListStorePointers((CxList *) list); |
1028 | } | |
1029 | ||
324 | 1030 | // allocate the array after the real elem_size is known |
1031 | list->data = cxCalloc(allocator, initial_capacity, elem_size); | |
440 | 1032 | if (list->data == NULL) { // LCOV_EXCL_START |
174 | 1033 | cxFree(allocator, list); |
1034 | return NULL; | |
440 | 1035 | } // LCOV_EXCL_STOP |
174 | 1036 | |
1037 | // configure the reallocator | |
440 | 1038 | list->reallocator = cx_array_reallocator(allocator, NULL); |
174 | 1039 | |
1040 | return (CxList *) list; | |
1041 | } |