Tue, 24 Sep 2019 20:16:00 +0200
adds array utility functions for user defined arrays
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2019 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
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.
15 *
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.
27 */
28 /**
29 * Dynamically allocated array implementation.
30 *
31 * @file array.h
32 * @author Mike Becker
33 * @author Olaf Wintermann
34 */
36 #ifndef UCX_ARRAY_H
37 #define UCX_ARRAY_H
39 #include "ucx.h"
40 #include "allocator.h"
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 /**
47 * UCX array type.
48 */
49 typedef struct {
50 /**
51 * The current capacity of the array.
52 */
53 size_t capacity;
54 /**
55 * The actual number of elements in the array.
56 */
57 size_t size;
58 /**
59 * The size of an individual element in bytes.
60 */
61 size_t elemsize;
62 /**
63 * A pointer to the data.
64 */
65 void* data;
66 /**
67 * The allocator used for the data.
68 */
69 UcxAllocator* allocator;
70 } UcxArray;
72 /**
73 * Sets an element in an arbitrary user defined array.
74 *
75 * If the capacity is insufficient, the array is automatically reallocated and
76 * the possibly new pointer is stored in the <code>array</code> argument.
77 *
78 * On reallocation the capacity of the array is doubled until it is sufficient.
79 * The new capacity is stored back to <code>capacity</code>.
80 *
81 * @param array a pointer to location of the array pointer
82 * @param capacity a pointer to the capacity
83 * @param elmsize the size of each element
84 * @param idx the index of the element to set
85 * @param data the element data
86 * @return zero on success or non-zero on error (errno will be set)
87 */
88 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \
89 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
90 elmsize, idx, data)
92 /**
93 * Convenience macro for ucx_array_util_set() which automatically computes
94 * <code>sizeof(data)</code>.
95 *
96 * @param array a pointer to location of the array pointer
97 * @param capacity a pointer to the capacity
98 * @param idx the index of the element to set
99 * @param data the element data
100 * @return zero on success or non-zero on error (errno will be set)
101 * @see ucx_array_util_set()
102 */
103 #define UCX_ARRAY_UTIL_SET(array, capacity, idx, data) \
104 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \
105 sizeof(data), idx, data)
107 /**
108 * Sets an element in an arbitrary user defined array.
109 *
110 * If the capacity is insufficient, the array is automatically reallocated
111 * using the specified allocator and the possibly new pointer is stored in
112 * the <code>array</code> argument.
113 *
114 * On reallocation the capacity of the array is doubled until it is sufficient.
115 * The new capacity is stored back to <code>capacity</code>.
116 *
117 * @param alloc the allocator that shall be used to reallocate the array
118 * @param array a pointer to location of the array pointer
119 * @param capacity a pointer to the capacity
120 * @param elmsize the size of each element
121 * @param idx the index of the element to set
122 * @param ... the element data
123 * @return zero on success or non-zero on error (errno will be set)
124 */
125 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity,
126 size_t elmsize, size_t idx, ...);
129 /**
130 * Convenience macro for ucx_array_util_set_a() which automatically computes
131 * <code>sizeof(data)</code>.
132 *
133 * @param alloc the allocator that shall be used to reallocate the array
134 * @param array a pointer to location of the array pointer
135 * @param capacity a pointer to the capacity
136 * @param idx the index of the element to set
137 * @param data the element data
138 * @return zero on success or non-zero on error (errno will be set)
139 * @see ucx_array_util_set_a()
140 */
141 #define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \
142 ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data)
144 /**
145 * Creates a new UCX array with the given capacity and element size.
146 * @param capacity the initial capacity
147 * @param elemsize the element size
148 * @return a new UCX array structure
149 */
150 UcxArray ucx_array_new(size_t capacity, size_t elemsize);
152 /**
153 * Creates a new UCX array using the specified allocator.
154 *
155 * @param capacity the initial capacity
156 * @param elemsize the element size
157 * @param allocator the allocator to use
158 * @return a new UCX array structure
159 */
160 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
161 UcxAllocator* allocator);
163 /**
164 * Creates an shallow copy of an array.
165 *
166 * This function clones the specified array by using memcpy().
167 *
168 * @param array the array to copy
169 * @return the copy (may be an empty array on allocation errors)
170 */
171 UcxArray ucx_array_clone(UcxArray array);
174 /**
175 * Compares two UCX arrays element-wise by using a compare function.
176 *
177 * Elements of the two specified arrays are compared by using the specified
178 * compare function and the additional data. The type and content of this
179 * additional data depends on the cmp_func() used.
180 *
181 * This function always returns zero, if the element sizes of the arrays do
182 * not match and performs no comparisons in this case.
183 *
184 * @param array1 the first array
185 * @param array2 the second array
186 * @param cmpfnc the compare function
187 * @param data additional data for the compare function
188 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
189 */
190 int ucx_array_equals(UcxArray array1, UcxArray array2,
191 cmp_func cmpfnc, void* data);
193 /**
194 * Destroys the array.
195 *
196 * The data is freed and both capacity and count are reset to zero.
197 * If the array structure itself has been dynamically allocated, it has to be
198 * freed separately.
199 *
200 * @param array the array to destroy
201 */
202 void ucx_array_destroy(UcxArray *array);
204 /**
205 * Inserts elements at the end of the array.
206 *
207 * This is an O(1) operation.
208 * The array will automatically grow, if the capacity is exceeded.
209 * If a pointer to data is provided, the data is copied into the array with
210 * memcpy(). Otherwise the new elements are completely zeroed.
211 *
212 * @param array a pointer the array where to append the data
213 * @param data a pointer to the data to insert (may be <code>NULL</code>)
214 * @param count number of elements to copy from data (if data is
215 * <code>NULL</code>, zeroed elements are appended)
216 * @return zero on success, non-zero if a reallocation was necessary but failed
217 * @see ucx_array_set_from()
218 * @see ucx_array_append()
219 */
220 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
223 /**
224 * Inserts elements at the beginning of the array.
225 *
226 * This is an expensive operation, because the contents must be moved.
227 * If there is no particular reason to prepend data, you should use
228 * ucx_array_append_from() instead.
229 *
230 * @param array a pointer the array where to prepend the data
231 * @param data a pointer to the data to insert (may be <code>NULL</code>)
232 * @param count number of elements to copy from data (if data is
233 * <code>NULL</code>, zeroed elements are inserted)
234 * @return zero on success, non-zero if a reallocation was necessary but failed
235 * @see ucx_array_append_from()
236 * @see ucx_array_set_from()
237 * @see ucx_array_prepend()
238 */
239 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
242 /**
243 * Sets elements starting at the specified index.
244 *
245 * If the any index is out of bounds, the array automatically grows.
246 * The pointer to the data may be NULL, in which case the elements are zeroed.
247 *
248 * @param array a pointer the array where to set the data
249 * @param index the index of the element to set
250 * @param data a pointer to the data to insert (may be <code>NULL</code>)
251 * @param count number of elements to copy from data (if data is
252 * <code>NULL</code>, the memory in the array is zeroed)
253 * @return zero on success, non-zero if a reallocation was necessary but failed
254 * @see ucx_array_append_from()
255 * @see ucx_array_set()
256 */
257 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
259 /**
260 * Inserts an element at the end of the array.
261 *
262 * This is an O(1) operation.
263 * The array will automatically grow, if the capacity is exceeded.
264 * If the type of the argument has a different size than the element size of
265 * this array, the behavior is undefined.
266 *
267 * @param array a pointer the array where to append the data
268 * @param elem the value to insert
269 * @return zero on success, non-zero if a reallocation was necessary but failed
270 * @see ucx_array_append_from()
271 * @see ucx_array_set()
272 */
273 #define ucx_array_append(array, elem) ucx_array_appendv(array, elem)
275 /**
276 * For internal use.
277 * Use ucx_array_append()
278 *
279 * @param array
280 * @param ...
281 * @return
282 * @see ucx_array_append()
283 */
284 int ucx_array_appendv(UcxArray *array, ...);
287 /**
288 * Inserts an element at the beginning of the array.
289 *
290 * This is an expensive operation, because the contents must be moved.
291 * If there is no particular reason to prepend data, you should use
292 * ucx_array_append() instead.
293 *
294 * @param array a pointer the array where to prepend the data
295 * @param elem the value to insert
296 * @return zero on success, non-zero if a reallocation was necessary but failed
297 * @see ucx_array_append()
298 * @see ucx_array_set_from()
299 * @see ucx_array_prepend_from()
300 */
301 #define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem)
303 /**
304 * For internal use.
305 * Use ucx_array_prepend()
306 *
307 * @param array
308 * @param ...
309 * @return
310 * @see ucx_array_prepend()
311 */
312 int ucx_array_prependv(UcxArray *array, ...);
315 /**
316 * Sets an element at the specified index.
317 *
318 * If the any index is out of bounds, the array automatically grows.
319 *
320 * @param array a pointer the array where to set the data
321 * @param index the index of the element to set
322 * @param elem the value to set
323 * @return zero on success, non-zero if a reallocation was necessary but failed
324 * @see ucx_array_append()
325 * @see ucx_array_set_from()
326 */
327 #define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem)
329 /**
330 * For internal use.
331 * Use ucx_array_set()
332 *
333 * @param array
334 * @param index
335 * @param ...
336 * @return
337 * @see ucx_array_set()
338 */
339 int ucx_array_setv(UcxArray *array, size_t index, ...);
341 /**
342 * Concatenates two arrays.
343 *
344 * The contents of the second array are appended to the first array in one
345 * single operation. The second array is otherwise left untouched.
346 *
347 * The first array may grow automatically. If this fails, both arrays remain
348 * unmodified.
349 *
350 * @param array1 first array
351 * @param array2 second array
352 * @return zero on success, non-zero if reallocation was necessary but failed
353 * or the element size does not match
354 */
355 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
357 /**
358 * Returns a pointer to the array element at the specified index.
359 *
360 * @param array the array to retrieve the element from
361 * @param index index of the element to return
362 * @return a pointer to the element at the specified index or <code>NULL</code>,
363 * if the index is greater than the array size
364 */
365 void *ucx_array_at(UcxArray array, size_t index);
367 /**
368 * Returns the index of an element containing the specified data.
369 *
370 * This function uses a cmp_func() to compare the data of each list element
371 * with the specified data. If no cmp_func is provided, memcmp() is used.
372 *
373 * If the array contains the data more than once, the index of the first
374 * occurrence is returned.
375 * If the array does not contain the data, the size of array is returned.
376 *
377 * @param array the array where to search for the data
378 * @param elem the element data
379 * @param cmpfnc the compare function
380 * @param data additional data for the compare function
381 * @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
383 */
384 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
386 /**
387 * Checks, if an array contains a specific element.
388 *
389 * An element is found, if ucx_array_find() returns a value less than the size.
390 *
391 * @param array the array where to search for the data
392 * @param elem the element data
393 * @param cmpfnc the compare function
394 * @param data additional data for the compare function
395 * @return 1, if and only if the array contains the specified element data
396 * @see ucx_array_find()
397 */
398 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
400 /**
401 * Sorts a UcxArray with the best available sort algorithm.
402 *
403 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
404 * The order of arguments is automatically adjusted for the FreeBSD and MacOS
405 * version of qsort_r().
406 *
407 * If qsort_r() is not available, a merge sort algorithm is used, which is
408 * guaranteed to use no more additional memory than for exactly one element.
409 *
410 * @param array the array to sort
411 * @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>
413 */
414 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
416 /**
417 * Removes an element from the array.
418 *
419 * This is in general an expensive operation, because several elements may
420 * be moved. If the order of the elements is not relevant, use
421 * ucx_array_remove_fast() instead.
422 *
423 * @param array pointer to the array from which the element shall be removed
424 * @param index the index of the element to remove
425 */
426 void ucx_array_remove(UcxArray *array, size_t index);
428 /**
429 * Removes an element from the array.
430 *
431 * This is an O(1) operation, but does not maintain the order of the elements.
432 * The last element in the array is moved to the location of the removed
433 * element.
434 *
435 * @param array pointer to the array from which the element shall be removed
436 * @param index the index of the element to remove
437 */
438 void ucx_array_remove_fast(UcxArray *array, size_t index);
440 /**
441 * Shrinks the memory to exactly fit the contents.
442 *
443 * After this operation, the capacity equals the size.
444 *
445 * @param array a pointer to the array
446 * @return zero on success, non-zero if reallocation failed
447 */
448 int ucx_array_shrink(UcxArray* array);
450 /**
451 * Sets the capacity of the array.
452 *
453 * If the new capacity is smaller than the size of the array, the elements
454 * are removed and the size is adjusted accordingly.
455 *
456 * @param array a pointer to the array
457 * @param capacity the new capacity
458 * @return zero on success, non-zero if reallocation failed
459 */
460 int ucx_array_resize(UcxArray* array, size_t capacity);
462 /**
463 * Resizes the array only, if the capacity is insufficient.
464 *
465 * If the requested capacity is smaller than the current capacity, this
466 * function does nothing.
467 *
468 * @param array a pointer to the array
469 * @param capacity the guaranteed capacity
470 * @return zero on success, non-zero if reallocation failed
471 */
472 int ucx_array_reserve(UcxArray* array, size_t capacity);
476 #ifdef __cplusplus
477 }
478 #endif
480 #endif /* UCX_ARRAY_H */