Sat, 10 Aug 2019 11:12:49 +0200
improves array append/prepend/set interface
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;
73 /**
74 * Creates a new UCX array with the given capacity and element size.
75 * @param capacity the initial capacity
76 * @param elemsize the element size
77 * @return a new UCX array structure
78 */
79 UcxArray ucx_array_new(size_t capacity, size_t elemsize);
81 /**
82 * Creates a new UCX array using the specified allocator.
83 *
84 * @param capacity the initial capacity
85 * @param elemsize the element size
86 * @param allocator the allocator to use
87 * @return a new UCX array structure
88 */
89 UcxArray ucx_array_new_a(size_t capacity, size_t elemsize,
90 UcxAllocator* allocator);
92 /**
93 * Creates an shallow copy of an array.
94 *
95 * This function clones the specified array by using memcpy().
96 *
97 * @param array the array to copy
98 * @return the copy (may be an empty array on allocation errors)
99 */
100 UcxArray ucx_array_clone(UcxArray array);
103 /**
104 * Compares two UCX arrays element-wise by using a compare function.
105 *
106 * Elements of the two specified arrays are compared by using the specified
107 * compare function and the additional data. The type and content of this
108 * additional data depends on the cmp_func() used.
109 *
110 * This function always returns zero, if the element sizes of the arrays do
111 * not match and performs no comparisons in this case.
112 *
113 * @param array1 the first array
114 * @param array2 the second array
115 * @param cmpfnc the compare function
116 * @param data additional data for the compare function
117 * @return 1, if and only if the two arrays equal element-wise, 0 otherwise
118 */
119 int ucx_array_equals(UcxArray array1, UcxArray array2,
120 cmp_func cmpfnc, void* data);
122 /**
123 * Destroys the array.
124 *
125 * The data is freed and both capacity and count are reset to zero.
126 * If the array structure itself has been dynamically allocated, it has to be
127 * freed separately.
128 *
129 * @param array the array to destroy
130 */
131 void ucx_array_destroy(UcxArray *array);
133 /**
134 * Inserts elements at the end of the array.
135 *
136 * This is an O(1) operation.
137 * The array will automatically grow, if the capacity is exceeded.
138 * If a pointer to data is provided, the data is copied into the array with
139 * memcpy(). Otherwise the new elements are completely zeroed.
140 *
141 * @param array a pointer the array where to append the data
142 * @param data a pointer to the data to insert (may be <code>NULL</code>)
143 * @param count number of elements to copy from data (if data is
144 * <code>NULL</code>, zeroed elements are appended)
145 * @return zero on success, non-zero if a reallocation was necessary but failed
146 * @see ucx_array_set_from()
147 * @see ucx_array_append()
148 */
149 int ucx_array_append_from(UcxArray *array, void *data, size_t count);
152 /**
153 * Inserts elements at the beginning of the array.
154 *
155 * This is an expensive operation, because the contents must be moved.
156 * If there is no particular reason to prepend data, you should use
157 * ucx_array_append_from() instead.
158 *
159 * @param array a pointer the array where to prepend the data
160 * @param data a pointer to the data to insert (may be <code>NULL</code>)
161 * @param count number of elements to copy from data (if data is
162 * <code>NULL</code>, zeroed elements are inserted)
163 * @return zero on success, non-zero if a reallocation was necessary but failed
164 * @see ucx_array_append_from()
165 * @see ucx_array_set_from()
166 * @see ucx_array_prepend()
167 */
168 int ucx_array_prepend_from(UcxArray *array, void *data, size_t count);
171 /**
172 * Sets elements starting at the specified index.
173 *
174 * If the any index is out of bounds, the array automatically grows.
175 * The pointer to the data may be NULL, in which case the elements are zeroed.
176 *
177 * @param array a pointer the array where to set the data
178 * @param index the index of the element to set
179 * @param data a pointer to the data to insert (may be <code>NULL</code>)
180 * @param count number of elements to copy from data (if data is
181 * <code>NULL</code>, the memory in the array is zeroed)
182 * @return zero on success, non-zero if a reallocation was necessary but failed
183 * @see ucx_array_append_from()
184 * @see ucx_array_set()
185 */
186 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count);
188 /**
189 * Inserts an element at the end of the array.
190 *
191 * This is an O(1) operation.
192 * The array will automatically grow, if the capacity is exceeded.
193 * If the type of the argument has a different size than the element size of
194 * this array, the behavior is undefined.
195 *
196 * @param array a pointer the array where to append the data
197 * @param elem the value to insert
198 * @return zero on success, non-zero if a reallocation was necessary but failed
199 * @see ucx_array_append_from()
200 * @see ucx_array_set()
201 */
202 #define ucx_array_append(array, elem) ucx_array_appendv(array, elem)
204 /**
205 * For internal use.
206 * Use ucx_array_append()
207 *
208 * @param array
209 * @param ...
210 * @return
211 * @see ucx_array_append()
212 */
213 int ucx_array_appendv(UcxArray *array, ...);
216 /**
217 * Inserts an element at the beginning of the array.
218 *
219 * This is an expensive operation, because the contents must be moved.
220 * If there is no particular reason to prepend data, you should use
221 * ucx_array_append() instead.
222 *
223 * @param array a pointer the array where to prepend the data
224 * @param elem the value to insert
225 * @return zero on success, non-zero if a reallocation was necessary but failed
226 * @see ucx_array_append()
227 * @see ucx_array_set_from()
228 * @see ucx_array_prepend_from()
229 */
230 #define ucx_array_prepend(array, elem) ucx_array_prependv(array, elem)
232 /**
233 * For internal use.
234 * Use ucx_array_prepend()
235 *
236 * @param array
237 * @param ...
238 * @return
239 * @see ucx_array_prepend()
240 */
241 int ucx_array_prependv(UcxArray *array, ...);
244 /**
245 * Sets an element at the specified index.
246 *
247 * If the any index is out of bounds, the array automatically grows.
248 *
249 * @param array a pointer the array where to set the data
250 * @param index the index of the element to set
251 * @param elem the value to set
252 * @return zero on success, non-zero if a reallocation was necessary but failed
253 * @see ucx_array_append()
254 * @see ucx_array_set_from()
255 */
256 #define ucx_array_set(array, index, elem) ucx_array_setv(array, index, elem)
258 /**
259 * For internal use.
260 * Use ucx_array_set()
261 *
262 * @param array
263 * @param index
264 * @param ...
265 * @return
266 * @see ucx_array_set()
267 */
268 int ucx_array_setv(UcxArray *array, size_t index, ...);
270 /**
271 * Concatenates two arrays.
272 *
273 * The contents of the second array are appended to the first array in one
274 * single operation. The second array is otherwise left untouched.
275 *
276 * The first array may grow automatically. If this fails, both arrays remain
277 * unmodified.
278 *
279 * @param array1 first array
280 * @param array2 second array
281 * @return zero on success, non-zero if reallocation was necessary but failed
282 * or the element size does not match
283 */
284 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
286 /**
287 * Returns a pointer to the array element at the specified index.
288 *
289 * @param array the array to retrieve the element from
290 * @param index index of the element to return
291 * @return a pointer to the element at the specified index or <code>NULL</code>,
292 * if the index is greater than the array size
293 */
294 void *ucx_array_at(UcxArray array, size_t index);
296 /**
297 * Returns the index of an element containing the specified data.
298 *
299 * This function uses a cmp_func() to compare the data of each list element
300 * with the specified data. If no cmp_func is provided, memcmp() is used.
301 *
302 * If the array contains the data more than once, the index of the first
303 * occurrence is returned.
304 * If the array does not contain the data, the size of array is returned.
305 *
306 * @param array the array where to search for the data
307 * @param elem the element data
308 * @param cmpfnc the compare function
309 * @param data additional data for the compare function
310 * @return the index of the element containing the specified data or the size of
311 * the array, if the data is not found in this array
312 */
313 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
315 /**
316 * Checks, if an array contains a specific element.
317 *
318 * An element is found, if ucx_array_find() returns a value less than the size.
319 *
320 * @param array the array where to search for the data
321 * @param elem the element data
322 * @param cmpfnc the compare function
323 * @param data additional data for the compare function
324 * @return 1, if and only if the array contains the specified element data
325 * @see ucx_array_find()
326 */
327 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
329 /**
330 * Sorts a UcxArray with the best available sort algorithm.
331 *
332 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
333 * The order of arguments is automatically adjusted for the FreeBSD and MacOS
334 * version of qsort_r().
335 *
336 * If qsort_r() is not available, a merge sort algorithm is used, which is
337 * guaranteed to use no more additional memory than for exactly one element.
338 *
339 * @param array the array to sort
340 * @param cmpfnc the function that shall be used to compare the element data
341 * @param data additional data for the cmp_func() or <code>NULL</code>
342 */
343 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
345 /**
346 * Removes an element from the array.
347 *
348 * This is in general an expensive operation, because several elements may
349 * be moved. If the order of the elements is not relevant, use
350 * ucx_array_remove_fast() instead.
351 *
352 * @param array pointer to the array from which the element shall be removed
353 * @param index the index of the element to remove
354 */
355 void ucx_array_remove(UcxArray *array, size_t index);
357 /**
358 * Removes an element from the array.
359 *
360 * This is an O(1) operation, but does not maintain the order of the elements.
361 * The last element in the array is moved to the location of the removed
362 * element.
363 *
364 * @param array pointer to the array from which the element shall be removed
365 * @param index the index of the element to remove
366 */
367 void ucx_array_remove_fast(UcxArray *array, size_t index);
369 /**
370 * Shrinks the memory to exactly fit the contents.
371 *
372 * After this operation, the capacity equals the size.
373 *
374 * @param array a pointer to the array
375 * @return zero on success, non-zero if reallocation failed
376 */
377 int ucx_array_shrink(UcxArray* array);
379 /**
380 * Sets the capacity of the array.
381 *
382 * If the new capacity is smaller than the size of the array, the elements
383 * are removed and the size is adjusted accordingly.
384 *
385 * @param array a pointer to the array
386 * @param capacity the new capacity
387 * @return zero on success, non-zero if reallocation failed
388 */
389 int ucx_array_resize(UcxArray* array, size_t capacity);
391 /**
392 * Resizes the array only, if the capacity is insufficient.
393 *
394 * If the requested capacity is smaller than the current capacity, this
395 * function does nothing.
396 *
397 * @param array a pointer to the array
398 * @param capacity the guaranteed capacity
399 * @return zero on success, non-zero if reallocation failed
400 */
401 int ucx_array_reserve(UcxArray* array, size_t capacity);
405 #ifdef __cplusplus
406 }
407 #endif
409 #endif /* UCX_ARRAY_H */