src/ucx/array.h

branch
feature/array
changeset 356
77efe51c6c9a
parent 355
d315a068235a
child 357
0f5732f0dc00
equal deleted inserted replaced
355:d315a068235a 356:77efe51c6c9a
143 143
144 /** 144 /**
145 * Creates a new UCX array with the given capacity and element size. 145 * Creates a new UCX array with the given capacity and element size.
146 * @param capacity the initial capacity 146 * @param capacity the initial capacity
147 * @param elemsize the element size 147 * @param elemsize the element size
148 * @return a new UCX array structure 148 * @return a pointer to a new UCX array structure
149 */ 149 */
150 UcxArray ucx_array_new(size_t capacity, size_t elemsize); 150 UcxArray* ucx_array_new(size_t capacity, size_t elemsize);
151 151
152 /** 152 /**
153 * Creates a new UCX array using the specified allocator. 153 * Creates a new UCX array using the specified allocator.
154 * 154 *
155 * @param capacity the initial capacity 155 * @param capacity the initial capacity
156 * @param elemsize the element size 156 * @param elemsize the element size
157 * @param allocator the allocator to use 157 * @param allocator the allocator to use
158 * @return a new UCX array structure 158 * @return a pointer to new UCX array structure
159 */ 159 */
160 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize, 160 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
161 UcxAllocator* allocator); 161 UcxAllocator* allocator);
162 162
163 /** 163 /**
164 * Initializes a UCX array structure with the given capacity and element size.
165 * The structure must be uninitialized as the data pointer will be overwritten.
166 *
167 * @param array the structure to initialize
168 * @param capacity the initial capacity
169 * @param elemsize the element size
170 */
171 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize);
172
173 /**
174 * Initializes a UCX array structure using the specified allocator.
175 * The structure must be uninitialized as the data pointer will be overwritten.
176 *
177 * @param capacity the initial capacity
178 * @param elemsize the element size
179 * @param allocator the allocator to use
180 */
181 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
182 UcxAllocator* allocator);
183
184 /**
164 * Creates an shallow copy of an array. 185 * Creates an shallow copy of an array.
165 * 186 *
166 * This function clones the specified array by using memcpy(). 187 * This function clones the specified array by using memcpy().
167 * 188 * If the destination capacity is insufficient, an automatic reallocation is
168 * @param array the array to copy 189 * attempted.
169 * @return the copy (may be an empty array on allocation errors) 190 *
170 */ 191 * @param dest the array to copy to
171 UcxArray ucx_array_clone(UcxArray array); 192 * @param src the array to copy from
193 * @return zero on success, non-zero on reallocation failure.
194 */
195 int ucx_array_clone(UcxArray* dest, UcxArray const* src);
172 196
173 197
174 /** 198 /**
175 * Compares two UCX arrays element-wise by using a compare function. 199 * Compares two UCX arrays element-wise by using a compare function.
176 * 200 *
185 * @param array2 the second array 209 * @param array2 the second array
186 * @param cmpfnc the compare function 210 * @param cmpfnc the compare function
187 * @param data additional data for the compare function 211 * @param data additional data for the compare function
188 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise 212 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
189 */ 213 */
190 int ucx_array_equals(UcxArray array1, UcxArray array2, 214 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
191 cmp_func cmpfnc, void* data); 215 cmp_func cmpfnc, void* data);
192 216
193 /** 217 /**
194 * Destroys the array. 218 * Destroys the array.
195 * 219 *
198 * freed separately. 222 * freed separately.
199 * 223 *
200 * @param array the array to destroy 224 * @param array the array to destroy
201 */ 225 */
202 void ucx_array_destroy(UcxArray *array); 226 void ucx_array_destroy(UcxArray *array);
227
228 /**
229 * Destroys and frees the array.
230 *
231 * @param array the array to free
232 */
233 void ucx_array_free(UcxArray *array);
203 234
204 /** 235 /**
205 * Inserts elements at the end of the array. 236 * Inserts elements at the end of the array.
206 * 237 *
207 * This is an O(1) operation. 238 * This is an O(1) operation.
360 * @param array the array to retrieve the element from 391 * @param array the array to retrieve the element from
361 * @param index index of the element to return 392 * @param index index of the element to return
362 * @return a pointer to the element at the specified index or <code>NULL</code>, 393 * @return a pointer to the element at the specified index or <code>NULL</code>,
363 * if the index is greater than the array size 394 * if the index is greater than the array size
364 */ 395 */
365 void *ucx_array_at(UcxArray array, size_t index); 396 void *ucx_array_at(UcxArray const* array, size_t index);
366 397
367 /** 398 /**
368 * Returns the index of an element containing the specified data. 399 * Returns the index of an element containing the specified data.
369 * 400 *
370 * This function uses a cmp_func() to compare the data of each list element 401 * This function uses a cmp_func() to compare the data of each list element
379 * @param cmpfnc the compare function 410 * @param cmpfnc the compare function
380 * @param data additional data for the compare function 411 * @param data additional data for the compare function
381 * @return the index of the element containing the specified data or the size of 412 * @return the index of the element containing the specified data or the size of
382 * the array, if the data is not found in this array 413 * the array, if the data is not found in this array
383 */ 414 */
384 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data); 415 size_t ucx_array_find(UcxArray const *array, void *elem,
416 cmp_func cmpfnc, void *data);
385 417
386 /** 418 /**
387 * Checks, if an array contains a specific element. 419 * Checks, if an array contains a specific element.
388 * 420 *
389 * An element is found, if ucx_array_find() returns a value less than the size. 421 * An element is found, if ucx_array_find() returns a value less than the size.
393 * @param cmpfnc the compare function 425 * @param cmpfnc the compare function
394 * @param data additional data for the compare function 426 * @param data additional data for the compare function
395 * @return 1, if and only if the array contains the specified element data 427 * @return 1, if and only if the array contains the specified element data
396 * @see ucx_array_find() 428 * @see ucx_array_find()
397 */ 429 */
398 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data); 430 int ucx_array_contains(UcxArray const *array, void *elem,
431 cmp_func cmpfnc, void *data);
399 432
400 /** 433 /**
401 * Sorts a UcxArray with the best available sort algorithm. 434 * Sorts a UcxArray with the best available sort algorithm.
402 * 435 *
403 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS). 436 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
409 * 442 *
410 * @param array the array to sort 443 * @param array the array to sort
411 * @param cmpfnc the function that shall be used to compare the element data 444 * @param cmpfnc the function that shall be used to compare the element data
412 * @param data additional data for the cmp_func() or <code>NULL</code> 445 * @param data additional data for the cmp_func() or <code>NULL</code>
413 */ 446 */
414 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data); 447 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data);
415 448
416 /** 449 /**
417 * Removes an element from the array. 450 * Removes an element from the array.
418 * 451 *
419 * This is in general an expensive operation, because several elements may 452 * This is in general an expensive operation, because several elements may

mercurial