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