xref: /aosp_15_r20/external/libxkbcommon/src/darray.h (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright (C) 2011 Joseph Adams <[email protected]>
3*2b949d04SAndroid Build Coastguard Worker  *
4*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a copy
5*2b949d04SAndroid Build Coastguard Worker  * of this software and associated documentation files (the "Software"), to deal
6*2b949d04SAndroid Build Coastguard Worker  * in the Software without restriction, including without limitation the rights
7*2b949d04SAndroid Build Coastguard Worker  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8*2b949d04SAndroid Build Coastguard Worker  * copies of the Software, and to permit persons to whom the Software is
9*2b949d04SAndroid Build Coastguard Worker  * furnished to do so, subject to the following conditions:
10*2b949d04SAndroid Build Coastguard Worker  *
11*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be included in
12*2b949d04SAndroid Build Coastguard Worker  * all copies or substantial portions of the Software.
13*2b949d04SAndroid Build Coastguard Worker  *
14*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17*2b949d04SAndroid Build Coastguard Worker  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19*2b949d04SAndroid Build Coastguard Worker  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE.
21*2b949d04SAndroid Build Coastguard Worker  */
22*2b949d04SAndroid Build Coastguard Worker 
23*2b949d04SAndroid Build Coastguard Worker #ifndef CCAN_DARRAY_H
24*2b949d04SAndroid Build Coastguard Worker #define CCAN_DARRAY_H
25*2b949d04SAndroid Build Coastguard Worker 
26*2b949d04SAndroid Build Coastguard Worker /* Originally taken from: https://ccodearchive.net/info/darray.html
27*2b949d04SAndroid Build Coastguard Worker  * But modified for libxkbcommon. */
28*2b949d04SAndroid Build Coastguard Worker 
29*2b949d04SAndroid Build Coastguard Worker #include <stdlib.h>
30*2b949d04SAndroid Build Coastguard Worker #include <string.h>
31*2b949d04SAndroid Build Coastguard Worker #include <assert.h>
32*2b949d04SAndroid Build Coastguard Worker #include <limits.h>
33*2b949d04SAndroid Build Coastguard Worker 
34*2b949d04SAndroid Build Coastguard Worker #define darray(type) struct { type *item; unsigned size; unsigned alloc; }
35*2b949d04SAndroid Build Coastguard Worker 
36*2b949d04SAndroid Build Coastguard Worker #define darray_new() { 0, 0, 0 }
37*2b949d04SAndroid Build Coastguard Worker 
38*2b949d04SAndroid Build Coastguard Worker #define darray_init(arr) do { \
39*2b949d04SAndroid Build Coastguard Worker     (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \
40*2b949d04SAndroid Build Coastguard Worker } while (0)
41*2b949d04SAndroid Build Coastguard Worker 
42*2b949d04SAndroid Build Coastguard Worker #define darray_free(arr) do { \
43*2b949d04SAndroid Build Coastguard Worker     free((arr).item); \
44*2b949d04SAndroid Build Coastguard Worker     darray_init(arr); \
45*2b949d04SAndroid Build Coastguard Worker } while (0)
46*2b949d04SAndroid Build Coastguard Worker 
47*2b949d04SAndroid Build Coastguard Worker #define darray_steal(arr, to, to_size) do { \
48*2b949d04SAndroid Build Coastguard Worker     *(to) = (arr).item; \
49*2b949d04SAndroid Build Coastguard Worker     if (to_size) \
50*2b949d04SAndroid Build Coastguard Worker         *(unsigned int *) (to_size) = (arr).size; \
51*2b949d04SAndroid Build Coastguard Worker     darray_init(arr); \
52*2b949d04SAndroid Build Coastguard Worker } while (0)
53*2b949d04SAndroid Build Coastguard Worker 
54*2b949d04SAndroid Build Coastguard Worker /*
55*2b949d04SAndroid Build Coastguard Worker  * Typedefs for darrays of common types.  These are useful
56*2b949d04SAndroid Build Coastguard Worker  * when you want to pass a pointer to an darray(T) around.
57*2b949d04SAndroid Build Coastguard Worker  *
58*2b949d04SAndroid Build Coastguard Worker  * The following will produce an incompatible pointer warning:
59*2b949d04SAndroid Build Coastguard Worker  *
60*2b949d04SAndroid Build Coastguard Worker  *     void foo(darray(int) *arr);
61*2b949d04SAndroid Build Coastguard Worker  *     darray(int) arr = darray_new();
62*2b949d04SAndroid Build Coastguard Worker  *     foo(&arr);
63*2b949d04SAndroid Build Coastguard Worker  *
64*2b949d04SAndroid Build Coastguard Worker  * The workaround:
65*2b949d04SAndroid Build Coastguard Worker  *
66*2b949d04SAndroid Build Coastguard Worker  *     void foo(darray_int *arr);
67*2b949d04SAndroid Build Coastguard Worker  *     darray_int arr = darray_new();
68*2b949d04SAndroid Build Coastguard Worker  *     foo(&arr);
69*2b949d04SAndroid Build Coastguard Worker  */
70*2b949d04SAndroid Build Coastguard Worker 
71*2b949d04SAndroid Build Coastguard Worker typedef darray (char)           darray_char;
72*2b949d04SAndroid Build Coastguard Worker typedef darray (signed char)    darray_schar;
73*2b949d04SAndroid Build Coastguard Worker typedef darray (unsigned char)  darray_uchar;
74*2b949d04SAndroid Build Coastguard Worker 
75*2b949d04SAndroid Build Coastguard Worker typedef darray (short)          darray_short;
76*2b949d04SAndroid Build Coastguard Worker typedef darray (int)            darray_int;
77*2b949d04SAndroid Build Coastguard Worker typedef darray (long)           darray_long;
78*2b949d04SAndroid Build Coastguard Worker 
79*2b949d04SAndroid Build Coastguard Worker typedef darray (unsigned short) darray_ushort;
80*2b949d04SAndroid Build Coastguard Worker typedef darray (unsigned int)   darray_uint;
81*2b949d04SAndroid Build Coastguard Worker typedef darray (unsigned long)  darray_ulong;
82*2b949d04SAndroid Build Coastguard Worker 
83*2b949d04SAndroid Build Coastguard Worker /*** Access ***/
84*2b949d04SAndroid Build Coastguard Worker 
85*2b949d04SAndroid Build Coastguard Worker #define darray_item(arr, i)     ((arr).item[i])
86*2b949d04SAndroid Build Coastguard Worker #define darray_size(arr)        ((arr).size)
87*2b949d04SAndroid Build Coastguard Worker #define darray_empty(arr)       ((arr).size == 0)
88*2b949d04SAndroid Build Coastguard Worker 
89*2b949d04SAndroid Build Coastguard Worker /*** Insertion (single item) ***/
90*2b949d04SAndroid Build Coastguard Worker 
91*2b949d04SAndroid Build Coastguard Worker #define darray_append(arr, ...)  do { \
92*2b949d04SAndroid Build Coastguard Worker     darray_resize(arr, (arr).size + 1); \
93*2b949d04SAndroid Build Coastguard Worker     (arr).item[(arr).size - 1] = (__VA_ARGS__); \
94*2b949d04SAndroid Build Coastguard Worker } while (0)
95*2b949d04SAndroid Build Coastguard Worker 
96*2b949d04SAndroid Build Coastguard Worker /*** Insertion (multiple items) ***/
97*2b949d04SAndroid Build Coastguard Worker 
98*2b949d04SAndroid Build Coastguard Worker #define darray_append_items(arr, items, count) do { \
99*2b949d04SAndroid Build Coastguard Worker     unsigned __count = (count), __oldSize = (arr).size; \
100*2b949d04SAndroid Build Coastguard Worker     darray_resize(arr, __oldSize + __count); \
101*2b949d04SAndroid Build Coastguard Worker     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
102*2b949d04SAndroid Build Coastguard Worker } while (0)
103*2b949d04SAndroid Build Coastguard Worker 
104*2b949d04SAndroid Build Coastguard Worker #define darray_from_items(arr, items, count) do { \
105*2b949d04SAndroid Build Coastguard Worker     unsigned __count = (count); \
106*2b949d04SAndroid Build Coastguard Worker     darray_resize(arr, __count); \
107*2b949d04SAndroid Build Coastguard Worker     if (__count != 0) \
108*2b949d04SAndroid Build Coastguard Worker         memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
109*2b949d04SAndroid Build Coastguard Worker } while (0)
110*2b949d04SAndroid Build Coastguard Worker 
111*2b949d04SAndroid Build Coastguard Worker #define darray_copy(arr_to, arr_from) \
112*2b949d04SAndroid Build Coastguard Worker     darray_from_items((arr_to), (arr_from).item, (arr_from).size)
113*2b949d04SAndroid Build Coastguard Worker 
114*2b949d04SAndroid Build Coastguard Worker #define darray_concat(arr_to, arr_from) \
115*2b949d04SAndroid Build Coastguard Worker     darray_append_items((arr_to), (arr_from).item, (arr_from).size)
116*2b949d04SAndroid Build Coastguard Worker 
117*2b949d04SAndroid Build Coastguard Worker /*** String buffer ***/
118*2b949d04SAndroid Build Coastguard Worker 
119*2b949d04SAndroid Build Coastguard Worker #define darray_append_string(arr, str) do { \
120*2b949d04SAndroid Build Coastguard Worker     const char *__str = (str); \
121*2b949d04SAndroid Build Coastguard Worker     darray_append_items(arr, __str, strlen(__str) + 1); \
122*2b949d04SAndroid Build Coastguard Worker     (arr).size--; \
123*2b949d04SAndroid Build Coastguard Worker } while (0)
124*2b949d04SAndroid Build Coastguard Worker 
125*2b949d04SAndroid Build Coastguard Worker #define darray_append_lit(arr, stringLiteral) do { \
126*2b949d04SAndroid Build Coastguard Worker     darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
127*2b949d04SAndroid Build Coastguard Worker     (arr).size--; \
128*2b949d04SAndroid Build Coastguard Worker } while (0)
129*2b949d04SAndroid Build Coastguard Worker 
130*2b949d04SAndroid Build Coastguard Worker #define darray_appends_nullterminate(arr, items, count) do { \
131*2b949d04SAndroid Build Coastguard Worker     unsigned __count = (count), __oldSize = (arr).size; \
132*2b949d04SAndroid Build Coastguard Worker     darray_resize(arr, __oldSize + __count + 1); \
133*2b949d04SAndroid Build Coastguard Worker     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
134*2b949d04SAndroid Build Coastguard Worker     (arr).item[--(arr).size] = 0; \
135*2b949d04SAndroid Build Coastguard Worker } while (0)
136*2b949d04SAndroid Build Coastguard Worker 
137*2b949d04SAndroid Build Coastguard Worker #define darray_prepends_nullterminate(arr, items, count) do { \
138*2b949d04SAndroid Build Coastguard Worker     unsigned __count = (count), __oldSize = (arr).size; \
139*2b949d04SAndroid Build Coastguard Worker     darray_resize(arr, __count + __oldSize + 1); \
140*2b949d04SAndroid Build Coastguard Worker     memmove((arr).item + __count, (arr).item, \
141*2b949d04SAndroid Build Coastguard Worker             __oldSize * sizeof(*(arr).item)); \
142*2b949d04SAndroid Build Coastguard Worker     memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
143*2b949d04SAndroid Build Coastguard Worker     (arr).item[--(arr).size] = 0; \
144*2b949d04SAndroid Build Coastguard Worker } while (0)
145*2b949d04SAndroid Build Coastguard Worker 
146*2b949d04SAndroid Build Coastguard Worker /*** Size management ***/
147*2b949d04SAndroid Build Coastguard Worker 
148*2b949d04SAndroid Build Coastguard Worker #define darray_resize(arr, newSize) \
149*2b949d04SAndroid Build Coastguard Worker     darray_growalloc(arr, (arr).size = (newSize))
150*2b949d04SAndroid Build Coastguard Worker 
151*2b949d04SAndroid Build Coastguard Worker #define darray_resize0(arr, newSize) do { \
152*2b949d04SAndroid Build Coastguard Worker     unsigned __oldSize = (arr).size, __newSize = (newSize); \
153*2b949d04SAndroid Build Coastguard Worker     (arr).size = __newSize; \
154*2b949d04SAndroid Build Coastguard Worker     if (__newSize > __oldSize) { \
155*2b949d04SAndroid Build Coastguard Worker         darray_growalloc(arr, __newSize); \
156*2b949d04SAndroid Build Coastguard Worker         memset(&(arr).item[__oldSize], 0, \
157*2b949d04SAndroid Build Coastguard Worker                (__newSize - __oldSize) * sizeof(*(arr).item)); \
158*2b949d04SAndroid Build Coastguard Worker     } \
159*2b949d04SAndroid Build Coastguard Worker } while (0)
160*2b949d04SAndroid Build Coastguard Worker 
161*2b949d04SAndroid Build Coastguard Worker #define darray_realloc(arr, newAlloc) do { \
162*2b949d04SAndroid Build Coastguard Worker     (arr).item = realloc((arr).item, \
163*2b949d04SAndroid Build Coastguard Worker                          ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
164*2b949d04SAndroid Build Coastguard Worker } while (0)
165*2b949d04SAndroid Build Coastguard Worker 
166*2b949d04SAndroid Build Coastguard Worker #define darray_growalloc(arr, need)   do { \
167*2b949d04SAndroid Build Coastguard Worker     unsigned __need = (need); \
168*2b949d04SAndroid Build Coastguard Worker     if (__need > (arr).alloc) \
169*2b949d04SAndroid Build Coastguard Worker         darray_realloc(arr, darray_next_alloc((arr).alloc, __need, \
170*2b949d04SAndroid Build Coastguard Worker                                               sizeof(*(arr).item))); \
171*2b949d04SAndroid Build Coastguard Worker } while (0)
172*2b949d04SAndroid Build Coastguard Worker 
173*2b949d04SAndroid Build Coastguard Worker #define darray_shrink(arr) do { \
174*2b949d04SAndroid Build Coastguard Worker     if ((arr).size > 0) \
175*2b949d04SAndroid Build Coastguard Worker         (arr).item = realloc((arr).item, \
176*2b949d04SAndroid Build Coastguard Worker                              ((arr).alloc = (arr).size) * sizeof(*(arr).item)); \
177*2b949d04SAndroid Build Coastguard Worker } while (0)
178*2b949d04SAndroid Build Coastguard Worker 
179*2b949d04SAndroid Build Coastguard Worker static inline unsigned
darray_next_alloc(unsigned alloc,unsigned need,unsigned itemSize)180*2b949d04SAndroid Build Coastguard Worker darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize)
181*2b949d04SAndroid Build Coastguard Worker {
182*2b949d04SAndroid Build Coastguard Worker     assert(need < UINT_MAX / itemSize / 2); /* Overflow. */
183*2b949d04SAndroid Build Coastguard Worker     if (alloc == 0)
184*2b949d04SAndroid Build Coastguard Worker         alloc = 4;
185*2b949d04SAndroid Build Coastguard Worker     while (alloc < need)
186*2b949d04SAndroid Build Coastguard Worker         alloc *= 2;
187*2b949d04SAndroid Build Coastguard Worker     return alloc;
188*2b949d04SAndroid Build Coastguard Worker }
189*2b949d04SAndroid Build Coastguard Worker 
190*2b949d04SAndroid Build Coastguard Worker /*** Traversal ***/
191*2b949d04SAndroid Build Coastguard Worker 
192*2b949d04SAndroid Build Coastguard Worker #define darray_foreach(i, arr) \
193*2b949d04SAndroid Build Coastguard Worker     for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
194*2b949d04SAndroid Build Coastguard Worker 
195*2b949d04SAndroid Build Coastguard Worker #define darray_foreach_from(i, arr, from) \
196*2b949d04SAndroid Build Coastguard Worker     for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
197*2b949d04SAndroid Build Coastguard Worker 
198*2b949d04SAndroid Build Coastguard Worker /* Iterate on index and value at the same time, like Python's enumerate. */
199*2b949d04SAndroid Build Coastguard Worker #define darray_enumerate(idx, val, arr) \
200*2b949d04SAndroid Build Coastguard Worker     for ((idx) = 0, (val) = &(arr).item[0]; \
201*2b949d04SAndroid Build Coastguard Worker          (idx) < (arr).size; \
202*2b949d04SAndroid Build Coastguard Worker          (idx)++, (val)++)
203*2b949d04SAndroid Build Coastguard Worker 
204*2b949d04SAndroid Build Coastguard Worker #define darray_enumerate_from(idx, val, arr, from) \
205*2b949d04SAndroid Build Coastguard Worker     for ((idx) = (from), (val) = &(arr).item[0]; \
206*2b949d04SAndroid Build Coastguard Worker          (idx) < (arr).size; \
207*2b949d04SAndroid Build Coastguard Worker          (idx)++, (val)++)
208*2b949d04SAndroid Build Coastguard Worker 
209*2b949d04SAndroid Build Coastguard Worker #define darray_foreach_reverse(i, arr) \
210*2b949d04SAndroid Build Coastguard Worker     for ((i) = &(arr).item[(arr).size - 1]; (arr).size > 0 && (i) >= &(arr).item[0]; (i)--)
211*2b949d04SAndroid Build Coastguard Worker 
212*2b949d04SAndroid Build Coastguard Worker #endif /* CCAN_DARRAY_H */
213