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