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