src/ucx/array.h

Sun, 03 Nov 2019 17:03:10 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 03 Nov 2019 17:03:10 +0100
changeset 365
72da0e4cbf4a
parent 357
0f5732f0dc00
child 369
28a8ccc442b0
permissions
-rw-r--r--

fix: missing documentation for first param in ucx_array_init_a()

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@355 74 *
universe@355 75 * If the capacity is insufficient, the array is automatically reallocated and
universe@355 76 * the possibly new pointer is stored in the <code>array</code> argument.
universe@355 77 *
universe@355 78 * On reallocation the capacity of the array is doubled until it is sufficient.
universe@355 79 * The new capacity is stored back to <code>capacity</code>.
universe@355 80 *
universe@355 81 * @param array a pointer to location of the array pointer
universe@355 82 * @param capacity a pointer to the capacity
universe@355 83 * @param elmsize the size of each element
universe@355 84 * @param idx the index of the element to set
universe@355 85 * @param data the element data
universe@355 86 * @return zero on success or non-zero on error (errno will be set)
universe@355 87 */
universe@355 88 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \
universe@355 89 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
universe@355 90 elmsize, idx, data)
universe@355 91
universe@355 92 /**
universe@355 93 * Convenience macro for ucx_array_util_set() which automatically computes
universe@355 94 * <code>sizeof(data)</code>.
universe@355 95 *
universe@355 96 * @param array a pointer to location of the array pointer
universe@355 97 * @param capacity a pointer to the capacity
universe@355 98 * @param idx the index of the element to set
universe@355 99 * @param data the element data
universe@355 100 * @return zero on success or non-zero on error (errno will be set)
universe@355 101 * @see ucx_array_util_set()
universe@355 102 */
universe@355 103 #define UCX_ARRAY_UTIL_SET(array, capacity, idx, data) \
universe@355 104 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
universe@355 105 sizeof(data), idx, data)
universe@355 106
universe@355 107 /**
universe@355 108 * Sets an element in an arbitrary user defined array.
universe@355 109 *
universe@355 110 * If the capacity is insufficient, the array is automatically reallocated
universe@355 111 * using the specified allocator and the possibly new pointer is stored in
universe@355 112 * the <code>array</code> argument.
universe@355 113 *
universe@355 114 * On reallocation the capacity of the array is doubled until it is sufficient.
universe@355 115 * The new capacity is stored back to <code>capacity</code>.
universe@355 116 *
universe@355 117 * @param alloc the allocator that shall be used to reallocate the array
universe@355 118 * @param array a pointer to location of the array pointer
universe@355 119 * @param capacity a pointer to the capacity
universe@355 120 * @param elmsize the size of each element
universe@355 121 * @param idx the index of the element to set
universe@355 122 * @param ... the element data
universe@355 123 * @return zero on success or non-zero on error (errno will be set)
universe@355 124 */
universe@355 125 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
universe@355 126 size_t elmsize, size_t idx, ...);
universe@355 127
universe@355 128
universe@355 129 /**
universe@355 130 * Convenience macro for ucx_array_util_set_a() which automatically computes
universe@355 131 * <code>sizeof(data)</code>.
universe@355 132 *
universe@355 133 * @param alloc the allocator that shall be used to reallocate the array
universe@355 134 * @param array a pointer to location of the array pointer
universe@355 135 * @param capacity a pointer to the capacity
universe@355 136 * @param idx the index of the element to set
universe@355 137 * @param data the element data
universe@355 138 * @return zero on success or non-zero on error (errno will be set)
universe@355 139 * @see ucx_array_util_set_a()
universe@355 140 */
universe@355 141 #define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \
universe@355 142 ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data)
universe@121 143
universe@123 144 /**
universe@334 145 * Creates a new UCX array with the given capacity and element size.
universe@334 146 * @param capacity the initial capacity
universe@334 147 * @param elemsize the element size
universe@356 148 * @return a pointer to a new UCX array structure
universe@123 149 */
universe@356 150 UcxArray* ucx_array_new(size_t capacity, size_t elemsize);
universe@146 151
universe@129 152 /**
universe@334 153 * Creates a new UCX array using the specified allocator.
universe@334 154 *
universe@334 155 * @param capacity the initial capacity
universe@334 156 * @param elemsize the element size
universe@334 157 * @param allocator the allocator to use
universe@356 158 * @return a pointer to new UCX array structure
universe@129 159 */
universe@356 160 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
universe@356 161 UcxAllocator* allocator);
universe@356 162
universe@356 163 /**
universe@356 164 * Initializes a UCX array structure with the given capacity and element size.
universe@356 165 * The structure must be uninitialized as the data pointer will be overwritten.
universe@356 166 *
universe@356 167 * @param array the structure to initialize
universe@356 168 * @param capacity the initial capacity
universe@356 169 * @param elemsize the element size
universe@356 170 */
universe@356 171 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize);
universe@356 172
universe@356 173 /**
universe@356 174 * Initializes a UCX array structure using the specified allocator.
universe@356 175 * The structure must be uninitialized as the data pointer will be overwritten.
universe@356 176 *
universe@365 177 * @param array the structure to initialize
universe@356 178 * @param capacity the initial capacity
universe@356 179 * @param elemsize the element size
universe@356 180 * @param allocator the allocator to use
universe@356 181 */
universe@356 182 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
universe@334 183 UcxAllocator* allocator);
universe@4 184
universe@128 185 /**
universe@334 186 * Creates an shallow copy of an array.
universe@128 187 *
universe@334 188 * This function clones the specified array by using memcpy().
universe@356 189 * If the destination capacity is insufficient, an automatic reallocation is
universe@356 190 * attempted.
universe@128 191 *
universe@357 192 * Note: if the destination array is uninitialized, the behavior is undefined.
universe@357 193 *
universe@356 194 * @param dest the array to copy to
universe@356 195 * @param src the array to copy from
universe@356 196 * @return zero on success, non-zero on reallocation failure.
universe@128 197 */
universe@356 198 int ucx_array_clone(UcxArray* dest, UcxArray const* src);
universe@334 199
universe@146 200
universe@128 201 /**
universe@334 202 * Compares two UCX arrays element-wise by using a compare function.
universe@334 203 *
universe@334 204 * Elements of the two specified arrays are compared by using the specified
universe@123 205 * compare function and the additional data. The type and content of this
universe@123 206 * additional data depends on the cmp_func() used.
universe@123 207 *
universe@334 208 * This function always returns zero, if the element sizes of the arrays do
universe@334 209 * not match and performs no comparisons in this case.
universe@123 210 *
universe@334 211 * @param array1 the first array
universe@334 212 * @param array2 the second array
universe@123 213 * @param cmpfnc the compare function
universe@123 214 * @param data additional data for the compare function
universe@334 215 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
universe@123 216 */
universe@356 217 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
universe@123 218 cmp_func cmpfnc, void* data);
universe@4 219
universe@123 220 /**
universe@334 221 * Destroys the array.
universe@123 222 *
universe@334 223 * The data is freed and both capacity and count are reset to zero.
universe@334 224 * If the array structure itself has been dynamically allocated, it has to be
universe@334 225 * freed separately.
universe@123 226 *
universe@353 227 * @param array the array to destroy
universe@123 228 */
universe@353 229 void ucx_array_destroy(UcxArray *array);
universe@146 230
universe@128 231 /**
universe@356 232 * Destroys and frees the array.
universe@356 233 *
universe@356 234 * @param array the array to free
universe@356 235 */
universe@356 236 void ucx_array_free(UcxArray *array);
universe@356 237
universe@356 238 /**
universe@354 239 * Inserts elements at the end of the array.
universe@354 240 *
universe@354 241 * This is an O(1) operation.
universe@354 242 * The array will automatically grow, if the capacity is exceeded.
universe@354 243 * If a pointer to data is provided, the data is copied into the array with
universe@354 244 * memcpy(). Otherwise the new elements are completely zeroed.
universe@354 245 *
universe@354 246 * @param array a pointer the array where to append the data
universe@354 247 * @param data a pointer to the data to insert (may be <code>NULL</code>)
universe@354 248 * @param count number of elements to copy from data (if data is
universe@354 249 * <code>NULL</code>, zeroed elements are appended)
universe@354 250 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@354 251 * @see ucx_array_set_from()
universe@354 252 * @see ucx_array_append()
universe@354 253 */
universe@354 254 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
universe@354 255
universe@354 256
universe@354 257 /**
universe@354 258 * Inserts elements at the beginning of the array.
universe@354 259 *
universe@354 260 * This is an expensive operation, because the contents must be moved.
universe@354 261 * If there is no particular reason to prepend data, you should use
universe@354 262 * ucx_array_append_from() instead.
universe@354 263 *
universe@354 264 * @param array a pointer the array where to prepend the data
universe@354 265 * @param data a pointer to the data to insert (may be <code>NULL</code>)
universe@354 266 * @param count number of elements to copy from data (if data is
universe@354 267 * <code>NULL</code>, zeroed elements are inserted)
universe@354 268 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@354 269 * @see ucx_array_append_from()
universe@354 270 * @see ucx_array_set_from()
universe@354 271 * @see ucx_array_prepend()
universe@354 272 */
universe@354 273 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
universe@354 274
universe@354 275
universe@354 276 /**
universe@354 277 * Sets elements starting at the specified index.
universe@354 278 *
universe@354 279 * If the any index is out of bounds, the array automatically grows.
universe@354 280 * The pointer to the data may be NULL, in which case the elements are zeroed.
universe@354 281 *
universe@354 282 * @param array a pointer the array where to set the data
universe@354 283 * @param index the index of the element to set
universe@354 284 * @param data a pointer to the data to insert (may be <code>NULL</code>)
universe@354 285 * @param count number of elements to copy from data (if data is
universe@354 286 * <code>NULL</code>, the memory in the array is zeroed)
universe@354 287 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@354 288 * @see ucx_array_append_from()
universe@354 289 * @see ucx_array_set()
universe@354 290 */
universe@354 291 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
universe@354 292
universe@354 293 /**
universe@334 294 * Inserts an element at the end of the array.
universe@128 295 *
universe@334 296 * This is an O(1) operation.
universe@334 297 * The array will automatically grow, if the capacity is exceeded.
universe@354 298 * If the type of the argument has a different size than the element size of
universe@354 299 * this array, the behavior is undefined.
universe@128 300 *
universe@334 301 * @param array a pointer the array where to append the data
universe@354 302 * @param elem the value to insert
universe@334 303 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@354 304 * @see ucx_array_append_from()
universe@354 305 * @see ucx_array_set()
universe@128 306 */
universe@354 307 #define ucx_array_append(array, elem) ucx_array_appendv(array, elem)
universe@354 308
universe@354 309 /**
universe@354 310 * For internal use.
universe@354 311 * Use ucx_array_append()
universe@354 312 *
universe@354 313 * @param array
universe@354 314 * @param ...
universe@354 315 * @return
universe@354 316 * @see ucx_array_append()
universe@354 317 */
universe@354 318 int ucx_array_appendv(UcxArray *array, ...);
universe@334 319
universe@146 320
universe@128 321 /**
universe@334 322 * Inserts an element at the beginning of the array.
universe@211 323 *
universe@334 324 * This is an expensive operation, because the contents must be moved.
universe@334 325 * If there is no particular reason to prepend data, you should use
universe@334 326 * ucx_array_append() instead.
universe@211 327 *
universe@334 328 * @param array a pointer the array where to prepend the data
universe@354 329 * @param elem the value to insert
universe@334 330 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@354 331 * @see ucx_array_append()
universe@354 332 * @see ucx_array_set_from()
universe@354 333 * @see ucx_array_prepend_from()
universe@211 334 */
universe@354 335 #define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem)
universe@354 336
universe@354 337 /**
universe@354 338 * For internal use.
universe@354 339 * Use ucx_array_prepend()
universe@354 340 *
universe@354 341 * @param array
universe@354 342 * @param ...
universe@354 343 * @return
universe@354 344 * @see ucx_array_prepend()
universe@354 345 */
universe@354 346 int ucx_array_prependv(UcxArray *array, ...);
universe@211 347
universe@337 348
universe@337 349 /**
universe@337 350 * Sets an element at the specified index.
universe@337 351 *
universe@354 352 * If the any index is out of bounds, the array automatically grows.
universe@337 353 *
universe@337 354 * @param array a pointer the array where to set the data
universe@337 355 * @param index the index of the element to set
universe@354 356 * @param elem the value to set
universe@337 357 * @return zero on success, non-zero if a reallocation was necessary but failed
universe@354 358 * @see ucx_array_append()
universe@354 359 * @see ucx_array_set_from()
universe@337 360 */
universe@354 361 #define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem)
universe@354 362
universe@354 363 /**
universe@354 364 * For internal use.
universe@354 365 * Use ucx_array_set()
universe@354 366 *
universe@354 367 * @param array
universe@354 368 * @param index
universe@354 369 * @param ...
universe@354 370 * @return
universe@354 371 * @see ucx_array_set()
universe@354 372 */
universe@354 373 int ucx_array_setv(UcxArray *array, size_t index, ...);
universe@337 374
universe@211 375 /**
universe@334 376 * Concatenates two arrays.
universe@128 377 *
universe@334 378 * The contents of the second array are appended to the first array in one
universe@334 379 * single operation. The second array is otherwise left untouched.
universe@128 380 *
universe@334 381 * The first array may grow automatically. If this fails, both arrays remain
universe@334 382 * unmodified.
universe@334 383 *
universe@334 384 * @param array1 first array
universe@334 385 * @param array2 second array
universe@334 386 * @return zero on success, non-zero if reallocation was necessary but failed
universe@334 387 * or the element size does not match
universe@128 388 */
universe@334 389 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
universe@146 390
universe@128 391 /**
universe@334 392 * Returns a pointer to the array element at the specified index.
universe@128 393 *
universe@334 394 * @param array the array to retrieve the element from
universe@334 395 * @param index index of the element to return
universe@334 396 * @return a pointer to the element at the specified index or <code>NULL</code>,
universe@334 397 * if the index is greater than the array size
universe@128 398 */
universe@356 399 void *ucx_array_at(UcxArray const* array, size_t index);
universe@291 400
universe@291 401 /**
universe@128 402 * Returns the index of an element containing the specified data.
universe@128 403 *
universe@128 404 * This function uses a cmp_func() to compare the data of each list element
universe@334 405 * with the specified data. If no cmp_func is provided, memcmp() is used.
universe@128 406 *
universe@334 407 * If the array contains the data more than once, the index of the first
universe@128 408 * occurrence is returned.
universe@334 409 * If the array does not contain the data, the size of array is returned.
universe@128 410 *
universe@334 411 * @param array the array where to search for the data
universe@128 412 * @param elem the element data
universe@128 413 * @param cmpfnc the compare function
universe@128 414 * @param data additional data for the compare function
universe@334 415 * @return the index of the element containing the specified data or the size of
universe@334 416 * the array, if the data is not found in this array
universe@128 417 */
universe@356 418 size_t ucx_array_find(UcxArray const *array, void *elem,
universe@356 419 cmp_func cmpfnc, void *data);
universe@146 420
universe@128 421 /**
universe@334 422 * Checks, if an array contains a specific element.
universe@128 423 *
universe@334 424 * An element is found, if ucx_array_find() returns a value less than the size.
universe@128 425 *
universe@334 426 * @param array the array where to search for the data
universe@128 427 * @param elem the element data
universe@128 428 * @param cmpfnc the compare function
universe@128 429 * @param data additional data for the compare function
universe@334 430 * @return 1, if and only if the array contains the specified element data
universe@334 431 * @see ucx_array_find()
universe@128 432 */
universe@356 433 int ucx_array_contains(UcxArray const *array, void *elem,
universe@356 434 cmp_func cmpfnc, void *data);
universe@35 435
universe@128 436 /**
universe@342 437 * Sorts a UcxArray with the best available sort algorithm.
universe@128 438 *
universe@345 439 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
universe@345 440 * The order of arguments is automatically adjusted for the FreeBSD and MacOS
universe@345 441 * version of qsort_r().
universe@345 442 *
universe@345 443 * If qsort_r() is not available, a merge sort algorithm is used, which is
universe@345 444 * guaranteed to use no more additional memory than for exactly one element.
universe@128 445 *
universe@339 446 * @param array the array to sort
universe@128 447 * @param cmpfnc the function that shall be used to compare the element data
universe@343 448 * @param data additional data for the cmp_func() or <code>NULL</code>
universe@128 449 */
universe@356 450 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data);
universe@123 451
universe@124 452 /**
universe@334 453 * Removes an element from the array.
universe@124 454 *
universe@334 455 * This is in general an expensive operation, because several elements may
universe@334 456 * be moved. If the order of the elements is not relevant, use
universe@334 457 * ucx_array_remove_fast() instead.
universe@124 458 *
universe@334 459 * @param array pointer to the array from which the element shall be removed
universe@334 460 * @param index the index of the element to remove
universe@124 461 */
universe@334 462 void ucx_array_remove(UcxArray *array, size_t index);
universe@146 463
universe@128 464 /**
universe@334 465 * Removes an element from the array.
universe@128 466 *
universe@334 467 * This is an O(1) operation, but does not maintain the order of the elements.
universe@334 468 * The last element in the array is moved to the location of the removed
universe@334 469 * element.
universe@128 470 *
universe@334 471 * @param array pointer to the array from which the element shall be removed
universe@334 472 * @param index the index of the element to remove
universe@128 473 */
universe@334 474 void ucx_array_remove_fast(UcxArray *array, size_t index);
universe@334 475
universe@334 476 /**
universe@334 477 * Shrinks the memory to exactly fit the contents.
universe@334 478 *
universe@334 479 * After this operation, the capacity equals the size.
universe@334 480 *
universe@334 481 * @param array a pointer to the array
universe@334 482 * @return zero on success, non-zero if reallocation failed
universe@334 483 */
universe@334 484 int ucx_array_shrink(UcxArray* array);
universe@334 485
universe@334 486 /**
universe@334 487 * Sets the capacity of the array.
universe@334 488 *
universe@334 489 * If the new capacity is smaller than the size of the array, the elements
universe@334 490 * are removed and the size is adjusted accordingly.
universe@334 491 *
universe@334 492 * @param array a pointer to the array
universe@334 493 * @param capacity the new capacity
universe@334 494 * @return zero on success, non-zero if reallocation failed
universe@334 495 */
universe@334 496 int ucx_array_resize(UcxArray* array, size_t capacity);
universe@334 497
universe@334 498 /**
universe@334 499 * Resizes the array only, if the capacity is insufficient.
universe@334 500 *
universe@334 501 * If the requested capacity is smaller than the current capacity, this
universe@334 502 * function does nothing.
universe@334 503 *
universe@334 504 * @param array a pointer to the array
universe@334 505 * @param capacity the guaranteed capacity
universe@334 506 * @return zero on success, non-zero if reallocation failed
universe@334 507 */
universe@334 508 int ucx_array_reserve(UcxArray* array, size_t capacity);
universe@334 509
universe@334 510
universe@4 511
universe@4 512 #ifdef __cplusplus
universe@4 513 }
universe@4 514 #endif
universe@4 515
universe@334 516 #endif /* UCX_ARRAY_H */
universe@4 517

mercurial