parser, symbols: drop unnecessary casts
[platform/upstream/libxkbcommon.git] / src / darray.h
1 /*
2  * Copyright (C) 2011 Joseph Adams <joeyadams3.14159@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22
23 #ifndef CCAN_DARRAY_H
24 #define CCAN_DARRAY_H
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 /*
30  * SYNOPSIS
31  *
32  * Life cycle of a darray (dynamically-allocated array):
33  *
34  *     darray(int) a = darray_new();
35  *     darray_free(a);
36  *
37  *     struct {darray(int) a;} foo;
38  *     darray_init(foo.a);
39  *     darray_free(foo.a);
40  *
41  * Typedefs for darrays of common types:
42  *
43  *     darray_char, darray_schar, darray_uchar
44  *     darray_short, darray_int, darray_long
45  *     darray_ushort, darray_uint, darray_ulong
46  *
47  * Access:
48  *
49  *     T      darray_item(darray(T) arr, size_t index);
50  *     size_t darray_size(darray(T) arr);
51  *     size_t darray_alloc(darray(T) arr);
52  *     bool   darray_empty(darray(T) arr);
53  *
54  *     // Access raw memory, starting from the item in offset.
55  *     // Not safe, be careful, etc.
56  *     T*     darray_mem(darray(T) arr, size_t offset);
57  *
58  * Insertion (single item):
59  *
60  *     void   darray_append(darray(T) arr, T item);
61  *     void   darray_prepend(darray(T) arr, T item);
62  *     void   darray_push(darray(T) arr, T item); // same as darray_append
63  *
64  * Insertion (multiple items):
65  *
66  *     void   darray_append_items(darray(T) arr, T *items, size_t count);
67  *     void   darray_prepend_items(darray(T) arr, T *items, size_t count);
68  *
69  *     void   darray_appends(darray(T) arr, [T item, [...]]);
70  *     void   darray_prepends(darray(T) arr, [T item, [...]]);
71  *
72  * Removal:
73  *
74  *     T      darray_pop(darray(T) arr | darray_size(arr) != 0);
75  *     T*     darray_pop_check(darray(T*) arr);
76  *
77  * Replacement:
78  *
79  *     void   darray_from_items(darray(T) arr, T *items, size_t count);
80  *     void   darray_from_c(darray(T) arr, T c_array[N]);
81  *
82  * String buffer:
83  *
84  *     void   darray_append_string(darray(char) arr, const char *str);
85  *     void   darray_append_lit(darray(char) arr, char stringLiteral[N+1]);
86  *
87  *     void   darray_prepend_string(darray(char) arr, const char *str);
88  *     void   darray_prepend_lit(darray(char) arr, char stringLiteral[N+1]);
89  *
90  *     void   darray_from_string(darray(T) arr, const char *str);
91  *     void   darray_from_lit(darray(char) arr, char stringLiteral[N+1]);
92  *
93  * Size management:
94  *
95  *     void   darray_resize(darray(T) arr, size_t newSize);
96  *     void   darray_resize0(darray(T) arr, size_t newSize);
97  *
98  *     void   darray_realloc(darray(T) arr, size_t newAlloc);
99  *     void   darray_growalloc(darray(T) arr, size_t newAlloc);
100  *
101  * Traversal:
102  *
103  *     darray_foreach(T *&i, darray(T) arr) {...}
104  *     darray_foreach_reverse(T *&i, darray(T) arr) {...}
105  *
106  * Except for darray_foreach and darray_foreach_reverse,
107  * all macros evaluate their non-darray arguments only once.
108  */
109
110 /*** Life cycle ***/
111
112 #define darray(type) struct { type *item; size_t size; size_t alloc; }
113
114 #define darray_new() { 0, 0, 0 }
115
116 #define darray_init(arr) do { \
117     (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \
118 } while (0)
119
120 #define darray_free(arr) do { \
121     free((arr).item); darray_init(arr); \
122 } while (0)
123
124 /*
125  * Typedefs for darrays of common types.  These are useful
126  * when you want to pass a pointer to an darray(T) around.
127  *
128  * The following will produce an incompatible pointer warning:
129  *
130  *     void foo(darray(int) *arr);
131  *     darray(int) arr = darray_new();
132  *     foo(&arr);
133  *
134  * The workaround:
135  *
136  *     void foo(darray_int *arr);
137  *     darray_int arr = darray_new();
138  *     foo(&arr);
139  */
140
141 typedef darray (char)           darray_char;
142 typedef darray (signed char)    darray_schar;
143 typedef darray (unsigned char)  darray_uchar;
144
145 typedef darray (short)          darray_short;
146 typedef darray (int)            darray_int;
147 typedef darray (long)           darray_long;
148
149 typedef darray (unsigned short) darray_ushort;
150 typedef darray (unsigned int)   darray_uint;
151 typedef darray (unsigned long)  darray_ulong;
152
153 /*** Access ***/
154
155 #define darray_item(arr, i)     ((arr).item[i])
156 #define darray_size(arr)        ((arr).size)
157 #define darray_alloc(arr)       ((arr).alloc)
158 #define darray_empty(arr)       ((arr).size == 0)
159
160 #define darray_mem(arr, offset) ((arr).item + (offset))
161 #define darray_same(arr1, arr2) ((arr1).item == (arr2).item)
162
163 /*** Insertion (single item) ***/
164
165 #define darray_append(arr, ...)  do { \
166     darray_resize(arr, (arr).size + 1); \
167     (arr).item[(arr).size - 1] = (__VA_ARGS__); \
168 } while (0)
169
170 #define darray_prepend(arr, ...) do { \
171     darray_resize(arr, (arr).size + 1); \
172     memmove((arr).item + 1, (arr).item, \
173             ((arr).size - 1) * sizeof(*(arr).item)); \
174     (arr).item[0] = (__VA_ARGS__); \
175 } while (0)
176
177 #define darray_push(arr, ...) darray_append(arr, __VA_ARGS__)
178
179 /*** Insertion (multiple items) ***/
180
181 #define darray_append_items(arr, items, count) do { \
182     size_t __count = (count), __oldSize = (arr).size; \
183     darray_resize(arr, __oldSize + __count); \
184     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
185 } while (0)
186
187 #define darray_prepend_items(arr, items, count) do { \
188     size_t __count = (count), __oldSize = (arr).size; \
189     darray_resize(arr, __count + __oldSize); \
190     memmove((arr).item + __count, (arr).item, \
191             __oldSize * sizeof(*(arr).item)); \
192     memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
193 } while (0)
194
195 #define darray_append_items_nullterminate(arr, items, count) do { \
196     size_t __count = (count), __oldSize = (arr).size; \
197     darray_resize(arr, __oldSize + __count + 1); \
198     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
199     (arr).item[--(arr).size] = 0; \
200 } while (0)
201
202 #define darray_prepend_items_nullterminate(arr, items, count) do { \
203     size_t __count = (count), __oldSize = (arr).size; \
204     darray_resize(arr, __count + __oldSize + 1); \
205     memmove((arr).item + __count, (arr).item, \
206             __oldSize * sizeof(*(arr).item)); \
207     memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
208     (arr).item[--(arr).size] = 0; \
209 } while (0)
210
211 #define darray_appends_t(arr, type, ...)  do { \
212     type __src[] = { __VA_ARGS__ }; \
213     darray_append_items(arr, __src, sizeof(__src) / sizeof(*__src)); \
214 } while (0)
215
216 #define darray_prepends_t(arr, type, ...) do { \
217     type __src[] = { __VA_ARGS__ }; \
218     darray_prepend_items(arr, __src, sizeof(__src) / sizeof(*__src)); \
219 } while (0)
220
221 /*** Removal ***/
222
223 /* Warning: Do not call darray_pop on an empty darray. */
224 #define darray_pop(arr) ((arr).item[--(arr).size])
225 #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
226
227 /*** Replacement ***/
228
229 #define darray_from_items(arr, items, count) do { \
230     size_t __count = (count); \
231     darray_resize(arr, __count); \
232     memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
233 } while (0)
234
235 #define darray_from_c(arr, c_array) \
236     darray_from_items(arr, c_array, sizeof(c_array) / sizeof(*(c_array)))
237
238 #define darray_copy(arr_to, arr_from) \
239     darray_from_items((arr_to), (arr_from).item, (arr_from).size)
240
241 /*** String buffer ***/
242
243 #define darray_append_string(arr, str) do { \
244     const char *__str = (str); \
245     darray_append_items(arr, __str, strlen(__str) + 1); \
246     (arr).size--; \
247 } while (0)
248
249 #define darray_append_lit(arr, stringLiteral) do { \
250     darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
251     (arr).size--; \
252 } while (0)
253
254 #define darray_prepend_string(arr, str) do { \
255     const char *__str = (str); \
256     darray_prepend_items_nullterminate(arr, __str, strlen(__str)); \
257 } while (0)
258
259 #define darray_prepend_lit(arr, stringLiteral) \
260     darray_prepend_items_nullterminate(arr, stringLiteral, \
261                                        sizeof(stringLiteral) - 1)
262
263 #define darray_from_string(arr, str) do { \
264     const char *__str = (str); \
265     darray_from_items(arr, __str, strlen(__str) + 1); \
266     (arr).size--; \
267 } while (0)
268
269 #define darray_from_lit(arr, stringLiteral) do { \
270     darray_from_items(arr, stringLiteral, sizeof(stringLiteral)); \
271     (arr).size--; \
272 } while (0)
273
274 /*** Size management ***/
275
276 #define darray_resize(arr, newSize) \
277     darray_growalloc(arr, (arr).size = (newSize))
278
279 #define darray_resize0(arr, newSize) do { \
280     size_t __oldSize = (arr).size, __newSize = (newSize); \
281     (arr).size = __newSize; \
282     if (__newSize > __oldSize) { \
283         darray_growalloc(arr, __newSize); \
284         memset(&(arr).item[__oldSize], 0, \
285                (__newSize - __oldSize) * sizeof(*(arr).item)); \
286     } \
287 } while (0)
288
289 #define darray_realloc(arr, newAlloc) do { \
290     (arr).item = realloc((arr).item, \
291                          ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
292 } while (0)
293
294 #define darray_growalloc(arr, need)   do { \
295     size_t __need = (need); \
296     if (__need > (arr).alloc) \
297     darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
298 } while (0)
299
300 static inline size_t
301 darray_next_alloc(size_t alloc, size_t need)
302 {
303     if (alloc == 0)
304         alloc = 4;
305     while (alloc < need)
306         alloc *= 2;
307     return alloc;
308 }
309
310 /*** Traversal ***/
311
312 /*
313  * darray_foreach(T *&i, darray(T) arr) {...}
314  *
315  * Traverse a darray.  `i` must be declared in advance as a pointer to an item.
316  */
317 #define darray_foreach(i, arr) \
318     for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
319
320 #define darray_foreach_from(i, arr, from) \
321     for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
322
323 /* Iterate on index and value at the same time, like Python's enumerate. */
324 #define darray_enumerate(idx, val, arr) \
325     for ((idx) = 0, (val) = &(arr).item[0]; \
326          (idx) < (arr).size; \
327          (idx)++, (val)++)
328
329 #define darray_enumerate_from(idx, val, arr, from) \
330     for ((idx) = (from), (val) = &(arr).item[0]; \
331          (idx) < (arr).size; \
332          (idx)++, (val)++)
333
334 /*
335  * darray_foreach_reverse(T *&i, darray(T) arr) {...}
336  *
337  * Like darray_foreach, but traverse in reverse order.
338  */
339 #define darray_foreach_reverse(i, arr) \
340     for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
341
342 #endif /* CCAN_DARRAY_H */
343
344 /*
345  *
346  * darray_growalloc(arr, newAlloc) sees if the darray can currently hold newAlloc items;
347  *      if not, it increases the alloc to satisfy this requirement, allocating slack
348  *      space to avoid having to reallocate for every size increment.
349  *
350  * darray_from_string(arr, str) copies a string to an darray_char.
351  *
352  * darray_push(arr, item) pushes an item to the end of the darray.
353  * darray_pop(arr) pops it back out.  Be sure there is at least one item in the darray before calling.
354  * darray_pop_check(arr) does the same as darray_pop, but returns NULL if there are no more items left in the darray.
355  *
356  * darray_make_room(arr, room) ensures there's 'room' elements of space after the end of the darray, and it returns a pointer to this space.
357  * Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function.
358  *
359  * The following require HAVE_TYPEOF==1 :
360  *
361  * darray_appends(arr, item0, item1...) appends a collection of comma-delimited items to the darray.
362  * darray_prepends(arr, item0, item1...) prepends a collection of comma-delimited items to the darray.\
363  *
364  *
365  * Examples:
366  *
367  *      darray(int)  arr;
368  *      int        *i;
369  *
370  *      darray_appends(arr, 0,1,2,3,4);
371  *      darray_appends(arr, -5,-4,-3,-2,-1);
372  *      darray_foreach(i, arr)
373  *              printf("%d ", *i);
374  *      printf("\n");
375  *
376  *      darray_free(arr);
377  *
378  *
379  *      typedef struct {int n,d;} Fraction;
380  *      darray(Fraction) fractions;
381  *      Fraction        *i;
382  *
383  *      darray_appends(fractions, {3,4}, {3,5}, {2,1});
384  *      darray_foreach(i, fractions)
385  *              printf("%d/%d\n", i->n, i->d);
386  *
387  *      darray_free(fractions);
388  */