Sat, 05 Oct 2019 17:07:16 +0200
adds missing include for strncasecmp() to avoid an implicit declaration
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 pointer to 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 pointer to new UCX array structure
159 */
160 UcxArray* ucx_array_new_a(size_t capacity, size_t elemsize,
161 UcxAllocator* allocator);
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);
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);
184 /**
185 * Creates an shallow copy of an array.
186 *
187 * This function clones the specified array by using memcpy().
188 * If the destination capacity is insufficient, an automatic reallocation is
189 * attempted.
190 *
191 * Note: if the destination array is uninitialized, the behavior is undefined.
192 *
193 * @param dest the array to copy to
194 * @param src the array to copy from
195 * @return zero on success, non-zero on reallocation failure.
196 */
197 int ucx_array_clone(UcxArray* dest, UcxArray const* src);
200 /**
201 * Compares two UCX arrays element-wise by using a compare function.
202 *
203 * Elements of the two specified arrays are compared by using the specified
204 * compare function and the additional data. The type and content of this
205 * additional data depends on the cmp_func() used.
206 *
207 * This function always returns zero, if the element sizes of the arrays do
208 * not match and performs no comparisons in this case.
209 *
210 * @param array1 the first array
211 * @param array2 the second array
212 * @param cmpfnc the compare function
213 * @param data additional data for the compare function
214 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
215 */
216 int ucx_array_equals(UcxArray const *array1, UcxArray const *array2,
217 cmp_func cmpfnc, void* data);
219 /**
220 * Destroys the array.
221 *
222 * The data is freed and both capacity and count are reset to zero.
223 * If the array structure itself has been dynamically allocated, it has to be
224 * freed separately.
225 *
226 * @param array the array to destroy
227 */
228 void ucx_array_destroy(UcxArray *array);
230 /**
231 * Destroys and frees the array.
232 *
233 * @param array the array to free
234 */
235 void ucx_array_free(UcxArray *array);
237 /**
238 * Inserts elements at the end of the array.
239 *
240 * This is an O(1) operation.
241 * The array will automatically grow, if the capacity is exceeded.
242 * If a pointer to data is provided, the data is copied into the array with
243 * memcpy(). Otherwise the new elements are completely zeroed.
244 *
245 * @param array a pointer the array where to append the data
246 * @param data a pointer to the data to insert (may be <code>NULL</code>)
247 * @param count number of elements to copy from data (if data is
248 * <code>NULL</code>, zeroed elements are appended)
249 * @return zero on success, non-zero if a reallocation was necessary but failed
250 * @see ucx_array_set_from()
251 * @see ucx_array_append()
252 */
253 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
256 /**
257 * Inserts elements at the beginning of the array.
258 *
259 * This is an expensive operation, because the contents must be moved.
260 * If there is no particular reason to prepend data, you should use
261 * ucx_array_append_from() instead.
262 *
263 * @param array a pointer the array where to prepend the data
264 * @param data a pointer to the data to insert (may be <code>NULL</code>)
265 * @param count number of elements to copy from data (if data is
266 * <code>NULL</code>, zeroed elements are inserted)
267 * @return zero on success, non-zero if a reallocation was necessary but failed
268 * @see ucx_array_append_from()
269 * @see ucx_array_set_from()
270 * @see ucx_array_prepend()
271 */
272 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
275 /**
276 * Sets elements starting at the specified index.
277 *
278 * If the any index is out of bounds, the array automatically grows.
279 * The pointer to the data may be NULL, in which case the elements are zeroed.
280 *
281 * @param array a pointer the array where to set the data
282 * @param index the index of the element to set
283 * @param data a pointer to the data to insert (may be <code>NULL</code>)
284 * @param count number of elements to copy from data (if data is
285 * <code>NULL</code>, the memory in the array is zeroed)
286 * @return zero on success, non-zero if a reallocation was necessary but failed
287 * @see ucx_array_append_from()
288 * @see ucx_array_set()
289 */
290 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
292 /**
293 * Inserts an element at the end of the array.
294 *
295 * This is an O(1) operation.
296 * The array will automatically grow, if the capacity is exceeded.
297 * If the type of the argument has a different size than the element size of
298 * this array, the behavior is undefined.
299 *
300 * @param array a pointer the array where to append the data
301 * @param elem the value to insert
302 * @return zero on success, non-zero if a reallocation was necessary but failed
303 * @see ucx_array_append_from()
304 * @see ucx_array_set()
305 */
306 #define ucx_array_append(array, elem) ucx_array_appendv(array, elem)
308 /**
309 * For internal use.
310 * Use ucx_array_append()
311 *
312 * @param array
313 * @param ...
314 * @return
315 * @see ucx_array_append()
316 */
317 int ucx_array_appendv(UcxArray *array, ...);
320 /**
321 * Inserts an element at the beginning of the array.
322 *
323 * This is an expensive operation, because the contents must be moved.
324 * If there is no particular reason to prepend data, you should use
325 * ucx_array_append() instead.
326 *
327 * @param array a pointer the array where to prepend the data
328 * @param elem the value to insert
329 * @return zero on success, non-zero if a reallocation was necessary but failed
330 * @see ucx_array_append()
331 * @see ucx_array_set_from()
332 * @see ucx_array_prepend_from()
333 */
334 #define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem)
336 /**
337 * For internal use.
338 * Use ucx_array_prepend()
339 *
340 * @param array
341 * @param ...
342 * @return
343 * @see ucx_array_prepend()
344 */
345 int ucx_array_prependv(UcxArray *array, ...);
348 /**
349 * Sets an element at the specified index.
350 *
351 * If the any index is out of bounds, the array automatically grows.
352 *
353 * @param array a pointer the array where to set the data
354 * @param index the index of the element to set
355 * @param elem the value to set
356 * @return zero on success, non-zero if a reallocation was necessary but failed
357 * @see ucx_array_append()
358 * @see ucx_array_set_from()
359 */
360 #define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem)
362 /**
363 * For internal use.
364 * Use ucx_array_set()
365 *
366 * @param array
367 * @param index
368 * @param ...
369 * @return
370 * @see ucx_array_set()
371 */
372 int ucx_array_setv(UcxArray *array, size_t index, ...);
374 /**
375 * Concatenates two arrays.
376 *
377 * The contents of the second array are appended to the first array in one
378 * single operation. The second array is otherwise left untouched.
379 *
380 * The first array may grow automatically. If this fails, both arrays remain
381 * unmodified.
382 *
383 * @param array1 first array
384 * @param array2 second array
385 * @return zero on success, non-zero if reallocation was necessary but failed
386 * or the element size does not match
387 */
388 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
390 /**
391 * Returns a pointer to the array element at the specified index.
392 *
393 * @param array the array to retrieve the element from
394 * @param index index of the element to return
395 * @return a pointer to the element at the specified index or <code>NULL</code>,
396 * if the index is greater than the array size
397 */
398 void *ucx_array_at(UcxArray const* array, size_t index);
400 /**
401 * Returns the index of an element containing the specified data.
402 *
403 * This function uses a cmp_func() to compare the data of each list element
404 * with the specified data. If no cmp_func is provided, memcmp() is used.
405 *
406 * If the array contains the data more than once, the index of the first
407 * occurrence is returned.
408 * If the array does not contain the data, the size of array is returned.
409 *
410 * @param array the array where to search for the data
411 * @param elem the element data
412 * @param cmpfnc the compare function
413 * @param data additional data for the compare function
414 * @return the index of the element containing the specified data or the size of
415 * the array, if the data is not found in this array
416 */
417 size_t ucx_array_find(UcxArray const *array, void *elem,
418 cmp_func cmpfnc, void *data);
420 /**
421 * Checks, if an array contains a specific element.
422 *
423 * An element is found, if ucx_array_find() returns a value less than the size.
424 *
425 * @param array the array where to search for the data
426 * @param elem the element data
427 * @param cmpfnc the compare function
428 * @param data additional data for the compare function
429 * @return 1, if and only if the array contains the specified element data
430 * @see ucx_array_find()
431 */
432 int ucx_array_contains(UcxArray const *array, void *elem,
433 cmp_func cmpfnc, void *data);
435 /**
436 * Sorts a UcxArray with the best available sort algorithm.
437 *
438 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
439 * The order of arguments is automatically adjusted for the FreeBSD and MacOS
440 * version of qsort_r().
441 *
442 * If qsort_r() is not available, a merge sort algorithm is used, which is
443 * guaranteed to use no more additional memory than for exactly one element.
444 *
445 * @param array the array to sort
446 * @param cmpfnc the function that shall be used to compare the element data
447 * @param data additional data for the cmp_func() or <code>NULL</code>
448 */
449 void ucx_array_sort(UcxArray* array, cmp_func cmpfnc, void *data);
451 /**
452 * Removes an element from the array.
453 *
454 * This is in general an expensive operation, because several elements may
455 * be moved. If the order of the elements is not relevant, use
456 * ucx_array_remove_fast() instead.
457 *
458 * @param array pointer to the array from which the element shall be removed
459 * @param index the index of the element to remove
460 */
461 void ucx_array_remove(UcxArray *array, size_t index);
463 /**
464 * Removes an element from the array.
465 *
466 * This is an O(1) operation, but does not maintain the order of the elements.
467 * The last element in the array is moved to the location of the removed
468 * element.
469 *
470 * @param array pointer to the array from which the element shall be removed
471 * @param index the index of the element to remove
472 */
473 void ucx_array_remove_fast(UcxArray *array, size_t index);
475 /**
476 * Shrinks the memory to exactly fit the contents.
477 *
478 * After this operation, the capacity equals the size.
479 *
480 * @param array a pointer to the array
481 * @return zero on success, non-zero if reallocation failed
482 */
483 int ucx_array_shrink(UcxArray* array);
485 /**
486 * Sets the capacity of the array.
487 *
488 * If the new capacity is smaller than the size of the array, the elements
489 * are removed and the size is adjusted accordingly.
490 *
491 * @param array a pointer to the array
492 * @param capacity the new capacity
493 * @return zero on success, non-zero if reallocation failed
494 */
495 int ucx_array_resize(UcxArray* array, size_t capacity);
497 /**
498 * Resizes the array only, if the capacity is insufficient.
499 *
500 * If the requested capacity is smaller than the current capacity, this
501 * function does nothing.
502 *
503 * @param array a pointer to the array
504 * @param capacity the guaranteed capacity
505 * @return zero on success, non-zero if reallocation failed
506 */
507 int ucx_array_reserve(UcxArray* array, size_t capacity);
511 #ifdef __cplusplus
512 }
513 #endif
515 #endif /* UCX_ARRAY_H */