src/ucx/array.h

branch
feature/array
changeset 334
bc81faa9afda
parent 323
b8c49e7a1dba
child 336
6d7aa8a1a3b3
equal deleted inserted replaced
333:b3ad9d1a20b7 334:bc81faa9afda
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 /**
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);
80
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);
91
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);
101
102
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);
121
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);
132
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);
146
147
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);
160
161 /**
162 * Concatenates two arrays.
163 *
164 * The contents of the second array are appended to the first array in one
165 * single operation. The second array is otherwise left untouched.
166 *
167 * The first array may grow automatically. If this fails, both arrays remain
168 * unmodified.
169 *
170 * @param array1 first array
171 * @param array2 second array
172 * @return zero on success, non-zero if reallocation was necessary but failed
173 * or the element size does not match
174 */
175 int ucx_array_concat(UcxArray *array1, const UcxArray *array2);
176
177 /**
178 * Returns a pointer to the array element at the specified index.
179 *
180 * @param array the array to retrieve the element from
181 * @param index index of the element to return
182 * @return a pointer to the element at the specified index or <code>NULL</code>,
183 * if the index is greater than the array size
184 */
185 void *ucx_array_at(UcxArray array, size_t index);
186
187 /**
188 * Returns an element of the specified type by value.
189 *
190 * This expression can also be assigned to.
191 *
192 * If <code>sizeof(type)</code> does not equal the array's element size, the
193 * behavior is undefined.
194 * If the index is out of bounds, the behavior is undefined.
195 *
196 * @param type the type of the element
197 * @param array the array to retrieve the element from
198 * @param index index of the element to return
199 * @return the requested element
200 * @see ucx_array_at()
201 */
202 #define ucx_array_at_typed(type, arr, i) (((type*)((arr).data))[i])
203
204 /**
205 * Shorthand for ucx_array_at_typed().
206 */
207 #define ucx_array_at_int(arr, i) ucx_array_at_typed(int, arr, i)
208
209 /**
210 * Shorthand for ucx_array_at_typed().
211 */
212 #define ucx_array_at_short(arr, i) ucx_array_at_typed(short, arr, i)
213
214 /**
215 * Shorthand for ucx_array_at_typed().
216 */
217 #define ucx_array_at_longint(arr, i) ucx_array_at_typed(long int, arr, i)
218
219 /**
220 * Shorthand for ucx_array_at_typed().
221 */
222 #define ucx_array_at_uint(arr, i) ucx_array_at_typed(unsigned int, arr, i)
223
224 /**
225 * Shorthand for ucx_array_at_typed().
226 */
227 #define ucx_array_at_ushort(arr, i) ucx_array_at_typed(unsigned short, arr, i)
228
229 /**
230 * Shorthand for ucx_array_at_typed().
231 */
232 #define ucx_array_at_ulongint(arr, i) \
233 ucx_array_at_typed(unsigned long int, arr, i)
234
235 /**
236 * Shorthand for ucx_array_get_typed().
237 */
238 #define ucx_array_get_float(arr, i) ucx_array_get_typed(float, arr, i)
239
240 /**
241 * Shorthand for ucx_array_get_typed().
242 */
243 #define ucx_array_get_double(arr, i) ucx_array_get_typed(double, arr, i)
244
245 /**
246 * Returns the index of an element containing the specified data.
247 *
248 * This function uses a cmp_func() to compare the data of each list element
249 * with the specified data. If no cmp_func is provided, memcmp() is used.
250 *
251 * If the array contains the data more than once, the index of the first
252 * occurrence is returned.
253 * If the array does not contain the data, the size of array is returned.
254 *
255 * @param array the array where to search for the data
256 * @param elem the element data
257 * @param cmpfnc the compare function
258 * @param data additional data for the compare function
259 * @return the index of the element containing the specified data or the size of
260 * the array, if the data is not found in this array
261 */
262 size_t ucx_array_find(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
263
264 /**
265 * Checks, if an array contains a specific element.
266 *
267 * An element is found, if ucx_array_find() returns a value less than the size.
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 1, if and only if the array contains the specified element data
274 * @see ucx_array_find()
275 */
276 int ucx_array_contains(UcxArray array, void *elem, cmp_func cmpfnc, void *data);
277
278 /**
279 * Sorts a UcxArray with natural merge sort.
280 *
281 * This function uses O(n) additional temporary memory for merge operations
282 * that is automatically freed after each merge.
283 *
284 * @param the array to sort
285 * @param cmpfnc the function that shall be used to compare the element data
286 * @param data additional data for the cmp_func()
287 * @return zero on success, non-zero if allocation of temporary memory failed
288 */
289 int ucx_array_sort(UcxArray array, cmp_func cmpfnc, void *data);
290
291 /**
292 * Removes an element from the array.
293 *
294 * This is in general an expensive operation, because several elements may
295 * be moved. If the order of the elements is not relevant, use
296 * ucx_array_remove_fast() instead.
297 *
298 * @param array pointer to the array from which the element shall be removed
299 * @param index the index of the element to remove
300 */
301 void ucx_array_remove(UcxArray *array, size_t index);
302
303 /**
304 * Removes an element from the array.
305 *
306 * This is an O(1) operation, but does not maintain the order of the elements.
307 * The last element in the array is moved to the location of the removed
308 * element.
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_fast(UcxArray *array, size_t index);
314
315 /**
316 * Shrinks the memory to exactly fit the contents.
317 *
318 * After this operation, the capacity equals the size.
319 *
320 * @param array a pointer to the array
321 * @return zero on success, non-zero if reallocation failed
322 */
323 int ucx_array_shrink(UcxArray* array);
324
325 /**
326 * Sets the capacity of the array.
327 *
328 * If the new capacity is smaller than the size of the array, the elements
329 * are removed and the size is adjusted accordingly.
330 *
331 * @param array a pointer to the array
332 * @param capacity the new capacity
333 * @return zero on success, non-zero if reallocation failed
334 */
335 int ucx_array_resize(UcxArray* array, size_t capacity);
336
337 /**
338 * Resizes the array only, if the capacity is insufficient.
339 *
340 * If the requested capacity is smaller than the current capacity, this
341 * function does nothing.
342 *
343 * @param array a pointer to the array
344 * @param capacity the guaranteed capacity
345 * @return zero on success, non-zero if reallocation failed
346 */
347 int ucx_array_reserve(UcxArray* array, size_t capacity);
348
349
350
351 #ifdef __cplusplus
352 }
353 #endif
354
355 #endif /* UCX_ARRAY_H */
356

mercurial