Compose: add iterator API
[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 /* Originally taken from: https://ccodearchive.net/info/darray.html
27  * But modified for libxkbcommon. */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <limits.h>
33
34 #define darray(type) struct { type *item; unsigned size; unsigned alloc; }
35
36 #define darray_new() { 0, 0, 0 }
37
38 #define darray_init(arr) do { \
39     (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \
40 } while (0)
41
42 #define darray_free(arr) do { \
43     free((arr).item); \
44     darray_init(arr); \
45 } while (0)
46
47 #define darray_steal(arr, to, to_size) do { \
48     *(to) = (arr).item; \
49     if (to_size) \
50         *(unsigned int *) (to_size) = (arr).size; \
51     darray_init(arr); \
52 } while (0)
53
54 /*
55  * Typedefs for darrays of common types.  These are useful
56  * when you want to pass a pointer to an darray(T) around.
57  *
58  * The following will produce an incompatible pointer warning:
59  *
60  *     void foo(darray(int) *arr);
61  *     darray(int) arr = darray_new();
62  *     foo(&arr);
63  *
64  * The workaround:
65  *
66  *     void foo(darray_int *arr);
67  *     darray_int arr = darray_new();
68  *     foo(&arr);
69  */
70
71 typedef darray (char)           darray_char;
72 typedef darray (signed char)    darray_schar;
73 typedef darray (unsigned char)  darray_uchar;
74
75 typedef darray (short)          darray_short;
76 typedef darray (int)            darray_int;
77 typedef darray (long)           darray_long;
78
79 typedef darray (unsigned short) darray_ushort;
80 typedef darray (unsigned int)   darray_uint;
81 typedef darray (unsigned long)  darray_ulong;
82
83 /*** Access ***/
84
85 #define darray_item(arr, i)     ((arr).item[i])
86 #define darray_size(arr)        ((arr).size)
87 #define darray_empty(arr)       ((arr).size == 0)
88
89 /*** Insertion (single item) ***/
90
91 #define darray_append(arr, ...)  do { \
92     darray_resize(arr, (arr).size + 1); \
93     (arr).item[(arr).size - 1] = (__VA_ARGS__); \
94 } while (0)
95
96 /*** Insertion (multiple items) ***/
97
98 #define darray_append_items(arr, items, count) do { \
99     unsigned __count = (count), __oldSize = (arr).size; \
100     darray_resize(arr, __oldSize + __count); \
101     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
102 } while (0)
103
104 #define darray_from_items(arr, items, count) do { \
105     unsigned __count = (count); \
106     darray_resize(arr, __count); \
107     if (__count != 0) \
108         memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
109 } while (0)
110
111 #define darray_copy(arr_to, arr_from) \
112     darray_from_items((arr_to), (arr_from).item, (arr_from).size)
113
114 #define darray_concat(arr_to, arr_from) \
115     darray_append_items((arr_to), (arr_from).item, (arr_from).size)
116
117 /*** Removal ***/
118
119 /* Warning: Do not call darray_remove_last on an empty darray. */
120 #define darray_remove_last(arr) (--(arr).size)
121
122 /*** String buffer ***/
123
124 #define darray_append_string(arr, str) do { \
125     const char *__str = (str); \
126     darray_append_items(arr, __str, strlen(__str) + 1); \
127     (arr).size--; \
128 } while (0)
129
130 #define darray_append_lit(arr, stringLiteral) do { \
131     darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
132     (arr).size--; \
133 } while (0)
134
135 #define darray_appends_nullterminate(arr, items, count) do { \
136     unsigned __count = (count), __oldSize = (arr).size; \
137     darray_resize(arr, __oldSize + __count + 1); \
138     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
139     (arr).item[--(arr).size] = 0; \
140 } while (0)
141
142 #define darray_prepends_nullterminate(arr, items, count) do { \
143     unsigned __count = (count), __oldSize = (arr).size; \
144     darray_resize(arr, __count + __oldSize + 1); \
145     memmove((arr).item + __count, (arr).item, \
146             __oldSize * sizeof(*(arr).item)); \
147     memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
148     (arr).item[--(arr).size] = 0; \
149 } while (0)
150
151 /*** Size management ***/
152
153 #define darray_resize(arr, newSize) \
154     darray_growalloc(arr, (arr).size = (newSize))
155
156 #define darray_resize0(arr, newSize) do { \
157     unsigned __oldSize = (arr).size, __newSize = (newSize); \
158     (arr).size = __newSize; \
159     if (__newSize > __oldSize) { \
160         darray_growalloc(arr, __newSize); \
161         memset(&(arr).item[__oldSize], 0, \
162                (__newSize - __oldSize) * sizeof(*(arr).item)); \
163     } \
164 } while (0)
165
166 #define darray_realloc(arr, newAlloc) do { \
167     (arr).item = realloc((arr).item, \
168                          ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
169 } while (0)
170
171 #define darray_growalloc(arr, need)   do { \
172     unsigned __need = (need); \
173     if (__need > (arr).alloc) \
174         darray_realloc(arr, darray_next_alloc((arr).alloc, __need, \
175                                               sizeof(*(arr).item))); \
176 } while (0)
177
178 #define darray_shrink(arr) do { \
179     if ((arr).size > 0) \
180         (arr).item = realloc((arr).item, \
181                              ((arr).alloc = (arr).size) * sizeof(*(arr).item)); \
182 } while (0)
183
184 static inline unsigned
185 darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize)
186 {
187     assert(need < UINT_MAX / itemSize / 2); /* Overflow. */
188     if (alloc == 0)
189         alloc = 4;
190     while (alloc < need)
191         alloc *= 2;
192     return alloc;
193 }
194
195 /*** Traversal ***/
196
197 #define darray_foreach(i, arr) \
198     for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
199
200 #define darray_foreach_from(i, arr, from) \
201     for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
202
203 /* Iterate on index and value at the same time, like Python's enumerate. */
204 #define darray_enumerate(idx, val, arr) \
205     for ((idx) = 0, (val) = &(arr).item[0]; \
206          (idx) < (arr).size; \
207          (idx)++, (val)++)
208
209 #define darray_enumerate_from(idx, val, arr, from) \
210     for ((idx) = (from), (val) = &(arr).item[0]; \
211          (idx) < (arr).size; \
212          (idx)++, (val)++)
213
214 #define darray_foreach_reverse(i, arr) \
215     for ((i) = &(arr).item[(arr).size - 1]; (arr).size > 0 && (i) >= &(arr).item[0]; (i)--)
216
217 #endif /* CCAN_DARRAY_H */