src/cx/array_list.h

changeset 883
3012e9b4214e
parent 855
35bcb3216c0d
child 884
d375d8056262
equal deleted inserted replaced
882:f8ca6e6c0d48 883:3012e9b4214e
53 * Declares variables for an array that can be used with the convenience macros. 53 * Declares variables for an array that can be used with the convenience macros.
54 * 54 *
55 * @see cx_array_simple_add() 55 * @see cx_array_simple_add()
56 * @see cx_array_simple_copy() 56 * @see cx_array_simple_copy()
57 * @see cx_array_initialize() 57 * @see cx_array_initialize()
58 * @see cx_array_simple_add_sorted()
59 * @see cx_array_simple_insert_sorted()
58 */ 60 */
59 #define CX_ARRAY_DECLARE(type, name) \ 61 #define CX_ARRAY_DECLARE(type, name) \
60 type * name; \ 62 type * name; \
61 size_t name##_size; \ 63 size_t name##_size; \
62 size_t name##_capacity 64 size_t name##_capacity
145 * this function ultimately returns a failure. 147 * this function ultimately returns a failure.
146 * 148 *
147 * @param target a pointer to the target array 149 * @param target a pointer to the target array
148 * @param size a pointer to the size of the target array 150 * @param size a pointer to the size of the target array
149 * @param capacity a pointer to the target array's capacity - 151 * @param capacity a pointer to the target array's capacity -
150 * \c NULL if only the size shall be used to bound the array 152 * \c NULL if only the size shall be used to bound the array (reallocations
153 * will NOT be supported in that case)
151 * @param index the index where the copied elements shall be placed 154 * @param index the index where the copied elements shall be placed
152 * @param src the source array 155 * @param src the source array
153 * @param elem_size the size of one element 156 * @param elem_size the size of one element
154 * @param elem_count the number of elements to copy 157 * @param elem_count the number of elements to copy
155 * @param reallocator the array reallocator to use, or \c NULL 158 * @param reallocator the array reallocator to use, or \c NULL
172 * 175 *
173 * @param array the name of the array (NOT a pointer to the array) 176 * @param array the name of the array (NOT a pointer to the array)
174 * @param index the index where the copied elements shall be placed 177 * @param index the index where the copied elements shall be placed
175 * @param src the source array 178 * @param src the source array
176 * @param count the number of elements to copy 179 * @param count the number of elements to copy
180 * @see CX_ARRAY_DECLARE()
177 */ 181 */
178 #define cx_array_simple_copy(array, index, src, count) \ 182 #define cx_array_simple_copy(array, index, src, count) \
179 cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ 183 cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \
180 index, src, sizeof((array)[0]), count, cx_array_default_reallocator) 184 index, src, sizeof((array)[0]), count, cx_array_default_reallocator)
181 185
200 */ 204 */
201 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ 205 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
202 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator) 206 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator)
203 207
204 /** 208 /**
205 * Convenience macro that uses cx_array_add() with a default layout and the default reallocator. 209 * Convenience macro that uses cx_array_add() with a default layout and
210 * the default reallocator.
206 * 211 *
207 * @param array the name of the array (NOT a pointer to the array) 212 * @param array the name of the array (NOT a pointer to the array)
208 * @param elem the element to add (NOT a pointer, address is automatically taken) 213 * @param elem the element to add (NOT a pointer, address is automatically taken)
214 * @see CX_ARRAY_DECLARE()
209 */ 215 */
210 #define cx_array_simple_add(array, elem) \ 216 #define cx_array_simple_add(array, elem) \
211 cx_array_simple_copy(array, array##_size, &(elem), 1) 217 cx_array_simple_copy(array, array##_size, &(elem), 1)
218
219
220 /**
221 * Inserts a sorted array into another sorted array.
222 *
223 * If either the target or the source array is not already sorted with respect
224 * to the specified \p cmp_func, the behavior is undefined.
225 *
226 * If the capacity is insufficient to hold the new data, a reallocation
227 * attempt is made.
228 *
229 * @param target a pointer to the target array
230 * @param size a pointer to the size of the target array
231 * @param capacity a pointer to the target array's capacity
232 * @param cmp_func the compare function for the elements
233 * @param src the source array
234 * @param elem_size the size of one element
235 * @param elem_count the number of elements to insert
236 * @param reallocator the array reallocator to use
237 * @return zero on success, non-zero error code on failure
238 */
239 enum cx_array_result cx_array_insert_sorted(
240 void **target,
241 size_t *size,
242 size_t *capacity,
243 cx_compare_func cmp_func,
244 void const *src,
245 size_t elem_size,
246 size_t elem_count,
247 struct cx_array_reallocator_s *reallocator
248 ) __attribute__((__nonnull__));
249
250 /**
251 * Inserts an element into a sorted array.
252 *
253 * If the target array is not already sorted with respect
254 * to the specified \p cmp_func, the behavior is undefined.
255 *
256 * If the capacity is insufficient to hold the new data, a reallocation
257 * attempt is made.
258 *
259 * @param target a pointer to the target array
260 * @param size a pointer to the size of the target array
261 * @param capacity a pointer to the target array's capacity
262 * @param elem_size the size of one element
263 * @param elem a pointer to the element to add
264 * @param reallocator the array reallocator to use
265 * @return zero on success, non-zero error code on failure
266 */
267 #define cx_array_add_sorted(target, size, capacity, elem_size, elem, cmp_func, reallocator) \
268 cx_array_insert_sorted((void**)(target), size, capacity, cmp_func, elem, elem_size, 1, reallocator)
269
270 /**
271 * Convenience macro for cx_array_add_sorted() with a default
272 * layout and the default reallocator.
273 *
274 * @param array the name of the array (NOT a pointer to the array)
275 * @param elem the element to add (NOT a pointer, address is automatically taken)
276 * @param cmp_func the compare function for the elements
277 * @see CX_ARRAY_DECLARE()
278 */
279 #define cx_array_simple_add_sorted(array, elem, cmp_func) \
280 cx_array_add_sorted(&array, &(array##_size), &(array##_capacity), \
281 sizeof((array)[0]), &(elem), cmp_func, cx_array_default_reallocator)
282
283 /**
284 * Convenience macro for cx_array_insert_sorted() with a default
285 * layout and the default reallocator.
286 *
287 * @param array the name of the array (NOT a pointer to the array)
288 * @param src pointer to the source array
289 * @param n number of elements in the source array
290 * @param cmp_func the compare function for the elements
291 * @see CX_ARRAY_DECLARE()
292 */
293 #define cx_array_simple_insert_sorted(array, src, n, cmp_func) \
294 cx_array_insert_sorted((void**)(&array), &(array##_size), &(array##_capacity), \
295 cmp_func, src, sizeof((array)[0]), n, cx_array_default_reallocator)
212 296
213 /** 297 /**
214 * Swaps two array elements. 298 * Swaps two array elements.
215 * 299 *
216 * @param arr the array 300 * @param arr the array

mercurial