Thu, 18 Dec 2025 17:50:15 +0100
update ucx
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | /** | |
| 440 | 29 | * @file array_list.h |
| 30 | * @brief Array list implementation. | |
| 31 | * @author Mike Becker | |
| 32 | * @author Olaf Wintermann | |
| 33 | * @copyright 2-Clause BSD License | |
| 174 | 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 | /** | |
| 845 | 47 | * The maximum item size in an array list that fits into |
| 48 | * a stack buffer when swapped. | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
49 | */ |
| 870 | 50 | CX_EXPORT extern const unsigned cx_array_swap_sbo_size; |
| 440 | 51 | |
| 52 | /** | |
| 1016 | 53 | * Declares a typed array with size and capacity. |
| 440 | 54 | * |
| 1016 | 55 | * @param type the type of the elements |
| 440 | 56 | * @param name the name of the array |
| 1016 | 57 | */ |
| 58 | #define CX_ARRAY(type, name) \ | |
| 59 | struct { \ | |
| 60 | type *data; \ | |
| 61 | size_t size; \ | |
| 62 | size_t capacity; \ | |
| 63 | } name | |
| 64 | ||
| 65 | /** | |
| 66 | * Internal structure for arrays. | |
| 440 | 67 | * |
| 1016 | 68 | * A generalization of array structures declared with CX_ARRAY(). |
| 440 | 69 | */ |
| 1016 | 70 | typedef struct cx_array_s { |
| 71 | /** The array data. */ | |
| 72 | void *data; | |
| 73 | /** The number of elements. */ | |
| 74 | size_t size; | |
| 75 | /** The maximum number of elements. */ | |
| 76 | size_t capacity; | |
| 77 | } CxArray; | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
78 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
79 | /** |
| 1016 | 80 | * Initializes an array by allocating memory. |
| 440 | 81 | * |
| 1016 | 82 | * Internal function - do not use manually. |
| 440 | 83 | * |
| 1016 | 84 | * @param allocator the allocator for the array |
| 85 | * @param array a pointer to the array structure | |
| 86 | * @param elem_size size of one element | |
| 87 | * @param capacity the initial maximum number of elements | |
| 88 | * @retval zero allocation was successful | |
| 89 | * @retval non-zero allocation failed | |
| 324 | 90 | */ |
| 1016 | 91 | cx_attr_nonnull |
| 92 | CX_EXPORT int cx_array_init_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); | |
| 324 | 93 | |
| 94 | /** | |
| 1016 | 95 | * Initializes an array by allocating memory. |
| 440 | 96 | * |
| 1016 | 97 | * The size is set to zero. |
| 98 | * | |
| 99 | * @attention If the array was already initialized, this will leak memory. | |
| 100 | * Use cx_array_reserve() to change the capacity of an initialized array. | |
| 440 | 101 | * |
| 1016 | 102 | * @param allocator (@c CxAllocator*) the allocator for the array |
| 103 | * @param array the name of the array | |
| 104 | * @param capacity (@c size_t) the initial maximum number of elements | |
| 105 | * @retval zero allocation was successful | |
| 106 | * @retval non-zero allocation failed | |
| 107 | */ | |
| 108 | #define cx_array_init_a(allocator, array, capacity) cx_array_init_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity) | |
| 109 | ||
| 110 | /** | |
| 111 | * Initializes an array by allocating memory. | |
| 440 | 112 | * |
| 1016 | 113 | * The size is set to zero. |
| 440 | 114 | * |
| 1016 | 115 | * @attention If the array was already initialized, this will leak memory. |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
116 | * |
| 440 | 117 | * @param array the name of the array |
| 1016 | 118 | * @param capacity (@c size_t) the initial maximum number of elements |
| 119 | * @retval zero allocation was successful | |
| 120 | * @retval non-zero allocation failed | |
| 324 | 121 | */ |
| 1016 | 122 | #define cx_array_init(array, capacity) cx_array_init_a(cxDefaultAllocator, array, capacity) |
| 324 | 123 | |
| 124 | /** | |
| 1016 | 125 | * Initializes an array with fixed size memory. |
| 126 | * | |
| 127 | * Internal function - do not use manually. | |
| 440 | 128 | * |
| 1016 | 129 | * @param array a pointer to the array structure |
| 130 | * @param data the fixed size array | |
| 131 | * @param capacity the capacity of the fixed size array | |
| 132 | * @param size the number of initialized elements in the fixed size array | |
| 133 | */ | |
| 134 | cx_attr_nonnull | |
| 135 | CX_EXPORT void cx_array_init_fixed_(CxArray *array, const void *data, size_t capacity, size_t size); | |
| 136 | ||
| 137 | /** | |
| 138 | * Initializes an array with fixed size memory. | |
| 440 | 139 | * |
| 1016 | 140 | * This is useful, for example, when you want to work with memory on the stack |
| 141 | * and only want to move to the heap when the stack memory is not enough. | |
| 440 | 142 | * |
| 1016 | 143 | * With the @p num_initialized argument you can specify how many elements in the |
| 144 | * fixed size array are already correctly initialized, which determines the | |
| 145 | * initial size of the array. | |
| 146 | * | |
| 147 | * The capacity is determined automatically by the compiler. | |
| 148 | * | |
| 149 | * @attention When you add elements to an array that was initialized with fixed | |
| 150 | * size memory, you MUST check the capacity before adding the element and invoke | |
| 151 | * cx_array_copy_to_new() when you intend to exceed the capacity. | |
| 440 | 152 | * |
| 1016 | 153 | * @attention When you pass a pointer to an array that does not have a fixed |
| 154 | * size, the behavior is unspecified. | |
| 155 | * | |
| 156 | * @param array the name of the array to initialize | |
| 157 | * @param fixed_size_array (@c void*) the fixed size array | |
| 158 | * @param num_initialized (@c size_t) the number of already initialized elements in the fixed size array | |
| 159 | * @see cx_array_copy_to_new() | |
| 440 | 160 | */ |
| 1016 | 161 | #define cx_array_init_fixed(array, fixed_size_array, num_initialized) \ |
| 162 | cx_array_init_fixed_((CxArray*)&(array), fixed_size_array, cx_nmemb(fixed_size_array), num_initialized) | |
| 440 | 163 | |
| 164 | /** | |
| 1016 | 165 | * Changes the capacity of an array. |
| 166 | * | |
| 167 | * Internal function - do not use. | |
| 168 | * | |
| 169 | * @param allocator the allocator | |
| 170 | * @param array a pointer to the array structure | |
| 171 | * @param elem_size the size of one element | |
| 172 | * @param capacity the new capacity | |
| 173 | * @retval zero allocation was successful | |
| 174 | * @retval non-zero allocation failed | |
| 174 | 175 | */ |
| 1016 | 176 | cx_attr_nonnull |
| 177 | CX_EXPORT int cx_array_reserve_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); | |
| 174 | 178 | |
| 179 | /** | |
| 1016 | 180 | * Changes the capacity of an array. |
| 181 | * | |
| 182 | * If required, the size is reduced to fit into the new capacity. | |
| 183 | * | |
| 184 | * @param allocator (@c CxAllocator*) the allocator for the array | |
| 185 | * @param array the name of the array | |
| 186 | * @param capacity (@c size_t) the new maximum number of elements | |
| 187 | * @retval zero allocation was successful | |
| 188 | * @retval non-zero allocation failed | |
| 440 | 189 | */ |
| 1016 | 190 | #define cx_array_reserve_a(allocator, array, capacity) \ |
| 191 | cx_array_reserve_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity) | |
| 440 | 192 | |
| 193 | /** | |
| 1016 | 194 | * Changes the capacity of an array. |
| 440 | 195 | * |
| 1016 | 196 | * If required, the size is reduced to fit into the new capacity. |
| 440 | 197 | * |
| 1016 | 198 | * @param array the name of the array |
| 199 | * @param capacity (@c size_t) the new maximum number of elements | |
| 200 | * @retval zero allocation was successful | |
| 201 | * @retval non-zero allocation failed | |
| 202 | */ | |
| 203 | #define cx_array_reserve(array, capacity) \ | |
| 204 | cx_array_reserve_a(cxDefaultAllocator, array, capacity) | |
| 205 | ||
| 206 | /** | |
| 207 | * Copies the array to a new memory region. | |
| 440 | 208 | * |
| 1016 | 209 | * Internal function - do not use. |
| 210 | * | |
| 211 | * @param allocator the allocator for new new memory | |
| 212 | * @param array a pointer to the array structure | |
| 213 | * @param elem_size the size of one element | |
| 214 | * @param capacity the new capacity | |
| 215 | * @retval zero allocation was successful | |
| 216 | * @retval non-zero allocation failed | |
| 440 | 217 | */ |
| 1016 | 218 | cx_attr_nonnull |
| 219 | CX_EXPORT int cx_array_copy_to_new_(const CxAllocator *allocator, CxArray *array, size_t elem_size, size_t capacity); | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
220 | |
|
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
221 | /** |
| 1016 | 222 | * Copies the array to a new memory region. |
| 440 | 223 | * |
| 1016 | 224 | * This is useful when you have initialized the array with a fixed size memory |
| 225 | * using cx_array_init_fixed(), and now you want to increase the capacity. | |
| 440 | 226 | * |
| 1016 | 227 | * @attention When the original memory does not belong to stack memory, and |
| 228 | * you do not have another reference to this memory, it will leak. | |
| 440 | 229 | * |
| 1016 | 230 | * @param allocator (@c CxAllocator*) the allocator for the new memory |
| 231 | * @param array the name of the array | |
| 232 | * @param capacity (@c size_t) the new maximum number of elements | |
| 233 | * @retval zero allocation was successful | |
| 234 | * @retval non-zero allocation failed | |
| 235 | * @see cx_array_init_fixed() | |
| 236 | */ | |
| 237 | #define cx_array_copy_to_new_a(allocator, array, capacity) \ | |
| 238 | cx_array_copy_to_new_(allocator, (CxArray*)&(array), sizeof((array).data[0]), capacity) | |
| 239 | ||
| 240 | /** | |
| 241 | * Copies the array to a new memory region. | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
242 | * |
| 1016 | 243 | * This is useful when you have initialized the array with a fixed size memory |
| 244 | * using cx_array_init_fixed(), and now you want to increase the capacity. | |
| 245 | * | |
| 246 | * @attention When the original memory does not belong to stack memory, and | |
| 247 | * you do not have another reference to this memory, it will leak. | |
| 248 | * | |
| 249 | * @param array the name of the array | |
| 250 | * @param capacity (@c size_t) the new maximum number of elements | |
| 251 | * @retval zero allocation was successful | |
| 252 | * @retval non-zero allocation failed | |
| 253 | * @see cx_array_init_fixed() | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
254 | */ |
| 1016 | 255 | #define cx_array_copy_to_new(array, capacity) \ |
| 256 | cx_array_copy_to_new_a(cxDefaultAllocator, array, capacity) | |
| 174 | 257 | |
| 258 | /** | |
| 1016 | 259 | * Inserts data into an array. |
| 174 | 260 | * |
| 1016 | 261 | * Internal function - do not use. |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
262 | * |
| 1016 | 263 | * @param allocator the allocator to use for a possible reallocation |
| 264 | * @param array a pointer to the array structure | |
| 174 | 265 | * @param elem_size the size of one element |
| 1016 | 266 | * @param index the index where to insert the @p other data |
| 267 | * @param other a pointer to an array of data that shall be inserted | |
| 268 | * @param n the number of elements that shall be inserted | |
| 440 | 269 | * @retval zero success |
| 1016 | 270 | * @retval non-zero a re-allocation was necessary but failed |
| 174 | 271 | */ |
| 1016 | 272 | cx_attr_nonnull_arg(1, 2) |
| 273 | CX_EXPORT int cx_array_insert_(const CxAllocator *allocator, CxArray *array, | |
| 274 | size_t elem_size, size_t index, const void *other, size_t n); | |
| 324 | 275 | |
| 276 | /** | |
| 1016 | 277 | * Appends an element to an array. |
| 278 | * | |
| 279 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. | |
| 324 | 280 | * |
| 1016 | 281 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation |
| 282 | * @param array the name of the array where the element shall be added | |
| 283 | * @param element (@c void*) a pointer to the element that shall be added | |
| 440 | 284 | * @retval zero success |
| 1016 | 285 | * @retval non-zero a re-allocation was necessary but failed |
| 440 | 286 | */ |
| 1016 | 287 | #define cx_array_add_a(allocator, array, element) \ |
| 288 | cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, element, 1) | |
| 440 | 289 | |
| 290 | /** | |
| 1016 | 291 | * Appends an element to an array. |
| 292 | * | |
| 293 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. | |
| 440 | 294 | * |
| 1016 | 295 | * @param array the name of the array where the element shall be added |
| 296 | * @param element (@c void*) a pointer to the element that shall be added | |
| 440 | 297 | * @retval zero success |
| 1016 | 298 | * @retval non-zero a re-allocation was necessary but failed |
| 324 | 299 | */ |
| 1016 | 300 | #define cx_array_add(array, element) \ |
| 301 | cx_array_add_a(cxDefaultAllocator, array, element) | |
| 440 | 302 | |
| 303 | /** | |
| 1016 | 304 | * Inserts an element into an array. |
| 305 | * | |
| 306 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. | |
| 440 | 307 | * |
| 1016 | 308 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation |
| 309 | * @param array the name of the array where the element shall be inserted | |
| 310 | * @param index (@c size_t) the index where to insert the @p element | |
| 311 | * @param element (@c void*) a pointer to the element that shall be inserted | |
| 440 | 312 | * @retval zero success |
| 1016 | 313 | * @retval non-zero a re-allocation was necessary but failed |
| 440 | 314 | */ |
| 1016 | 315 | #define cx_array_insert_a(allocator, array, index, element) \ |
| 316 | cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, element, 1) | |
| 440 | 317 | |
| 318 | /** | |
| 1016 | 319 | * Inserts an element into an array. |
| 320 | * | |
| 321 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. | |
| 440 | 322 | * |
| 1016 | 323 | * @param array the name of the array where the element shall be inserted |
| 324 | * @param index (@c size_t) the index where to insert the @p element | |
| 325 | * @param element (@c void*) a pointer to the element that shall be inserted | |
| 440 | 326 | * @retval zero success |
| 1016 | 327 | * @retval non-zero a re-allocation was necessary but failed |
| 440 | 328 | */ |
| 1016 | 329 | #define cx_array_insert(array, index, element) \ |
| 330 | cx_array_insert_a(cxDefaultAllocator, array, index, element) | |
| 331 | ||
| 332 | /** | |
| 333 | * Inserts data into an array. | |
| 334 | * | |
| 335 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. | |
| 336 | * | |
| 337 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation | |
| 338 | * @param array the name of the array where the elements shall be inserted | |
| 339 | * @param index (@c size_t) the index where to insert the @p other data | |
| 340 | * @param other (@c void*) a pointer to an array of data that shall be inserted | |
| 341 | * @param n (@c size_t) the number of elements that shall be inserted | |
| 342 | * @retval zero success | |
| 343 | * @retval non-zero a re-allocation was necessary but failed | |
| 344 | */ | |
| 345 | #define cx_array_insert_array_a(allocator, array, index, other, n) \ | |
| 346 | cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), index, other, n) | |
| 174 | 347 | |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
348 | /** |
| 1016 | 349 | * Inserts data into an array. |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
350 | * |
| 1016 | 351 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. |
| 440 | 352 | * |
| 1016 | 353 | * @param array the name of the array where the elements shall be inserted |
| 354 | * @param index (@c size_t) the index where to insert the @p other data | |
| 355 | * @param other (@c void*) a pointer to an array of data that shall be inserted | |
| 356 | * @param n (@c size_t) the number of elements that shall be inserted | |
| 440 | 357 | * @retval zero success |
| 1016 | 358 | * @retval non-zero a re-allocation was necessary but failed |
|
253
087cc9216f28
initial newapi GTK port
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
174
diff
changeset
|
359 | */ |
| 1016 | 360 | #define cx_array_insert_array(array, index, other, n) \ |
| 361 | cx_array_insert_array_a(cxDefaultAllocator, array, index, other, n) | |
| 362 | ||
| 363 | /** | |
| 364 | * Appends data to an array. | |
| 365 | * | |
| 366 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. | |
| 367 | * | |
| 368 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation | |
| 369 | * @param array the name of the array where the elements shall be added | |
| 370 | * @param other (@c void*) a pointer to an array of data that shall be added | |
| 371 | * @param n (@c size_t) the number of elements that shall be added | |
| 372 | * @retval zero success | |
| 373 | * @retval non-zero a re-allocation was necessary but failed | |
| 374 | */ | |
| 375 | #define cx_array_add_array_a(allocator, array, other, n) \ | |
| 376 | cx_array_insert_(allocator, (CxArray*)&(array), sizeof((array).data[0]), (array).size, other, n) | |
| 440 | 377 | |
| 378 | /** | |
| 1016 | 379 | * Appends data to an array. |
| 380 | * | |
| 381 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. | |
| 440 | 382 | * |
| 1016 | 383 | * @param array the name of the array where the elements shall be added |
| 384 | * @param other (@c void*) a pointer to an array of data that shall be added | |
| 385 | * @param n (@c size_t) the number of elements that shall be added | |
| 440 | 386 | * @retval zero success |
| 1016 | 387 | * @retval non-zero a re-allocation was necessary but failed |
| 440 | 388 | */ |
| 1016 | 389 | #define cx_array_add_array(array, other, n) \ |
| 390 | cx_array_add_array_a(cxDefaultAllocator, array, other, n) | |
| 174 | 391 | |
| 392 | /** | |
| 1016 | 393 | * Inserts sorted data into a sorted array. |
| 324 | 394 | * |
| 1016 | 395 | * Internal function - do not use. |
| 324 | 396 | * |
| 1016 | 397 | * @param allocator the allocator to use for a possible reallocation |
| 398 | * @param array a pointer to the array structure | |
| 324 | 399 | * @param elem_size the size of one element |
| 1016 | 400 | * @param cmp_func |
| 401 | * @param sorted_data a pointer to an array of data that shall be inserted | |
| 402 | * @param n the number of elements that shall be inserted | |
| 403 | * @param allow_duplicates @c false if duplicates shall be skipped during insertion | |
| 440 | 404 | * @retval zero success |
| 1016 | 405 | * @retval non-zero a re-allocation was necessary but failed |
| 324 | 406 | */ |
| 1016 | 407 | cx_attr_nonnull |
| 408 | CX_EXPORT int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array, | |
| 409 | size_t elem_size, cx_compare_func cmp_func, const void *sorted_data, size_t n, | |
| 410 | bool allow_duplicates); | |
| 324 | 411 | |
| 412 | /** | |
| 413 | * Inserts an element into a sorted array. | |
| 414 | * | |
| 1016 | 415 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. |
| 324 | 416 | * |
| 1016 | 417 | * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. |
| 440 | 418 | * |
| 1016 | 419 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation |
| 420 | * @param array the name of the array where the elements shall be inserted | |
| 421 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 422 | * @param element (@c void*) a pointer to element that shall be inserted | |
| 440 | 423 | * @retval zero success |
| 1016 | 424 | * @retval non-zero a re-allocation was necessary but failed |
| 324 | 425 | */ |
| 1016 | 426 | #define cx_array_insert_sorted_a(allocator, array, cmp_func, element) \ |
| 427 | cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, true) | |
| 324 | 428 | |
| 429 | /** | |
| 1016 | 430 | * Inserts an element into a sorted array. |
| 431 | * | |
| 432 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. | |
| 440 | 433 | * |
| 1016 | 434 | * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. |
| 435 | * | |
| 436 | * @param array the name of the array where the elements shall be inserted | |
| 437 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 438 | * @param element (@c void*) a pointer to element that shall be inserted | |
| 440 | 439 | * @retval zero success |
| 1016 | 440 | * @retval non-zero a re-allocation was necessary but failed |
| 440 | 441 | */ |
| 1016 | 442 | #define cx_array_insert_sorted(array, cmp_func, element) \ |
| 443 | cx_array_insert_sorted_a(cxDefaultAllocator, array, cmp_func, element) | |
| 444 | ||
| 445 | /** | |
| 446 | * Inserts sorted data into a sorted array. | |
| 447 | * | |
| 448 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. | |
| 449 | * | |
| 450 | * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. | |
| 451 | * | |
| 452 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation | |
| 453 | * @param array the name of the array where the elements shall be inserted | |
| 454 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 455 | * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted | |
| 456 | * @param n (@c size_t) the number of elements that shall be inserted | |
| 457 | * @retval zero success | |
| 458 | * @retval non-zero a re-allocation was necessary but failed | |
| 459 | */ | |
| 460 | #define cx_array_insert_sorted_array_a(allocator, array, cmp_func, sorted_data, n) \ | |
| 461 | cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, true) | |
| 440 | 462 | |
| 463 | /** | |
| 1016 | 464 | * Inserts sorted data into a sorted array. |
| 465 | * | |
| 466 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. | |
| 324 | 467 | * |
| 1016 | 468 | * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. |
| 469 | * | |
| 470 | * @param array the name of the array where the elements shall be inserted | |
| 471 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 472 | * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted | |
| 473 | * @param n (@c size_t) the number of elements that shall be inserted | |
| 440 | 474 | * @retval zero success |
| 1016 | 475 | * @retval non-zero a re-allocation was necessary but failed |
| 324 | 476 | */ |
| 1016 | 477 | #define cx_array_insert_sorted_array(array, cmp_func, sorted_data, n) \ |
| 478 | cx_array_insert_sorted_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n) | |
| 440 | 479 | |
| 480 | /** | |
| 1016 | 481 | * Inserts an element into a sorted array if it is not already contained. |
| 482 | * | |
| 483 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. | |
| 484 | * | |
| 485 | * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. | |
| 440 | 486 | * |
| 1016 | 487 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation |
| 488 | * @param array the name of the array where the elements shall be inserted | |
| 489 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 490 | * @param element (@c void*) a pointer to element that shall be inserted | |
| 440 | 491 | * @retval zero success |
| 1016 | 492 | * @retval non-zero a re-allocation was necessary but failed |
| 440 | 493 | */ |
| 1016 | 494 | #define cx_array_insert_unique_a(allocator, array, cmp_func, element) \ |
| 495 | cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, false) | |
| 324 | 496 | |
| 497 | /** | |
| 1016 | 498 | * Inserts an element into a sorted array if it is not already contained. |
| 499 | * | |
| 500 | * When the capacity is not enough to hold the new element, a re-allocation is attempted. | |
| 324 | 501 | * |
| 1016 | 502 | * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. |
| 503 | * | |
| 504 | * @param array the name of the array where the elements shall be inserted | |
| 505 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 506 | * @param element (@c void*) a pointer to element that shall be inserted | |
| 440 | 507 | * @retval zero success |
| 1016 | 508 | * @retval non-zero a re-allocation was necessary but failed |
| 324 | 509 | */ |
| 1016 | 510 | #define cx_array_insert_unique(array, cmp_func, element) \ |
| 511 | cx_array_insert_unique_a(cxDefaultAllocator, array, cmp_func, element) | |
| 845 | 512 | |
| 513 | /** | |
| 1016 | 514 | * Inserts sorted data into a sorted array, skipping duplicates. |
| 845 | 515 | * |
| 1016 | 516 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. |
| 517 | * | |
| 518 | * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. | |
| 845 | 519 | * |
| 1016 | 520 | * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation |
| 521 | * @param array the name of the array where the elements shall be inserted | |
| 522 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 523 | * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted | |
| 524 | * @param n (@c size_t) the number of elements that shall be inserted | |
| 845 | 525 | * @retval zero success |
| 1016 | 526 | * @retval non-zero a re-allocation was necessary but failed |
| 845 | 527 | */ |
| 1016 | 528 | #define cx_array_insert_unique_array_a(allocator, array, cmp_func, sorted_data, n) \ |
| 529 | cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, false) | |
| 845 | 530 | |
| 531 | /** | |
| 1016 | 532 | * Inserts sorted data into a sorted array, skipping duplicates. |
| 845 | 533 | * |
| 1016 | 534 | * When the capacity is not enough to hold the new elements, a re-allocation is attempted. |
| 845 | 535 | * |
| 1016 | 536 | * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. |
| 845 | 537 | * |
| 1016 | 538 | * @param array the name of the array where the elements shall be inserted |
| 539 | * @param cmp_func (@c cx_compare_func) the compare function that establishes the order | |
| 540 | * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted | |
| 541 | * @param n (@c size_t) the number of elements that shall be inserted | |
| 542 | * @retval zero success | |
| 543 | * @retval non-zero a re-allocation was necessary but failed | |
| 845 | 544 | */ |
| 1016 | 545 | #define cx_array_insert_unique_array(array, cmp_func, sorted_data, n) \ |
| 546 | cx_array_insert_unique_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n) | |
| 547 | ||
| 548 | /** | |
| 549 | * Creates an iterator over the elements of an array. | |
| 550 | * | |
| 551 | * Internal function - do not use. | |
| 552 | * | |
| 553 | * @param array a pointer to the array structure | |
| 554 | * @param elem_size the size of one element | |
| 555 | * @return an iterator over the elements | |
| 556 | */ | |
| 557 | cx_attr_nodiscard cx_attr_nonnull | |
| 558 | CX_EXPORT CxIterator cx_array_iterator_(CxArray *array, size_t elem_size); | |
| 845 | 559 | |
| 560 | /** | |
| 1016 | 561 | * Creates an iterator over the elements of an array. |
| 562 | * | |
| 563 | * The iterator will yield pointers to the elements. | |
| 845 | 564 | * |
| 1016 | 565 | * @param array the name of the array |
| 566 | * @return an iterator over the elements | |
| 567 | * @see cx_array_iterator_ptr() | |
| 845 | 568 | */ |
| 1016 | 569 | #define cx_array_iterator(array) \ |
| 570 | cx_array_iterator_((CxArray*)&(array), sizeof((array).data[0])) | |
| 845 | 571 | |
| 572 | /** | |
| 1016 | 573 | * Creates an iterator over the elements of an array containing pointers. |
| 574 | * | |
| 575 | * Internal function - do not use. | |
| 845 | 576 | * |
| 1016 | 577 | * @param array the name of the array |
| 578 | * @return an iterator over the elements | |
| 845 | 579 | */ |
| 1016 | 580 | cx_attr_nodiscard cx_attr_nonnull |
| 581 | CX_EXPORT CxIterator cx_array_iterator_ptr_(CxArray *array); | |
| 845 | 582 | |
| 583 | /** | |
| 1016 | 584 | * Creates an iterator over the elements of an array containing pointers. |
| 585 | * | |
| 586 | * The iterator will yield the elements themselves, which are supposed to | |
| 587 | * be pointers. | |
| 845 | 588 | * |
| 1016 | 589 | * @param array the name of the array |
| 590 | * @return an iterator over the elements | |
| 591 | * @see cx_array_iterator() | |
| 845 | 592 | */ |
| 1016 | 593 | #define cx_array_iterator_ptr(array) \ |
| 594 | cx_array_iterator_ptr_((CxArray*)&(array)) | |
| 845 | 595 | |
| 596 | /** | |
| 1016 | 597 | * Deallocates an array. |
| 598 | * | |
| 599 | * Internal function - do not use. | |
| 600 | * | |
| 601 | * @param allocator (@c CxAllocator*) the allocator which was used to allocate the array | |
| 602 | * @param array a pointer to the array structure | |
| 603 | */ | |
| 604 | cx_attr_nonnull | |
| 605 | CX_EXPORT void cx_array_free_(const CxAllocator *allocator, CxArray *array); | |
| 606 | ||
| 607 | /** | |
| 608 | * Deallocates an array. | |
| 609 | * | |
| 610 | * The structure is reset to zero and can be re-initialized with | |
| 611 | * cx_array_inita(). | |
| 845 | 612 | * |
| 1016 | 613 | * @param array the name of the array |
| 845 | 614 | */ |
| 1016 | 615 | #define cx_array_free(array) cx_array_free_(cxDefaultAllocator, (CxArray*)&(array)) |
| 616 | ||
| 617 | /** | |
| 618 | * Deallocates an array. | |
| 619 | * | |
| 620 | * The structure is reset to zero and can be re-initialized with | |
| 621 | * cx_array_init_a(). | |
| 622 | * | |
| 623 | * @param allocator (@c CxAllocator*) the allocator which was used to allocate the array | |
| 624 | * @param array the name of the array | |
| 625 | */ | |
| 626 | #define cx_array_free_a(allocator, array) cx_array_free_(allocator, (CxArray*)&(array)) | |
| 627 | ||
| 845 | 628 | |
| 324 | 629 | /** |
| 630 | * Searches the largest lower bound in a sorted array. | |
| 631 | * | |
| 632 | * In other words, this function returns the index of the largest element | |
| 440 | 633 | * in @p arr that is less or equal to @p elem with respect to @p cmp_func. |
| 634 | * When no such element exists, @p size is returned. | |
| 324 | 635 | * |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
636 | * When such an element exists more than once, the largest index of all those |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
637 | * elements is returned. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
638 | * |
| 440 | 639 | * If @p elem is contained in the array, this is identical to |
| 324 | 640 | * #cx_array_binary_search(). |
| 641 | * | |
| 440 | 642 | * If the array is not sorted with respect to the @p cmp_func, the behavior |
| 324 | 643 | * is undefined. |
| 644 | * | |
| 645 | * @param arr the array to search | |
| 646 | * @param size the size of the array | |
| 647 | * @param elem_size the size of one element | |
| 648 | * @param elem the element to find | |
| 649 | * @param cmp_func the compare function | |
| 440 | 650 | * @return the index of the largest lower bound, or @p size |
| 651 | * @see cx_array_binary_search_sup() | |
| 652 | * @see cx_array_binary_search() | |
| 324 | 653 | */ |
| 440 | 654 | cx_attr_nonnull |
| 870 | 655 | CX_EXPORT size_t cx_array_binary_search_inf(const void *arr, size_t size, |
| 656 | size_t elem_size, const void *elem, cx_compare_func cmp_func); | |
| 324 | 657 | |
| 658 | /** | |
| 659 | * Searches an item in a sorted array. | |
| 660 | * | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
661 | * When such an element exists more than once, the largest index of all those |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
662 | * elements is returned. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
663 | * |
| 440 | 664 | * If the array is not sorted with respect to the @p cmp_func, the behavior |
| 324 | 665 | * is undefined. |
| 666 | * | |
| 667 | * @param arr the array to search | |
| 668 | * @param size the size of the array | |
| 669 | * @param elem_size the size of one element | |
| 670 | * @param elem the element to find | |
| 671 | * @param cmp_func the compare function | |
| 440 | 672 | * @return the index of the element in the array, or @p size if the element |
| 324 | 673 | * cannot be found |
| 440 | 674 | * @see cx_array_binary_search_inf() |
| 675 | * @see cx_array_binary_search_sup() | |
| 324 | 676 | */ |
| 440 | 677 | cx_attr_nonnull |
| 870 | 678 | CX_EXPORT size_t cx_array_binary_search(const void *arr, size_t size, |
| 679 | size_t elem_size, const void *elem, cx_compare_func cmp_func); | |
| 324 | 680 | |
| 681 | /** | |
| 682 | * Searches the smallest upper bound in a sorted array. | |
| 683 | * | |
| 684 | * In other words, this function returns the index of the smallest element | |
| 440 | 685 | * in @p arr that is greater or equal to @p elem with respect to @p cmp_func. |
| 686 | * When no such element exists, @p size is returned. | |
| 324 | 687 | * |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
688 | * When such an element exists more than once, the smallest index of all those |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
689 | * elements is returned. |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
690 | * |
| 440 | 691 | * If @p elem is contained in the array, this is identical to |
| 324 | 692 | * #cx_array_binary_search(). |
| 693 | * | |
| 440 | 694 | * If the array is not sorted with respect to the @p cmp_func, the behavior |
| 324 | 695 | * is undefined. |
| 696 | * | |
| 697 | * @param arr the array to search | |
| 698 | * @param size the size of the array | |
| 699 | * @param elem_size the size of one element | |
| 700 | * @param elem the element to find | |
| 701 | * @param cmp_func the compare function | |
| 440 | 702 | * @return the index of the smallest upper bound, or @p size |
| 703 | * @see cx_array_binary_search_inf() | |
| 704 | * @see cx_array_binary_search() | |
| 324 | 705 | */ |
| 440 | 706 | cx_attr_nonnull |
| 870 | 707 | CX_EXPORT size_t cx_array_binary_search_sup(const void *arr, size_t size, |
| 708 | size_t elem_size, const void *elem, cx_compare_func cmp_func); | |
| 324 | 709 | |
| 1016 | 710 | |
| 711 | /** | |
| 712 | * Searches the largest lower bound in a sorted array. | |
| 713 | * | |
| 714 | * In other words, this function returns the index of the largest element | |
| 715 | * in @p arr that is less or equal to @p elem with respect to @p cmp_func. | |
| 716 | * When no such element exists, @p size is returned. | |
| 717 | * | |
| 718 | * When such an element exists more than once, the largest index of all those | |
| 719 | * elements is returned. | |
| 720 | * | |
| 721 | * If @p elem is contained in the array, this is identical to | |
| 722 | * #cx_array_binary_search(). | |
| 723 | * | |
| 724 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 725 | * is undefined. | |
| 726 | * | |
| 727 | * @param arr the array to search | |
| 728 | * @param size the size of the array | |
| 729 | * @param elem_size the size of one element | |
| 730 | * @param elem the element to find | |
| 731 | * @param cmp_func the compare function | |
| 732 | * @param context the context for the compare function | |
| 733 | * @return the index of the largest lower bound, or @p size | |
| 734 | * @see cx_array_binary_search_sup() | |
| 735 | * @see cx_array_binary_search() | |
| 736 | */ | |
| 737 | cx_attr_nonnull | |
| 738 | CX_EXPORT size_t cx_array_binary_search_inf_c(const void *arr, size_t size, | |
| 739 | size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); | |
| 740 | ||
| 741 | /** | |
| 742 | * Searches an item in a sorted array. | |
| 743 | * | |
| 744 | * When such an element exists more than once, the largest index of all those | |
| 745 | * elements is returned. | |
| 746 | * | |
| 747 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 748 | * is undefined. | |
| 749 | * | |
| 750 | * @param arr the array to search | |
| 751 | * @param size the size of the array | |
| 752 | * @param elem_size the size of one element | |
| 753 | * @param elem the element to find | |
| 754 | * @param cmp_func the compare function | |
| 755 | * @param context the context for the compare function | |
| 756 | * @return the index of the element in the array, or @p size if the element | |
| 757 | * cannot be found | |
| 758 | * @see cx_array_binary_search_inf() | |
| 759 | * @see cx_array_binary_search_sup() | |
| 760 | */ | |
| 761 | cx_attr_nonnull | |
| 762 | CX_EXPORT size_t cx_array_binary_search_c(const void *arr, size_t size, | |
| 763 | size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); | |
| 764 | ||
| 765 | /** | |
| 766 | * Searches the smallest upper bound in a sorted array. | |
| 767 | * | |
| 768 | * In other words, this function returns the index of the smallest element | |
| 769 | * in @p arr that is greater or equal to @p elem with respect to @p cmp_func. | |
| 770 | * When no such element exists, @p size is returned. | |
| 771 | * | |
| 772 | * When such an element exists more than once, the smallest index of all those | |
| 773 | * elements is returned. | |
| 774 | * | |
| 775 | * If @p elem is contained in the array, this is identical to | |
| 776 | * #cx_array_binary_search(). | |
| 777 | * | |
| 778 | * If the array is not sorted with respect to the @p cmp_func, the behavior | |
| 779 | * is undefined. | |
| 780 | * | |
| 781 | * @param arr the array to search | |
| 782 | * @param size the size of the array | |
| 783 | * @param elem_size the size of one element | |
| 784 | * @param elem the element to find | |
| 785 | * @param cmp_func the compare function | |
| 786 | * @param context the context for the compare function | |
| 787 | * @return the index of the smallest upper bound, or @p size | |
| 788 | * @see cx_array_binary_search_inf() | |
| 789 | * @see cx_array_binary_search() | |
| 790 | */ | |
| 791 | cx_attr_nonnull | |
| 792 | CX_EXPORT size_t cx_array_binary_search_sup_c(const void *arr, size_t size, | |
| 793 | size_t elem_size, const void *elem, cx_compare_func2 cmp_func, void *context); | |
| 794 | ||
| 324 | 795 | /** |
| 174 | 796 | * Swaps two array elements. |
| 797 | * | |
| 798 | * @param arr the array | |
| 799 | * @param elem_size the element size | |
| 845 | 800 | * @param idx1 index of the first element |
| 801 | * @param idx2 index of the second element | |
| 174 | 802 | */ |
| 440 | 803 | cx_attr_nonnull |
| 870 | 804 | CX_EXPORT void cx_array_swap(void *arr, size_t elem_size, size_t idx1, size_t idx2); |
| 174 | 805 | |
| 806 | /** | |
| 440 | 807 | * Allocates an array list for storing elements with @p elem_size bytes each. |
| 174 | 808 | * |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
809 | * If @p elem_size is #CX_STORE_POINTERS, the created list stores pointers instead of |
| 845 | 810 | * copies of the added elements, and the compare function will be automatically set |
| 1016 | 811 | * to cx_cmp_ptr(). |
| 174 | 812 | * |
| 813 | * @param allocator the allocator for allocating the list memory | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
814 | * (if @c NULL, the cxDefaultAllocator will be used) |
| 324 | 815 | * @param elem_size the size of each element in bytes |
| 174 | 816 | * @param initial_capacity the initial number of elements the array can store |
| 817 | * @return the created list | |
| 818 | */ | |
| 440 | 819 | cx_attr_nodiscard |
| 820 | cx_attr_malloc | |
| 821 | cx_attr_dealloc(cxListFree, 1) | |
| 870 | 822 | CX_EXPORT CxList *cxArrayListCreate(const CxAllocator *allocator, |
| 1016 | 823 | size_t elem_size, size_t initial_capacity); |
| 174 | 824 | |
| 825 | #ifdef __cplusplus | |
| 826 | } // extern "C" | |
| 827 | #endif | |
| 828 | ||
| 829 | #endif // UCX_ARRAY_LIST_H |