Thu, 07 Nov 2019 10:10:36 +0100
removes some bugs by redesigning the array API
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@103 | 3 | * |
universe@334 | 4 | * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@4 | 27 | */ |
universe@123 | 28 | /** |
universe@334 | 29 | * Dynamically allocated array implementation. |
universe@123 | 30 | * |
universe@334 | 31 | * @file array.h |
universe@123 | 32 | * @author Mike Becker |
universe@123 | 33 | * @author Olaf Wintermann |
universe@123 | 34 | */ |
universe@4 | 35 | |
universe@334 | 36 | #ifndef UCX_ARRAY_H |
universe@334 | 37 | #define UCX_ARRAY_H |
universe@4 | 38 | |
universe@259 | 39 | #include "ucx.h" |
universe@259 | 40 | #include "allocator.h" |
universe@7 | 41 | |
universe@4 | 42 | #ifdef __cplusplus |
universe@4 | 43 | extern "C" { |
universe@4 | 44 | #endif |
universe@4 | 45 | |
universe@121 | 46 | /** |
universe@334 | 47 | * UCX array type. |
universe@121 | 48 | */ |
universe@334 | 49 | typedef struct { |
universe@334 | 50 | /** |
universe@334 | 51 | * The current capacity of the array. |
universe@334 | 52 | */ |
universe@334 | 53 | size_t capacity; |
universe@334 | 54 | /** |
universe@334 | 55 | * The actual number of elements in the array. |
universe@334 | 56 | */ |
universe@334 | 57 | size_t size; |
universe@334 | 58 | /** |
universe@334 | 59 | * The size of an individual element in bytes. |
universe@334 | 60 | */ |
universe@334 | 61 | size_t elemsize; |
universe@334 | 62 | /** |
universe@334 | 63 | * A pointer to the data. |
universe@334 | 64 | */ |
universe@334 | 65 | void* data; |
universe@334 | 66 | /** |
universe@334 | 67 | * The allocator used for the data. |
universe@334 | 68 | */ |
universe@334 | 69 | UcxAllocator* allocator; |
universe@334 | 70 | } UcxArray; |
universe@334 | 71 | |
universe@355 | 72 | /** |
universe@355 | 73 | * Sets an element in an arbitrary user defined array. |
universe@369 | 74 | * The data is copied from the specified data location. |
universe@355 | 75 | * |
universe@355 | 76 | * If the capacity is insufficient, the array is automatically reallocated and |
universe@355 | 77 | * the possibly new pointer is stored in the <code>array</code> argument. |
universe@355 | 78 | * |
universe@355 | 79 | * On reallocation the capacity of the array is doubled until it is sufficient. |
universe@355 | 80 | * The new capacity is stored back to <code>capacity</code>. |
universe@355 | 81 | * |
universe@355 | 82 | * @param array a pointer to location of the array pointer |
universe@355 | 83 | * @param capacity a pointer to the capacity |
universe@355 | 84 | * @param elmsize the size of each element |
universe@355 | 85 | * @param idx the index of the element to set |
universe@369 | 86 | * @param data a pointer to the element data |
universe@355 | 87 | * @return zero on success or non-zero on error (errno will be set) |
universe@355 | 88 | */ |
universe@355 | 89 | #define ucx_array_util_set(array, capacity, elmsize, idx, data) \ |
universe@355 | 90 | ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ |
universe@355 | 91 | elmsize, idx, data) |
universe@355 | 92 | |
universe@355 | 93 | /** |
universe@355 | 94 | * Sets an element in an arbitrary user defined array. |
universe@369 | 95 | * The data is copied from the specified data location. |
universe@355 | 96 | * |
universe@355 | 97 | * If the capacity is insufficient, the array is automatically reallocated |
universe@355 | 98 | * using the specified allocator and the possibly new pointer is stored in |
universe@355 | 99 | * the <code>array</code> argument. |
universe@355 | 100 | * |
universe@355 | 101 | * On reallocation the capacity of the array is doubled until it is sufficient. |
universe@355 | 102 | * The new capacity is stored back to <code>capacity</code>. |
universe@355 | 103 | * |
universe@355 | 104 | * @param alloc the allocator that shall be used to reallocate the array |
universe@355 | 105 | * @param array a pointer to location of the array pointer |
universe@355 | 106 | * @param capacity a pointer to the capacity |
universe@355 | 107 | * @param elmsize the size of each element |
universe@355 | 108 | * @param idx the index of the element to set |
universe@369 | 109 | * @param data a pointer to the element data |
universe@355 | 110 | * @return zero on success or non-zero on error (errno will be set) |
universe@355 | 111 | */ |
universe@355 | 112 | int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, |
universe@369 | 113 | size_t elmsize, size_t idx, void* data); |
universe@355 | 114 | |
universe@355 | 115 | /** |
universe@369 | 116 | * Stores a pointer in an arbitrary user defined array. |
universe@369 | 117 | * The element size of the array must be sizeof(void*). |
universe@369 | 118 | * |
universe@369 | 119 | * If the capacity is insufficient, the array is automatically reallocated and |
universe@369 | 120 | * the possibly new pointer is stored in the <code>array</code> argument. |
universe@369 | 121 | * |
universe@369 | 122 | * On reallocation the capacity of the array is doubled until it is sufficient. |
universe@369 | 123 | * The new capacity is stored back to <code>capacity</code>. |
universe@369 | 124 | * |
universe@369 | 125 | * @param array a pointer to location of the array pointer |
universe@369 | 126 | * @param capacity a pointer to the capacity |
universe@369 | 127 | * @param idx the index of the element to set |
universe@369 | 128 | * @param ptr the pointer to store |
universe@369 | 129 | * @return zero on success or non-zero on error (errno will be set) |
universe@369 | 130 | */ |
universe@369 | 131 | #define ucx_array_util_setptr(array, capacity, idx, ptr) \ |
universe@369 | 132 | ucx_array_util_setptr_a(ucx_default_allocator(), (void**)(array), \ |
universe@369 | 133 | capacity, idx, ptr) |
universe@369 | 134 | |
universe@369 | 135 | /** |
universe@369 | 136 | * Stores a pointer in an arbitrary user defined array. |
universe@369 | 137 | * The element size of the array must be sizeof(void*). |
universe@369 | 138 | * |
universe@369 | 139 | * If the capacity is insufficient, the array is automatically reallocated |
universe@369 | 140 | * using the specified allocator and the possibly new pointer is stored in |
universe@369 | 141 | * the <code>array</code> argument. |
universe@369 | 142 | * |
universe@369 | 143 | * On reallocation the capacity of the array is doubled until it is sufficient. |
universe@369 | 144 | * The new capacity is stored back to <code>capacity</code>. |
universe@355 | 145 | * |
universe@355 | 146 | * @param alloc the allocator that shall be used to reallocate the array |
universe@355 | 147 | * @param array a pointer to location of the array pointer |
universe@355 | 148 | * @param capacity a pointer to the capacity |
universe@355 | 149 | * @param idx the index of the element to set |
universe@369 | 150 | * @param ptr the pointer to store |
universe@355 | 151 | * @return zero on success or non-zero on error (errno will be set) |
universe@355 | 152 | */ |
universe@369 | 153 | int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity, |
universe@369 | 154 | size_t idx, void* ptr); |
universe@369 | 155 | |
universe@121 | 156 | |
universe@123 | 157 | /** |
universe@334 | 158 | * Creates a new UCX array with the given capacity and element size. |
universe@334 | 159 | * @param capacity the initial capacity |
universe@334 | 160 | * @param elemsize the element size |
universe@356 | 161 | * @return a pointer to a new UCX array structure |
universe@123 | 162 | */ |
universe@356 | 163 | UcxArray* ucx_array_new(size_t capacity, size_t elemsize); |
universe@146 | 164 | |
universe@129 | 165 | /** |
universe@334 | 166 | * Creates a new UCX array using the specified allocator. |
universe@334 | 167 | * |
universe@334 | 168 | * @param capacity the initial capacity |
universe@334 | 169 | * @param elemsize the element size |
universe@334 | 170 | * @param allocator the allocator to use |
universe@356 | 171 | * @return a pointer to new UCX array structure |
universe@129 | 172 | */ |
universe@356 | 173 | UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize, |
universe@356 | 174 | UcxAllocator* allocator); |
universe@356 | 175 | |
universe@356 | 176 | /** |
universe@356 | 177 | * Initializes a UCX array structure with the given capacity and element size. |
universe@356 | 178 | * The structure must be uninitialized as the data pointer will be overwritten. |
universe@356 | 179 | * |
universe@356 | 180 | * @param array the structure to initialize |
universe@356 | 181 | * @param capacity the initial capacity |
universe@356 | 182 | * @param elemsize the element size |
universe@356 | 183 | */ |
universe@356 | 184 | void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize); |
universe@356 | 185 | |
universe@356 | 186 | /** |
universe@356 | 187 | * Initializes a UCX array structure using the specified allocator. |
universe@356 | 188 | * The structure must be uninitialized as the data pointer will be overwritten. |
universe@356 | 189 | * |
universe@365 | 190 | * @param array the structure to initialize |
universe@356 | 191 | * @param capacity the initial capacity |
universe@356 | 192 | * @param elemsize the element size |
universe@356 | 193 | * @param allocator the allocator to use |
universe@356 | 194 | */ |
universe@356 | 195 | void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize, |
universe@334 | 196 | UcxAllocator* allocator); |
universe@4 | 197 | |
universe@128 | 198 | /** |
universe@334 | 199 | * Creates an shallow copy of an array. |
universe@128 | 200 | * |
universe@334 | 201 | * This function clones the specified array by using memcpy(). |
universe@356 | 202 | * If the destination capacity is insufficient, an automatic reallocation is |
universe@356 | 203 | * attempted. |
universe@128 | 204 | * |
universe@357 | 205 | * Note: if the destination array is uninitialized, the behavior is undefined. |
universe@357 | 206 | * |
universe@356 | 207 | * @param dest the array to copy to |
universe@356 | 208 | * @param src the array to copy from |
universe@356 | 209 | * @return zero on success, non-zero on reallocation failure. |
universe@128 | 210 | */ |
universe@356 | 211 | int ucx_array_clone(UcxArray* dest, UcxArray const* src); |
universe@334 | 212 | |
universe@146 | 213 | |
universe@128 | 214 | /** |
universe@334 | 215 | * Compares two UCX arrays element-wise by using a compare function. |
universe@334 | 216 | * |
universe@334 | 217 | * Elements of the two specified arrays are compared by using the specified |
universe@123 | 218 | * compare function and the additional data. The type and content of this |
universe@123 | 219 | * additional data depends on the cmp_func() used. |
universe@123 | 220 | * |
universe@334 | 221 | * This function always returns zero, if the element sizes of the arrays do |
universe@334 | 222 | * not match and performs no comparisons in this case. |
universe@123 | 223 | * |
universe@334 | 224 | * @param array1 the first array |
universe@334 | 225 | * @param array2 the second array |
universe@123 | 226 | * @param cmpfnc the compare function |
universe@123 | 227 | * @param data additional data for the compare function |
universe@334 | 228 | * @return 1, if and only if the two arrays equal element-wise, 0 otherwise |
universe@123 | 229 | */ |
universe@356 | 230 | int ucx_array_equals(UcxArray const *array1, UcxArray const *array2, |
universe@123 | 231 | cmp_func cmpfnc, void* data); |
universe@4 | 232 | |
universe@123 | 233 | /** |
universe@334 | 234 | * Destroys the array. |
universe@123 | 235 | * |
universe@334 | 236 | * The data is freed and both capacity and count are reset to zero. |
universe@334 | 237 | * If the array structure itself has been dynamically allocated, it has to be |
universe@334 | 238 | * freed separately. |
universe@123 | 239 | * |
universe@353 | 240 | * @param array the array to destroy |
universe@123 | 241 | */ |
universe@353 | 242 | void ucx_array_destroy(UcxArray *array); |
universe@146 | 243 | |
universe@128 | 244 | /** |
universe@356 | 245 | * Destroys and frees the array. |
universe@356 | 246 | * |
universe@356 | 247 | * @param array the array to free |
universe@356 | 248 | */ |
universe@356 | 249 | void ucx_array_free(UcxArray *array); |
universe@356 | 250 | |
universe@356 | 251 | /** |
universe@354 | 252 | * Inserts elements at the end of the array. |
universe@354 | 253 | * |
universe@354 | 254 | * This is an O(1) operation. |
universe@354 | 255 | * The array will automatically grow, if the capacity is exceeded. |
universe@354 | 256 | * If a pointer to data is provided, the data is copied into the array with |
universe@354 | 257 | * memcpy(). Otherwise the new elements are completely zeroed. |
universe@354 | 258 | * |
universe@354 | 259 | * @param array a pointer the array where to append the data |
universe@354 | 260 | * @param data a pointer to the data to insert (may be <code>NULL</code>) |
universe@354 | 261 | * @param count number of elements to copy from data (if data is |
universe@354 | 262 | * <code>NULL</code>, zeroed elements are appended) |
universe@354 | 263 | * @return zero on success, non-zero if a reallocation was necessary but failed |
universe@354 | 264 | * @see ucx_array_set_from() |
universe@354 | 265 | * @see ucx_array_append() |
universe@354 | 266 | */ |
universe@354 | 267 | int ucx_array_append_from(UcxArray *array, void *data, size_t count); |
universe@354 | 268 | |
universe@354 | 269 | |
universe@354 | 270 | /** |
universe@354 | 271 | * Inserts elements at the beginning of the array. |
universe@354 | 272 | * |
universe@354 | 273 | * This is an expensive operation, because the contents must be moved. |
universe@354 | 274 | * If there is no particular reason to prepend data, you should use |
universe@354 | 275 | * ucx_array_append_from() instead. |
universe@354 | 276 | * |
universe@354 | 277 | * @param array a pointer the array where to prepend the data |
universe@354 | 278 | * @param data a pointer to the data to insert (may be <code>NULL</code>) |
universe@354 | 279 | * @param count number of elements to copy from data (if data is |
universe@354 | 280 | * <code>NULL</code>, zeroed elements are inserted) |
universe@354 | 281 | * @return zero on success, non-zero if a reallocation was necessary but failed |
universe@354 | 282 | * @see ucx_array_append_from() |
universe@354 | 283 | * @see ucx_array_set_from() |
universe@354 | 284 | * @see ucx_array_prepend() |
universe@354 | 285 | */ |
universe@354 | 286 | int ucx_array_prepend_from(UcxArray *array, void *data, size_t count); |
universe@354 | 287 | |
universe@354 | 288 | |
universe@354 | 289 | /** |
universe@354 | 290 | * Sets elements starting at the specified index. |
universe@354 | 291 | * |
universe@354 | 292 | * If the any index is out of bounds, the array automatically grows. |
universe@354 | 293 | * The pointer to the data may be NULL, in which case the elements are zeroed. |
universe@354 | 294 | * |
universe@354 | 295 | * @param array a pointer the array where to set the data |
universe@354 | 296 | * @param index the index of the element to set |
universe@354 | 297 | * @param data a pointer to the data to insert (may be <code>NULL</code>) |
universe@354 | 298 | * @param count number of elements to copy from data (if data is |
universe@354 | 299 | * <code>NULL</code>, the memory in the array is zeroed) |
universe@354 | 300 | * @return zero on success, non-zero if a reallocation was necessary but failed |
universe@354 | 301 | * @see ucx_array_append_from() |
universe@354 | 302 | * @see ucx_array_set() |
universe@354 | 303 | */ |
universe@354 | 304 | int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count); |
universe@354 | 305 | |
universe@354 | 306 | /** |
universe@334 | 307 | * Concatenates two arrays. |
universe@128 | 308 | * |
universe@334 | 309 | * The contents of the second array are appended to the first array in one |
universe@334 | 310 | * single operation. The second array is otherwise left untouched. |
universe@128 | 311 | * |
universe@334 | 312 | * The first array may grow automatically. If this fails, both arrays remain |
universe@334 | 313 | * unmodified. |
universe@334 | 314 | * |
universe@334 | 315 | * @param array1 first array |
universe@334 | 316 | * @param array2 second array |
universe@334 | 317 | * @return zero on success, non-zero if reallocation was necessary but failed |
universe@334 | 318 | * or the element size does not match |
universe@128 | 319 | */ |
universe@334 | 320 | int ucx_array_concat(UcxArray *array1, const UcxArray *array2); |
universe@146 | 321 | |
universe@128 | 322 | /** |
universe@334 | 323 | * Returns a pointer to the array element at the specified index. |
universe@128 | 324 | * |
universe@334 | 325 | * @param array the array to retrieve the element from |
universe@334 | 326 | * @param index index of the element to return |
universe@334 | 327 | * @return a pointer to the element at the specified index or <code>NULL</code>, |
universe@334 | 328 | * if the index is greater than the array size |
universe@128 | 329 | */ |
universe@356 | 330 | void *ucx_array_at(UcxArray const* array, size_t index); |
universe@291 | 331 | |
universe@291 | 332 | /** |
universe@128 | 333 | * Returns the index of an element containing the specified data. |
universe@128 | 334 | * |
universe@128 | 335 | * This function uses a cmp_func() to compare the data of each list element |
universe@334 | 336 | * with the specified data. If no cmp_func is provided, memcmp() is used. |
universe@128 | 337 | * |
universe@334 | 338 | * If the array contains the data more than once, the index of the first |
universe@128 | 339 | * occurrence is returned. |
universe@334 | 340 | * If the array does not contain the data, the size of array is returned. |
universe@128 | 341 | * |
universe@334 | 342 | * @param array the array where to search for the data |
universe@128 | 343 | * @param elem the element data |
universe@128 | 344 | * @param cmpfnc the compare function |
universe@128 | 345 | * @param data additional data for the compare function |
universe@334 | 346 | * @return the index of the element containing the specified data or the size of |
universe@334 | 347 | * the array, if the data is not found in this array |
universe@128 | 348 | */ |
universe@356 | 349 | size_t ucx_array_find(UcxArray const *array, void *elem, |
universe@356 | 350 | cmp_func cmpfnc, void *data); |
universe@146 | 351 | |
universe@128 | 352 | /** |
universe@334 | 353 | * Checks, if an array contains a specific element. |
universe@128 | 354 | * |
universe@334 | 355 | * An element is found, if ucx_array_find() returns a value less than the size. |
universe@128 | 356 | * |
universe@334 | 357 | * @param array the array where to search for the data |
universe@128 | 358 | * @param elem the element data |
universe@128 | 359 | * @param cmpfnc the compare function |
universe@128 | 360 | * @param data additional data for the compare function |
universe@334 | 361 | * @return 1, if and only if the array contains the specified element data |
universe@334 | 362 | * @see ucx_array_find() |
universe@128 | 363 | */ |
universe@356 | 364 | int ucx_array_contains(UcxArray const *array, void *elem, |
universe@356 | 365 | cmp_func cmpfnc, void *data); |
universe@35 | 366 | |
universe@128 | 367 | /** |
universe@342 | 368 | * Sorts a UcxArray with the best available sort algorithm. |
universe@128 | 369 | * |
universe@345 | 370 | * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS). |
universe@345 | 371 | * The order of arguments is automatically adjusted for the FreeBSD and MacOS |
universe@345 | 372 | * version of qsort_r(). |
universe@345 | 373 | * |
universe@345 | 374 | * If qsort_r() is not available, a merge sort algorithm is used, which is |
universe@345 | 375 | * guaranteed to use no more additional memory than for exactly one element. |
universe@128 | 376 | * |
universe@339 | 377 | * @param array the array to sort |
universe@128 | 378 | * @param cmpfnc the function that shall be used to compare the element data |
universe@343 | 379 | * @param data additional data for the cmp_func() or <code>NULL</code> |
universe@128 | 380 | */ |
universe@356 | 381 | void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data); |
universe@123 | 382 | |
universe@124 | 383 | /** |
universe@334 | 384 | * Removes an element from the array. |
universe@124 | 385 | * |
universe@334 | 386 | * This is in general an expensive operation, because several elements may |
universe@334 | 387 | * be moved. If the order of the elements is not relevant, use |
universe@334 | 388 | * ucx_array_remove_fast() instead. |
universe@124 | 389 | * |
universe@334 | 390 | * @param array pointer to the array from which the element shall be removed |
universe@334 | 391 | * @param index the index of the element to remove |
universe@124 | 392 | */ |
universe@334 | 393 | void ucx_array_remove(UcxArray *array, size_t index); |
universe@146 | 394 | |
universe@128 | 395 | /** |
universe@334 | 396 | * Removes an element from the array. |
universe@128 | 397 | * |
universe@334 | 398 | * This is an O(1) operation, but does not maintain the order of the elements. |
universe@334 | 399 | * The last element in the array is moved to the location of the removed |
universe@334 | 400 | * element. |
universe@128 | 401 | * |
universe@334 | 402 | * @param array pointer to the array from which the element shall be removed |
universe@334 | 403 | * @param index the index of the element to remove |
universe@128 | 404 | */ |
universe@334 | 405 | void ucx_array_remove_fast(UcxArray *array, size_t index); |
universe@334 | 406 | |
universe@334 | 407 | /** |
universe@334 | 408 | * Shrinks the memory to exactly fit the contents. |
universe@334 | 409 | * |
universe@334 | 410 | * After this operation, the capacity equals the size. |
universe@334 | 411 | * |
universe@334 | 412 | * @param array a pointer to the array |
universe@334 | 413 | * @return zero on success, non-zero if reallocation failed |
universe@334 | 414 | */ |
universe@334 | 415 | int ucx_array_shrink(UcxArray* array); |
universe@334 | 416 | |
universe@334 | 417 | /** |
universe@334 | 418 | * Sets the capacity of the array. |
universe@334 | 419 | * |
universe@334 | 420 | * If the new capacity is smaller than the size of the array, the elements |
universe@334 | 421 | * are removed and the size is adjusted accordingly. |
universe@334 | 422 | * |
universe@334 | 423 | * @param array a pointer to the array |
universe@334 | 424 | * @param capacity the new capacity |
universe@334 | 425 | * @return zero on success, non-zero if reallocation failed |
universe@334 | 426 | */ |
universe@334 | 427 | int ucx_array_resize(UcxArray* array, size_t capacity); |
universe@334 | 428 | |
universe@334 | 429 | /** |
universe@334 | 430 | * Resizes the array only, if the capacity is insufficient. |
universe@334 | 431 | * |
universe@334 | 432 | * If the requested capacity is smaller than the current capacity, this |
universe@334 | 433 | * function does nothing. |
universe@334 | 434 | * |
universe@334 | 435 | * @param array a pointer to the array |
universe@334 | 436 | * @param capacity the guaranteed capacity |
universe@334 | 437 | * @return zero on success, non-zero if reallocation failed |
universe@334 | 438 | */ |
universe@334 | 439 | int ucx_array_reserve(UcxArray* array, size_t capacity); |
universe@334 | 440 | |
universe@369 | 441 | /** |
universe@369 | 442 | * Resizes the capacity, if the specified number of elements would not fit. |
universe@369 | 443 | * |
universe@369 | 444 | * A call to ucx_array_grow(array, count) is effectively the same as |
universe@369 | 445 | * ucx_array_reserve(array, array->size+count). |
universe@369 | 446 | * |
universe@369 | 447 | * @param array a pointer to the array |
universe@369 | 448 | * @param count the number of elements that should additionally fit |
universe@369 | 449 | * into the array |
universe@369 | 450 | * @return zero on success, non-zero if reallocation failed |
universe@369 | 451 | */ |
universe@369 | 452 | int ucx_array_grow(UcxArray* array, size_t count); |
universe@334 | 453 | |
universe@4 | 454 | |
universe@4 | 455 | #ifdef __cplusplus |
universe@4 | 456 | } |
universe@4 | 457 | #endif |
universe@4 | 458 | |
universe@334 | 459 | #endif /* UCX_ARRAY_H */ |
universe@4 | 460 |