src/ucx/array.h

Thu, 03 Oct 2019 10:55:39 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 Oct 2019 10:55:39 +0200
branch
feature/array
changeset 356
77efe51c6c9a
parent 355
d315a068235a
child 357
0f5732f0dc00
permissions
-rw-r--r--

changes UcxArray from value to pointer semantics

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

mercurial