src/ucx/array.h

Tue, 24 Sep 2019 20:16:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 24 Sep 2019 20:16:00 +0200
branch
feature/array
changeset 355
d315a068235a
parent 354
7fd13b9f8f60
child 356
77efe51c6c9a
permissions
-rw-r--r--

adds array utility functions for user defined arrays

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

mercurial