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 | /** | |
| 579 | 29 | * @file array_list.h |
| 30 | * @brief Array list implementation. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 436 | 34 | */ |
| 35 | ||
| 36 | ||
| 37 | #ifndef UCX_ARRAY_LIST_H | |
| 38 | #define UCX_ARRAY_LIST_H | |
| 39 | ||
| 40 | #include "list.h" | |
| 41 | ||
| 42 | #ifdef __cplusplus | |
| 43 | extern "C" { | |
| 44 | #endif | |
| 45 | ||
| 46 | /** | |
| 579 | 47 | * The maximum item size in an array list that fits into stack buffer |
| 48 | * when swapped. | |
| 49 | */ | |
| 50 | cx_attr_export | |
| 51 | extern const unsigned cx_array_swap_sbo_size; | |
| 52 | ||
| 53 | /** | |
| 54 | * Declares variables for an array that can be used with the convenience macros. | |
| 55 | * | |
| 56 | * @par Examples | |
| 57 | * @code | |
| 58 | * // integer array with at most 255 elements | |
| 59 | * CX_ARRAY_DECLARE_SIZED(int, myarray, uint8_t) | |
| 60 | * | |
| 61 | * // array of MyObject* pointers where size and capacity are stored as unsigned int | |
| 62 | * CX_ARRAY_DECLARE_SIZED(MyObject*, objects, unsigned int) | |
| 63 | * | |
| 64 | * // initializing code | |
| 65 | * cx_array_initialize(myarray, 16); // reserve space for 16 | |
| 66 | * cx_array_initialize(objects, 100); // reserve space for 100 | |
| 67 | * @endcode | |
| 68 | * | |
| 69 | * @param type the type of the data | |
| 70 | * @param name the name of the array | |
| 71 | * @param size_type the type of the size (should be uint8_t, uint16_t, uint32_t, or size_t) | |
| 72 | * | |
| 73 | * @see cx_array_initialize() | |
| 74 | * @see cx_array_simple_add() | |
| 75 | * @see cx_array_simple_copy() | |
| 76 | * @see cx_array_simple_add_sorted() | |
| 77 | * @see cx_array_simple_insert_sorted() | |
| 78 | */ | |
| 79 | #define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \ | |
| 80 | type * name; \ | |
| 81 | /** Array size. */ size_type name##_size; \ | |
| 82 | /** Array capacity. */ size_type name##_capacity | |
| 83 | ||
| 84 | /** | |
| 85 | * Declares variables for an array that can be used with the convenience macros. | |
| 86 | * | |
| 87 | * The size and capacity variables will have @c size_t type. | |
| 88 | * Use #CX_ARRAY_DECLARE_SIZED() to specify a different type. | |
| 89 | * | |
| 90 | * @par Examples | |
| 91 | * @code | |
| 92 | * // int array | |
| 93 | * CX_ARRAY_DECLARE(int, myarray) | |
| 94 | * | |
| 95 | * // initializing code | |
| 96 | * cx_array_initialize(myarray, 32); // reserve space for 32 | |
| 97 | * @endcode | |
| 98 | * | |
| 99 | * @param type the type of the data | |
| 100 | * @param name the name of the array | |
| 101 | * | |
| 102 | * @see cx_array_initialize() | |
| 103 | * @see cx_array_simple_add() | |
| 104 | * @see cx_array_simple_copy() | |
| 105 | * @see cx_array_simple_add_sorted() | |
| 106 | * @see cx_array_simple_insert_sorted() | |
| 107 | */ | |
| 108 | #define CX_ARRAY_DECLARE(type, name) CX_ARRAY_DECLARE_SIZED(type, name, size_t) | |
| 109 | ||
| 110 | /** | |
| 111 | * Initializes an array with the given capacity. | |
| 112 | * | |
| 113 | * The type of the capacity depends on the type used during declaration. | |
| 114 | * | |
| 115 | * @par Examples | |
| 116 | * @code | |
| 117 | * CX_ARRAY_DECLARE_SIZED(int, arr1, uint8_t) | |
| 118 | * CX_ARRAY_DECLARE(int, arr2) // size and capacity are implicitly size_t | |
| 119 | * | |
| 120 | * // initializing code | |
| 121 | * cx_array_initialize(arr1, 500); // error: maximum for uint8_t is 255 | |
| 122 | * cx_array_initialize(arr2, 500); // OK | |
| 123 | * @endcode | |
| 124 | * | |
| 125 | * | |
| 582 | 126 | * The memory for the array is allocated with the cxDefaultAllocator. |
| 127 | * | |
| 579 | 128 | * @param array the name of the array |
| 129 | * @param capacity the initial capacity | |
| 130 | * @see cx_array_initialize_a() | |
| 131 | * @see CX_ARRAY_DECLARE_SIZED() | |
| 132 | * @see CX_ARRAY_DECLARE() | |
| 133 | */ | |
| 134 | #define cx_array_initialize(array, capacity) \ | |
| 135 | array##_capacity = capacity; \ | |
| 136 | array##_size = 0; \ | |
| 582 | 137 | array = cxMallocDefault(sizeof(array[0]) * capacity) |
| 579 | 138 | |
| 139 | /** | |
| 140 | * Initializes an array with the given capacity using the specified allocator. | |
| 141 | * | |
| 142 | * @par Example | |
| 143 | * @code | |
| 144 | * CX_ARRAY_DECLARE(int, myarray) | |
| 145 | * | |
| 146 | * | |
| 147 | * const CxAllocator *al = // ... | |
| 148 | * cx_array_initialize_a(al, myarray, 128); | |
| 149 | * // ... | |
| 150 | * cxFree(al, myarray); // don't forget to free with same allocator | |
| 151 | * @endcode | |
| 152 | * | |
| 153 | * @param allocator (@c CxAllocator*) the allocator | |
| 154 | * @param array the name of the array | |
| 155 | * @param capacity the initial capacity | |
| 156 | * @see cx_array_initialize() | |
| 157 | * @see CX_ARRAY_DECLARE_SIZED() | |
| 158 | * @see CX_ARRAY_DECLARE() | |
| 159 | */ | |
| 160 | #define cx_array_initialize_a(allocator, array, capacity) \ | |
| 161 | array##_capacity = capacity; \ | |
| 162 | array##_size = 0; \ | |
| 163 | array = cxMalloc(allocator, sizeof(array[0]) * capacity) | |
| 164 | ||
| 165 | /** | |
| 436 | 166 | * Defines a reallocation mechanism for arrays. |
| 579 | 167 | * You can create your own, use cx_array_reallocator(), or |
| 168 | * use the #cx_array_default_reallocator. | |
| 436 | 169 | */ |
| 170 | struct cx_array_reallocator_s { | |
| 171 | /** | |
| 579 | 172 | * Reallocates space for the given array. |
| 436 | 173 | * |
| 174 | * Implementations are not required to free the original array. | |
| 579 | 175 | * This allows reallocation of static memory by allocating heap memory |
| 176 | * and copying the array contents. The information in the custom fields of | |
| 177 | * the referenced allocator can be used to track the state of the memory | |
| 178 | * or to transport other additional data. | |
| 436 | 179 | * |
| 180 | * @param array the array to reallocate | |
| 582 | 181 | * @param old_capacity the old number of elements |
| 182 | * @param new_capacity the new number of elements | |
| 436 | 183 | * @param elem_size the size of each element |
| 184 | * @param alloc a reference to this allocator | |
| 579 | 185 | * @return a pointer to the reallocated memory or @c NULL on failure |
| 436 | 186 | */ |
| 579 | 187 | cx_attr_nodiscard |
| 582 | 188 | cx_attr_nonnull_arg(5) |
| 189 | cx_attr_allocsize(3, 4) | |
| 436 | 190 | void *(*realloc)( |
| 191 | void *array, | |
| 582 | 192 | size_t old_capacity, |
| 193 | size_t new_capacity, | |
| 436 | 194 | size_t elem_size, |
| 195 | struct cx_array_reallocator_s *alloc | |
| 196 | ); | |
| 197 | ||
| 198 | /** | |
| 199 | * Custom data pointer. | |
| 200 | */ | |
| 201 | void *ptr1; | |
| 202 | /** | |
| 203 | * Custom data pointer. | |
| 204 | */ | |
| 205 | void *ptr2; | |
| 206 | /** | |
| 207 | * Custom data integer. | |
| 208 | */ | |
| 209 | size_t int1; | |
| 210 | /** | |
| 211 | * Custom data integer. | |
| 212 | */ | |
| 213 | size_t int2; | |
| 214 | }; | |
| 215 | ||
| 216 | /** | |
| 579 | 217 | * Typedef for the array reallocator struct. |
| 218 | */ | |
| 219 | typedef struct cx_array_reallocator_s CxArrayReallocator; | |
| 220 | ||
| 221 | /** | |
| 582 | 222 | * A default array reallocator that is based on the cxDefaultAllocator. |
| 579 | 223 | */ |
| 224 | cx_attr_export | |
| 225 | extern CxArrayReallocator *cx_array_default_reallocator; | |
| 226 | ||
| 227 | /** | |
| 228 | * Creates a new array reallocator. | |
| 229 | * | |
| 582 | 230 | * When @p allocator is @c NULL, the cxDefaultAllocator will be used. |
| 579 | 231 | * |
| 232 | * When @p stackmem is not @c NULL, the reallocator is supposed to be used | |
| 233 | * @em only for the specific array that is initially located at @p stackmem. | |
| 234 | * When reallocation is needed, the reallocator checks, if the array is | |
| 235 | * still located at @p stackmem and copies the contents to the heap. | |
| 236 | * | |
| 237 | * @note Invoking this function with both arguments @c NULL will return a | |
| 238 | * reallocator that behaves like #cx_array_default_reallocator. | |
| 239 | * | |
| 240 | * @param allocator the allocator this reallocator shall be based on | |
| 241 | * @param stackmem the address of the array when the array is initially located | |
| 242 | * on the stack or shall not reallocated in place | |
| 243 | * @return an array reallocator | |
| 436 | 244 | */ |
| 579 | 245 | cx_attr_export |
| 246 | CxArrayReallocator cx_array_reallocator( | |
| 247 | const struct cx_allocator_s *allocator, | |
| 248 | const void *stackmem | |
| 249 | ); | |
| 250 | ||
| 251 | /** | |
| 252 | * Reserves memory for additional elements. | |
| 253 | * | |
| 254 | * This function checks if the @p capacity of the array is sufficient to hold | |
| 255 | * at least @p size plus @p elem_count elements. If not, a reallocation is | |
| 256 | * performed with the specified @p reallocator. | |
| 257 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
| 258 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
| 259 | * | |
| 260 | * This function can be useful to replace subsequent calls to cx_array_copy() | |
| 261 | * with one single cx_array_reserve() and then - after guaranteeing a | |
| 262 | * sufficient capacity - use simple memmove() or memcpy(). | |
| 263 | * | |
| 264 | * The @p width in bytes refers to the size and capacity. | |
| 265 | * Both must have the same width. | |
| 266 | * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64 bit | |
| 267 | * architecture. If set to zero, the native word width is used. | |
| 268 | * | |
| 269 | * @param array a pointer to the target array | |
| 270 | * @param size a pointer to the size of the array | |
| 271 | * @param capacity a pointer to the capacity of the array | |
| 272 | * @param width the width in bytes for the @p size and @p capacity or zero for default | |
| 273 | * @param elem_size the size of one element | |
| 274 | * @param elem_count the number of expected additional elements | |
| 275 | * @param reallocator the array reallocator to use | |
| 276 | * (@c NULL defaults to #cx_array_default_reallocator) | |
| 277 | * @retval zero success | |
| 278 | * @retval non-zero failure | |
| 279 | * @see cx_array_reallocator() | |
| 280 | */ | |
| 281 | cx_attr_nonnull_arg(1, 2, 3) | |
| 282 | cx_attr_export | |
| 283 | int cx_array_reserve( | |
| 284 | void **array, | |
| 285 | void *size, | |
| 286 | void *capacity, | |
| 287 | unsigned width, | |
| 288 | size_t elem_size, | |
| 289 | size_t elem_count, | |
| 290 | CxArrayReallocator *reallocator | |
| 291 | ); | |
| 436 | 292 | |
| 293 | /** | |
| 294 | * Copies elements from one array to another. | |
| 295 | * | |
| 579 | 296 | * The elements are copied to the @p target array at the specified @p index, |
| 297 | * overwriting possible elements. The @p index does not need to be in range of | |
| 298 | * the current array @p size. If the new index plus the number of elements added | |
| 299 | * would extend the array's size, the remaining @p capacity is used. | |
| 300 | * | |
| 301 | * If the @p capacity is also insufficient to hold the new data, a reallocation | |
| 302 | * attempt is made with the specified @p reallocator. | |
| 303 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
| 304 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
| 436 | 305 | * |
| 579 | 306 | * The @p width in bytes refers to the size and capacity. |
| 307 | * Both must have the same width. | |
| 308 | * Supported are 0, 1, 2, and 4, as well as 8 if running on a 64 bit | |
| 309 | * architecture. If set to zero, the native word width is used. | |
| 436 | 310 | * |
| 579 | 311 | * @param target a pointer to the target array |
| 436 | 312 | * @param size a pointer to the size of the target array |
| 579 | 313 | * @param capacity a pointer to the capacity of the target array |
| 314 | * @param width the width in bytes for the @p size and @p capacity or zero for default | |
| 436 | 315 | * @param index the index where the copied elements shall be placed |
| 316 | * @param src the source array | |
| 317 | * @param elem_size the size of one element | |
| 318 | * @param elem_count the number of elements to copy | |
| 579 | 319 | * @param reallocator the array reallocator to use |
| 320 | * (@c NULL defaults to #cx_array_default_reallocator) | |
| 321 | * @retval zero success | |
| 322 | * @retval non-zero failure | |
| 323 | * @see cx_array_reallocator() | |
| 324 | */ | |
| 325 | cx_attr_nonnull_arg(1, 2, 3, 6) | |
| 326 | cx_attr_export | |
| 327 | int cx_array_copy( | |
| 328 | void **target, | |
| 329 | void *size, | |
| 330 | void *capacity, | |
| 331 | unsigned width, | |
| 332 | size_t index, | |
| 333 | const void *src, | |
| 334 | size_t elem_size, | |
| 335 | size_t elem_count, | |
| 336 | CxArrayReallocator *reallocator | |
| 337 | ); | |
| 338 | ||
| 339 | /** | |
| 340 | * Convenience macro that uses cx_array_copy() with a default layout and | |
| 341 | * the specified reallocator. | |
| 342 | * | |
| 343 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 344 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 345 | * @param index (@c size_t) the index where the copied elements shall be placed | |
| 346 | * @param src (@c void*) the source array | |
| 347 | * @param count (@c size_t) the number of elements to copy | |
| 348 | * @retval zero success | |
| 349 | * @retval non-zero failure | |
| 350 | * @see CX_ARRAY_DECLARE() | |
| 351 | * @see cx_array_simple_copy() | |
| 352 | */ | |
| 353 | #define cx_array_simple_copy_a(reallocator, array, index, src, count) \ | |
| 354 | cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ | |
| 355 | sizeof(array##_size), index, src, sizeof((array)[0]), count, \ | |
| 356 | reallocator) | |
| 357 | ||
| 358 | /** | |
| 359 | * Convenience macro that uses cx_array_copy() with a default layout and | |
| 360 | * the default reallocator. | |
| 361 | * | |
| 362 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 363 | * @param index (@c size_t) the index where the copied elements shall be placed | |
| 364 | * @param src (@c void*) the source array | |
| 365 | * @param count (@c size_t) the number of elements to copy | |
| 366 | * @retval zero success | |
| 367 | * @retval non-zero failure | |
| 368 | * @see CX_ARRAY_DECLARE() | |
| 369 | * @see cx_array_simple_copy_a() | |
| 370 | */ | |
| 371 | #define cx_array_simple_copy(array, index, src, count) \ | |
| 372 | cx_array_simple_copy_a(NULL, array, index, src, count) | |
| 373 | ||
| 374 | /** | |
| 375 | * Convenience macro that uses cx_array_reserve() with a default layout and | |
| 376 | * the specified reallocator. | |
| 377 | * | |
| 378 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 379 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 380 | * @param count (@c size_t) the number of expected @em additional elements | |
| 381 | * @retval zero success | |
| 382 | * @retval non-zero failure | |
| 383 | * @see CX_ARRAY_DECLARE() | |
| 384 | * @see cx_array_simple_reserve() | |
| 385 | */ | |
| 386 | #define cx_array_simple_reserve_a(reallocator, array, count) \ | |
| 387 | cx_array_reserve((void**)&(array), &(array##_size), &(array##_capacity), \ | |
| 388 | sizeof(array##_size), sizeof((array)[0]), count, \ | |
| 389 | reallocator) | |
| 390 | ||
| 391 | /** | |
| 392 | * Convenience macro that uses cx_array_reserve() with a default layout and | |
| 393 | * the default reallocator. | |
| 394 | * | |
| 395 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 396 | * @param count (@c size_t) the number of expected additional elements | |
| 397 | * @retval zero success | |
| 398 | * @retval non-zero failure | |
| 399 | * @see CX_ARRAY_DECLARE() | |
| 400 | * @see cx_array_simple_reserve_a() | |
| 436 | 401 | */ |
| 579 | 402 | #define cx_array_simple_reserve(array, count) \ |
| 403 | cx_array_simple_reserve_a(NULL, array, count) | |
| 404 | ||
| 405 | /** | |
| 406 | * Adds an element to an array with the possibility of allocating more space. | |
| 407 | * | |
| 408 | * The element @p elem is added to the end of the @p target array which contains | |
| 409 | * @p size elements, already. The @p capacity must point to a variable denoting | |
| 410 | * the current maximum number of elements the array can hold. | |
| 411 | * | |
| 412 | * If the capacity is insufficient to hold the new element, an attempt to | |
| 413 | * increase the @p capacity is made and the new capacity is written back. | |
| 414 | * | |
| 415 | * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. | |
| 416 | * It is important, however, that @p size and @p capacity are pointers to | |
| 417 | * variables of the same type. | |
| 418 | * | |
| 419 | * @param target (@c void**) a pointer to the target array | |
| 420 | * @param size (@c SIZE_TYPE*) a pointer to the size of the target array | |
| 421 | * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array | |
| 422 | * @param elem_size (@c size_t) the size of one element | |
| 423 | * @param elem (@c void*) a pointer to the element to add | |
| 424 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 425 | * @retval zero success | |
| 426 | * @retval non-zero failure | |
| 427 | */ | |
| 428 | #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ | |
| 429 | cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \ | |
| 430 | *(size), elem, elem_size, 1, reallocator) | |
| 431 | ||
| 432 | /** | |
| 433 | * Convenience macro that uses cx_array_add() with a default layout and | |
| 434 | * the specified reallocator. | |
| 435 | * | |
| 436 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 437 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 438 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 439 | * @retval zero success | |
| 440 | * @retval non-zero failure | |
| 441 | * @see CX_ARRAY_DECLARE() | |
| 442 | * @see cx_array_simple_add() | |
| 443 | */ | |
| 444 | #define cx_array_simple_add_a(reallocator, array, elem) \ | |
| 445 | cx_array_simple_copy_a(reallocator, array, array##_size, &(elem), 1) | |
| 446 | ||
| 447 | /** | |
| 448 | * Convenience macro that uses cx_array_add() with a default layout and | |
| 449 | * the default reallocator. | |
| 450 | * | |
| 451 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 452 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 453 | * @retval zero success | |
| 454 | * @retval non-zero failure | |
| 455 | * @see CX_ARRAY_DECLARE() | |
| 456 | * @see cx_array_simple_add_a() | |
| 457 | */ | |
| 458 | #define cx_array_simple_add(array, elem) \ | |
| 459 | cx_array_simple_add_a(cx_array_default_reallocator, array, elem) | |
| 460 | ||
| 461 | /** | |
| 462 | * Inserts a sorted array into another sorted array. | |
| 463 | * | |
| 464 | * If either the target or the source array is not already sorted with respect | |
| 465 | * to the specified @p cmp_func, the behavior is undefined. | |
| 466 | * | |
| 467 | * If the capacity is insufficient to hold the new data, a reallocation | |
| 468 | * attempt is made. | |
| 469 | * You can create your own reallocator by hand, use #cx_array_default_reallocator, | |
| 470 | * or use the convenience function cx_array_reallocator() to create a custom reallocator. | |
| 471 | * | |
| 472 | * @param target a pointer to the target array | |
| 473 | * @param size a pointer to the size of the target array | |
| 474 | * @param capacity a pointer to the capacity of the target array | |
| 475 | * @param cmp_func the compare function for the elements | |
| 476 | * @param src the source array | |
| 477 | * @param elem_size the size of one element | |
| 478 | * @param elem_count the number of elements to insert | |
| 479 | * @param reallocator the array reallocator to use | |
| 480 | * (@c NULL defaults to #cx_array_default_reallocator) | |
| 481 | * @retval zero success | |
| 482 | * @retval non-zero failure | |
| 483 | */ | |
| 484 | cx_attr_nonnull_arg(1, 2, 3, 5) | |
| 485 | cx_attr_export | |
| 486 | int cx_array_insert_sorted( | |
| 436 | 487 | void **target, |
| 488 | size_t *size, | |
| 489 | size_t *capacity, | |
| 579 | 490 | cx_compare_func cmp_func, |
| 491 | const void *src, | |
| 436 | 492 | size_t elem_size, |
| 493 | size_t elem_count, | |
| 579 | 494 | CxArrayReallocator *reallocator |
| 495 | ); | |
| 496 | ||
| 497 | /** | |
| 498 | * Inserts an element into a sorted array. | |
| 499 | * | |
| 500 | * If the target array is not already sorted with respect | |
| 501 | * to the specified @p cmp_func, the behavior is undefined. | |
| 502 | * | |
| 503 | * If the capacity is insufficient to hold the new data, a reallocation | |
| 504 | * attempt is made. | |
| 505 | * | |
| 506 | * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. | |
| 507 | * It is important, however, that @p size and @p capacity are pointers to | |
| 508 | * variables of the same type. | |
| 509 | * | |
| 510 | * @param target (@c void**) a pointer to the target array | |
| 511 | * @param size (@c SIZE_TYPE*) a pointer to the size of the target array | |
| 512 | * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array | |
| 513 | * @param elem_size (@c size_t) the size of one element | |
| 514 | * @param elem (@c void*) a pointer to the element to add | |
| 515 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 516 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 517 | * @retval zero success | |
| 518 | * @retval non-zero failure | |
| 519 | */ | |
| 520 | #define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \ | |
| 521 | cx_array_insert_sorted((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator) | |
| 522 | ||
| 523 | /** | |
| 524 | * Convenience macro for cx_array_add_sorted() with a default | |
| 525 | * layout and the specified reallocator. | |
| 526 | * | |
| 527 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 528 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 529 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 530 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 531 | * @retval zero success | |
| 532 | * @retval non-zero failure | |
| 533 | * @see CX_ARRAY_DECLARE() | |
| 534 | * @see cx_array_simple_add_sorted() | |
| 535 | */ | |
| 536 | #define cx_array_simple_add_sorted_a(reallocator, array, elem, cmp_func) \ | |
| 537 | cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \ | |
| 538 | sizeof((array)[0]), &(elem), cmp_func, reallocator) | |
| 539 | ||
| 540 | /** | |
| 541 | * Convenience macro for cx_array_add_sorted() with a default | |
| 542 | * layout and the default reallocator. | |
| 543 | * | |
| 544 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 545 | * @param elem the element to add (NOT a pointer, address is automatically taken) | |
| 546 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 547 | * @retval zero success | |
| 548 | * @retval non-zero failure | |
| 549 | * @see CX_ARRAY_DECLARE() | |
| 550 | * @see cx_array_simple_add_sorted_a() | |
| 551 | */ | |
| 552 | #define cx_array_simple_add_sorted(array, elem, cmp_func) \ | |
| 553 | cx_array_simple_add_sorted_a(NULL, array, elem, cmp_func) | |
| 554 | ||
| 555 | /** | |
| 556 | * Convenience macro for cx_array_insert_sorted() with a default | |
| 557 | * layout and the specified reallocator. | |
| 558 | * | |
| 559 | * @param reallocator (@c CxArrayReallocator*) the array reallocator to use | |
| 560 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 561 | * @param src (@c void*) pointer to the source array | |
| 562 | * @param n (@c size_t) number of elements in the source array | |
| 563 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 564 | * @retval zero success | |
| 565 | * @retval non-zero failure | |
| 566 | * @see CX_ARRAY_DECLARE() | |
| 567 | * @see cx_array_simple_insert_sorted() | |
| 568 | */ | |
| 569 | #define cx_array_simple_insert_sorted_a(reallocator, array, src, n, cmp_func) \ | |
| 570 | cx_array_insert_sorted((void**)(&array), &(array##_size), &(array##_capacity), \ | |
| 571 | cmp_func, src, sizeof((array)[0]), n, reallocator) | |
| 436 | 572 | |
| 579 | 573 | /** |
| 574 | * Convenience macro for cx_array_insert_sorted() with a default | |
| 575 | * layout and the default reallocator. | |
| 576 | * | |
| 577 | * @param array the name of the array (NOT a pointer or alias to the array) | |
| 578 | * @param src (@c void*) pointer to the source array | |
| 579 | * @param n (@c size_t) number of elements in the source array | |
| 580 | * @param cmp_func (@c cx_cmp_func) the compare function for the elements | |
| 581 | * @retval zero success | |
| 582 | * @retval non-zero failure | |
| 583 | * @see CX_ARRAY_DECLARE() | |
| 584 | * @see cx_array_simple_insert_sorted_a() | |
| 585 | */ | |
| 586 | #define cx_array_simple_insert_sorted(array, src, n, cmp_func) \ | |
| 587 | cx_array_simple_insert_sorted_a(NULL, array, src, n, cmp_func) | |
| 588 | ||
| 589 | /** | |
| 590 | * Searches the largest lower bound in a sorted array. | |
| 591 | * | |
| 592 | * In other words, this function returns the index of the largest element | |
| 593 | * in @p arr that is less or equal to @p elem with respect to @p cmp_func. | |
| 594 | * When no such element exists, @p size is returned. | |
| 595 | * | |
| 596 | * If @p elem is contained in the array, this is identical to | |
| 597 | * #cx_array_binary_search(). | |
| 598 | * | |
| 599 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 600 | * is undefined. | |
| 601 | * | |
| 602 | * @param arr the array to search | |
| 603 | * @param size the size of the array | |
| 604 | * @param elem_size the size of one element | |
| 605 | * @param elem the element to find | |
| 606 | * @param cmp_func the compare function | |
| 607 | * @return the index of the largest lower bound, or @p size | |
| 608 | * @see cx_array_binary_search_sup() | |
| 609 | * @see cx_array_binary_search() | |
| 610 | */ | |
| 611 | cx_attr_nonnull | |
| 612 | cx_attr_export | |
| 613 | size_t cx_array_binary_search_inf( | |
| 614 | const void *arr, | |
| 615 | size_t size, | |
| 616 | size_t elem_size, | |
| 617 | const void *elem, | |
| 618 | cx_compare_func cmp_func | |
| 619 | ); | |
| 620 | ||
| 621 | /** | |
| 622 | * Searches an item in a sorted array. | |
| 623 | * | |
| 624 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 625 | * is undefined. | |
| 626 | * | |
| 627 | * @param arr the array to search | |
| 628 | * @param size the size of the array | |
| 629 | * @param elem_size the size of one element | |
| 630 | * @param elem the element to find | |
| 631 | * @param cmp_func the compare function | |
| 632 | * @return the index of the element in the array, or @p size if the element | |
| 633 | * cannot be found | |
| 634 | * @see cx_array_binary_search_inf() | |
| 635 | * @see cx_array_binary_search_sup() | |
| 636 | */ | |
| 637 | cx_attr_nonnull | |
| 638 | cx_attr_export | |
| 639 | size_t cx_array_binary_search( | |
| 640 | const void *arr, | |
| 641 | size_t size, | |
| 642 | size_t elem_size, | |
| 643 | const void *elem, | |
| 644 | cx_compare_func cmp_func | |
| 645 | ); | |
| 646 | ||
| 647 | /** | |
| 648 | * Searches the smallest upper bound in a sorted array. | |
| 649 | * | |
| 650 | * In other words, this function returns the index of the smallest element | |
| 651 | * in @p arr that is greater or equal to @p elem with respect to @p cmp_func. | |
| 652 | * When no such element exists, @p size is returned. | |
| 653 | * | |
| 654 | * If @p elem is contained in the array, this is identical to | |
| 655 | * #cx_array_binary_search(). | |
| 656 | * | |
| 657 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 658 | * is undefined. | |
| 659 | * | |
| 660 | * @param arr the array to search | |
| 661 | * @param size the size of the array | |
| 662 | * @param elem_size the size of one element | |
| 663 | * @param elem the element to find | |
| 664 | * @param cmp_func the compare function | |
| 665 | * @return the index of the smallest upper bound, or @p size | |
| 666 | * @see cx_array_binary_search_inf() | |
| 667 | * @see cx_array_binary_search() | |
| 668 | */ | |
| 669 | cx_attr_nonnull | |
| 670 | cx_attr_export | |
| 671 | size_t cx_array_binary_search_sup( | |
| 672 | const void *arr, | |
| 673 | size_t size, | |
| 674 | size_t elem_size, | |
| 675 | const void *elem, | |
| 676 | cx_compare_func cmp_func | |
| 677 | ); | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
678 | |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
679 | /** |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
680 | * Swaps two array elements. |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
681 | * |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
682 | * @param arr the array |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
683 | * @param elem_size the element size |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
684 | * @param idx1 index of first element |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
685 | * @param idx2 index of second element |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
686 | */ |
| 579 | 687 | cx_attr_nonnull |
| 688 | cx_attr_export | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
689 | void cx_array_swap( |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
690 | void *arr, |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
691 | size_t elem_size, |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
692 | size_t idx1, |
|
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
693 | size_t idx2 |
| 579 | 694 | ); |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
695 | |
| 436 | 696 | /** |
| 579 | 697 | * Allocates an array list for storing elements with @p elem_size bytes each. |
| 436 | 698 | * |
| 579 | 699 | * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
| 700 | * copies of the added elements and the compare function will be automatically set | |
| 701 | * to cx_cmp_ptr(), if none is given. | |
| 490 | 702 | * |
| 436 | 703 | * @param allocator the allocator for allocating the list memory |
| 582 | 704 | * (if @c NULL, the cxDefaultAllocator will be used) |
| 436 | 705 | * @param comparator the comparator for the elements |
| 579 | 706 | * (if @c NULL, and the list is not storing pointers, sort and find |
| 707 | * functions will not work) | |
| 708 | * @param elem_size the size of each element in bytes | |
| 436 | 709 | * @param initial_capacity the initial number of elements the array can store |
| 710 | * @return the created list | |
| 711 | */ | |
| 579 | 712 | cx_attr_nodiscard |
| 713 | cx_attr_malloc | |
| 714 | cx_attr_dealloc(cxListFree, 1) | |
| 715 | cx_attr_export | |
| 436 | 716 | CxList *cxArrayListCreate( |
| 579 | 717 | const CxAllocator *allocator, |
| 490 | 718 | cx_compare_func comparator, |
| 579 | 719 | size_t elem_size, |
| 436 | 720 | size_t initial_capacity |
| 490 | 721 | ); |
| 436 | 722 | |
| 490 | 723 | /** |
| 579 | 724 | * Allocates an array list for storing elements with @p elem_size bytes each. |
| 490 | 725 | * |
| 579 | 726 | * The list will use the cxDefaultAllocator and @em NO compare function. |
| 490 | 727 | * If you want to call functions that need a compare function, you have to |
| 728 | * set it immediately after creation or use cxArrayListCreate(). | |
| 729 | * | |
| 579 | 730 | * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
| 731 | * copies of the added elements and the compare function will be automatically set | |
| 582 | 732 | * to cx_cmp_ptr(). |
| 490 | 733 | * |
| 579 | 734 | * @param elem_size (@c size_t) the size of each element in bytes |
| 735 | * @param initial_capacity (@c size_t) the initial number of elements the array can store | |
| 490 | 736 | * @return the created list |
| 737 | */ | |
| 579 | 738 | #define cxArrayListCreateSimple(elem_size, initial_capacity) \ |
| 739 | cxArrayListCreate(NULL, NULL, elem_size, initial_capacity) | |
| 436 | 740 | |
| 741 | #ifdef __cplusplus | |
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
742 | } // extern "C" |
| 436 | 743 | #endif |
| 744 | ||
|
438
22eca559aded
refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
436
diff
changeset
|
745 | #endif // UCX_ARRAY_LIST_H |