2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 * Dynamically allocated array implementation.
33 * @author Olaf Wintermann
40 #include "allocator.h"
51 * The current capacity of the array.
55 * The actual number of elements in the array.
59 * The size of an individual element in bytes.
63 * A pointer to the data.
67 * The allocator used for the data.
69 UcxAllocator* allocator;
73 * Sets an element in an arbitrary user defined array.
74 * The data is copied from the specified data location.
76 * If the capacity is insufficient, the array is automatically reallocated and
77 * the possibly new pointer is stored in the <code>array</code> argument.
79 * On reallocation the capacity of the array is doubled until it is sufficient.
80 * The new capacity is stored back to <code>capacity</code>.
82 * @param array a pointer to location of the array pointer
83 * @param capacity a pointer to the capacity
84 * @param elmsize the size of each element
85 * @param idx the index of the element to set
86 * @param data a pointer to the element data
87 * @return zero on success or non-zero on error (errno will be set)
89 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \
90 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
94 * Sets an element in an arbitrary user defined array.
95 * The data is copied from the specified data location.
97 * If the capacity is insufficient, the array is automatically reallocated
98 * using the specified allocator and the possibly new pointer is stored in
99 * the <code>array</code> argument.
101 * On reallocation the capacity of the array is doubled until it is sufficient.
102 * The new capacity is stored back to <code>capacity</code>.
104 * @param alloc the allocator that shall be used to reallocate the array
105 * @param array a pointer to location of the array pointer
106 * @param capacity a pointer to the capacity
107 * @param elmsize the size of each element
108 * @param idx the index of the element to set
109 * @param data a pointer to the element data
110 * @return zero on success or non-zero on error (errno will be set)
112 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
113 size_t elmsize, size_t idx, void* data);
116 * Stores a pointer in an arbitrary user defined array.
117 * The element size of the array must be sizeof(void*).
119 * If the capacity is insufficient, the array is automatically reallocated and
120 * the possibly new pointer is stored in the <code>array</code> argument.
122 * On reallocation the capacity of the array is doubled until it is sufficient.
123 * The new capacity is stored back to <code>capacity</code>.
125 * @param array a pointer to location of the array pointer
126 * @param capacity a pointer to the capacity
127 * @param idx the index of the element to set
128 * @param ptr the pointer to store
129 * @return zero on success or non-zero on error (errno will be set)
131 #define ucx_array_util_setptr(array, capacity, idx, ptr) \
132 ucx_array_util_setptr_a(ucx_default_allocator(), (void**)(array), \
136 * Stores a pointer in an arbitrary user defined array.
137 * The element size of the array must be sizeof(void*).
139 * If the capacity is insufficient, the array is automatically reallocated
140 * using the specified allocator and the possibly new pointer is stored in
141 * the <code>array</code> argument.
143 * On reallocation the capacity of the array is doubled until it is sufficient.
144 * The new capacity is stored back to <code>capacity</code>.
146 * @param alloc the allocator that shall be used to reallocate the array
147 * @param array a pointer to location of the array pointer
148 * @param capacity a pointer to the capacity
149 * @param idx the index of the element to set
150 * @param ptr the pointer to store
151 * @return zero on success or non-zero on error (errno will be set)
153 int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity,
154 size_t idx, void* ptr);
158 * Creates a new UCX array with the given capacity and element size.
159 * @param capacity the initial capacity
160 * @param elemsize the element size
161 * @return a pointer to a new UCX array structure
163 UcxArray* ucx_array_new(size_t capacity, size_t elemsize);
166 * Creates a new UCX array using the specified allocator.
168 * @param capacity the initial capacity
169 * @param elemsize the element size
170 * @param allocator the allocator to use
171 * @return a pointer to new UCX array structure
173 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
174 UcxAllocator* allocator);
177 * Initializes a UCX array structure with the given capacity and element size.
178 * The structure must be uninitialized as the data pointer will be overwritten.
180 * @param array the structure to initialize
181 * @param capacity the initial capacity
182 * @param elemsize the element size
184 void ucx_array_init(UcxArray* array, size_t capacity, size_t elemsize);
187 * Initializes a UCX array structure using the specified allocator.
188 * The structure must be uninitialized as the data pointer will be overwritten.
190 * @param array the structure to initialize
191 * @param capacity the initial capacity
192 * @param elemsize the element size
193 * @param allocator the allocator to use
195 void ucx_array_init_a(UcxArray* array, size_t capacity, size_t elemsize,
196 UcxAllocator* allocator);
199 * Creates an shallow copy of an array.
201 * This function clones the specified array by using memcpy().
202 * If the destination capacity is insufficient, an automatic reallocation is
205 * Note: if the destination array is uninitialized, the behavior is undefined.
207 * @param dest the array to copy to
208 * @param src the array to copy from
209 * @return zero on success, non-zero on reallocation failure.
211 int ucx_array_clone(UcxArray* dest, UcxArray const* src);
215 * Compares two UCX arrays element-wise by using a compare function.
217 * Elements of the two specified arrays are compared by using the specified
218 * compare function and the additional data. The type and content of this
219 * additional data depends on the cmp_func() used.
221 * This function always returns zero, if the element sizes of the arrays do
222 * not match and performs no comparisons in this case.
224 * @param array1 the first array
225 * @param array2 the second array
226 * @param cmpfnc the compare function
227 * @param data additional data for the compare function
228 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
230 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
231 cmp_func cmpfnc, void* data);
234 * Destroys the array.
236 * The data is freed and both capacity and count are reset to zero.
237 * If the array structure itself has been dynamically allocated, it has to be
240 * @param array the array to destroy
242 void ucx_array_destroy(UcxArray *array);
245 * Destroys and frees the array.
247 * @param array the array to free
249 void ucx_array_free(UcxArray *array);
252 * Inserts elements at the end of the array.
254 * This is an O(1) operation.
255 * The array will automatically grow, if the capacity is exceeded.
256 * If a pointer to data is provided, the data is copied into the array with
257 * memcpy(). Otherwise the new elements are completely zeroed.
259 * @param array a pointer the array where to append the data
260 * @param data a pointer to the data to insert (may be <code>NULL</code>)
261 * @param count number of elements to copy from data (if data is
262 * <code>NULL</code>, zeroed elements are appended)
263 * @return zero on success, non-zero if a reallocation was necessary but failed
264 * @see ucx_array_set_from()
265 * @see ucx_array_append()
267 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
271 * Inserts elements at the beginning of the array.
273 * This is an expensive operation, because the contents must be moved.
274 * If there is no particular reason to prepend data, you should use
275 * ucx_array_append_from() instead.
277 * @param array a pointer the array where to prepend the data
278 * @param data a pointer to the data to insert (may be <code>NULL</code>)
279 * @param count number of elements to copy from data (if data is
280 * <code>NULL</code>, zeroed elements are inserted)
281 * @return zero on success, non-zero if a reallocation was necessary but failed
282 * @see ucx_array_append_from()
283 * @see ucx_array_set_from()
284 * @see ucx_array_prepend()
286 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
290 * Sets elements starting at the specified index.
292 * If the any index is out of bounds, the array automatically grows.
293 * The pointer to the data may be NULL, in which case the elements are zeroed.
295 * @param array a pointer the array where to set the data
296 * @param index the index of the element to set
297 * @param data a pointer to the data to insert (may be <code>NULL</code>)
298 * @param count number of elements to copy from data (if data is
299 * <code>NULL</code>, the memory in the array is zeroed)
300 * @return zero on success, non-zero if a reallocation was necessary but failed
301 * @see ucx_array_append_from()
302 * @see ucx_array_set()
304 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
307 * Concatenates two arrays.
309 * The contents of the second array are appended to the first array in one
310 * single operation. The second array is otherwise left untouched.
312 * The first array may grow automatically. If this fails, both arrays remain
315 * @param array1 first array
316 * @param array2 second array
317 * @return zero on success, non-zero if reallocation was necessary but failed
318 * or the element size does not match
320 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
323 * Returns a pointer to the array element at the specified index.
325 * @param array the array to retrieve the element from
326 * @param index index of the element to return
327 * @return a pointer to the element at the specified index or <code>NULL</code>,
328 * if the index is greater than the array size
330 void *ucx_array_at(UcxArray const* array, size_t index);
333 * Returns the index of an element containing the specified data.
335 * This function uses a cmp_func() to compare the data of each list element
336 * with the specified data. If no cmp_func is provided, memcmp() is used.
338 * If the array contains the data more than once, the index of the first
339 * occurrence is returned.
340 * If the array does not contain the data, the size of array is returned.
342 * @param array the array where to search for the data
343 * @param elem the element data
344 * @param cmpfnc the compare function
345 * @param data additional data for the compare function
346 * @return the index of the element containing the specified data or the size of
347 * the array, if the data is not found in this array
349 size_t ucx_array_find(UcxArray const *array, void *elem,
350 cmp_func cmpfnc, void *data);
353 * Checks, if an array contains a specific element.
355 * An element is found, if ucx_array_find() returns a value less than the size.
357 * @param array the array where to search for the data
358 * @param elem the element data
359 * @param cmpfnc the compare function
360 * @param data additional data for the compare function
361 * @return 1, if and only if the array contains the specified element data
362 * @see ucx_array_find()
364 int ucx_array_contains(UcxArray const *array, void *elem,
365 cmp_func cmpfnc, void *data);
368 * Sorts a UcxArray with the best available sort algorithm.
370 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
371 * The order of arguments is automatically adjusted for the FreeBSD and MacOS
372 * version of qsort_r().
374 * If qsort_r() is not available, a merge sort algorithm is used, which is
375 * guaranteed to use no more additional memory than for exactly one element.
377 * @param array the array to sort
378 * @param cmpfnc the function that shall be used to compare the element data
379 * @param data additional data for the cmp_func() or <code>NULL</code>
381 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data);
384 * Removes an element from the array.
386 * This is in general an expensive operation, because several elements may
387 * be moved. If the order of the elements is not relevant, use
388 * ucx_array_remove_fast() instead.
390 * @param array pointer to the array from which the element shall be removed
391 * @param index the index of the element to remove
393 void ucx_array_remove(UcxArray *array, size_t index);
396 * Removes an element from the array.
398 * This is an O(1) operation, but does not maintain the order of the elements.
399 * The last element in the array is moved to the location of the removed
402 * @param array pointer to the array from which the element shall be removed
403 * @param index the index of the element to remove
405 void ucx_array_remove_fast(UcxArray *array, size_t index);
408 * Shrinks the memory to exactly fit the contents.
410 * After this operation, the capacity equals the size.
412 * @param array a pointer to the array
413 * @return zero on success, non-zero if reallocation failed
415 int ucx_array_shrink(UcxArray* array);
418 * Sets the capacity of the array.
420 * If the new capacity is smaller than the size of the array, the elements
421 * are removed and the size is adjusted accordingly.
423 * @param array a pointer to the array
424 * @param capacity the new capacity
425 * @return zero on success, non-zero if reallocation failed
427 int ucx_array_resize(UcxArray* array, size_t capacity);
430 * Resizes the array only, if the capacity is insufficient.
432 * If the requested capacity is smaller than the current capacity, this
433 * function does nothing.
435 * @param array a pointer to the array
436 * @param capacity the guaranteed capacity
437 * @return zero on success, non-zero if reallocation failed
439 int ucx_array_reserve(UcxArray* array, size_t capacity);
442 * Resizes the capacity, if the specified number of elements would not fit.
444 * A call to ucx_array_grow(array, count) is effectively the same as
445 * ucx_array_reserve(array, array->size+count).
447 * @param array a pointer to the array
448 * @param count the number of elements that should additionally fit
450 * @return zero on success, non-zero if reallocation failed
452 int ucx_array_grow(UcxArray* array, size_t count);
459 #endif /* UCX_ARRAY_H */