2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Dynamic and flexible arrays for C.
23 * @author Markus Armbruster, Michael Beck, Matthias Braun, Sebastian Hack
26 #ifndef FIRM_ADT_ARRAY_H
27 #define FIRM_ADT_ARRAY_H
38 * Creates a flexible array.
40 * @param type The element type of the new array.
41 * @param nelts a size_t expression evaluating to the number of elements
43 * This macro creates a flexible array of a given type at runtime.
44 * The size of the array can be changed later.
46 * @return A pointer to the flexible array (can be used as a pointer to the
47 * first element of this array).
49 #define NEW_ARR_F(type, nelts) \
50 ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
53 * Creates a new flexible array with the same number of elements as a
56 * @param type The element type of the new array.
57 * @param arr An array from which the number of elements will be taken
59 * This macro creates a flexible array of a given type at runtime.
60 * The size of the array can be changed later.
62 * @return A pointer to the flexible array (can be used as a pointer to the
63 * first element of this array).
65 #define CLONE_ARR_F(type, arr) \
66 NEW_ARR_F(type, ARR_LEN((arr)))
69 * Duplicates an array and returns the new flexible one.
71 * @param type The element type of the new array.
72 * @param arr An array from which the elements will be duplicated
74 * This macro creates a flexible array of a given type at runtime.
75 * The size of the array can be changed later.
77 * @return A pointer to the flexible array (can be used as a pointer to the
78 * first element of this array).
80 #define DUP_ARR_F(type, arr) \
81 memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
84 * Delete a flexible array.
86 * @param arr The flexible array.
88 #define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
91 * Creates a dynamic array on an obstack.
93 * @param type The element type of the new array.
94 * @param obstack A struct obstack * were the data will be allocated
95 * @param nelts A size_t expression evaluating to the number of elements
97 * This macro creates a dynamic array of a given type at runtime.
98 * The size of the array cannot be changed later.
100 * @return A pointer to the dynamic array (can be used as a pointer to the
101 * first element of this array).
103 #define NEW_ARR_D(type, obstack, nelts) \
105 ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts)) \
106 : (type *)arr_mt_descr.v.elts)
109 * Creates a new dynamic array with the same number of elements as a
112 * @param type The element type of the new array.
113 * @param obstack An struct obstack * were the data will be allocated
114 * @param arr An array from which the number of elements will be taken
116 * This macro creates a dynamic array of a given type at runtime.
117 * The size of the array cannot be changed later.
119 * @return A pointer to the dynamic array (can be used as a pointer to the
120 * first element of this array).
122 #define CLONE_ARR_D(type, obstack, arr) \
123 NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
126 * Duplicates an array and returns the new dynamic one.
128 * @param type The element type of the new array.
129 * @param obstack An struct obstack * were the data will be allocated
130 * @param arr An array from which the elements will be duplicated
132 * This macro creates a dynamic array of a given type at runtime.
133 * The size of the array cannot be changed later.
135 * @return A pointer to the dynamic array (can be used as a pointer to the
136 * first element of this array).
138 #define DUP_ARR_D(type, obstack, arr) \
139 memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
142 * Returns the length of an array
144 * @param arr a flexible, dynamic, automatic or static array.
146 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
149 * Resize a flexible array, allocate more data if needed but do NOT
152 * @param type The element type of the array.
153 * @param arr The array, which must be an lvalue.
154 * @param n The new size of the array.
156 * @remark This macro may change arr, so update all references!
158 #define ARR_RESIZE(type, arr, n) \
159 ((arr) = ir_arr_resize((void *)(arr), (n), sizeof(type)))
162 * Resize a flexible array, always reallocate data.
164 * @param type The element type of the array.
165 * @param arr The array, which must be an lvalue.
166 * @param n The new size of the array.
168 * @remark This macro may change arr, so update all references!
170 #define ARR_SETLEN(type, arr, n) \
171 ((arr) = ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
173 /** Set a length smaller than the current length of the array. Do not
174 * resize. len must be <= ARR_LEN(arr). */
175 #define ARR_SHRINKLEN(arr,len) \
176 (ARR_VRFY((arr)), assert(ARR_DESCR((arr))->nelts >= len), \
177 ARR_DESCR((arr))->nelts = len)
180 * Resize a flexible array by growing it by delta elements.
182 * @param type The element type of the array.
183 * @param arr The array, which must be an lvalue.
184 * @param delta The delta number of elements.
186 * @remark This macro may change arr, so update all references!
188 #define ARR_EXTEND(type, arr, delta) \
189 ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
192 * Resize a flexible array to hold n elements only if it is currently shorter
195 * @param type The element type of the array.
196 * @param arr The array, which must be an lvalue.
197 * @param n The new size of the array.
199 * @remark This macro may change arr, so update all references!
201 #define ARR_EXTO(type, arr, n) \
202 ((n) >= ARR_LEN((arr)) ? ARR_RESIZE(type, (arr), (n)+1) : (arr))
205 * Append one element to a flexible array.
207 * @param type The element type of the array.
208 * @param arr The array, which must be an lvalue.
209 * @param elt The new element, must be of type (type).
211 #define ARR_APP1(type, arr, elt) \
212 (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
215 # define ARR_VRFY(arr) ((void)0)
216 # define ARR_IDX_VRFY(arr, idx) ((void)0)
218 # define ARR_VRFY(arr) ir_verify_arr(arr)
219 # define ARR_IDX_VRFY(arr, idx) \
220 assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
225 * Construct an array header.
227 #define ARR_STRUCT(type, rnelts) \
232 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
233 int allocated; /* flexible: #slots allocated */ \
237 type elts[(rnelts)]; \
238 aligned_type align[1]; \
243 * The array descriptor header type.
245 typedef ARR_STRUCT(aligned_type, 1) ir_arr_descr;
247 extern ir_arr_descr arr_mt_descr;
249 void *ir_new_arr_f(int nelts, size_t elts_size);
250 void ir_del_arr_f(void *elts);
251 void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size);
252 void *ir_arr_resize(void *elts, int nelts, size_t elts_size);
253 void *ir_arr_setlen(void *elts, int nelts, size_t elts_size);
254 void ir_verify_arr(const void *elts);
256 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, v.elts)
257 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
261 / ___| ___ _ __| |_ ___ __| | / \ _ __ _ __ __ _ _ _ ___
262 \___ \ / _ \| '__| __/ _ \/ _` | / _ \ | '__| '__/ _` | | | / __|
263 ___) | (_) | | | || __/ (_| | / ___ \| | | | | (_| | |_| \__ \
264 |____/ \___/|_| \__\___|\__,_| /_/ \_\_| |_| \__,_|\__, |___/
268 typedef int (ir_arr_cmp_func_t)(const void *a, const void *b);
271 * Do a binary search in an array.
272 * @param arr The array.
273 * @param elm_size The size of an array element.
274 * @param cmp A comparison function for two array elements (see qsort(3) for example).
275 * @param elm A pointer to the element we are looking for.
276 * @return This is somewhat tricky. Let <code>res</code> be the return value.
277 * If the return value is negative, then <code>elm</code> was not in the array
278 * but <code>-res - 1</code> gives the proper location where it should be inserted.
279 * If <code>res >= 0</code> then the element is in the array and <code>res</code>
280 * represents its index.
281 * That allows for testing membership and finding proper insertion indices.
282 * @note The differences to bsearch(3) which does not give proper insert locations
283 * in the case that the element is not conatined in the array.
286 ir_arr_bsearch(const void *arr, size_t elm_size, ir_arr_cmp_func_t *cmp, const void *elm)
288 int hi = ARR_LEN(arr);
292 int md = lo + ((hi - lo) >> 1);
293 int res = cmp((char *) arr + md * elm_size, elm);
305 #define ARR_SET_INSERT(arr, cmp, elm) \
307 int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
310 memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (ARR_DESCR((arr))->nelts - idx)); \
311 (arr)[idx] = *(elm); \
312 ++ARR_DESCR((arr))->nelts; \
316 #define ARR_SET_INSERT_EXT(type, arr, cmp, elm) \
318 int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
320 int len = ARR_LEN(arr); \
322 ARR_EXTO(type, arr, len + 1); \
323 memmove(&(arr)[idx+1], &(arr)[idx], sizeof((arr)[0]) * (len - idx)); \
324 (arr)[idx] = *(elm); \
328 #define ARR_SET_REMOVE(arr, cmp, elm) \
330 int idx = ir_arr_bsearch((arr), sizeof((arr)[0]), (cmp), (elm)); \
332 --ARR_DESCR((arr))->nelts; \
333 memmove(&(arr)[idx], &(arr)[idx+1], sizeof((arr)[0]) * (ARR_DESCR((arr))->nelts - idx)); \
338 * Return the index of an element in an array set.
339 * To check for containment, use the expression:
340 * (ARR_SET_GET_IDX(arr, cmp, elm) >= 0)
342 * @return The index or some value < 0 if the element was not in the set.
344 #define ARR_SET_GET_IDX(arr, cmp, elm) \
345 (ARR_VRFY((arr)), ir_arr_bsearch((arr), sizeof((arr)[0]), cmp, (elm)))
348 #define ARR_SET_GET(arr, cmp, elm) \
349 ({ int idx = ARR_SET_GET_IDX(arr, cmp, elm); idx >= 0 ? &(arr)[idx] : NULL; })
351 #define ARR_SET_GET(arr, cmp, elm) \
352 (ARR_SET_GET_IDX(arr, cmp, elm) >= 0 ? &(arr)[ARR_SET_GET_IDX(arr, cmp, elm)] : NULL)
356 #define ARR_SET_CONTAINS(arr, cmp, elm) \
357 (ARR_SET_GET_IDX((arr), (cmp), (elm)) >= 0)
360 * Reset the array set.
361 * This just initializes the size to zero but does not wipe out any element.
363 #define ARR_SET_CLEAR(arr) ARR_SHRINKLEN(arr, 0)
365 #endif /* FIRM_ADT_ARRAY_H */