1.1 --- a/src/ucx/array.h Wed Nov 06 21:01:25 2019 +0100 1.2 +++ b/src/ucx/array.h Thu Nov 07 10:10:36 2019 +0100 1.3 @@ -71,6 +71,7 @@ 1.4 1.5 /** 1.6 * Sets an element in an arbitrary user defined array. 1.7 + * The data is copied from the specified data location. 1.8 * 1.9 * If the capacity is insufficient, the array is automatically reallocated and 1.10 * the possibly new pointer is stored in the <code>array</code> argument. 1.11 @@ -82,7 +83,7 @@ 1.12 * @param capacity a pointer to the capacity 1.13 * @param elmsize the size of each element 1.14 * @param idx the index of the element to set 1.15 - * @param data the element data 1.16 + * @param data a pointer to the element data 1.17 * @return zero on success or non-zero on error (errno will be set) 1.18 */ 1.19 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \ 1.20 @@ -90,22 +91,8 @@ 1.21 elmsize, idx, data) 1.22 1.23 /** 1.24 - * Convenience macro for ucx_array_util_set() which automatically computes 1.25 - * <code>sizeof(data)</code>. 1.26 - * 1.27 - * @param array a pointer to location of the array pointer 1.28 - * @param capacity a pointer to the capacity 1.29 - * @param idx the index of the element to set 1.30 - * @param data the element data 1.31 - * @return zero on success or non-zero on error (errno will be set) 1.32 - * @see ucx_array_util_set() 1.33 - */ 1.34 -#define UCX_ARRAY_UTIL_SET(array, capacity, idx, data) \ 1.35 - ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ 1.36 - sizeof(data), idx, data) 1.37 - 1.38 -/** 1.39 * Sets an element in an arbitrary user defined array. 1.40 + * The data is copied from the specified data location. 1.41 * 1.42 * If the capacity is insufficient, the array is automatically reallocated 1.43 * using the specified allocator and the possibly new pointer is stored in 1.44 @@ -119,27 +106,53 @@ 1.45 * @param capacity a pointer to the capacity 1.46 * @param elmsize the size of each element 1.47 * @param idx the index of the element to set 1.48 - * @param ... the element data 1.49 + * @param data a pointer to the element data 1.50 * @return zero on success or non-zero on error (errno will be set) 1.51 */ 1.52 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, 1.53 - size_t elmsize, size_t idx, ...); 1.54 - 1.55 + size_t elmsize, size_t idx, void* data); 1.56 1.57 /** 1.58 - * Convenience macro for ucx_array_util_set_a() which automatically computes 1.59 - * <code>sizeof(data)</code>. 1.60 + * Stores a pointer in an arbitrary user defined array. 1.61 + * The element size of the array must be sizeof(void*). 1.62 + * 1.63 + * If the capacity is insufficient, the array is automatically reallocated and 1.64 + * the possibly new pointer is stored in the <code>array</code> argument. 1.65 + * 1.66 + * On reallocation the capacity of the array is doubled until it is sufficient. 1.67 + * The new capacity is stored back to <code>capacity</code>. 1.68 + * 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 ptr the pointer to store 1.73 + * @return zero on success or non-zero on error (errno will be set) 1.74 + */ 1.75 +#define ucx_array_util_setptr(array, capacity, idx, ptr) \ 1.76 + ucx_array_util_setptr_a(ucx_default_allocator(), (void**)(array), \ 1.77 + capacity, idx, ptr) 1.78 + 1.79 +/** 1.80 + * Stores a pointer in an arbitrary user defined array. 1.81 + * The element size of the array must be sizeof(void*). 1.82 + * 1.83 + * If the capacity is insufficient, the array is automatically reallocated 1.84 + * using the specified allocator and the possibly new pointer is stored in 1.85 + * the <code>array</code> argument. 1.86 + * 1.87 + * On reallocation the capacity of the array is doubled until it is sufficient. 1.88 + * The new capacity is stored back to <code>capacity</code>. 1.89 * 1.90 * @param alloc the allocator that shall be used to reallocate the array 1.91 * @param array a pointer to location of the array pointer 1.92 * @param capacity a pointer to the capacity 1.93 * @param idx the index of the element to set 1.94 - * @param data the element data 1.95 + * @param ptr the pointer to store 1.96 * @return zero on success or non-zero on error (errno will be set) 1.97 - * @see ucx_array_util_set_a() 1.98 */ 1.99 -#define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \ 1.100 - ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data) 1.101 +int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity, 1.102 + size_t idx, void* ptr); 1.103 + 1.104 1.105 /** 1.106 * Creates a new UCX array with the given capacity and element size. 1.107 @@ -291,88 +304,6 @@ 1.108 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count); 1.109 1.110 /** 1.111 - * Inserts an element at the end of the array. 1.112 - * 1.113 - * This is an O(1) operation. 1.114 - * The array will automatically grow, if the capacity is exceeded. 1.115 - * If the type of the argument has a different size than the element size of 1.116 - * this array, the behavior is undefined. 1.117 - * 1.118 - * @param array a pointer the array where to append the data 1.119 - * @param elem the value to insert 1.120 - * @return zero on success, non-zero if a reallocation was necessary but failed 1.121 - * @see ucx_array_append_from() 1.122 - * @see ucx_array_set() 1.123 - */ 1.124 -#define ucx_array_append(array, elem) ucx_array_appendv(array, elem) 1.125 - 1.126 -/** 1.127 - * For internal use. 1.128 - * Use ucx_array_append() 1.129 - * 1.130 - * @param array 1.131 - * @param ... 1.132 - * @return 1.133 - * @see ucx_array_append() 1.134 - */ 1.135 -int ucx_array_appendv(UcxArray *array, ...); 1.136 - 1.137 - 1.138 -/** 1.139 - * Inserts an element at the beginning of the array. 1.140 - * 1.141 - * This is an expensive operation, because the contents must be moved. 1.142 - * If there is no particular reason to prepend data, you should use 1.143 - * ucx_array_append() instead. 1.144 - * 1.145 - * @param array a pointer the array where to prepend the data 1.146 - * @param elem the value to insert 1.147 - * @return zero on success, non-zero if a reallocation was necessary but failed 1.148 - * @see ucx_array_append() 1.149 - * @see ucx_array_set_from() 1.150 - * @see ucx_array_prepend_from() 1.151 - */ 1.152 -#define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem) 1.153 - 1.154 -/** 1.155 - * For internal use. 1.156 - * Use ucx_array_prepend() 1.157 - * 1.158 - * @param array 1.159 - * @param ... 1.160 - * @return 1.161 - * @see ucx_array_prepend() 1.162 - */ 1.163 -int ucx_array_prependv(UcxArray *array, ...); 1.164 - 1.165 - 1.166 -/** 1.167 - * Sets an element at the specified index. 1.168 - * 1.169 - * If the any index is out of bounds, the array automatically grows. 1.170 - * 1.171 - * @param array a pointer the array where to set the data 1.172 - * @param index the index of the element to set 1.173 - * @param elem the value to set 1.174 - * @return zero on success, non-zero if a reallocation was necessary but failed 1.175 - * @see ucx_array_append() 1.176 - * @see ucx_array_set_from() 1.177 - */ 1.178 -#define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem) 1.179 - 1.180 -/** 1.181 - * For internal use. 1.182 - * Use ucx_array_set() 1.183 - * 1.184 - * @param array 1.185 - * @param index 1.186 - * @param ... 1.187 - * @return 1.188 - * @see ucx_array_set() 1.189 - */ 1.190 -int ucx_array_setv(UcxArray *array, size_t index, ...); 1.191 - 1.192 -/** 1.193 * Concatenates two arrays. 1.194 * 1.195 * The contents of the second array are appended to the first array in one 1.196 @@ -507,6 +438,18 @@ 1.197 */ 1.198 int ucx_array_reserve(UcxArray* array, size_t capacity); 1.199 1.200 +/** 1.201 + * Resizes the capacity, if the specified number of elements would not fit. 1.202 + * 1.203 + * A call to ucx_array_grow(array, count) is effectively the same as 1.204 + * ucx_array_reserve(array, array->size+count). 1.205 + * 1.206 + * @param array a pointer to the array 1.207 + * @param count the number of elements that should additionally fit 1.208 + * into the array 1.209 + * @return zero on success, non-zero if reallocation failed 1.210 + */ 1.211 +int ucx_array_grow(UcxArray* array, size_t count); 1.212 1.213 1.214 #ifdef __cplusplus