Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/support/vector-impl.h

raw
1 #ifndef CARYLL_SUPPORT_VECTOR_IMPL_H
2 #define CARYLL_SUPPORT_VECTOR_IMPL_H
3
4 #include "caryll/vector.h"
5 #include "element-impl.h"
6
7 #ifndef __CARYLL_INLINE__
8 #ifdef _MSC_VER
9 #define __CARYLL_INLINE__ __forceinline /* use __forceinline (VC++ specific) */
10 #else
11 #define __CARYLL_INLINE__ inline /* use standard inline */
12 #endif
13 #endif
14
15 #if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
16 #define __CARYLL_MAY_UNUSED__ __attribute__((__unused__))
17 #elif (__clang_major__ > 2)
18 #define __CARYLL_MAY_UNUSED__ __attribute__((__unused__))
19 #else
20 #define __CARYLL_MAY_UNUSED__
21 #endif
22
23 #define __CARYLL_VECTOR_INITIAL_SIZE 2
24
25 #define caryll_VectorImplFreeIndependent(__TV, __T, __ti) \
26 static __CARYLL_INLINE__ void __TV##_dispose(__TV *arr) { \
27 if (!arr) return; \
28 if ((__ti).dispose) { \
29 for (size_t j = arr->length; j--;) { \
30 (__ti).dispose(&arr->items[j]); \
31 } \
32 } \
33 __caryll_free(arr->items); \
34 arr->items = NULL; \
35 arr->length = 0; \
36 arr->capacity = 0; \
37 }
38
39 #define caryll_VectorImplFreeDependent(__TV, __T, __TX, fn) \
40 static __CARYLL_INLINE__ void __TV##_disposeDependent(__TV *arr, const __TX *enclosure) { \
41 if (!arr) return; \
42 for (size_t j = arr->length; j--;) { \
43 fn(&arr->items[j], enclosure); \
44 } \
45 __caryll_free(arr->items); \
46 arr->items = NULL; \
47 arr->length = 0; \
48 arr->capacity = 0; \
49 }
50
51 #define caryll_VectorImplFunctionsCommon(__TV, __T, __ti) \
52 static __CARYLL_INLINE__ void __TV##_init(MODIFY __TV *arr) { \
53 arr->length = 0; \
54 arr->capacity = 0; \
55 arr->items = NULL; \
56 } \
57 caryll_trivialCreate(__TV) \
58 caryll_trivialFree(__TV) \
59 static __CARYLL_INLINE__ void __TV##_growTo(MODIFY __TV *arr, size_t target) { \
60 if (target <= arr->capacity) return; \
61 if (arr->capacity < __CARYLL_VECTOR_INITIAL_SIZE) \
62 arr->capacity = __CARYLL_VECTOR_INITIAL_SIZE; \
63 while (arr->capacity < target) { \
64 arr->capacity += arr->capacity / 2; \
65 } \
66 if (arr->items) { \
67 arr->items = __caryll_realloc(arr->items, arr->capacity * sizeof(__T)); \
68 } else { \
69 arr->items = __caryll_calloc(arr->capacity, sizeof(__T)); \
70 } \
71 } \
72 static __CARYLL_INLINE__ void __TV##_growToN(MODIFY __TV *arr, size_t target) { \
73 if (target <= arr->capacity) return; \
74 if (arr->capacity < __CARYLL_VECTOR_INITIAL_SIZE) \
75 arr->capacity = __CARYLL_VECTOR_INITIAL_SIZE; \
76 if (arr->capacity < target) { arr->capacity = target + 1; } \
77 if (arr->items) { \
78 arr->items = __caryll_realloc(arr->items, arr->capacity * sizeof(__T)); \
79 } else { \
80 arr->items = __caryll_calloc(arr->capacity, sizeof(__T)); \
81 } \
82 } \
83 static __CARYLL_INLINE__ void __TV##_resizeTo(MODIFY __TV *arr, size_t target) { \
84 arr->capacity = target; \
85 if (arr->items) { \
86 arr->items = __caryll_realloc(arr->items, arr->capacity * sizeof(__T)); \
87 } else { \
88 arr->items = __caryll_calloc(arr->capacity, sizeof(__T)); \
89 } \
90 } \
91 static __CARYLL_INLINE__ void __TV##_shrinkToFit(MODIFY __TV *arr) { \
92 __TV##_resizeTo(arr, arr->length); \
93 } \
94 static __CARYLL_INLINE__ void __TV##_grow(MODIFY __TV *arr) { \
95 __TV##_growTo(arr, arr->length + 1); \
96 } \
97 static __CARYLL_INLINE__ void __TV##_push(MODIFY __TV *arr, MOVE __T elem) { \
98 __TV##_grow(arr); \
99 (arr)->items[(arr)->length++] = (elem); \
100 } \
101 static __CARYLL_INLINE__ __T __TV##_pop(MODIFY __TV *arr) { \
102 __T t = arr->items[arr->length - 1]; \
103 arr->length -= 1; \
104 return t; \
105 } \
106 static __CARYLL_INLINE__ void __TV##_fill(MODIFY __TV *arr, size_t n) { \
107 while (arr->length < n) { \
108 __T x; \
109 if ((__ti).init) { \
110 (__ti).init(&x); \
111 } else { \
112 memset(&x, 0, sizeof(x)); \
113 } \
114 __TV##_push(arr, x); \
115 } \
116 } \
117 static __CARYLL_INLINE__ void __TV##_initN(MODIFY __TV *arr, size_t n) { \
118 __TV##_init(arr); \
119 __TV##_growToN(arr, n); \
120 __TV##_fill(arr, n); \
121 } \
122 static __CARYLL_INLINE__ void __TV##_initCapN(MODIFY __TV *arr, size_t n) { \
123 __TV##_init(arr); \
124 __TV##_growToN(arr, n); \
125 } \
126 static __CARYLL_INLINE__ __TV *__TV##_createN(size_t n) { \
127 __TV *t = __caryll_malloc(sizeof(__TV)); \
128 __TV##_initN(t, n); \
129 return t; \
130 } \
131 static __CARYLL_INLINE__ void __TV##_move(MODIFY __TV *dst, MOVE __TV *src) { \
132 *dst = *src; \
133 __TV##_init(src); \
134 } \
135 static __CARYLL_INLINE__ void __TV##_copy(MODIFY __TV *dst, const __TV *src) { \
136 __TV##_init(dst); \
137 __TV##_growTo(dst, src->length); \
138 dst->length = src->length; \
139 if ((__ti).copy) { \
140 for (size_t j = 0; j < src->length; j++) { \
141 (__ti).copy(&dst->items[j], (const __T *)&src->items[j]); \
142 } \
143 } else { \
144 for (size_t j = 0; j < src->length; j++) { \
145 dst->items[j] = src->items[j]; \
146 } \
147 } \
148 } \
149 static __CARYLL_INLINE__ void __TV##_sort(MODIFY __TV *arr, \
150 int (*fn)(const __T *a, const __T *b)) { \
151 qsort(arr->items, arr->length, sizeof(arr->items[0]), \
152 (int (*)(const void *, const void *))fn); \
153 } \
154 static __CARYLL_INLINE__ void __TV##_filterEnv( \
155 MODIFY __TV *arr, bool (*fn)(const __T *a, void *env), void *env) { \
156 size_t j = 0; \
157 for (size_t k = 0; k < arr->length; k++) { \
158 if (fn(&arr->items[k], env)) { \
159 if (j != k) arr->items[j] = arr->items[k]; \
160 j++; \
161 } else { \
162 (__ti).dispose ? (__ti).dispose(&((arr)->items[k])) : (void)0; \
163 } \
164 }; \
165 arr->length = j; \
166 } \
167 static __CARYLL_INLINE__ void __TV##_disposeItem(MODIFY __TV *arr, size_t n) { \
168 (__ti).dispose ? (__ti).dispose(&((arr)->items[n])) : (void)0; \
169 } \
170 caryll_trivialReplace(__TV);
171
172 #define caryll_VectorImplFunctions(__TV, __T, __ti) \
173 caryll_VectorImplFreeIndependent(__TV, __T, __ti); \
174 caryll_VectorImplFunctionsCommon(__TV, __T, __ti);
175
176 #define caryll_VectorImplAssignments(__TV, __T, __ti) \
177 .init = __TV##_init, .copy = __TV##_copy, .dispose = __TV##_dispose, .create = __TV##_create, \
178 .createN = __TV##_createN, .free = __TV##_free, .initN = __TV##_initN, \
179 .initCapN = __TV##_initCapN, .clear = __TV##_dispose, .replace = __TV##_replace, \
180 .copyReplace = __TV##_copyReplace, .push = __TV##_push, .pop = __TV##_pop, \
181 .fill = __TV##_fill, .sort = __TV##_sort, .disposeItem = __TV##_disposeItem, \
182 .filterEnv = __TV##_filterEnv, .move = __TV##_move, .shrinkToFit = __TV##_shrinkToFit
183
184 #define caryll_standardVectorImpl(__TV, __T, __ti, __name) \
185 caryll_VectorImplFunctions(__TV, __T, __ti); \
186 caryll_VectorInterfaceTypeName(__TV) __name = { \
187 caryll_VectorImplAssignments(__TV, __T, __ti), \
188 };
189
190 #endif