80 * |
81 * |
81 * @param array a pointer to location of the array pointer |
82 * @param array a pointer to location of the array pointer |
82 * @param capacity a pointer to the capacity |
83 * @param capacity a pointer to the capacity |
83 * @param elmsize the size of each element |
84 * @param elmsize the size of each element |
84 * @param idx the index of the element to set |
85 * @param idx the index of the element to set |
85 * @param data the element data |
86 * @param data a pointer to the element data |
86 * @return zero on success or non-zero on error (errno will be set) |
87 * @return zero on success or non-zero on error (errno will be set) |
87 */ |
88 */ |
88 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \ |
89 #define ucx_array_util_set(array, capacity, elmsize, idx, data) \ |
89 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ |
90 ucx_array_util_set_a(ucx_default_allocator(), (void**)(array), capacity, \ |
90 elmsize, idx, data) |
91 elmsize, idx, data) |
91 |
92 |
92 /** |
93 /** |
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. |
94 * Sets an element in an arbitrary user defined array. |
|
95 * The data is copied from the specified data location. |
109 * |
96 * |
110 * If the capacity is insufficient, the array is automatically reallocated |
97 * If the capacity is insufficient, the array is automatically reallocated |
111 * using the specified allocator and the possibly new pointer is stored in |
98 * using the specified allocator and the possibly new pointer is stored in |
112 * the <code>array</code> argument. |
99 * the <code>array</code> argument. |
113 * |
100 * |
117 * @param alloc the allocator that shall be used to reallocate the array |
104 * @param alloc the allocator that shall be used to reallocate the array |
118 * @param array a pointer to location of the array pointer |
105 * @param array a pointer to location of the array pointer |
119 * @param capacity a pointer to the capacity |
106 * @param capacity a pointer to the capacity |
120 * @param elmsize the size of each element |
107 * @param elmsize the size of each element |
121 * @param idx the index of the element to set |
108 * @param idx the index of the element to set |
122 * @param ... the element data |
109 * @param data a pointer to the element data |
123 * @return zero on success or non-zero on error (errno will be set) |
110 * @return zero on success or non-zero on error (errno will be set) |
124 */ |
111 */ |
125 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, |
112 int ucx_array_util_set_a(UcxAllocator* alloc, void** array, size_t* capacity, |
126 size_t elmsize, size_t idx, ...); |
113 size_t elmsize, size_t idx, void* data); |
127 |
114 |
128 |
115 /** |
129 /** |
116 * Stores a pointer in an arbitrary user defined array. |
130 * Convenience macro for ucx_array_util_set_a() which automatically computes |
117 * The element size of the array must be sizeof(void*). |
131 * <code>sizeof(data)</code>. |
118 * |
|
119 * If the capacity is insufficient, the array is automatically reallocated and |
|
120 * the possibly new pointer is stored in the <code>array</code> argument. |
|
121 * |
|
122 * On reallocation the capacity of the array is doubled until it is sufficient. |
|
123 * The new capacity is stored back to <code>capacity</code>. |
|
124 * |
|
125 * @param array a pointer to location of the array pointer |
|
126 * @param capacity a pointer to the capacity |
|
127 * @param idx the index of the element to set |
|
128 * @param ptr the pointer to store |
|
129 * @return zero on success or non-zero on error (errno will be set) |
|
130 */ |
|
131 #define ucx_array_util_setptr(array, capacity, idx, ptr) \ |
|
132 ucx_array_util_setptr_a(ucx_default_allocator(), (void**)(array), \ |
|
133 capacity, idx, ptr) |
|
134 |
|
135 /** |
|
136 * Stores a pointer in an arbitrary user defined array. |
|
137 * The element size of the array must be sizeof(void*). |
|
138 * |
|
139 * If the capacity is insufficient, the array is automatically reallocated |
|
140 * using the specified allocator and the possibly new pointer is stored in |
|
141 * the <code>array</code> argument. |
|
142 * |
|
143 * On reallocation the capacity of the array is doubled until it is sufficient. |
|
144 * The new capacity is stored back to <code>capacity</code>. |
132 * |
145 * |
133 * @param alloc the allocator that shall be used to reallocate the array |
146 * @param alloc the allocator that shall be used to reallocate the array |
134 * @param array a pointer to location of the array pointer |
147 * @param array a pointer to location of the array pointer |
135 * @param capacity a pointer to the capacity |
148 * @param capacity a pointer to the capacity |
136 * @param idx the index of the element to set |
149 * @param idx the index of the element to set |
137 * @param data the element data |
150 * @param ptr the pointer to store |
138 * @return zero on success or non-zero on error (errno will be set) |
151 * @return zero on success or non-zero on error (errno will be set) |
139 * @see ucx_array_util_set_a() |
152 */ |
140 */ |
153 int ucx_array_util_setptr_a(UcxAllocator* alloc, void** array, size_t* capacity, |
141 #define UCX_ARRAY_UTIL_SET_A(alloc, array, capacity, idx, data) \ |
154 size_t idx, void* ptr); |
142 ucx_array_util_set_a(alloc, capacity, sizeof(data), idx, data) |
155 |
143 |
156 |
144 /** |
157 /** |
145 * Creates a new UCX array with the given capacity and element size. |
158 * Creates a new UCX array with the given capacity and element size. |
146 * @param capacity the initial capacity |
159 * @param capacity the initial capacity |
147 * @param elemsize the element size |
160 * @param elemsize the element size |
287 * @return zero on success, non-zero if a reallocation was necessary but failed |
300 * @return zero on success, non-zero if a reallocation was necessary but failed |
288 * @see ucx_array_append_from() |
301 * @see ucx_array_append_from() |
289 * @see ucx_array_set() |
302 * @see ucx_array_set() |
290 */ |
303 */ |
291 int ucx_array_set_from(UcxArray *array, size_t index, void *data, size_t count); |
304 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 |
305 |
375 /** |
306 /** |
376 * Concatenates two arrays. |
307 * Concatenates two arrays. |
377 * |
308 * |
378 * The contents of the second array are appended to the first array in one |
309 * The contents of the second array are appended to the first array in one |
505 * @param capacity the guaranteed capacity |
436 * @param capacity the guaranteed capacity |
506 * @return zero on success, non-zero if reallocation failed |
437 * @return zero on success, non-zero if reallocation failed |
507 */ |
438 */ |
508 int ucx_array_reserve(UcxArray* array, size_t capacity); |
439 int ucx_array_reserve(UcxArray* array, size_t capacity); |
509 |
440 |
|
441 /** |
|
442 * Resizes the capacity, if the specified number of elements would not fit. |
|
443 * |
|
444 * A call to ucx_array_grow(array, count) is effectively the same as |
|
445 * ucx_array_reserve(array, array->size+count). |
|
446 * |
|
447 * @param array a pointer to the array |
|
448 * @param count the number of elements that should additionally fit |
|
449 * into the array |
|
450 * @return zero on success, non-zero if reallocation failed |
|
451 */ |
|
452 int ucx_array_grow(UcxArray* array, size_t count); |
510 |
453 |
511 |
454 |
512 #ifdef __cplusplus |
455 #ifdef __cplusplus |
513 } |
456 } |
514 #endif |
457 #endif |