Thu, 04 Jul 2019 22:32:03 +0200
adds ucx_array_set()
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 free
130 */
131 void ucx_array_free(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 an element of the specified type by value.
203 *
204 * This expression can also be assigned to.
205 *
206 * If <code>sizeof(type)</code> does not equal the array's element size, the
207 * behavior is undefined.
208 * If the index is out of bounds, the behavior is undefined.
209 *
210 * @param type the type of the element
211 * @param array the array to retrieve the element from
212 * @param index index of the element to return
213 * @return the requested element
214 * @see ucx_array_at()
215 */
216 #define ucx_array_at_typed(type, arr, i) (((type*)((arr).data))[i])
218 /**
219 * Shorthand for ucx_array_at_typed().
220 */
221 #define ucx_array_at_int(arr, i) ucx_array_at_typed(int, arr, i)
223 /**
224 * Shorthand for ucx_array_at_typed().
225 */
226 #define ucx_array_at_short(arr, i) ucx_array_at_typed(short, arr, i)
228 /**
229 * Shorthand for ucx_array_at_typed().
230 */
231 #define ucx_array_at_longint(arr, i) ucx_array_at_typed(long int, arr, i)
233 /**
234 * Shorthand for ucx_array_at_typed().
235 */
236 #define ucx_array_at_uint(arr, i) ucx_array_at_typed(unsigned int, arr, i)
238 /**
239 * Shorthand for ucx_array_at_typed().
240 */
241 #define ucx_array_at_ushort(arr, i) ucx_array_at_typed(unsigned short, arr, i)
243 /**
244 * Shorthand for ucx_array_at_typed().
245 */
246 #define ucx_array_at_ulongint(arr, i) \
247 ucx_array_at_typed(unsigned long int, arr, i)
249 /**
250 * Shorthand for ucx_array_get_typed().
251 */
252 #define ucx_array_get_float(arr, i) ucx_array_get_typed(float, arr, i)
254 /**
255 * Shorthand for ucx_array_get_typed().
256 */
257 #define ucx_array_get_double(arr, i) ucx_array_get_typed(double, arr, i)
259 /**
260 * Returns the index of an element containing the specified data.
261 *
262 * This function uses a cmp_func() to compare the data of each list element
263 * with the specified data. If no cmp_func is provided, memcmp() is used.
264 *
265 * If the array contains the data more than once, the index of the first
266 * occurrence is returned.
267 * If the array does not contain the data, the size of array is returned.
268 *
269 * @param array the array where to search for the data
270 * @param elem the element data
271 * @param cmpfnc the compare function
272 * @param data additional data for the compare function
273 * @return the index of the element containing the specified data or the size of
274 * the array, if the data is not found in this array
275 */
276 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
278 /**
279 * Checks, if an array contains a specific element.
280 *
281 * An element is found, if ucx_array_find() returns a value less than the size.
282 *
283 * @param array the array where to search for the data
284 * @param elem the element data
285 * @param cmpfnc the compare function
286 * @param data additional data for the compare function
287 * @return 1, if and only if the array contains the specified element data
288 * @see ucx_array_find()
289 */
290 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
292 /**
293 * Sorts a UcxArray with an almost in-place merge sort.
294 *
295 * This function uses additional memory for exactly one element.
296 *
297 * @param the array to sort
298 * @param cmpfnc the function that shall be used to compare the element data
299 * @param data additional data for the cmp_func()
300 */
301 void ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
303 /**
304 * Removes an element from the array.
305 *
306 * This is in general an expensive operation, because several elements may
307 * be moved. If the order of the elements is not relevant, use
308 * ucx_array_remove_fast() instead.
309 *
310 * @param array pointer to the array from which the element shall be removed
311 * @param index the index of the element to remove
312 */
313 void ucx_array_remove(UcxArray *array, size_t index);
315 /**
316 * Removes an element from the array.
317 *
318 * This is an O(1) operation, but does not maintain the order of the elements.
319 * The last element in the array is moved to the location of the removed
320 * element.
321 *
322 * @param array pointer to the array from which the element shall be removed
323 * @param index the index of the element to remove
324 */
325 void ucx_array_remove_fast(UcxArray *array, size_t index);
327 /**
328 * Shrinks the memory to exactly fit the contents.
329 *
330 * After this operation, the capacity equals the size.
331 *
332 * @param array a pointer to the array
333 * @return zero on success, non-zero if reallocation failed
334 */
335 int ucx_array_shrink(UcxArray* array);
337 /**
338 * Sets the capacity of the array.
339 *
340 * If the new capacity is smaller than the size of the array, the elements
341 * are removed and the size is adjusted accordingly.
342 *
343 * @param array a pointer to the array
344 * @param capacity the new capacity
345 * @return zero on success, non-zero if reallocation failed
346 */
347 int ucx_array_resize(UcxArray* array, size_t capacity);
349 /**
350 * Resizes the array only, if the capacity is insufficient.
351 *
352 * If the requested capacity is smaller than the current capacity, this
353 * function does nothing.
354 *
355 * @param array a pointer to the array
356 * @param capacity the guaranteed capacity
357 * @return zero on success, non-zero if reallocation failed
358 */
359 int ucx_array_reserve(UcxArray* array, size_t capacity);
363 #ifdef __cplusplus
364 }
365 #endif
367 #endif /* UCX_ARRAY_H */