universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@103: * universe@334: * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved. universe@103: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@103: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@4: */ universe@123: /** universe@334: * Dynamically allocated array implementation. universe@123: * universe@334: * @file array.h universe@123: * @author Mike Becker universe@123: * @author Olaf Wintermann universe@123: */ universe@4: universe@334: #ifndef UCX_ARRAY_H universe@334: #define UCX_ARRAY_H universe@4: universe@259: #include "ucx.h" universe@259: #include "allocator.h" universe@7: universe@4: #ifdef __cplusplus universe@4: extern "C" { universe@4: #endif universe@4: universe@121: /** universe@334: * UCX array type. universe@121: */ universe@334: typedef struct { universe@334: /** universe@334: * The current capacity of the array. universe@334: */ universe@334: size_t capacity; universe@334: /** universe@334: * The actual number of elements in the array. universe@334: */ universe@334: size_t size; universe@334: /** universe@334: * The size of an individual element in bytes. universe@334: */ universe@334: size_t elemsize; universe@334: /** universe@334: * A pointer to the data. universe@334: */ universe@334: void* data; universe@334: /** universe@334: * The allocator used for the data. universe@334: */ universe@334: UcxAllocator* allocator; universe@334: } UcxArray; universe@334: universe@355: /** universe@355: * Sets an element in an arbitrary user defined array. universe@355: * universe@355: * If the capacity is insufficient, the array is automatically reallocated and universe@355: * the possibly new pointer is stored in the array argument. universe@355: * universe@355: * On reallocation the capacity of the array is doubled until it is sufficient. universe@355: * The new capacity is stored back to capacity. universe@355: * universe@355: * @param array a pointer to location of the array pointer universe@355: * @param capacity a pointer to the capacity universe@355: * @param elmsize the size of each element universe@355: * @param idx the index of the element to set universe@355: * @param data the element data universe@355: * @return zero on success or non-zero on error (errno will be set) universe@355: */ universe@355: #define ucx_array_util_set(array, capacity, elmsize, idx, data) \ universe@355: ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ universe@355: elmsize, idx, data) universe@355: universe@355: /** universe@355: * Convenience macro for ucx_array_util_set() which automatically computes universe@355: * sizeof(data). universe@355: * universe@355: * @param array a pointer to location of the array pointer universe@355: * @param capacity a pointer to the capacity universe@355: * @param idx the index of the element to set universe@355: * @param data the element data universe@355: * @return zero on success or non-zero on error (errno will be set) universe@355: * @see ucx_array_util_set() universe@355: */ universe@355: #define UCX_ARRAY_UTIL_SET(array, capacity, idx, data) \ universe@355: ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ universe@355: sizeof(data), idx, data) universe@355: universe@355: /** universe@355: * Sets an element in an arbitrary user defined array. universe@355: * universe@355: * If the capacity is insufficient, the array is automatically reallocated universe@355: * using the specified allocator and the possibly new pointer is stored in universe@355: * the array argument. universe@355: * universe@355: * On reallocation the capacity of the array is doubled until it is sufficient. universe@355: * The new capacity is stored back to capacity. universe@355: * universe@355: * @param alloc the allocator that shall be used to reallocate the array universe@355: * @param array a pointer to location of the array pointer universe@355: * @param capacity a pointer to the capacity universe@355: * @param elmsize the size of each element universe@355: * @param idx the index of the element to set universe@355: * @param ... the element data universe@355: * @return zero on success or non-zero on error (errno will be set) universe@355: */ universe@355: int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, universe@355: size_t elmsize, size_t idx, ...); universe@355: universe@355: universe@355: /** universe@355: * Convenience macro for ucx_array_util_set_a() which automatically computes universe@355: * sizeof(data). universe@355: * universe@355: * @param alloc the allocator that shall be used to reallocate the array universe@355: * @param array a pointer to location of the array pointer universe@355: * @param capacity a pointer to the capacity universe@355: * @param idx the index of the element to set universe@355: * @param data the element data universe@355: * @return zero on success or non-zero on error (errno will be set) universe@355: * @see ucx_array_util_set_a() universe@355: */ universe@355: #define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \ universe@355: ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data) universe@121: universe@123: /** universe@334: * Creates a new UCX array with the given capacity and element size. universe@334: * @param capacity the initial capacity universe@334: * @param elemsize the element size universe@334: * @return a new UCX array structure universe@123: */ universe@334: UcxArray ucx_array_new(size_t capacity, size_t elemsize); universe@146: universe@129: /** universe@334: * Creates a new UCX array using the specified allocator. universe@334: * universe@334: * @param capacity the initial capacity universe@334: * @param elemsize the element size universe@334: * @param allocator the allocator to use universe@334: * @return a new UCX array structure universe@129: */ universe@334: UcxArray ucx_array_new_a(size_t capacity, size_t elemsize, universe@334: UcxAllocator* allocator); universe@4: universe@128: /** universe@334: * Creates an shallow copy of an array. universe@128: * universe@334: * This function clones the specified array by using memcpy(). universe@128: * universe@334: * @param array the array to copy universe@334: * @return the copy (may be an empty array on allocation errors) universe@128: */ universe@334: UcxArray ucx_array_clone(UcxArray array); universe@334: universe@146: universe@128: /** universe@334: * Compares two UCX arrays element-wise by using a compare function. universe@334: * universe@334: * Elements of the two specified arrays are compared by using the specified universe@123: * compare function and the additional data. The type and content of this universe@123: * additional data depends on the cmp_func() used. universe@123: * universe@334: * This function always returns zero, if the element sizes of the arrays do universe@334: * not match and performs no comparisons in this case. universe@123: * universe@334: * @param array1 the first array universe@334: * @param array2 the second array universe@123: * @param cmpfnc the compare function universe@123: * @param data additional data for the compare function universe@334: * @return 1, if and only if the two arrays equal element-wise, 0 otherwise universe@123: */ universe@334: int ucx_array_equals(UcxArray array1, UcxArray array2, universe@123: cmp_func cmpfnc, void* data); universe@4: universe@123: /** universe@334: * Destroys the array. universe@123: * universe@334: * The data is freed and both capacity and count are reset to zero. universe@334: * If the array structure itself has been dynamically allocated, it has to be universe@334: * freed separately. universe@123: * universe@353: * @param array the array to destroy universe@123: */ universe@353: void ucx_array_destroy(UcxArray *array); universe@146: universe@128: /** universe@354: * Inserts elements at the end of the array. universe@354: * universe@354: * This is an O(1) operation. universe@354: * The array will automatically grow, if the capacity is exceeded. universe@354: * If a pointer to data is provided, the data is copied into the array with universe@354: * memcpy(). Otherwise the new elements are completely zeroed. universe@354: * universe@354: * @param array a pointer the array where to append the data universe@354: * @param data a pointer to the data to insert (may be NULL) universe@354: * @param count number of elements to copy from data (if data is universe@354: * NULL, zeroed elements are appended) universe@354: * @return zero on success, non-zero if a reallocation was necessary but failed universe@354: * @see ucx_array_set_from() universe@354: * @see ucx_array_append() universe@354: */ universe@354: int ucx_array_append_from(UcxArray *array, void *data, size_t count); universe@354: universe@354: universe@354: /** universe@354: * Inserts elements at the beginning of the array. universe@354: * universe@354: * This is an expensive operation, because the contents must be moved. universe@354: * If there is no particular reason to prepend data, you should use universe@354: * ucx_array_append_from() instead. universe@354: * universe@354: * @param array a pointer the array where to prepend the data universe@354: * @param data a pointer to the data to insert (may be NULL) universe@354: * @param count number of elements to copy from data (if data is universe@354: * NULL, zeroed elements are inserted) universe@354: * @return zero on success, non-zero if a reallocation was necessary but failed universe@354: * @see ucx_array_append_from() universe@354: * @see ucx_array_set_from() universe@354: * @see ucx_array_prepend() universe@354: */ universe@354: int ucx_array_prepend_from(UcxArray *array, void *data, size_t count); universe@354: universe@354: universe@354: /** universe@354: * Sets elements starting at the specified index. universe@354: * universe@354: * If the any index is out of bounds, the array automatically grows. universe@354: * The pointer to the data may be NULL, in which case the elements are zeroed. universe@354: * universe@354: * @param array a pointer the array where to set the data universe@354: * @param index the index of the element to set universe@354: * @param data a pointer to the data to insert (may be NULL) universe@354: * @param count number of elements to copy from data (if data is universe@354: * NULL, the memory in the array is zeroed) universe@354: * @return zero on success, non-zero if a reallocation was necessary but failed universe@354: * @see ucx_array_append_from() universe@354: * @see ucx_array_set() universe@354: */ universe@354: int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count); universe@354: universe@354: /** universe@334: * Inserts an element at the end of the array. universe@128: * universe@334: * This is an O(1) operation. universe@334: * The array will automatically grow, if the capacity is exceeded. universe@354: * If the type of the argument has a different size than the element size of universe@354: * this array, the behavior is undefined. universe@128: * universe@334: * @param array a pointer the array where to append the data universe@354: * @param elem the value to insert universe@334: * @return zero on success, non-zero if a reallocation was necessary but failed universe@354: * @see ucx_array_append_from() universe@354: * @see ucx_array_set() universe@128: */ universe@354: #define ucx_array_append(array, elem) ucx_array_appendv(array, elem) universe@354: universe@354: /** universe@354: * For internal use. universe@354: * Use ucx_array_append() universe@354: * universe@354: * @param array universe@354: * @param ... universe@354: * @return universe@354: * @see ucx_array_append() universe@354: */ universe@354: int ucx_array_appendv(UcxArray *array, ...); universe@334: universe@146: universe@128: /** universe@334: * Inserts an element at the beginning of the array. universe@211: * universe@334: * This is an expensive operation, because the contents must be moved. universe@334: * If there is no particular reason to prepend data, you should use universe@334: * ucx_array_append() instead. universe@211: * universe@334: * @param array a pointer the array where to prepend the data universe@354: * @param elem the value to insert universe@334: * @return zero on success, non-zero if a reallocation was necessary but failed universe@354: * @see ucx_array_append() universe@354: * @see ucx_array_set_from() universe@354: * @see ucx_array_prepend_from() universe@211: */ universe@354: #define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem) universe@354: universe@354: /** universe@354: * For internal use. universe@354: * Use ucx_array_prepend() universe@354: * universe@354: * @param array universe@354: * @param ... universe@354: * @return universe@354: * @see ucx_array_prepend() universe@354: */ universe@354: int ucx_array_prependv(UcxArray *array, ...); universe@211: universe@337: universe@337: /** universe@337: * Sets an element at the specified index. universe@337: * universe@354: * If the any index is out of bounds, the array automatically grows. universe@337: * universe@337: * @param array a pointer the array where to set the data universe@337: * @param index the index of the element to set universe@354: * @param elem the value to set universe@337: * @return zero on success, non-zero if a reallocation was necessary but failed universe@354: * @see ucx_array_append() universe@354: * @see ucx_array_set_from() universe@337: */ universe@354: #define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem) universe@354: universe@354: /** universe@354: * For internal use. universe@354: * Use ucx_array_set() universe@354: * universe@354: * @param array universe@354: * @param index universe@354: * @param ... universe@354: * @return universe@354: * @see ucx_array_set() universe@354: */ universe@354: int ucx_array_setv(UcxArray *array, size_t index, ...); universe@337: universe@211: /** universe@334: * Concatenates two arrays. universe@128: * universe@334: * The contents of the second array are appended to the first array in one universe@334: * single operation. The second array is otherwise left untouched. universe@128: * universe@334: * The first array may grow automatically. If this fails, both arrays remain universe@334: * unmodified. universe@334: * universe@334: * @param array1 first array universe@334: * @param array2 second array universe@334: * @return zero on success, non-zero if reallocation was necessary but failed universe@334: * or the element size does not match universe@128: */ universe@334: int ucx_array_concat(UcxArray *array1, const UcxArray *array2); universe@146: universe@128: /** universe@334: * Returns a pointer to the array element at the specified index. universe@128: * universe@334: * @param array the array to retrieve the element from universe@334: * @param index index of the element to return universe@334: * @return a pointer to the element at the specified index or NULL, universe@334: * if the index is greater than the array size universe@128: */ universe@334: void *ucx_array_at(UcxArray array, size_t index); universe@291: universe@291: /** universe@128: * Returns the index of an element containing the specified data. universe@128: * universe@128: * This function uses a cmp_func() to compare the data of each list element universe@334: * with the specified data. If no cmp_func is provided, memcmp() is used. universe@128: * universe@334: * If the array contains the data more than once, the index of the first universe@128: * occurrence is returned. universe@334: * If the array does not contain the data, the size of array is returned. universe@128: * universe@334: * @param array the array where to search for the data universe@128: * @param elem the element data universe@128: * @param cmpfnc the compare function universe@128: * @param data additional data for the compare function universe@334: * @return the index of the element containing the specified data or the size of universe@334: * the array, if the data is not found in this array universe@128: */ universe@334: size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data); universe@146: universe@128: /** universe@334: * Checks, if an array contains a specific element. universe@128: * universe@334: * An element is found, if ucx_array_find() returns a value less than the size. universe@128: * universe@334: * @param array the array where to search for the data universe@128: * @param elem the element data universe@128: * @param cmpfnc the compare function universe@128: * @param data additional data for the compare function universe@334: * @return 1, if and only if the array contains the specified element data universe@334: * @see ucx_array_find() universe@128: */ universe@334: int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data); universe@35: universe@128: /** universe@342: * Sorts a UcxArray with the best available sort algorithm. universe@128: * universe@345: * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS). universe@345: * The order of arguments is automatically adjusted for the FreeBSD and MacOS universe@345: * version of qsort_r(). universe@345: * universe@345: * If qsort_r() is not available, a merge sort algorithm is used, which is universe@345: * guaranteed to use no more additional memory than for exactly one element. universe@128: * universe@339: * @param array the array to sort universe@128: * @param cmpfnc the function that shall be used to compare the element data universe@343: * @param data additional data for the cmp_func() or NULL universe@128: */ universe@336: void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data); universe@123: universe@124: /** universe@334: * Removes an element from the array. universe@124: * universe@334: * This is in general an expensive operation, because several elements may universe@334: * be moved. If the order of the elements is not relevant, use universe@334: * ucx_array_remove_fast() instead. universe@124: * universe@334: * @param array pointer to the array from which the element shall be removed universe@334: * @param index the index of the element to remove universe@124: */ universe@334: void ucx_array_remove(UcxArray *array, size_t index); universe@146: universe@128: /** universe@334: * Removes an element from the array. universe@128: * universe@334: * This is an O(1) operation, but does not maintain the order of the elements. universe@334: * The last element in the array is moved to the location of the removed universe@334: * element. universe@128: * universe@334: * @param array pointer to the array from which the element shall be removed universe@334: * @param index the index of the element to remove universe@128: */ universe@334: void ucx_array_remove_fast(UcxArray *array, size_t index); universe@334: universe@334: /** universe@334: * Shrinks the memory to exactly fit the contents. universe@334: * universe@334: * After this operation, the capacity equals the size. universe@334: * universe@334: * @param array a pointer to the array universe@334: * @return zero on success, non-zero if reallocation failed universe@334: */ universe@334: int ucx_array_shrink(UcxArray* array); universe@334: universe@334: /** universe@334: * Sets the capacity of the array. universe@334: * universe@334: * If the new capacity is smaller than the size of the array, the elements universe@334: * are removed and the size is adjusted accordingly. universe@334: * universe@334: * @param array a pointer to the array universe@334: * @param capacity the new capacity universe@334: * @return zero on success, non-zero if reallocation failed universe@334: */ universe@334: int ucx_array_resize(UcxArray* array, size_t capacity); universe@334: universe@334: /** universe@334: * Resizes the array only, if the capacity is insufficient. universe@334: * universe@334: * If the requested capacity is smaller than the current capacity, this universe@334: * function does nothing. universe@334: * universe@334: * @param array a pointer to the array universe@334: * @param capacity the guaranteed capacity universe@334: * @return zero on success, non-zero if reallocation failed universe@334: */ universe@334: int ucx_array_reserve(UcxArray* array, size_t capacity); universe@334: universe@334: universe@4: universe@4: #ifdef __cplusplus universe@4: } universe@4: #endif universe@4: universe@334: #endif /* UCX_ARRAY_H */ universe@4: