Debian Codegraph @

ncls/src/utarray.h

raw
1 /* /\* */
2 /* Copyright (c) 2008-2018, Troy D. Hanson http://troydhanson.github.com/uthash/ */
3 /* All rights reserved. */
4 /* Redistribution and use in source and binary forms, with or without */
5 /* modification, are permitted provided that the following conditions are met: */
6 /* * Redistributions of source code must retain the above copyright */
7 /* notice, this list of conditions and the following disclaimer. */
8 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS */
9 /* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
10 /* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
11 /* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER */
12 /* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
13 /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, */
14 /* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR */
15 /* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
16 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
17 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
18 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
19 /* *\/ */
20
21 /* /\* a dynamic array implementation using macros */
22 /* *\/ */
23 /* #ifndef UTARRAY_H */
24 /* #define UTARRAY_H */
25
26 /* #define UTARRAY_VERSION 2.0.2 */
27
28 /* #include <stddef.h> /\* size_t *\/ */
29 /* #include <string.h> /\* memset, etc *\/ */
30 /* #include <stdlib.h> /\* exit *\/ */
31
32 /* #ifdef __GNUC__ */
33 /* #define UTARRAY_UNUSED __attribute__((__unused__)) */
34 /* #else */
35 /* #define UTARRAY_UNUSED */
36 /* #endif */
37
38 /* #ifndef oom */
39 /* #define oom() exit(-1) */
40 /* #endif */
41
42 /* typedef void (ctor_f)(void *dst, const void *src); */
43 /* typedef void (dtor_f)(void *elt); */
44 /* typedef void (init_f)(void *elt); */
45 /* typedef struct { */
46 /* size_t sz; */
47 /* init_f *init; */
48 /* ctor_f *copy; */
49 /* dtor_f *dtor; */
50 /* } UT_icd; */
51
52 /* typedef struct { */
53 /* unsigned i,n;/\* i: index of next available slot, n: num slots *\/ */
54 /* UT_icd icd; /\* initializer, copy and destructor functions *\/ */
55 /* char *d; /\* n slots of size icd->sz*\/ */
56 /* } UT_array; */
57
58 /* #define utarray_init(a,_icd) do { \ */
59 /* memset(a,0,sizeof(UT_array)); \ */
60 /* (a)->icd = *(_icd); \ */
61 /* } while(0) */
62
63 /* #define utarray_done(a) do { \ */
64 /* if ((a)->n) { \ */
65 /* if ((a)->icd.dtor) { \ */
66 /* unsigned _ut_i; \ */
67 /* for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ */
68 /* (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \ */
69 /* } \ */
70 /* } \ */
71 /* free((a)->d); \ */
72 /* } \ */
73 /* (a)->n=0; \ */
74 /* } while(0) */
75
76 /* #define utarray_new(a,_icd) do { \ */
77 /* (a) = (UT_array*)malloc(sizeof(UT_array)); \ */
78 /* if ((a) == NULL) oom(); \ */
79 /* utarray_init(a,_icd); \ */
80 /* } while(0) */
81
82 /* #define utarray_free(a) do { \ */
83 /* utarray_done(a); \ */
84 /* free(a); \ */
85 /* } while(0) */
86
87 /* #define utarray_reserve(a,by) do { \ */
88 /* if (((a)->i+(by)) > (a)->n) { \ */
89 /* char *utarray_tmp; \ */
90 /* while (((a)->i+(by)) > (a)->n) { (a)->n = ((a)->n ? (2*(a)->n) : 8); } \ */
91 /* utarray_tmp=(char*)realloc((a)->d, (a)->n*(a)->icd.sz); \ */
92 /* if (utarray_tmp == NULL) oom(); \ */
93 /* (a)->d=utarray_tmp; \ */
94 /* } \ */
95 /* } while(0) */
96
97 /* #define utarray_push_back(a,p) do { \ */
98 /* utarray_reserve(a,1); \ */
99 /* if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,(a)->i++), p); } \ */
100 /* else { memcpy(_utarray_eltptr(a,(a)->i++), p, (a)->icd.sz); }; \ */
101 /* } while(0) */
102
103 /* #define utarray_pop_back(a) do { \ */
104 /* if ((a)->icd.dtor) { (a)->icd.dtor( _utarray_eltptr(a,--((a)->i))); } \ */
105 /* else { (a)->i--; } \ */
106 /* } while(0) */
107
108 /* #define utarray_extend_back(a) do { \ */
109 /* utarray_reserve(a,1); \ */
110 /* if ((a)->icd.init) { (a)->icd.init(_utarray_eltptr(a,(a)->i)); } \ */
111 /* else { memset(_utarray_eltptr(a,(a)->i),0,(a)->icd.sz); } \ */
112 /* (a)->i++; \ */
113 /* } while(0) */
114
115 /* #define utarray_len(a) ((a)->i) */
116
117 /* #define utarray_eltptr(a,j) (((j) < (a)->i) ? _utarray_eltptr(a,j) : NULL) */
118 /* #define _utarray_eltptr(a,j) ((a)->d + ((a)->icd.sz * (j))) */
119
120 /* #define utarray_insert(a,p,j) do { \ */
121 /* if ((j) > (a)->i) utarray_resize(a,j); \ */
122 /* utarray_reserve(a,1); \ */
123 /* if ((j) < (a)->i) { \ */
124 /* memmove( _utarray_eltptr(a,(j)+1), _utarray_eltptr(a,j), \ */
125 /* ((a)->i - (j))*((a)->icd.sz)); \ */
126 /* } \ */
127 /* if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,j), p); } \ */
128 /* else { memcpy(_utarray_eltptr(a,j), p, (a)->icd.sz); }; \ */
129 /* (a)->i++; \ */
130 /* } while(0) */
131
132 /* #define utarray_inserta(a,w,j) do { \ */
133 /* if (utarray_len(w) == 0) break; \ */
134 /* if ((j) > (a)->i) utarray_resize(a,j); \ */
135 /* utarray_reserve(a,utarray_len(w)); \ */
136 /* if ((j) < (a)->i) { \ */
137 /* memmove(_utarray_eltptr(a,(j)+utarray_len(w)), \ */
138 /* _utarray_eltptr(a,j), \ */
139 /* ((a)->i - (j))*((a)->icd.sz)); \ */
140 /* } \ */
141 /* if ((a)->icd.copy) { \ */
142 /* unsigned _ut_i; \ */
143 /* for(_ut_i=0;_ut_i<(w)->i;_ut_i++) { \ */
144 /* (a)->icd.copy(_utarray_eltptr(a, (j) + _ut_i), _utarray_eltptr(w, _ut_i)); \ */
145 /* } \ */
146 /* } else { \ */
147 /* memcpy(_utarray_eltptr(a,j), _utarray_eltptr(w,0), \ */
148 /* utarray_len(w)*((a)->icd.sz)); \ */
149 /* } \ */
150 /* (a)->i += utarray_len(w); \ */
151 /* } while(0) */
152
153 /* #define utarray_resize(dst,num) do { \ */
154 /* unsigned _ut_i; \ */
155 /* if ((dst)->i > (unsigned)(num)) { \ */
156 /* if ((dst)->icd.dtor) { \ */
157 /* for (_ut_i = (num); _ut_i < (dst)->i; ++_ut_i) { \ */
158 /* (dst)->icd.dtor(_utarray_eltptr(dst, _ut_i)); \ */
159 /* } \ */
160 /* } \ */
161 /* } else if ((dst)->i < (unsigned)(num)) { \ */
162 /* utarray_reserve(dst, (num) - (dst)->i); \ */
163 /* if ((dst)->icd.init) { \ */
164 /* for (_ut_i = (dst)->i; _ut_i < (unsigned)(num); ++_ut_i) { \ */
165 /* (dst)->icd.init(_utarray_eltptr(dst, _ut_i)); \ */
166 /* } \ */
167 /* } else { \ */
168 /* memset(_utarray_eltptr(dst, (dst)->i), 0, (dst)->icd.sz*((num) - (dst)->i)); \ */
169 /* } \ */
170 /* } \ */
171 /* (dst)->i = (num); \ */
172 /* } while(0) */
173
174 /* #define utarray_concat(dst,src) do { \ */
175 /* utarray_inserta(dst, src, utarray_len(dst)); \ */
176 /* } while(0) */
177
178 /* #define utarray_erase(a,pos,len) do { \ */
179 /* if ((a)->icd.dtor) { \ */
180 /* unsigned _ut_i; \ */
181 /* for (_ut_i = 0; _ut_i < (len); _ut_i++) { \ */
182 /* (a)->icd.dtor(utarray_eltptr(a, (pos) + _ut_i)); \ */
183 /* } \ */
184 /* } \ */
185 /* if ((a)->i > ((pos) + (len))) { \ */
186 /* memmove(_utarray_eltptr(a, pos), _utarray_eltptr(a, (pos) + (len)), \ */
187 /* ((a)->i - ((pos) + (len))) * (a)->icd.sz); \ */
188 /* } \ */
189 /* (a)->i -= (len); \ */
190 /* } while(0) */
191
192 /* #define utarray_renew(a,u) do { \ */
193 /* if (a) utarray_clear(a); \ */
194 /* else utarray_new(a, u); \ */
195 /* } while(0) */
196
197 /* #define utarray_clear(a) do { \ */
198 /* if ((a)->i > 0) { \ */
199 /* if ((a)->icd.dtor) { \ */
200 /* unsigned _ut_i; \ */
201 /* for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ */
202 /* (a)->icd.dtor(_utarray_eltptr(a, _ut_i)); \ */
203 /* } \ */
204 /* } \ */
205 /* (a)->i = 0; \ */
206 /* } \ */
207 /* } while(0) */
208
209 /* #define utarray_sort(a,cmp) do { \ */
210 /* qsort((a)->d, (a)->i, (a)->icd.sz, cmp); \ */
211 /* } while(0) */
212
213 /* #define utarray_find(a,v,cmp) bsearch((v),(a)->d,(a)->i,(a)->icd.sz,cmp) */
214
215 /* #define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a,0)) : NULL) */
216 /* #define utarray_next(a,e) (((e)==NULL) ? utarray_front(a) : ((((a)->i) > (utarray_eltidx(a,e)+1)) ? _utarray_eltptr(a,utarray_eltidx(a,e)+1) : NULL)) */
217 /* #define utarray_prev(a,e) (((e)==NULL) ? utarray_back(a) : ((utarray_eltidx(a,e) > 0) ? _utarray_eltptr(a,utarray_eltidx(a,e)-1) : NULL)) */
218 /* #define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a,(a)->i-1)) : NULL) */
219 /* #define utarray_eltidx(a,e) (((char*)(e) >= (a)->d) ? (((char*)(e) - (a)->d)/(a)->icd.sz) : -1) */
220
221 /* /\* last we pre-define a few icd for common utarrays of ints and strings *\/ */
222 /* static void utarray_str_cpy(void *dst, const void *src) { */
223 /* char **_src = (char**)src, **_dst = (char**)dst; */
224 /* *_dst = (*_src == NULL) ? NULL : strdup(*_src); */
225 /* } */
226 /* static void utarray_str_dtor(void *elt) { */
227 /* char **eltc = (char**)elt; */
228 /* if (*eltc != NULL) free(*eltc); */
229 /* } */
230 /* static const UT_icd ut_str_icd UTARRAY_UNUSED = {sizeof(char*),NULL,utarray_str_cpy,utarray_str_dtor}; */
231 /* static const UT_icd ut_int_icd UTARRAY_UNUSED = {sizeof(int),NULL,NULL,NULL}; */
232 /* static const UT_icd ut_ptr_icd UTARRAY_UNUSED = {sizeof(void*),NULL,NULL,NULL}; */
233
234
235 /* #endif /\* UTARRAY_H *\/ */