Debian Codegraph @

Xtree-house/test-grammars/markdown/src/tree_sitter/array.h

raw symbols SCIP
1 #ifndef TREE_SITTER_ARRAY_H_
2 #define TREE_SITTER_ARRAY_H_
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include "./alloc.h"
9
10 #include <assert.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #ifdef _MSC_VER
17 #pragma warning(disable : 4101)
18 #elif defined(__GNUC__) || defined(__clang__)
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Wunused-variable"
21 #endif
22
23 #define Array(T) \
24 struct { \
25 T *contents; \
26 uint32_t size; \
27 uint32_t capacity; \
28 }
29
30 /// Initialize an array.
31 #define array_init(self) \
32 ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
33
34 /// Create an empty array.
35 #define array_new() \
36 { NULL, 0, 0 }
37
38 /// Get a pointer to the element at a given `index` in the array.
39 #define array_get(self, _index) \
40 (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index])
41
42 /// Get a pointer to the first element in the array.
43 #define array_front(self) array_get(self, 0)
44
45 /// Get a pointer to the last element in the array.
46 #define array_back(self) array_get(self, (self)->size - 1)
47
48 /// Clear the array, setting its size to zero. Note that this does not free any
49 /// memory allocated for the array's contents.
50 #define array_clear(self) ((self)->size = 0)
51
52 /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is
53 /// less than the array's current capacity, this function has no effect.
54 #define array_reserve(self, new_capacity) \
55 _array__reserve((Array *)(self), array_elem_size(self), new_capacity)
56
57 /// Free any memory allocated for this array. Note that this does not free any
58 /// memory allocated for the array's contents.
59 #define array_delete(self) _array__delete((Array *)(self))
60
61 /// Push a new `element` onto the end of the array.
62 #define array_push(self, element) \
63 (_array__grow((Array *)(self), 1, array_elem_size(self)), \
64 (self)->contents[(self)->size++] = (element))
65
66 /// Increase the array's size by `count` elements.
67 /// New elements are zero-initialized.
68 #define array_grow_by(self, count) \
69 (_array__grow((Array *)(self), count, array_elem_size(self)), \
70 memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)), \
71 (self)->size += (count))
72
73 /// Append all elements from one array to the end of another.
74 #define array_push_all(self, other) \
75 array_extend((self), (other)->size, (other)->contents)
76
77 /// Append `count` elements to the end of the array, reading their values from the
78 /// `contents` pointer.
79 #define array_extend(self, count, contents) \
80 _array__splice( \
81 (Array *)(self), array_elem_size(self), (self)->size, \
82 0, count, contents \
83 )
84
85 /// Remove `old_count` elements from the array starting at the given `index`. At
86 /// the same index, insert `new_count` new elements, reading their values from the
87 /// `new_contents` pointer.
88 #define array_splice(self, _index, old_count, new_count, new_contents) \
89 _array__splice( \
90 (Array *)(self), array_elem_size(self), _index, \
91 old_count, new_count, new_contents \
92 )
93
94 /// Insert one `element` into the array at the given `index`.
95 #define array_insert(self, _index, element) \
96 _array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element))
97
98 /// Remove one element from the array at the given `index`.
99 #define array_erase(self, _index) \
100 _array__erase((Array *)(self), array_elem_size(self), _index)
101
102 /// Pop the last element off the array, returning the element by value.
103 #define array_pop(self) ((self)->contents[--(self)->size])
104
105 /// Assign the contents of one array to another, reallocating if necessary.
106 #define array_assign(self, other) \
107 _array__assign((Array *)(self), (const Array *)(other), array_elem_size(self))
108
109 /// Swap one array with another
110 #define array_swap(self, other) \
111 _array__swap((Array *)(self), (Array *)(other))
112
113 /// Get the size of the array contents
114 #define array_elem_size(self) (sizeof *(self)->contents)
115
116 /// Search a sorted array for a given `needle` value, using the given `compare`
117 /// callback to determine the order.
118 ///
119 /// If an existing element is found to be equal to `needle`, then the `index`
120 /// out-parameter is set to the existing value's index, and the `exists`
121 /// out-parameter is set to true. Otherwise, `index` is set to an index where
122 /// `needle` should be inserted in order to preserve the sorting, and `exists`
123 /// is set to false.
124 #define array_search_sorted_with(self, compare, needle, _index, _exists) \
125 _array__search_sorted(self, 0, compare, , needle, _index, _exists)
126
127 /// Search a sorted array for a given `needle` value, using integer comparisons
128 /// of a given struct field (specified with a leading dot) to determine the order.
129 ///
130 /// See also `array_search_sorted_with`.
131 #define array_search_sorted_by(self, field, needle, _index, _exists) \
132 _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists)
133
134 /// Insert a given `value` into a sorted array, using the given `compare`
135 /// callback to determine the order.
136 #define array_insert_sorted_with(self, compare, value) \
137 do { \
138 unsigned _index, _exists; \
139 array_search_sorted_with(self, compare, &(value), &_index, &_exists); \
140 if (!_exists) array_insert(self, _index, value); \
141 } while (0)
142
143 /// Insert a given `value` into a sorted array, using integer comparisons of
144 /// a given struct field (specified with a leading dot) to determine the order.
145 ///
146 /// See also `array_search_sorted_by`.
147 #define array_insert_sorted_by(self, field, value) \
148 do { \
149 unsigned _index, _exists; \
150 array_search_sorted_by(self, field, (value) field, &_index, &_exists); \
151 if (!_exists) array_insert(self, _index, value); \
152 } while (0)
153
154 // Private
155
156 typedef Array(void) Array;
157
158 /// This is not what you're looking for, see `array_delete`.
159 static inline void _array__delete(Array *self) {
160 if (self->contents) {
161 ts_free(self->contents);
162 self->contents = NULL;
163 self->size = 0;
164 self->capacity = 0;
165 }
166 }
167
168 /// This is not what you're looking for, see `array_erase`.
169 static inline void _array__erase(Array *self, size_t element_size,
170 uint32_t index) {
171 assert(index < self->size);
172 char *contents = (char *)self->contents;
173 memmove(contents + index * element_size, contents + (index + 1) * element_size,
174 (self->size - index - 1) * element_size);
175 self->size--;
176 }
177
178 /// This is not what you're looking for, see `array_reserve`.
179 static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) {
180 if (new_capacity > self->capacity) {
181 if (self->contents) {
182 self->contents = ts_realloc(self->contents, new_capacity * element_size);
183 } else {
184 self->contents = ts_malloc(new_capacity * element_size);
185 }
186 self->capacity = new_capacity;
187 }
188 }
189
190 /// This is not what you're looking for, see `array_assign`.
191 static inline void _array__assign(Array *self, const Array *other, size_t element_size) {
192 _array__reserve(self, element_size, other->size);
193 self->size = other->size;
194 memcpy(self->contents, other->contents, self->size * element_size);
195 }
196
197 /// This is not what you're looking for, see `array_swap`.
198 static inline void _array__swap(Array *self, Array *other) {
199 Array swap = *other;
200 *other = *self;
201 *self = swap;
202 }
203
204 /// This is not what you're looking for, see `array_push` or `array_grow_by`.
205 static inline void _array__grow(Array *self, uint32_t count, size_t element_size) {
206 uint32_t new_size = self->size + count;
207 if (new_size > self->capacity) {
208 uint32_t new_capacity = self->capacity * 2;
209 if (new_capacity < 8) new_capacity = 8;
210 if (new_capacity < new_size) new_capacity = new_size;
211 _array__reserve(self, element_size, new_capacity);
212 }
213 }
214
215 /// This is not what you're looking for, see `array_splice`.
216 static inline void _array__splice(Array *self, size_t element_size,
217 uint32_t index, uint32_t old_count,
218 uint32_t new_count, const void *elements) {
219 uint32_t new_size = self->size + new_count - old_count;
220 uint32_t old_end = index + old_count;
221 uint32_t new_end = index + new_count;
222 assert(old_end <= self->size);
223
224 _array__reserve(self, element_size, new_size);
225
226 char *contents = (char *)self->contents;
227 if (self->size > old_end) {
228 memmove(
229 contents + new_end * element_size,
230 contents + old_end * element_size,
231 (self->size - old_end) * element_size
232 );
233 }
234 if (new_count > 0) {
235 if (elements) {
236 memcpy(
237 (contents + index * element_size),
238 elements,
239 new_count * element_size
240 );
241 } else {
242 memset(
243 (contents + index * element_size),
244 0,
245 new_count * element_size
246 );
247 }
248 }
249 self->size += new_count - old_count;
250 }
251
252 /// A binary search routine, based on Rust's `std::slice::binary_search_by`.
253 /// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`.
254 #define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \
255 do { \
256 *(_index) = start; \
257 *(_exists) = false; \
258 uint32_t size = (self)->size - *(_index); \
259 if (size == 0) break; \
260 int comparison; \
261 while (size > 1) { \
262 uint32_t half_size = size / 2; \
263 uint32_t mid_index = *(_index) + half_size; \
264 comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
265 if (comparison <= 0) *(_index) = mid_index; \
266 size -= half_size; \
267 } \
268 comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \
269 if (comparison == 0) *(_exists) = true; \
270 else if (comparison < 0) *(_index) += 1; \
271 } while (0)
272
273 /// Helper macro for the `_sorted_by` routines below. This takes the left (existing)
274 /// parameter by reference in order to work with the generic sorting function above.
275 #define _compare_int(a, b) ((int)*(a) - (int)(b))
276
277 #ifdef _MSC_VER
278 #pragma warning(default : 4101)
279 #elif defined(__GNUC__) || defined(__clang__)
280 #pragma GCC diagnostic pop
281 #endif
282
283 #ifdef __cplusplus
284 }
285 #endif
286
287 #endif // TREE_SITTER_ARRAY_H_