diff -r 97c53f7ef5e4 -r 28a8ccc442b0 src/ucx/array.h --- a/src/ucx/array.h Wed Nov 06 21:01:25 2019 +0100 +++ b/src/ucx/array.h Thu Nov 07 10:10:36 2019 +0100 @@ -71,6 +71,7 @@ /** * Sets an element in an arbitrary user defined array. + * The data is copied from the specified data location. * * If the capacity is insufficient, the array is automatically reallocated and * the possibly new pointer is stored in the array argument. @@ -82,7 +83,7 @@ * @param capacity a pointer to the capacity * @param elmsize the size of each element * @param idx the index of the element to set - * @param data the element data + * @param data a pointer to the element data * @return zero on success or non-zero on error (errno will be set) */ #define ucx_array_util_set(array, capacity, elmsize, idx, data) \ @@ -90,22 +91,8 @@ elmsize, idx, data) /** - * Convenience macro for ucx_array_util_set() which automatically computes - * sizeof(data). - * - * @param array a pointer to location of the array pointer - * @param capacity a pointer to the capacity - * @param idx the index of the element to set - * @param data the element data - * @return zero on success or non-zero on error (errno will be set) - * @see ucx_array_util_set() - */ -#define UCX_ARRAY_UTIL_SET(array, capacity, idx, data) \ - ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ - sizeof(data), idx, data) - -/** * Sets an element in an arbitrary user defined array. + * The data is copied from the specified data location. * * If the capacity is insufficient, the array is automatically reallocated * using the specified allocator and the possibly new pointer is stored in @@ -119,27 +106,53 @@ * @param capacity a pointer to the capacity * @param elmsize the size of each element * @param idx the index of the element to set - * @param ... the element data + * @param data a pointer to the element data * @return zero on success or non-zero on error (errno will be set) */ int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, - size_t elmsize, size_t idx, ...); - + size_t elmsize, size_t idx, void* data); /** - * Convenience macro for ucx_array_util_set_a() which automatically computes - * sizeof(data). + * Stores a pointer in an arbitrary user defined array. + * The element size of the array must be sizeof(void*). + * + * If the capacity is insufficient, the array is automatically reallocated and + * the possibly new pointer is stored in the array argument. + * + * On reallocation the capacity of the array is doubled until it is sufficient. + * The new capacity is stored back to capacity. + * + * @param array a pointer to location of the array pointer + * @param capacity a pointer to the capacity + * @param idx the index of the element to set + * @param ptr the pointer to store + * @return zero on success or non-zero on error (errno will be set) + */ +#define ucx_array_util_setptr(array, capacity, idx, ptr) \ + ucx_array_util_setptr_a(ucx_default_allocator(), (void**)(array), \ + capacity, idx, ptr) + +/** + * Stores a pointer in an arbitrary user defined array. + * The element size of the array must be sizeof(void*). + * + * If the capacity is insufficient, the array is automatically reallocated + * using the specified allocator and the possibly new pointer is stored in + * the array argument. + * + * On reallocation the capacity of the array is doubled until it is sufficient. + * The new capacity is stored back to capacity. * * @param alloc the allocator that shall be used to reallocate the array * @param array a pointer to location of the array pointer * @param capacity a pointer to the capacity * @param idx the index of the element to set - * @param data the element data + * @param ptr the pointer to store * @return zero on success or non-zero on error (errno will be set) - * @see ucx_array_util_set_a() */ -#define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \ - ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data) +int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity, + size_t idx, void* ptr); + /** * Creates a new UCX array with the given capacity and element size. @@ -291,88 +304,6 @@ int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count); /** - * Inserts an element at the end of the array. - * - * This is an O(1) operation. - * The array will automatically grow, if the capacity is exceeded. - * If the type of the argument has a different size than the element size of - * this array, the behavior is undefined. - * - * @param array a pointer the array where to append the data - * @param elem the value to insert - * @return zero on success, non-zero if a reallocation was necessary but failed - * @see ucx_array_append_from() - * @see ucx_array_set() - */ -#define ucx_array_append(array, elem) ucx_array_appendv(array, elem) - -/** - * For internal use. - * Use ucx_array_append() - * - * @param array - * @param ... - * @return - * @see ucx_array_append() - */ -int ucx_array_appendv(UcxArray *array, ...); - - -/** - * Inserts an element at the beginning of the array. - * - * This is an expensive operation, because the contents must be moved. - * If there is no particular reason to prepend data, you should use - * ucx_array_append() instead. - * - * @param array a pointer the array where to prepend the data - * @param elem the value to insert - * @return zero on success, non-zero if a reallocation was necessary but failed - * @see ucx_array_append() - * @see ucx_array_set_from() - * @see ucx_array_prepend_from() - */ -#define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem) - -/** - * For internal use. - * Use ucx_array_prepend() - * - * @param array - * @param ... - * @return - * @see ucx_array_prepend() - */ -int ucx_array_prependv(UcxArray *array, ...); - - -/** - * Sets an element at the specified index. - * - * If the any index is out of bounds, the array automatically grows. - * - * @param array a pointer the array where to set the data - * @param index the index of the element to set - * @param elem the value to set - * @return zero on success, non-zero if a reallocation was necessary but failed - * @see ucx_array_append() - * @see ucx_array_set_from() - */ -#define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem) - -/** - * For internal use. - * Use ucx_array_set() - * - * @param array - * @param index - * @param ... - * @return - * @see ucx_array_set() - */ -int ucx_array_setv(UcxArray *array, size_t index, ...); - -/** * Concatenates two arrays. * * The contents of the second array are appended to the first array in one @@ -507,6 +438,18 @@ */ int ucx_array_reserve(UcxArray* array, size_t capacity); +/** + * Resizes the capacity, if the specified number of elements would not fit. + * + * A call to ucx_array_grow(array, count) is effectively the same as + * ucx_array_reserve(array, array->size+count). + * + * @param array a pointer to the array + * @param count the number of elements that should additionally fit + * into the array + * @return zero on success, non-zero if reallocation failed + */ +int ucx_array_grow(UcxArray* array, size_t count); #ifdef __cplusplus