1 #ifndef TREE_SITTER_ARRAY_H_
2 #define TREE_SITTER_ARRAY_H_
22 #define array_init(self) \
23 ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL)
28 #define array_get(self, index) \
29 (assert((uint32_t)index < (self)->size), &(self)->contents[index])
31 #define array_front(self) array_get(self, 0)
33 #define array_back(self) array_get(self, (self)->size - 1)
35 #define array_clear(self) ((self)->size = 0)
37 #define array_reserve(self, new_capacity) \
38 array__reserve((VoidArray *)(self), array__elem_size(self), new_capacity)
41 #define array_delete(self) array__delete((VoidArray *)self)
43 #define array_push(self, element) \
44 (array__grow((VoidArray *)(self), 1, array__elem_size(self)), \
45 (self)->contents[(self)->size++] = (element))
49 #define array_grow_by(self, count) \
50 (array__grow((VoidArray *)(self), count, array__elem_size(self)), \
51 memset((self)->contents + (self)->size, 0, (count) * array__elem_size(self)), \
52 (self)->size += (count))
54 #define array_push_all(self, other) \
55 array_extend((self), (other)->size, (other)->contents)
59 #define array_extend(self, count, contents) \
61 (VoidArray *)(self), array__elem_size(self), (self)->size, \
68 #define array_splice(self, index, old_count, new_count, new_contents) \
70 (VoidArray *)(self), array__elem_size(self), index, \
71 old_count, new_count, new_contents \
75 #define array_insert(self, index, element) \
76 array__splice((VoidArray *)(self), array__elem_size(self), index, 0, 1, &element)
79 #define array_erase(self, index) \
80 array__erase((VoidArray *)(self), array__elem_size(self), index)
82 #define array_pop(self) ((self)->contents[--(self)->size])
84 #define array_assign(self, other) \
85 array__assign((VoidArray *)(self), (const VoidArray *)(other), array__elem_size(self))
87 #define array_swap(self, other) \
88 array__swap((VoidArray *)(self), (VoidArray *)(other))
98 #define array_search_sorted_with(self, compare, needle, index, exists) \
99 array__search_sorted(self, 0, compare, , needle, index, exists)
105 #define array_search_sorted_by(self, field, needle, index, exists) \
106 array__search_sorted(self, 0, _compare_int, field, needle, index, exists)
110 #define array_insert_sorted_with(self, compare, value) \
112 unsigned index, exists; \
113 array_search_sorted_with(self, compare, &(value), &index, &exists); \
114 if (!exists) array_insert(self, index, value); \
121 #define array_insert_sorted_by(self, field, value) \
123 unsigned index, exists; \
124 array_search_sorted_by(self, field, (value) field, &index, &exists); \
125 if (!exists) array_insert(self, index, value); \
132 #define array__elem_size(self) sizeof(*(self)->contents)
136 self->contents =
NULL;
144 char *contents = (
char *)self->contents;
145 memmove(contents + index * element_size, contents + (index + 1) * element_size,
146 (
self->size - index - 1) * element_size);
151 if (new_capacity > self->capacity) {
152 if (self->contents) {
153 self->contents =
ts_realloc(self->contents, new_capacity * element_size);
155 self->contents =
ts_malloc(new_capacity * element_size);
157 self->capacity = new_capacity;
161 static inline void array__assign(VoidArray *
self,
const VoidArray *other,
size_t element_size) {
163 self->size = other->size;
164 memcpy(self->contents, other->contents, self->size * element_size);
167 static inline void array__swap(VoidArray *
self, VoidArray *other) {
168 VoidArray
swap = *other;
174 size_t new_size =
self->size +
count;
175 if (new_size > self->capacity) {
176 size_t new_capacity =
self->capacity * 2;
177 if (new_capacity < 8) new_capacity = 8;
178 if (new_capacity < new_size) new_capacity = new_size;
185 uint32_t new_count,
const void *elements) {
186 uint32_t new_size =
self->size + new_count - old_count;
187 uint32_t old_end = index + old_count;
188 uint32_t new_end = index + new_count;
193 char *contents = (
char *)self->contents;
194 if (self->size > old_end) {
196 contents + new_end * element_size,
197 contents + old_end * element_size,
198 (self->size - old_end) * element_size
204 (contents + index * element_size),
206 new_count * element_size
210 (contents + index * element_size),
212 new_count * element_size
216 self->size += new_count - old_count;
220 #define array__search_sorted(self, start, compare, suffix, needle, index, exists) \
224 uint32_t size = (self)->size - *(index); \
225 if (size == 0) break; \
228 uint32_t half_size = size / 2; \
229 uint32_t mid_index = *(index) + half_size; \
230 comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \
231 if (comparison <= 0) *(index) = mid_index; \
234 comparison = compare(&((self)->contents[*(index)] suffix), (needle)); \
235 if (comparison == 0) *(exists) = true; \
236 else if (comparison < 0) *(index) += 1; \
241 #define _compare_int(a, b) ((int)*(a) - (int)(b))
static void array__grow(VoidArray *self, size_t count, size_t element_size)
static void array__splice(VoidArray *self, size_t element_size, uint32_t index, uint32_t old_count, uint32_t new_count, const void *elements)
static void array__reserve(VoidArray *self, size_t element_size, uint32_t new_capacity)
static void array__erase(VoidArray *self, size_t element_size, uint32_t index)
static void array__swap(VoidArray *self, VoidArray *other)
static void array__assign(VoidArray *self, const VoidArray *other, size_t element_size)
static void array__delete(VoidArray *self)
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
assert(limit<=UINT32_MAX/2)
if(dbg->bits==RZ_SYS_BITS_64)