src/ucx/array.h

Thu, 03 Oct 2019 11:15:48 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 Oct 2019 11:15:48 +0200
branch
feature/array
changeset 357
0f5732f0dc00
parent 356
77efe51c6c9a
child 365
72da0e4cbf4a
permissions
-rw-r--r--

fixes two bugs: clone to uninitialized array and missing return in ucx_array_ensurecap()

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

mercurial