| 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(push) |
| 18 |
#pragma warning(disable : 4101) |
| 19 |
#elif defined(__GNUC__) || defined(__clang__) |
| 20 |
#pragma GCC diagnostic push |
| 21 |
#pragma GCC diagnostic ignored "-Wunused-variable" |
| 22 |
#endif |
| 23 |
|
| 24 |
#define Array(T) \ |
| 25 |
struct { \ |
| 26 |
T *contents; \ |
| 27 |
uint32_t size; \ |
| 28 |
uint32_t capacity; \ |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
#define array_init(self) \ |
| 33 |
((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) |
| 34 |
|
| 35 |
|
| 36 |
#define array_new() \ |
| 37 |
{ NULL, 0, 0 } |
| 38 |
|
| 39 |
|
| 40 |
#define array_get(self, _index) \ |
| 41 |
(assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) |
| 42 |
|
| 43 |
|
| 44 |
#define array_front(self) array_get(self, 0) |
| 45 |
|
| 46 |
|
| 47 |
#define array_back(self) array_get(self, (self)->size - 1) |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#define array_clear(self) ((self)->size = 0) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#define array_reserve(self, new_capacity) \ |
| 56 |
_array__reserve((Array *)(self), array_elem_size(self), new_capacity) |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#define array_delete(self) _array__delete((Array *)(self)) |
| 61 |
|
| 62 |
|
| 63 |
#define array_push(self, element) \ |
| 64 |
(_array__grow((Array *)(self), 1, array_elem_size(self)), \ |
| 65 |
(self)->contents[(self)->size++] = (element)) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#define array_grow_by(self, count) \ |
| 70 |
do { \ |
| 71 |
if ((count) == 0) break; \ |
| 72 |
_array__grow((Array *)(self), count, array_elem_size(self)); \ |
| 73 |
memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ |
| 74 |
(self)->size += (count); \ |
| 75 |
} while (0) |
| 76 |
|
| 77 |
|
| 78 |
#define array_push_all(self, other) \ |
| 79 |
array_extend((self), (other)->size, (other)->contents) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#define array_extend(self, count, contents) \ |
| 84 |
_array__splice( \ |
| 85 |
(Array *)(self), array_elem_size(self), (self)->size, \ |
| 86 |
0, count, contents \ |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
#define array_splice(self, _index, old_count, new_count, new_contents) \ |
| 93 |
_array__splice( \ |
| 94 |
(Array *)(self), array_elem_size(self), _index, \ |
| 95 |
old_count, new_count, new_contents \ |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
#define array_insert(self, _index, element) \ |
| 100 |
_array__splice((Array *)(self), array_elem_size(self), _index, 0, 1, &(element)) |
| 101 |
|
| 102 |
|
| 103 |
#define array_erase(self, _index) \ |
| 104 |
_array__erase((Array *)(self), array_elem_size(self), _index) |
| 105 |
|
| 106 |
|
| 107 |
#define array_pop(self) ((self)->contents[--(self)->size]) |
| 108 |
|
| 109 |
|
| 110 |
#define array_assign(self, other) \ |
| 111 |
_array__assign((Array *)(self), (const Array *)(other), array_elem_size(self)) |
| 112 |
|
| 113 |
|
| 114 |
#define array_swap(self, other) \ |
| 115 |
_array__swap((Array *)(self), (Array *)(other)) |
| 116 |
|
| 117 |
|
| 118 |
#define array_elem_size(self) (sizeof *(self)->contents) |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#define array_search_sorted_with(self, compare, needle, _index, _exists) \ |
| 129 |
_array__search_sorted(self, 0, compare, , needle, _index, _exists) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#define array_search_sorted_by(self, field, needle, _index, _exists) \ |
| 136 |
_array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#define array_insert_sorted_with(self, compare, value) \ |
| 141 |
do { \ |
| 142 |
unsigned _index, _exists; \ |
| 143 |
array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ |
| 144 |
if (!_exists) array_insert(self, _index, value); \ |
| 145 |
} while (0) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
#define array_insert_sorted_by(self, field, value) \ |
| 152 |
do { \ |
| 153 |
unsigned _index, _exists; \ |
| 154 |
array_search_sorted_by(self, field, (value) field, &_index, &_exists); \ |
| 155 |
if (!_exists) array_insert(self, _index, value); \ |
| 156 |
} while (0) |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
typedef Array(void) Array; |
| 161 |
|
| 162 |
|
| 163 |
static inline void _array__delete(Array *self) { |
| 164 |
if (self->contents) { |
| 165 |
ts_free(self->contents); |
| 166 |
self->contents = NULL; |
| 167 |
self->size = 0; |
| 168 |
self->capacity = 0; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
static inline void _array__erase(Array *self, size_t element_size, |
| 174 |
uint32_t index) { |
| 175 |
assert(index < self->size); |
| 176 |
char *contents = (char *)self->contents; |
| 177 |
memmove(contents + index * element_size, contents + (index + 1) * element_size, |
| 178 |
(self->size - index - 1) * element_size); |
| 179 |
self->size--; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
static inline void _array__reserve(Array *self, size_t element_size, uint32_t new_capacity) { |
| 184 |
if (new_capacity > self->capacity) { |
| 185 |
if (self->contents) { |
| 186 |
self->contents = ts_realloc(self->contents, new_capacity * element_size); |
| 187 |
} else { |
| 188 |
self->contents = ts_malloc(new_capacity * element_size); |
| 189 |
} |
| 190 |
self->capacity = new_capacity; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
static inline void _array__assign(Array *self, const Array *other, size_t element_size) { |
| 196 |
_array__reserve(self, element_size, other->size); |
| 197 |
self->size = other->size; |
| 198 |
memcpy(self->contents, other->contents, self->size * element_size); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
static inline void _array__swap(Array *self, Array *other) { |
| 203 |
Array swap = *other; |
| 204 |
*other = *self; |
| 205 |
*self = swap; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
static inline void _array__grow(Array *self, uint32_t count, size_t element_size) { |
| 210 |
uint32_t new_size = self->size + count; |
| 211 |
if (new_size > self->capacity) { |
| 212 |
uint32_t new_capacity = self->capacity * 2; |
| 213 |
if (new_capacity < 8) new_capacity = 8; |
| 214 |
if (new_capacity < new_size) new_capacity = new_size; |
| 215 |
_array__reserve(self, element_size, new_capacity); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
static inline void _array__splice(Array *self, size_t element_size, |
| 221 |
uint32_t index, uint32_t old_count, |
| 222 |
uint32_t new_count, const void *elements) { |
| 223 |
uint32_t new_size = self->size + new_count - old_count; |
| 224 |
uint32_t old_end = index + old_count; |
| 225 |
uint32_t new_end = index + new_count; |
| 226 |
assert(old_end <= self->size); |
| 227 |
|
| 228 |
_array__reserve(self, element_size, new_size); |
| 229 |
|
| 230 |
char *contents = (char *)self->contents; |
| 231 |
if (self->size > old_end) { |
| 232 |
memmove( |
| 233 |
contents + new_end * element_size, |
| 234 |
contents + old_end * element_size, |
| 235 |
(self->size - old_end) * element_size |
| 236 |
); |
| 237 |
} |
| 238 |
if (new_count > 0) { |
| 239 |
if (elements) { |
| 240 |
memcpy( |
| 241 |
(contents + index * element_size), |
| 242 |
elements, |
| 243 |
new_count * element_size |
| 244 |
); |
| 245 |
} else { |
| 246 |
memset( |
| 247 |
(contents + index * element_size), |
| 248 |
0, |
| 249 |
new_count * element_size |
| 250 |
); |
| 251 |
} |
| 252 |
} |
| 253 |
self->size += new_count - old_count; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
#define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ |
| 259 |
do { \ |
| 260 |
*(_index) = start; \ |
| 261 |
*(_exists) = false; \ |
| 262 |
uint32_t size = (self)->size - *(_index); \ |
| 263 |
if (size == 0) break; \ |
| 264 |
int comparison; \ |
| 265 |
while (size > 1) { \ |
| 266 |
uint32_t half_size = size / 2; \ |
| 267 |
uint32_t mid_index = *(_index) + half_size; \ |
| 268 |
comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ |
| 269 |
if (comparison <= 0) *(_index) = mid_index; \ |
| 270 |
size -= half_size; \ |
| 271 |
} \ |
| 272 |
comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \ |
| 273 |
if (comparison == 0) *(_exists) = true; \ |
| 274 |
else if (comparison < 0) *(_index) += 1; \ |
| 275 |
} while (0) |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
#define _compare_int(a, b) ((int)*(a) - (int)(b)) |
| 280 |
|
| 281 |
#ifdef _MSC_VER |
| 282 |
#pragma warning(pop) |
| 283 |
#elif defined(__GNUC__) || defined(__clang__) |
| 284 |
#pragma GCC diagnostic pop |
| 285 |
#endif |
| 286 |
|
| 287 |
#ifdef __cplusplus |
| 288 |
} |
| 289 |
#endif |
| 290 |
|
| 291 |
#endif |