1.1 --- a/src/ucx/array.h Sat Aug 10 11:12:49 2019 +0200 1.2 +++ b/src/ucx/array.h Tue Sep 24 20:16:00 2019 +0200 1.3 @@ -69,6 +69,77 @@ 1.4 UcxAllocator* allocator; 1.5 } UcxArray; 1.6 1.7 +/** 1.8 + * Sets an element in an arbitrary user defined array. 1.9 + * 1.10 + * If the capacity is insufficient, the array is automatically reallocated and 1.11 + * the possibly new pointer is stored in the <code>array</code> argument. 1.12 + * 1.13 + * On reallocation the capacity of the array is doubled until it is sufficient. 1.14 + * The new capacity is stored back to <code>capacity</code>. 1.15 + * 1.16 + * @param array a pointer to location of the array pointer 1.17 + * @param capacity a pointer to the capacity 1.18 + * @param elmsize the size of each element 1.19 + * @param idx the index of the element to set 1.20 + * @param data the element data 1.21 + * @return zero on success or non-zero on error (errno will be set) 1.22 + */ 1.23 +#define ucx_array_util_set(array, capacity, elmsize, idx, data) \ 1.24 + ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ 1.25 + elmsize, idx, data) 1.26 + 1.27 +/** 1.28 + * Convenience macro for ucx_array_util_set() which automatically computes 1.29 + * <code>sizeof(data)</code>. 1.30 + * 1.31 + * @param array a pointer to location of the array pointer 1.32 + * @param capacity a pointer to the capacity 1.33 + * @param idx the index of the element to set 1.34 + * @param data the element data 1.35 + * @return zero on success or non-zero on error (errno will be set) 1.36 + * @see ucx_array_util_set() 1.37 + */ 1.38 +#define UCX_ARRAY_UTIL_SET(array, capacity, idx, data) \ 1.39 + ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ 1.40 + sizeof(data), idx, data) 1.41 + 1.42 +/** 1.43 + * Sets an element in an arbitrary user defined array. 1.44 + * 1.45 + * If the capacity is insufficient, the array is automatically reallocated 1.46 + * using the specified allocator and the possibly new pointer is stored in 1.47 + * the <code>array</code> argument. 1.48 + * 1.49 + * On reallocation the capacity of the array is doubled until it is sufficient. 1.50 + * The new capacity is stored back to <code>capacity</code>. 1.51 + * 1.52 + * @param alloc the allocator that shall be used to reallocate the array 1.53 + * @param array a pointer to location of the array pointer 1.54 + * @param capacity a pointer to the capacity 1.55 + * @param elmsize the size of each element 1.56 + * @param idx the index of the element to set 1.57 + * @param ... the element data 1.58 + * @return zero on success or non-zero on error (errno will be set) 1.59 + */ 1.60 +int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, 1.61 + size_t elmsize, size_t idx, ...); 1.62 + 1.63 + 1.64 +/** 1.65 + * Convenience macro for ucx_array_util_set_a() which automatically computes 1.66 + * <code>sizeof(data)</code>. 1.67 + * 1.68 + * @param alloc the allocator that shall be used to reallocate the array 1.69 + * @param array a pointer to location of the array pointer 1.70 + * @param capacity a pointer to the capacity 1.71 + * @param idx the index of the element to set 1.72 + * @param data the element data 1.73 + * @return zero on success or non-zero on error (errno will be set) 1.74 + * @see ucx_array_util_set_a() 1.75 + */ 1.76 +#define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \ 1.77 + ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data) 1.78 1.79 /** 1.80 * Creates a new UCX array with the given capacity and element size.