Sat, 10 Aug 2019 09:47:59 +0200
renames ucx_array_free() to ucx_array_destroy()
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 an element 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 element is 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 * @return zero on success, non-zero if a reallocation was necessary but failed
144 */
145 int ucx_array_append(UcxArray *array, void *data);
148 /**
149 * Inserts an element at the beginning of the array.
150 *
151 * This is an expensive operation, because the contents must be moved.
152 * If there is no particular reason to prepend data, you should use
153 * ucx_array_append() instead.
154 *
155 * @param array a pointer the array where to prepend the data
156 * @param data a pointer to the data to insert (may be <code>NULL</code>)
157 * @return zero on success, non-zero if a reallocation was necessary but failed
158 */
159 int ucx_array_prepend(UcxArray *array, void *data);
162 /**
163 * Sets an element at the specified index.
164 *
165 * If the index is out of bounds, the array automatically grows.
166 * The pointer to the data may be NULL, in which case the element is zeroed.
167 *
168 * @param array a pointer the array where to set the data
169 * @param index the index of the element to set
170 * @param data a pointer to the data to insert (may be <code>NULL</code>)
171 * @return zero on success, non-zero if a reallocation was necessary but failed
172 */
173 int ucx_array_set(UcxArray *array, size_t index, void *data);
175 /**
176 * Concatenates two arrays.
177 *
178 * The contents of the second array are appended to the first array in one
179 * single operation. The second array is otherwise left untouched.
180 *
181 * The first array may grow automatically. If this fails, both arrays remain
182 * unmodified.
183 *
184 * @param array1 first array
185 * @param array2 second array
186 * @return zero on success, non-zero if reallocation was necessary but failed
187 * or the element size does not match
188 */
189 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
191 /**
192 * Returns a pointer to the array element at the specified index.
193 *
194 * @param array the array to retrieve the element from
195 * @param index index of the element to return
196 * @return a pointer to the element at the specified index or <code>NULL</code>,
197 * if the index is greater than the array size
198 */
199 void *ucx_array_at(UcxArray array, size_t index);
201 /**
202 * Returns the index of an element containing the specified data.
203 *
204 * This function uses a cmp_func() to compare the data of each list element
205 * with the specified data. If no cmp_func is provided, memcmp() is used.
206 *
207 * If the array contains the data more than once, the index of the first
208 * occurrence is returned.
209 * If the array does not contain the data, the size of array is returned.
210 *
211 * @param array the array where to search for the data
212 * @param elem the element data
213 * @param cmpfnc the compare function
214 * @param data additional data for the compare function
215 * @return the index of the element containing the specified data or the size of
216 * the array, if the data is not found in this array
217 */
218 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
220 /**
221 * Checks, if an array contains a specific element.
222 *
223 * An element is found, if ucx_array_find() returns a value less than the size.
224 *
225 * @param array the array where to search for the data
226 * @param elem the element data
227 * @param cmpfnc the compare function
228 * @param data additional data for the compare function
229 * @return 1, if and only if the array contains the specified element data
230 * @see ucx_array_find()
231 */
232 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
234 /**
235 * Sorts a UcxArray with the best available sort algorithm.
236 *
237 * The qsort_r() function is used, if available (glibc, FreeBSD or MacOS).
238 * The order of arguments is automatically adjusted for the FreeBSD and MacOS
239 * version of qsort_r().
240 *
241 * If qsort_r() is not available, a merge sort algorithm is used, which is
242 * guaranteed to use no more additional memory than for exactly one element.
243 *
244 * @param array the array to sort
245 * @param cmpfnc the function that shall be used to compare the element data
246 * @param data additional data for the cmp_func() or <code>NULL</code>
247 */
248 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
250 /**
251 * Removes an element from the array.
252 *
253 * This is in general an expensive operation, because several elements may
254 * be moved. If the order of the elements is not relevant, use
255 * ucx_array_remove_fast() instead.
256 *
257 * @param array pointer to the array from which the element shall be removed
258 * @param index the index of the element to remove
259 */
260 void ucx_array_remove(UcxArray *array, size_t index);
262 /**
263 * Removes an element from the array.
264 *
265 * This is an O(1) operation, but does not maintain the order of the elements.
266 * The last element in the array is moved to the location of the removed
267 * element.
268 *
269 * @param array pointer to the array from which the element shall be removed
270 * @param index the index of the element to remove
271 */
272 void ucx_array_remove_fast(UcxArray *array, size_t index);
274 /**
275 * Shrinks the memory to exactly fit the contents.
276 *
277 * After this operation, the capacity equals the size.
278 *
279 * @param array a pointer to the array
280 * @return zero on success, non-zero if reallocation failed
281 */
282 int ucx_array_shrink(UcxArray* array);
284 /**
285 * Sets the capacity of the array.
286 *
287 * If the new capacity is smaller than the size of the array, the elements
288 * are removed and the size is adjusted accordingly.
289 *
290 * @param array a pointer to the array
291 * @param capacity the new capacity
292 * @return zero on success, non-zero if reallocation failed
293 */
294 int ucx_array_resize(UcxArray* array, size_t capacity);
296 /**
297 * Resizes the array only, if the capacity is insufficient.
298 *
299 * If the requested capacity is smaller than the current capacity, this
300 * function does nothing.
301 *
302 * @param array a pointer to the array
303 * @param capacity the guaranteed capacity
304 * @return zero on success, non-zero if reallocation failed
305 */
306 int ucx_array_reserve(UcxArray* array, size_t capacity);
310 #ifdef __cplusplus
311 }
312 #endif
314 #endif /* UCX_ARRAY_H */