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