Wed, 19 Nov 2025 13:02:55 +0100
implement listview selection events (Win32)
| 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, |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
39 | cx_attr_unused size_t old_capacity, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
40 | size_t new_capacity, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
41 | size_t elem_size, |
| 440 | 42 | cx_attr_unused CxArrayReallocator *alloc |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
43 | ) { |
| 440 | 44 | size_t n; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
45 | if (cx_szmul(new_capacity, elem_size, &n)) { |
| 440 | 46 | errno = EOVERFLOW; |
| 47 | return NULL; | |
| 48 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
49 | return cxReallocDefault(array, n); |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
50 | } |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
51 | |
| 440 | 52 | CxArrayReallocator cx_array_default_reallocator_impl = { |
| 870 | 53 | cx_array_default_realloc, NULL, NULL |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
54 | }; |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
55 | |
| 440 | 56 | CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl; |
| 57 | ||
| 58 | // Stack-aware array reallocator | |
| 59 | ||
| 60 | static void *cx_array_advanced_realloc( | |
| 61 | void *array, | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
62 | size_t old_capacity, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
63 | size_t new_capacity, |
| 440 | 64 | size_t elem_size, |
| 65 | cx_attr_unused CxArrayReallocator *alloc | |
| 66 | ) { | |
| 67 | // check for overflow | |
| 68 | size_t n; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
69 | if (cx_szmul(new_capacity, elem_size, &n)) { |
| 440 | 70 | errno = EOVERFLOW; |
| 71 | return NULL; | |
| 72 | } | |
| 73 | ||
| 74 | // retrieve the pointer to the actual allocator | |
| 870 | 75 | const CxAllocator *al = alloc->allocator; |
| 440 | 76 | |
| 77 | // check if the array is still located on the stack | |
| 78 | void *newmem; | |
| 870 | 79 | if (array == alloc->stack_ptr) { |
| 440 | 80 | newmem = cxMalloc(al, n); |
| 81 | if (newmem != NULL && array != NULL) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
82 | memcpy(newmem, array, old_capacity*elem_size); |
| 440 | 83 | } |
| 84 | } else { | |
| 85 | newmem = cxRealloc(al, array, n); | |
| 86 | } | |
| 87 | return newmem; | |
| 88 | } | |
| 89 | ||
| 90 | struct cx_array_reallocator_s cx_array_reallocator( | |
| 91 | const struct cx_allocator_s *allocator, | |
| 870 | 92 | const void *stack_ptr |
| 440 | 93 | ) { |
| 94 | if (allocator == NULL) { | |
| 95 | allocator = cxDefaultAllocator; | |
| 96 | } | |
| 97 | return (struct cx_array_reallocator_s) { | |
| 98 | cx_array_advanced_realloc, | |
| 870 | 99 | allocator, stack_ptr, |
| 440 | 100 | }; |
| 101 | } | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
102 | |
| 174 | 103 | // LOW LEVEL ARRAY LIST FUNCTIONS |
| 104 | ||
| 440 | 105 | static size_t cx_array_align_capacity( |
| 106 | size_t cap, | |
| 107 | size_t alignment, | |
| 108 | size_t max | |
| 109 | ) { | |
| 110 | if (cap > max - alignment) { | |
| 111 | return cap; | |
| 112 | } else { | |
| 113 | return cap - (cap % alignment) + alignment; | |
| 114 | } | |
| 115 | } | |
| 116 | ||
| 117 | int cx_array_reserve( | |
| 118 | void **array, | |
| 119 | void *size, | |
| 120 | void *capacity, | |
| 121 | unsigned width, | |
| 122 | size_t elem_size, | |
| 123 | size_t elem_count, | |
| 124 | CxArrayReallocator *reallocator | |
| 125 | ) { | |
| 126 | // assert pointers | |
| 127 | assert(array != NULL); | |
| 128 | assert(size != NULL); | |
| 129 | assert(capacity != NULL); | |
| 130 | ||
| 131 | // default reallocator | |
| 132 | if (reallocator == NULL) { | |
| 133 | reallocator = cx_array_default_reallocator; | |
| 134 | } | |
| 135 | ||
| 136 | // determine size and capacity | |
| 137 | size_t oldcap; | |
| 138 | size_t oldsize; | |
| 139 | size_t max_size; | |
| 140 | if (width == 0 || width == sizeof(size_t)) { | |
| 141 | oldcap = *(size_t*) capacity; | |
| 142 | oldsize = *(size_t*) size; | |
| 143 | max_size = SIZE_MAX; | |
| 144 | } else if (width == sizeof(uint16_t)) { | |
| 145 | oldcap = *(uint16_t*) capacity; | |
| 146 | oldsize = *(uint16_t*) size; | |
| 147 | max_size = UINT16_MAX; | |
| 148 | } else if (width == sizeof(uint8_t)) { | |
| 149 | oldcap = *(uint8_t*) capacity; | |
| 150 | oldsize = *(uint8_t*) size; | |
| 151 | max_size = UINT8_MAX; | |
| 152 | } | |
| 153 | #if CX_WORDSIZE == 64 | |
| 154 | else if (width == sizeof(uint32_t)) { | |
| 155 | oldcap = *(uint32_t*) capacity; | |
| 156 | oldsize = *(uint32_t*) size; | |
| 157 | max_size = UINT32_MAX; | |
| 158 | } | |
| 159 | #endif | |
| 160 | else { | |
| 161 | errno = EINVAL; | |
| 162 | return 1; | |
| 163 | } | |
| 164 | ||
| 165 | // assert that the array is allocated when it has capacity | |
| 166 | assert(*array != NULL || oldcap == 0); | |
| 167 | ||
| 168 | // check for overflow | |
| 169 | if (elem_count > max_size - oldsize) { | |
| 170 | errno = EOVERFLOW; | |
| 171 | return 1; | |
| 172 | } | |
| 173 | ||
| 174 | // determine new capacity | |
| 175 | size_t newcap = oldsize + elem_count; | |
| 176 | ||
| 177 | // reallocate if possible | |
| 178 | if (newcap > oldcap) { | |
| 179 | // calculate new capacity (next number divisible by 16) | |
| 180 | newcap = cx_array_align_capacity(newcap, 16, max_size); | |
| 181 | ||
| 182 | // perform reallocation | |
| 183 | void *newmem = reallocator->realloc( | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
184 | *array, oldcap, newcap, elem_size, reallocator |
| 440 | 185 | ); |
| 186 | if (newmem == NULL) { | |
| 187 | return 1; // LCOV_EXCL_LINE | |
| 188 | } | |
| 189 | ||
| 190 | // store new pointer | |
| 191 | *array = newmem; | |
| 192 | ||
| 193 | // store new capacity | |
| 194 | if (width == 0 || width == sizeof(size_t)) { | |
| 195 | *(size_t*) capacity = newcap; | |
| 196 | } else if (width == sizeof(uint16_t)) { | |
| 197 | *(uint16_t*) capacity = (uint16_t) newcap; | |
| 198 | } else if (width == sizeof(uint8_t)) { | |
| 199 | *(uint8_t*) capacity = (uint8_t) newcap; | |
| 200 | } | |
| 201 | #if CX_WORDSIZE == 64 | |
| 202 | else if (width == sizeof(uint32_t)) { | |
| 203 | *(uint32_t*) capacity = (uint32_t) newcap; | |
| 204 | } | |
| 205 | #endif | |
| 206 | } | |
| 207 | ||
| 208 | return 0; | |
| 209 | } | |
| 210 | ||
| 211 | int cx_array_copy( | |
| 174 | 212 | void **target, |
| 440 | 213 | void *size, |
| 214 | void *capacity, | |
| 215 | unsigned width, | |
| 174 | 216 | size_t index, |
| 324 | 217 | const void *src, |
| 174 | 218 | size_t elem_size, |
| 219 | size_t elem_count, | |
| 440 | 220 | CxArrayReallocator *reallocator |
| 174 | 221 | ) { |
| 222 | // assert pointers | |
| 223 | assert(target != NULL); | |
| 224 | assert(size != NULL); | |
| 440 | 225 | assert(capacity != NULL); |
| 174 | 226 | assert(src != NULL); |
| 227 | ||
| 440 | 228 | // default reallocator |
| 229 | if (reallocator == NULL) { | |
| 230 | reallocator = cx_array_default_reallocator; | |
| 231 | } | |
| 232 | ||
| 233 | // determine size and capacity | |
| 234 | size_t oldcap; | |
| 235 | size_t oldsize; | |
| 236 | size_t max_size; | |
| 237 | if (width == 0 || width == sizeof(size_t)) { | |
| 238 | oldcap = *(size_t*) capacity; | |
| 239 | oldsize = *(size_t*) size; | |
| 240 | max_size = SIZE_MAX; | |
| 241 | } else if (width == sizeof(uint16_t)) { | |
| 242 | oldcap = *(uint16_t*) capacity; | |
| 243 | oldsize = *(uint16_t*) size; | |
| 244 | max_size = UINT16_MAX; | |
| 245 | } else if (width == sizeof(uint8_t)) { | |
| 246 | oldcap = *(uint8_t*) capacity; | |
| 247 | oldsize = *(uint8_t*) size; | |
| 248 | max_size = UINT8_MAX; | |
| 249 | } | |
| 250 | #if CX_WORDSIZE == 64 | |
| 251 | else if (width == sizeof(uint32_t)) { | |
| 252 | oldcap = *(uint32_t*) capacity; | |
| 253 | oldsize = *(uint32_t*) size; | |
| 254 | max_size = UINT32_MAX; | |
| 255 | } | |
| 256 | #endif | |
| 257 | else { | |
| 258 | errno = EINVAL; | |
| 259 | return 1; | |
| 260 | } | |
| 261 | ||
| 262 | // assert that the array is allocated when it has capacity | |
| 263 | assert(*target != NULL || oldcap == 0); | |
| 264 | ||
| 265 | // check for overflow | |
| 266 | if (index > max_size || elem_count > max_size - index) { | |
| 267 | errno = EOVERFLOW; | |
| 268 | return 1; | |
| 269 | } | |
| 174 | 270 | |
| 271 | // check if resize is required | |
| 272 | size_t minsize = index + elem_count; | |
| 440 | 273 | size_t newsize = oldsize < minsize ? minsize : oldsize; |
| 174 | 274 | |
| 275 | // reallocate if possible | |
| 440 | 276 | size_t newcap = oldcap; |
| 277 | if (newsize > oldcap) { | |
| 174 | 278 | // check, if we need to repair the src pointer |
| 279 | uintptr_t targetaddr = (uintptr_t) *target; | |
| 280 | uintptr_t srcaddr = (uintptr_t) src; | |
| 281 | bool repairsrc = targetaddr <= srcaddr | |
| 440 | 282 | && srcaddr < targetaddr + oldcap * elem_size; |
| 174 | 283 | |
| 284 | // calculate new capacity (next number divisible by 16) | |
| 440 | 285 | newcap = cx_array_align_capacity(newsize, 16, max_size); |
| 286 | assert(newcap > newsize); | |
| 174 | 287 | |
| 288 | // perform reallocation | |
| 289 | void *newmem = reallocator->realloc( | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
290 | *target, oldcap, newcap, elem_size, reallocator |
| 174 | 291 | ); |
| 292 | if (newmem == NULL) { | |
| 845 | 293 | return 1; // LCOV_EXCL_LINE |
| 174 | 294 | } |
| 295 | ||
| 296 | // repair src pointer, if necessary | |
| 297 | if (repairsrc) { | |
| 298 | src = ((char *) newmem) + (srcaddr - targetaddr); | |
| 299 | } | |
| 300 | ||
| 440 | 301 | // store new pointer |
| 174 | 302 | *target = newmem; |
| 303 | } | |
| 304 | ||
| 305 | // determine target pointer | |
| 306 | char *start = *target; | |
| 307 | start += index * elem_size; | |
| 308 | ||
| 309 | // copy elements and set new size | |
| 440 | 310 | // note: no overflow check here, b/c we cannot get here w/o allocation |
| 174 | 311 | memmove(start, src, elem_count * elem_size); |
| 440 | 312 | |
| 313 | // if any of size or capacity changed, store them back | |
| 314 | if (newsize != oldsize || newcap != oldcap) { | |
| 315 | if (width == 0 || width == sizeof(size_t)) { | |
| 316 | *(size_t*) capacity = newcap; | |
| 317 | *(size_t*) size = newsize; | |
| 318 | } else if (width == sizeof(uint16_t)) { | |
| 319 | *(uint16_t*) capacity = (uint16_t) newcap; | |
| 320 | *(uint16_t*) size = (uint16_t) newsize; | |
| 321 | } else if (width == sizeof(uint8_t)) { | |
| 322 | *(uint8_t*) capacity = (uint8_t) newcap; | |
| 323 | *(uint8_t*) size = (uint8_t) newsize; | |
| 324 | } | |
| 325 | #if CX_WORDSIZE == 64 | |
| 326 | else if (width == sizeof(uint32_t)) { | |
| 327 | *(uint32_t*) capacity = (uint32_t) newcap; | |
| 328 | *(uint32_t*) size = (uint32_t) newsize; | |
| 329 | } | |
| 330 | #endif | |
| 331 | } | |
| 174 | 332 | |
| 333 | // return successfully | |
| 440 | 334 | return 0; |
| 174 | 335 | } |
| 336 | ||
| 845 | 337 | static int cx_array_insert_sorted_impl( |
| 324 | 338 | void **target, |
| 339 | size_t *size, | |
| 340 | size_t *capacity, | |
| 341 | cx_compare_func cmp_func, | |
| 342 | const void *sorted_data, | |
| 343 | size_t elem_size, | |
| 344 | size_t elem_count, | |
| 845 | 345 | CxArrayReallocator *reallocator, |
| 346 | bool allow_duplicates | |
| 324 | 347 | ) { |
| 348 | // assert pointers | |
| 349 | assert(target != NULL); | |
| 350 | assert(size != NULL); | |
| 351 | assert(capacity != NULL); | |
| 352 | assert(cmp_func != NULL); | |
| 353 | assert(sorted_data != NULL); | |
| 440 | 354 | |
| 355 | // default reallocator | |
| 356 | if (reallocator == NULL) { | |
| 357 | reallocator = cx_array_default_reallocator; | |
| 358 | } | |
| 324 | 359 | |
| 360 | // corner case | |
| 361 | if (elem_count == 0) return 0; | |
| 362 | ||
| 440 | 363 | // overflow check |
| 364 | if (elem_count > SIZE_MAX - *size) { | |
| 365 | errno = EOVERFLOW; | |
| 366 | return 1; | |
| 367 | } | |
| 368 | ||
| 324 | 369 | // store some counts |
| 370 | size_t old_size = *size; | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
371 | size_t old_capacity = *capacity; |
| 845 | 372 | // the necessary capacity is the worst case assumption, including duplicates |
| 324 | 373 | size_t needed_capacity = old_size + elem_count; |
| 374 | ||
| 375 | // if we need more than we have, try a reallocation | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
376 | if (needed_capacity > old_capacity) { |
| 440 | 377 | size_t new_capacity = cx_array_align_capacity(needed_capacity, 16, SIZE_MAX); |
| 324 | 378 | void *new_mem = reallocator->realloc( |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
379 | *target, old_capacity, new_capacity, elem_size, reallocator |
| 324 | 380 | ); |
| 381 | if (new_mem == NULL) { | |
| 382 | // give it up right away, there is no contract | |
| 383 | // that requires us to insert as much as we can | |
| 440 | 384 | return 1; // LCOV_EXCL_LINE |
| 324 | 385 | } |
| 386 | *target = new_mem; | |
| 387 | *capacity = new_capacity; | |
| 388 | } | |
| 389 | ||
| 390 | // now we have guaranteed that we can insert everything | |
| 391 | size_t new_size = old_size + elem_count; | |
| 392 | *size = new_size; | |
| 393 | ||
| 394 | // declare the source and destination indices/pointers | |
| 395 | size_t si = 0, di = 0; | |
| 396 | const char *src = sorted_data; | |
| 397 | char *dest = *target; | |
| 398 | ||
| 399 | // find the first insertion point | |
| 400 | di = cx_array_binary_search_sup(dest, old_size, elem_size, src, cmp_func); | |
| 401 | dest += di * elem_size; | |
| 402 | ||
| 403 | // move the remaining elements in the array completely to the right | |
| 404 | // we will call it the "buffer" for parked elements | |
| 405 | size_t buf_size = old_size - di; | |
| 406 | size_t bi = new_size - buf_size; | |
| 407 | char *bptr = ((char *) *target) + bi * elem_size; | |
| 408 | memmove(bptr, dest, buf_size * elem_size); | |
| 409 | ||
| 410 | // while there are both source and buffered elements left, | |
| 411 | // copy them interleaving | |
| 412 | while (si < elem_count && bi < new_size) { | |
| 413 | // determine how many source elements can be inserted | |
| 414 | size_t copy_len, bytes_copied; | |
| 415 | copy_len = cx_array_binary_search_sup( | |
| 416 | src, | |
| 417 | elem_count - si, | |
| 418 | elem_size, | |
| 419 | bptr, | |
| 420 | cmp_func | |
| 421 | ); | |
| 845 | 422 | // binary search gives us the smallest index; |
| 423 | // we also want to include equal elements here | |
| 424 | while (si + copy_len < elem_count | |
| 425 | && cmp_func(bptr, src+copy_len*elem_size) == 0) { | |
| 426 | copy_len++; | |
| 427 | } | |
| 324 | 428 | |
| 429 | // copy the source elements | |
| 845 | 430 | if (copy_len > 0) { |
| 431 | if (allow_duplicates) { | |
| 432 | // we can copy the entire chunk | |
| 433 | bytes_copied = copy_len * elem_size; | |
| 434 | memcpy(dest, src, bytes_copied); | |
| 435 | dest += bytes_copied; | |
| 436 | src += bytes_copied; | |
| 437 | si += copy_len; | |
| 438 | di += copy_len; | |
| 439 | } else { | |
| 440 | // first, check the end of the source chunk | |
| 441 | // for being a duplicate of the bptr | |
| 442 | const char *end_of_src = src + (copy_len - 1) * elem_size; | |
| 443 | size_t skip_len = 0; | |
| 444 | while (copy_len > 0 && cmp_func(bptr, end_of_src) == 0) { | |
| 445 | end_of_src -= elem_size; | |
| 446 | skip_len++; | |
| 447 | copy_len--; | |
| 448 | } | |
| 449 | char *last = dest == *target ? NULL : dest - elem_size; | |
| 450 | // then iterate through the source chunk | |
| 451 | // and skip all duplicates with the last element in the array | |
| 452 | size_t more_skipped = 0; | |
| 453 | for (unsigned j = 0; j < copy_len; j++) { | |
| 454 | if (last != NULL && cmp_func(last, src) == 0) { | |
| 455 | // duplicate - skip | |
| 456 | src += elem_size; | |
| 457 | si++; | |
| 458 | more_skipped++; | |
| 459 | } else { | |
| 460 | memcpy(dest, src, elem_size); | |
| 461 | src += elem_size; | |
| 462 | last = dest; | |
| 463 | dest += elem_size; | |
| 464 | si++; | |
| 465 | di++; | |
| 466 | } | |
| 467 | } | |
| 468 | // skip the previously identified elements as well | |
| 469 | src += skip_len * elem_size; | |
| 470 | si += skip_len; | |
| 471 | skip_len += more_skipped; | |
| 472 | // reduce the actual size by the number of skipped elements | |
| 473 | *size -= skip_len; | |
| 474 | } | |
| 475 | } | |
| 324 | 476 | |
| 477 | // when all source elements are in place, we are done | |
| 478 | if (si >= elem_count) break; | |
| 479 | ||
| 480 | // determine how many buffered elements need to be restored | |
| 481 | copy_len = cx_array_binary_search_sup( | |
| 482 | bptr, | |
| 483 | new_size - bi, | |
| 484 | elem_size, | |
| 485 | src, | |
| 486 | cmp_func | |
| 487 | ); | |
| 488 | ||
| 489 | // restore the buffered elements | |
| 490 | bytes_copied = copy_len * elem_size; | |
| 491 | memmove(dest, bptr, bytes_copied); | |
| 492 | dest += bytes_copied; | |
| 493 | bptr += bytes_copied; | |
| 845 | 494 | di += copy_len; |
| 324 | 495 | bi += copy_len; |
| 496 | } | |
| 497 | ||
| 845 | 498 | // still source elements left? |
| 324 | 499 | if (si < elem_count) { |
| 845 | 500 | if (allow_duplicates) { |
| 501 | // duplicates allowed or nothing inserted yet: simply copy everything | |
| 502 | memcpy(dest, src, elem_size * (elem_count - si)); | |
| 503 | } else { | |
| 504 | if (dest != *target) { | |
| 505 | // skip all source elements that equal the last element | |
| 506 | char *last = dest - elem_size; | |
| 507 | while (si < elem_count) { | |
| 508 | if (last != NULL && cmp_func(last, src) == 0) { | |
| 509 | src += elem_size; | |
| 510 | si++; | |
| 511 | (*size)--; | |
| 512 | } else { | |
| 513 | break; | |
| 514 | } | |
| 515 | } | |
| 516 | } | |
| 517 | // we must check the elements in the chunk one by one | |
| 518 | while (si < elem_count) { | |
| 519 | // find a chain of elements that can be copied | |
| 520 | size_t copy_len = 1, skip_len = 0; | |
| 521 | { | |
| 522 | const char *left_src = src; | |
| 523 | while (si + copy_len < elem_count) { | |
| 524 | const char *right_src = left_src + elem_size; | |
| 525 | int d = cmp_func(left_src, right_src); | |
| 526 | if (d < 0) { | |
| 527 | if (skip_len > 0) { | |
| 528 | // new larger element found; | |
| 529 | // handle it in the next cycle | |
| 530 | break; | |
| 531 | } | |
| 532 | left_src += elem_size; | |
| 533 | copy_len++; | |
| 534 | } else if (d == 0) { | |
| 535 | left_src += elem_size; | |
| 536 | skip_len++; | |
| 537 | } else { | |
| 538 | break; | |
| 539 | } | |
| 540 | } | |
| 541 | } | |
| 542 | size_t bytes_copied = copy_len * elem_size; | |
| 543 | memcpy(dest, src, bytes_copied); | |
| 544 | dest += bytes_copied; | |
| 545 | src += bytes_copied + skip_len * elem_size; | |
| 546 | si += copy_len + skip_len; | |
| 547 | di += copy_len; | |
| 548 | *size -= skip_len; | |
| 549 | } | |
| 550 | } | |
| 324 | 551 | } |
| 552 | ||
| 845 | 553 | // buffered elements need to be moved when we skipped duplicates |
| 554 | size_t total_skipped = new_size - *size; | |
| 555 | if (bi < new_size && total_skipped > 0) { | |
| 556 | // move the remaining buffer to the end of the array | |
| 557 | memmove(dest, bptr, elem_size * (new_size - bi)); | |
| 558 | } | |
| 324 | 559 | |
| 440 | 560 | return 0; |
| 324 | 561 | } |
| 562 | ||
| 845 | 563 | int cx_array_insert_sorted( |
| 564 | void **target, | |
| 565 | size_t *size, | |
| 566 | size_t *capacity, | |
| 567 | cx_compare_func cmp_func, | |
| 568 | const void *sorted_data, | |
| 569 | size_t elem_size, | |
| 570 | size_t elem_count, | |
| 571 | CxArrayReallocator *reallocator | |
| 572 | ) { | |
| 573 | return cx_array_insert_sorted_impl(target, size, capacity, | |
| 574 | cmp_func, sorted_data, elem_size, elem_count, reallocator, true); | |
| 575 | } | |
| 576 | ||
| 577 | int cx_array_insert_unique( | |
| 578 | void **target, | |
| 579 | size_t *size, | |
| 580 | size_t *capacity, | |
| 581 | cx_compare_func cmp_func, | |
| 582 | const void *sorted_data, | |
| 583 | size_t elem_size, | |
| 584 | size_t elem_count, | |
| 585 | CxArrayReallocator *reallocator | |
| 586 | ) { | |
| 587 | return cx_array_insert_sorted_impl(target, size, capacity, | |
| 588 | cmp_func, sorted_data, elem_size, elem_count, reallocator, false); | |
| 589 | } | |
| 590 | ||
| 324 | 591 | size_t cx_array_binary_search_inf( |
| 592 | const void *arr, | |
| 593 | size_t size, | |
| 594 | size_t elem_size, | |
| 595 | const void *elem, | |
| 596 | cx_compare_func cmp_func | |
| 597 | ) { | |
| 598 | // special case: empty array | |
| 599 | if (size == 0) return 0; | |
| 600 | ||
| 601 | // declare a variable that will contain the compare results | |
| 602 | int result; | |
| 603 | ||
| 604 | // cast the array pointer to something we can use offsets with | |
| 605 | const char *array = arr; | |
| 606 | ||
| 607 | // check the first array element | |
| 608 | result = cmp_func(elem, array); | |
| 609 | if (result < 0) { | |
| 610 | return size; | |
| 611 | } else if (result == 0) { | |
| 612 | return 0; | |
| 613 | } | |
| 614 | ||
| 440 | 615 | // special case: there is only one element and that is smaller |
| 616 | if (size == 1) return 0; | |
| 617 | ||
| 324 | 618 | // check the last array element |
| 619 | result = cmp_func(elem, array + elem_size * (size - 1)); | |
| 620 | if (result >= 0) { | |
| 621 | return size - 1; | |
| 622 | } | |
| 623 | ||
| 624 | // the element is now guaranteed to be somewhere in the list | |
| 625 | // so start the binary search | |
| 626 | size_t left_index = 1; | |
| 627 | size_t right_index = size - 1; | |
| 628 | size_t pivot_index; | |
| 629 | ||
| 630 | while (left_index <= right_index) { | |
| 631 | pivot_index = left_index + (right_index - left_index) / 2; | |
| 632 | const char *arr_elem = array + pivot_index * elem_size; | |
| 633 | result = cmp_func(elem, arr_elem); | |
| 634 | if (result == 0) { | |
| 635 | // found it! | |
| 845 | 636 | // check previous elements; |
| 637 | // when they are equal, report the smallest index | |
| 638 | arr_elem -= elem_size; | |
| 639 | while (pivot_index > 0 && cmp_func(elem, arr_elem) == 0) { | |
| 640 | pivot_index--; | |
| 641 | arr_elem -= elem_size; | |
| 642 | } | |
| 324 | 643 | return pivot_index; |
| 644 | } else if (result < 0) { | |
| 645 | // element is smaller than pivot, continue search left | |
| 646 | right_index = pivot_index - 1; | |
| 647 | } else { | |
| 648 | // element is larger than pivot, continue search right | |
| 649 | left_index = pivot_index + 1; | |
| 650 | } | |
| 651 | } | |
| 652 | ||
| 653 | // report the largest upper bound | |
| 654 | return result < 0 ? (pivot_index - 1) : pivot_index; | |
| 655 | } | |
| 656 | ||
| 440 | 657 | size_t cx_array_binary_search( |
| 658 | const void *arr, | |
| 659 | size_t size, | |
| 660 | size_t elem_size, | |
| 661 | const void *elem, | |
| 662 | cx_compare_func cmp_func | |
| 663 | ) { | |
| 664 | size_t index = cx_array_binary_search_inf( | |
| 665 | arr, size, elem_size, elem, cmp_func | |
| 666 | ); | |
| 667 | if (index < size && | |
| 668 | cmp_func(((const char *) arr) + index * elem_size, elem) == 0) { | |
| 669 | return index; | |
| 670 | } else { | |
| 671 | return size; | |
| 672 | } | |
| 673 | } | |
| 674 | ||
| 675 | size_t cx_array_binary_search_sup( | |
| 676 | const void *arr, | |
| 677 | size_t size, | |
| 678 | size_t elem_size, | |
| 679 | const void *elem, | |
| 680 | cx_compare_func cmp_func | |
| 681 | ) { | |
| 682 | size_t inf = cx_array_binary_search_inf( | |
| 683 | arr, size, elem_size, elem, cmp_func | |
| 684 | ); | |
| 685 | if (inf == size) { | |
| 686 | // no infimum means, first element is supremum | |
| 687 | return 0; | |
| 688 | } else if (cmp_func(((const char *) arr) + inf * elem_size, elem) == 0) { | |
| 689 | return inf; | |
| 690 | } else { | |
| 691 | return inf + 1; | |
| 692 | } | |
| 693 | } | |
| 694 | ||
| 174 | 695 | #ifndef CX_ARRAY_SWAP_SBO_SIZE |
|
187
24ce2c326d85
implement toggle button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
696 | #define CX_ARRAY_SWAP_SBO_SIZE 128 |
| 174 | 697 | #endif |
| 440 | 698 | const unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE; |
| 174 | 699 | |
| 700 | void cx_array_swap( | |
| 701 | void *arr, | |
| 702 | size_t elem_size, | |
| 703 | size_t idx1, | |
| 704 | size_t idx2 | |
| 705 | ) { | |
| 706 | assert(arr != NULL); | |
| 707 | ||
| 708 | // short circuit | |
| 709 | if (idx1 == idx2) return; | |
| 710 | ||
| 711 | char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; | |
| 712 | void *tmp; | |
| 713 | ||
| 714 | // decide if we can use the local buffer | |
| 715 | if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
716 | tmp = cxMallocDefault(elem_size); |
| 174 | 717 | // we don't want to enforce error handling |
| 718 | if (tmp == NULL) abort(); | |
| 719 | } else { | |
| 720 | tmp = sbo_mem; | |
| 721 | } | |
| 722 | ||
| 723 | // calculate memory locations | |
| 724 | char *left = arr, *right = arr; | |
| 725 | left += idx1 * elem_size; | |
| 726 | right += idx2 * elem_size; | |
| 727 | ||
| 728 | // three-way swap | |
| 729 | memcpy(tmp, left, elem_size); | |
| 730 | memcpy(left, right, elem_size); | |
| 731 | memcpy(right, tmp, elem_size); | |
| 732 | ||
| 733 | // free dynamic memory, if it was needed | |
| 734 | if (tmp != sbo_mem) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
735 | cxFreeDefault(tmp); |
| 174 | 736 | } |
| 737 | } | |
| 738 | ||
| 739 | // HIGH LEVEL ARRAY LIST FUNCTIONS | |
| 740 | ||
| 741 | typedef struct { | |
| 742 | struct cx_list_s base; | |
| 743 | void *data; | |
| 744 | size_t capacity; | |
| 440 | 745 | CxArrayReallocator reallocator; |
| 174 | 746 | } cx_array_list; |
| 747 | ||
| 748 | static void cx_arl_destructor(struct cx_list_s *list) { | |
| 749 | cx_array_list *arl = (cx_array_list *) list; | |
| 750 | ||
| 751 | char *ptr = arl->data; | |
| 752 | ||
| 324 | 753 | if (list->collection.simple_destructor) { |
| 754 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 755 | cx_invoke_simple_destructor(list, ptr); |
| 324 | 756 | ptr += list->collection.elem_size; |
| 174 | 757 | } |
| 758 | } | |
| 324 | 759 | if (list->collection.advanced_destructor) { |
| 760 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 761 | cx_invoke_advanced_destructor(list, ptr); |
| 324 | 762 | ptr += list->collection.elem_size; |
| 174 | 763 | } |
| 764 | } | |
| 765 | ||
| 324 | 766 | cxFree(list->collection.allocator, arl->data); |
| 767 | cxFree(list->collection.allocator, list); | |
| 174 | 768 | } |
| 769 | ||
| 770 | static size_t cx_arl_insert_array( | |
| 771 | struct cx_list_s *list, | |
| 772 | size_t index, | |
| 324 | 773 | const void *array, |
| 174 | 774 | size_t n |
| 775 | ) { | |
| 776 | // out of bounds and special case check | |
| 324 | 777 | if (index > list->collection.size || n == 0) return 0; |
| 174 | 778 | |
| 779 | // get a correctly typed pointer to the list | |
| 780 | cx_array_list *arl = (cx_array_list *) list; | |
| 781 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
782 | // guarantee enough capacity |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
783 | if (arl->capacity < list->collection.size + n) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
784 | size_t new_capacity = list->collection.size + n; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
785 | new_capacity = new_capacity - (new_capacity % 16) + 16; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
786 | if (cxReallocateArray( |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
787 | list->collection.allocator, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
788 | &arl->data, new_capacity, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
789 | list->collection.elem_size) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
790 | ) { |
| 174 | 791 | return 0; |
| 792 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
793 | arl->capacity = new_capacity; |
| 174 | 794 | } |
| 795 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
796 | // determine insert position |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
797 | char *arl_data = arl->data; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
798 | char *insert_pos = arl_data + index * list->collection.elem_size; |
| 174 | 799 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
800 | // do we need to move some elements? |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
801 | if (index < list->collection.size) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
802 | size_t elems_to_move = list->collection.size - index; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
803 | char *target = insert_pos + n * list->collection.elem_size; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
804 | memmove(target, insert_pos, elems_to_move * list->collection.elem_size); |
| 324 | 805 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
806 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
807 | // place the new elements, if any |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
808 | if (array != NULL) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
809 | memcpy(insert_pos, array, n * list->collection.elem_size); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
810 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
811 | list->collection.size += n; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
812 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
813 | return n; |
| 324 | 814 | } |
| 815 | ||
| 816 | static size_t cx_arl_insert_sorted( | |
| 817 | struct cx_list_s *list, | |
| 818 | const void *sorted_data, | |
| 819 | size_t n | |
| 820 | ) { | |
| 821 | // get a correctly typed pointer to the list | |
| 822 | cx_array_list *arl = (cx_array_list *) list; | |
| 823 | ||
| 440 | 824 | if (cx_array_insert_sorted( |
| 324 | 825 | &arl->data, |
| 826 | &list->collection.size, | |
| 827 | &arl->capacity, | |
| 828 | list->collection.cmpfunc, | |
| 829 | sorted_data, | |
| 830 | list->collection.elem_size, | |
| 174 | 831 | n, |
| 832 | &arl->reallocator | |
| 833 | )) { | |
| 834 | // array list implementation is "all or nothing" | |
| 835 | return 0; | |
| 440 | 836 | } else { |
| 837 | return n; | |
| 174 | 838 | } |
| 839 | } | |
| 840 | ||
| 845 | 841 | static size_t cx_arl_insert_unique( |
| 842 | struct cx_list_s *list, | |
| 843 | const void *sorted_data, | |
| 844 | size_t n | |
| 845 | ) { | |
| 846 | // get a correctly typed pointer to the list | |
| 847 | cx_array_list *arl = (cx_array_list *) list; | |
| 848 | ||
| 849 | if (cx_array_insert_unique( | |
| 850 | &arl->data, | |
| 851 | &list->collection.size, | |
| 852 | &arl->capacity, | |
| 853 | list->collection.cmpfunc, | |
| 854 | sorted_data, | |
| 855 | list->collection.elem_size, | |
| 856 | n, | |
| 857 | &arl->reallocator | |
| 858 | )) { | |
| 859 | // array list implementation is "all or nothing" | |
| 860 | return 0; | |
| 861 | } else { | |
| 862 | return n; | |
| 863 | } | |
| 864 | } | |
| 865 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
866 | static void *cx_arl_insert_element( |
| 174 | 867 | struct cx_list_s *list, |
| 868 | size_t index, | |
| 324 | 869 | const void *element |
| 174 | 870 | ) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
871 | if (cx_arl_insert_array(list, index, element, 1) == 1) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
872 | return ((char*)((cx_array_list *) list)->data) + index * list->collection.elem_size; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
873 | } else { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
874 | return NULL; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
875 | } |
| 174 | 876 | } |
| 877 | ||
| 878 | static int cx_arl_insert_iter( | |
| 324 | 879 | struct cx_iterator_s *iter, |
| 880 | const void *elem, | |
| 174 | 881 | int prepend |
| 882 | ) { | |
| 870 | 883 | struct cx_list_s *list = iter->src_handle; |
| 324 | 884 | if (iter->index < list->collection.size) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
885 | if (cx_arl_insert_element(list, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
886 | iter->index + 1 - prepend, elem) == NULL) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
887 | return 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
888 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
889 | iter->elem_count++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
890 | if (prepend != 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
891 | iter->index++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
892 | iter->elem_handle = ((char *) iter->elem_handle) + list->collection.elem_size; |
| 174 | 893 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
894 | return 0; |
| 174 | 895 | } else { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
896 | if (cx_arl_insert_element(list, list->collection.size, elem) == NULL) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
897 | return 1; |
| 324 | 898 | } |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
899 | iter->elem_count++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
900 | iter->index = list->collection.size; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
901 | return 0; |
| 174 | 902 | } |
| 903 | } | |
| 904 | ||
| 440 | 905 | static size_t cx_arl_remove( |
| 174 | 906 | struct cx_list_s *list, |
| 440 | 907 | size_t index, |
| 908 | size_t num, | |
| 909 | void *targetbuf | |
| 174 | 910 | ) { |
| 911 | cx_array_list *arl = (cx_array_list *) list; | |
| 912 | ||
| 913 | // out-of-bounds check | |
| 440 | 914 | size_t remove; |
| 324 | 915 | if (index >= list->collection.size) { |
| 440 | 916 | remove = 0; |
| 917 | } else if (index + num > list->collection.size) { | |
| 918 | remove = list->collection.size - index; | |
| 919 | } else { | |
| 920 | remove = num; | |
| 174 | 921 | } |
| 922 | ||
| 440 | 923 | // easy exit |
| 924 | if (remove == 0) return 0; | |
| 174 | 925 | |
| 440 | 926 | // destroy or copy contents |
| 927 | if (targetbuf == NULL) { | |
| 928 | for (size_t idx = index; idx < index + remove; idx++) { | |
| 929 | cx_invoke_destructor( | |
| 930 | list, | |
| 931 | ((char *) arl->data) + idx * list->collection.elem_size | |
| 932 | ); | |
| 933 | } | |
| 934 | } else { | |
| 935 | memcpy( | |
| 936 | targetbuf, | |
| 937 | ((char *) arl->data) + index * list->collection.elem_size, | |
| 938 | remove * list->collection.elem_size | |
| 939 | ); | |
| 174 | 940 | } |
| 941 | ||
| 440 | 942 | // short-circuit removal of last elements |
| 943 | if (index + remove == list->collection.size) { | |
| 944 | list->collection.size -= remove; | |
| 945 | return remove; | |
| 946 | } | |
| 947 | ||
| 948 | // just move the elements to the left | |
| 949 | cx_array_copy( | |
| 174 | 950 | &arl->data, |
| 324 | 951 | &list->collection.size, |
| 174 | 952 | &arl->capacity, |
| 440 | 953 | 0, |
| 174 | 954 | index, |
| 440 | 955 | ((char *) arl->data) + (index + remove) * list->collection.elem_size, |
| 324 | 956 | list->collection.elem_size, |
| 440 | 957 | list->collection.size - index - remove, |
| 174 | 958 | &arl->reallocator |
| 959 | ); | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
960 | |
| 440 | 961 | // decrease the size |
| 962 | list->collection.size -= remove; | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
963 | |
| 440 | 964 | return remove; |
| 174 | 965 | } |
| 966 | ||
| 967 | static void cx_arl_clear(struct cx_list_s *list) { | |
| 324 | 968 | if (list->collection.size == 0) return; |
| 174 | 969 | |
| 970 | cx_array_list *arl = (cx_array_list *) list; | |
| 971 | char *ptr = arl->data; | |
| 972 | ||
| 324 | 973 | if (list->collection.simple_destructor) { |
| 974 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 975 | cx_invoke_simple_destructor(list, ptr); |
| 324 | 976 | ptr += list->collection.elem_size; |
| 174 | 977 | } |
| 978 | } | |
| 324 | 979 | if (list->collection.advanced_destructor) { |
| 980 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 174 | 981 | cx_invoke_advanced_destructor(list, ptr); |
| 324 | 982 | ptr += list->collection.elem_size; |
| 174 | 983 | } |
| 984 | } | |
| 985 | ||
| 324 | 986 | memset(arl->data, 0, list->collection.size * list->collection.elem_size); |
| 987 | list->collection.size = 0; | |
| 174 | 988 | } |
| 989 | ||
| 990 | static int cx_arl_swap( | |
| 991 | struct cx_list_s *list, | |
| 992 | size_t i, | |
| 993 | size_t j | |
| 994 | ) { | |
| 324 | 995 | if (i >= list->collection.size || j >= list->collection.size) return 1; |
| 174 | 996 | cx_array_list *arl = (cx_array_list *) list; |
| 324 | 997 | cx_array_swap(arl->data, list->collection.elem_size, i, j); |
| 174 | 998 | return 0; |
| 999 | } | |
| 1000 | ||
| 1001 | static void *cx_arl_at( | |
| 324 | 1002 | const struct cx_list_s *list, |
| 174 | 1003 | size_t index |
| 1004 | ) { | |
| 324 | 1005 | if (index < list->collection.size) { |
| 1006 | const cx_array_list *arl = (const cx_array_list *) list; | |
| 174 | 1007 | char *space = arl->data; |
| 324 | 1008 | return space + index * list->collection.elem_size; |
| 174 | 1009 | } else { |
| 1010 | return NULL; | |
| 1011 | } | |
| 1012 | } | |
| 1013 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1014 | static size_t cx_arl_find_remove( |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1015 | struct cx_list_s *list, |
| 324 | 1016 | const void *elem, |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1017 | bool remove |
| 174 | 1018 | ) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1019 | assert(list != NULL); |
| 324 | 1020 | assert(list->collection.cmpfunc != NULL); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1021 | if (list->collection.size == 0) return 0; |
| 324 | 1022 | char *cur = ((const cx_array_list *) list)->data; |
| 174 | 1023 | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1024 | // optimize with binary search, when sorted |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1025 | if (list->collection.sorted) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1026 | size_t i = cx_array_binary_search( |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1027 | cur, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1028 | list->collection.size, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1029 | list->collection.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
|
1030 | elem, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1031 | list->collection.cmpfunc |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1032 | ); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1033 | if (remove && i < list->collection.size) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1034 | cx_arl_remove(list, i, 1, NULL); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1035 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1036 | return i; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1037 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1038 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1039 | // fallback: linear search |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1040 | for (size_t i = 0; i < list->collection.size; i++) { |
| 324 | 1041 | if (0 == list->collection.cmpfunc(elem, cur)) { |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1042 | if (remove) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1043 | cx_arl_remove(list, i, 1, NULL); |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1044 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1045 | return i; |
| 174 | 1046 | } |
| 324 | 1047 | cur += list->collection.elem_size; |
| 174 | 1048 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1049 | return list->collection.size; |
| 174 | 1050 | } |
| 1051 | ||
| 1052 | static void cx_arl_sort(struct cx_list_s *list) { | |
| 324 | 1053 | assert(list->collection.cmpfunc != NULL); |
| 174 | 1054 | qsort(((cx_array_list *) list)->data, |
| 324 | 1055 | list->collection.size, |
| 1056 | list->collection.elem_size, | |
| 1057 | list->collection.cmpfunc | |
| 174 | 1058 | ); |
| 1059 | } | |
| 1060 | ||
| 1061 | static int cx_arl_compare( | |
| 324 | 1062 | const struct cx_list_s *list, |
| 1063 | const struct cx_list_s *other | |
| 174 | 1064 | ) { |
| 324 | 1065 | assert(list->collection.cmpfunc != NULL); |
| 1066 | if (list->collection.size == other->collection.size) { | |
| 1067 | const char *left = ((const cx_array_list *) list)->data; | |
| 1068 | const char *right = ((const cx_array_list *) other)->data; | |
| 1069 | for (size_t i = 0; i < list->collection.size; i++) { | |
| 1070 | int d = list->collection.cmpfunc(left, right); | |
| 174 | 1071 | if (d != 0) { |
| 1072 | return d; | |
| 1073 | } | |
| 324 | 1074 | left += list->collection.elem_size; |
| 1075 | right += other->collection.elem_size; | |
| 174 | 1076 | } |
| 1077 | return 0; | |
| 1078 | } else { | |
| 324 | 1079 | return list->collection.size < other->collection.size ? -1 : 1; |
| 174 | 1080 | } |
| 1081 | } | |
| 1082 | ||
| 1083 | static void cx_arl_reverse(struct cx_list_s *list) { | |
| 324 | 1084 | if (list->collection.size < 2) return; |
| 1085 | void *data = ((const cx_array_list *) list)->data; | |
| 1086 | size_t half = list->collection.size / 2; | |
| 174 | 1087 | for (size_t i = 0; i < half; i++) { |
| 324 | 1088 | cx_array_swap(data, list->collection.elem_size, i, list->collection.size - 1 - i); |
| 174 | 1089 | } |
| 1090 | } | |
| 1091 | ||
| 324 | 1092 | static bool cx_arl_iter_valid(const void *it) { |
| 1093 | const struct cx_iterator_s *iter = it; | |
| 870 | 1094 | const struct cx_list_s *list = iter->src_handle; |
| 324 | 1095 | return iter->index < list->collection.size; |
| 174 | 1096 | } |
| 1097 | ||
| 324 | 1098 | static void *cx_arl_iter_current(const void *it) { |
| 1099 | const struct cx_iterator_s *iter = it; | |
| 174 | 1100 | return iter->elem_handle; |
| 1101 | } | |
| 1102 | ||
| 1103 | static void cx_arl_iter_next(void *it) { | |
| 324 | 1104 | struct cx_iterator_s *iter = it; |
| 1105 | if (iter->base.remove) { | |
| 1106 | iter->base.remove = false; | |
| 870 | 1107 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 845 | 1108 | iter->elem_count--; |
| 174 | 1109 | } else { |
| 1110 | iter->index++; | |
| 1111 | iter->elem_handle = | |
| 1112 | ((char *) iter->elem_handle) | |
| 870 | 1113 | + ((const struct cx_list_s *) iter->src_handle)->collection.elem_size; |
| 174 | 1114 | } |
| 1115 | } | |
| 1116 | ||
| 1117 | static void cx_arl_iter_prev(void *it) { | |
| 324 | 1118 | struct cx_iterator_s *iter = it; |
| 1119 | if (iter->base.remove) { | |
| 1120 | iter->base.remove = false; | |
| 870 | 1121 | cx_arl_remove(iter->src_handle, iter->index, 1, NULL); |
| 845 | 1122 | iter->elem_count--; |
| 174 | 1123 | } |
| 1124 | iter->index--; | |
| 870 | 1125 | cx_array_list *list = iter->src_handle; |
| 324 | 1126 | if (iter->index < list->base.collection.size) { |
| 174 | 1127 | iter->elem_handle = ((char *) list->data) |
| 324 | 1128 | + iter->index * list->base.collection.elem_size; |
| 174 | 1129 | } |
| 1130 | } | |
| 1131 | ||
| 1132 | ||
| 1133 | static struct cx_iterator_s cx_arl_iterator( | |
| 324 | 1134 | const struct cx_list_s *list, |
| 174 | 1135 | size_t index, |
| 1136 | bool backwards | |
| 1137 | ) { | |
| 1138 | struct cx_iterator_s iter; | |
| 1139 | ||
| 1140 | iter.index = index; | |
| 870 | 1141 | iter.src_handle = (void*)list; |
| 174 | 1142 | iter.elem_handle = cx_arl_at(list, index); |
| 324 | 1143 | iter.elem_size = list->collection.elem_size; |
| 1144 | iter.elem_count = list->collection.size; | |
| 174 | 1145 | iter.base.valid = cx_arl_iter_valid; |
| 1146 | iter.base.current = cx_arl_iter_current; | |
| 1147 | iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next; | |
| 1148 | iter.base.remove = false; | |
| 870 | 1149 | iter.base.allow_remove = true; |
| 174 | 1150 | |
| 1151 | return iter; | |
| 1152 | } | |
| 1153 | ||
| 1154 | static cx_list_class cx_array_list_class = { | |
| 1155 | cx_arl_destructor, | |
| 1156 | cx_arl_insert_element, | |
| 1157 | cx_arl_insert_array, | |
| 324 | 1158 | cx_arl_insert_sorted, |
| 845 | 1159 | cx_arl_insert_unique, |
| 174 | 1160 | cx_arl_insert_iter, |
| 1161 | cx_arl_remove, | |
| 1162 | cx_arl_clear, | |
| 1163 | cx_arl_swap, | |
| 1164 | cx_arl_at, | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
187
diff
changeset
|
1165 | cx_arl_find_remove, |
| 174 | 1166 | cx_arl_sort, |
| 1167 | cx_arl_compare, | |
| 1168 | cx_arl_reverse, | |
| 1169 | cx_arl_iterator, | |
| 1170 | }; | |
| 1171 | ||
| 1172 | CxList *cxArrayListCreate( | |
| 324 | 1173 | const CxAllocator *allocator, |
| 174 | 1174 | cx_compare_func comparator, |
| 324 | 1175 | size_t elem_size, |
| 174 | 1176 | size_t initial_capacity |
| 1177 | ) { | |
| 1178 | if (allocator == NULL) { | |
| 1179 | allocator = cxDefaultAllocator; | |
| 1180 | } | |
| 1181 | ||
| 1182 | cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); | |
| 1183 | if (list == NULL) return NULL; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1184 | cx_list_init((CxList*)list, &cx_array_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
|
1185 | allocator, comparator, elem_size); |
| 174 | 1186 | list->capacity = initial_capacity; |
| 1187 | ||
| 324 | 1188 | // allocate the array after the real elem_size is known |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1189 | list->data = cxCalloc(allocator, initial_capacity, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1190 | list->base.collection.elem_size); |
| 440 | 1191 | if (list->data == NULL) { // LCOV_EXCL_START |
| 174 | 1192 | cxFree(allocator, list); |
| 1193 | return NULL; | |
| 440 | 1194 | } // LCOV_EXCL_STOP |
| 174 | 1195 | |
| 1196 | // configure the reallocator | |
| 440 | 1197 | list->reallocator = cx_array_reallocator(allocator, NULL); |
| 174 | 1198 | |
| 1199 | return (CxList *) list; | |
| 1200 | } |