tree-sitter-omega
aien-dev/tree-sitter-omega
Brought by drakestapleton
tree-sitter-omega / src / tree_sitter / array.h
| 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 | /// Initialize an array. |
| 32 | #define array_init(self) \ |
| 33 | ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) |
| 34 | |
| 35 | /// Create an empty array. |
| 36 | #define array_new() \ |
| 37 | { NULL, 0, 0 } |
| 38 | |
| 39 | /// Get a pointer to the element at a given `index` in the array. |
| 40 | #define array_get(self, _index) \ |
| 41 | (assert((uint32_t)(_index) < (self)->size), &(self)->contents[_index]) |
| 42 | |
| 43 | /// Get a pointer to the first element in the array. |
| 44 | #define array_front(self) array_get(self, 0) |
| 45 | |
| 46 | /// Get a pointer to the last element in the array. |
| 47 | #define array_back(self) array_get(self, (self)->size - 1) |
| 48 | |
| 49 | /// Clear the array, setting its size to zero. Note that this does not free any |
| 50 | /// memory allocated for the array's contents. |
| 51 | #define array_clear(self) ((self)->size = 0) |
| 52 | |
| 53 | #ifdef __cplusplus |
| 54 | #define _array__cast(self, expr) (decltype((self)->contents))(expr) |
| 55 | #else |
| 56 | #define _array__cast(self, expr) (expr) |
| 57 | #endif |
| 58 | |
| 59 | /// Reserve `new_capacity` elements of space in the array. If `new_capacity` is |
| 60 | /// less than the array's current capacity, this function has no effect. |
| 61 | #define array_reserve(self, new_capacity) \ |
| 62 | ((self)->contents = _array__cast(self, _array__reserve( \ |
| 63 | (void *)(self)->contents, &(self)->capacity, \ |
| 64 | array_elem_size(self), new_capacity)) \ |
| 65 | ) |
| 66 | |
| 67 | /// Free any memory allocated for this array. Note that this does not free any |
| 68 | /// memory allocated for the array's contents. |
| 69 | #define array_delete(self) \ |
| 70 | do { \ |
| 71 | if ((self)->contents) ts_free((self)->contents); \ |
| 72 | (self)->contents = NULL; \ |
| 73 | (self)->size = 0; \ |
| 74 | (self)->capacity = 0; \ |
| 75 | } while (0) |
| 76 | |
| 77 | /// Push a new `element` onto the end of the array. |
| 78 | #define array_push(self, element) \ |
| 79 | do { \ |
| 80 | (self)->contents = _array__cast(self, _array__grow( \ |
| 81 | (void *)(self)->contents, (self)->size, &(self)->capacity, \ |
| 82 | 1, array_elem_size(self) \ |
| 83 | )); \ |
| 84 | (self)->contents[(self)->size++] = (element); \ |
| 85 | } while(0) |
| 86 | |
| 87 | /// Increase the array's size by `count` elements. |
| 88 | /// New elements are zero-initialized. |
| 89 | #define array_grow_by(self, count) \ |
| 90 | do { \ |
| 91 | if ((count) == 0) break; \ |
| 92 | (self)->contents = _array__cast(self, _array__grow( \ |
| 93 | (self)->contents, (self)->size, &(self)->capacity, \ |
| 94 | count, array_elem_size(self) \ |
| 95 | )); \ |
| 96 | memset((self)->contents + (self)->size, 0, (count) * array_elem_size(self)); \ |
| 97 | (self)->size += (count); \ |
| 98 | } while (0) |
| 99 | |
| 100 | /// Append all elements from one array to the end of another. |
| 101 | #define array_push_all(self, other) \ |
| 102 | array_extend((self), (other)->size, (other)->contents) |
| 103 | |
| 104 | /// Append `count` elements to the end of the array, reading their values from the |
| 105 | /// `contents` pointer. |
| 106 | #define array_extend(self, count, other_contents) \ |
| 107 | ((self)->contents = _array__cast(self, _array__splice( \ |
| 108 | (void*)(self)->contents, &(self)->size, &(self)->capacity, \ |
| 109 | array_elem_size(self), (self)->size, 0, count, other_contents \ |
| 110 | ))) |
| 111 | |
| 112 | /// Remove `old_count` elements from the array starting at the given `index`. At |
| 113 | /// the same index, insert `new_count` new elements, reading their values from the |
| 114 | /// `new_contents` pointer. |
| 115 | #define array_splice(self, _index, old_count, new_count, new_contents) \ |
| 116 | ((self)->contents = _array__cast(self, _array__splice( \ |
| 117 | (void *)(self)->contents, &(self)->size, &(self)->capacity, \ |
| 118 | array_elem_size(self), _index, old_count, new_count, new_contents \ |
| 119 | ))) |
| 120 | |
| 121 | /// Insert one `element` into the array at the given `index`. |
| 122 | #define array_insert(self, _index, element) \ |
| 123 | ((self)->contents = _array__cast(self, _array__splice( \ |
| 124 | (void *)(self)->contents, &(self)->size, &(self)->capacity, \ |
| 125 | array_elem_size(self), _index, 0, 1, &(element) \ |
| 126 | ))) |
| 127 | |
| 128 | /// Remove one element from the array at the given `index`. |
| 129 | #define array_erase(self, _index) \ |
| 130 | _array__erase((void *)(self)->contents, &(self)->size, array_elem_size(self), _index) |
| 131 | |
| 132 | /// Pop the last element off the array, returning the element by value. |
| 133 | #define array_pop(self) ((self)->contents[--(self)->size]) |
| 134 | |
| 135 | /// Assign the contents of one array to another, reallocating if necessary. |
| 136 | #define array_assign(self, other) \ |
| 137 | ((self)->contents = _array__cast(self, _array__assign( \ |
| 138 | (void *)(self)->contents, &(self)->size, &(self)->capacity, \ |
| 139 | (const void *)(other)->contents, (other)->size, array_elem_size(self) \ |
| 140 | ))) |
| 141 | |
| 142 | /// Swap one array with another |
| 143 | #define array_swap(self, other) \ |
| 144 | do { \ |
| 145 | void *_array_swap_tmp = (void *)(self)->contents; \ |
| 146 | (self)->contents = (other)->contents; \ |
| 147 | (other)->contents = _array__cast(other, _array_swap_tmp); \ |
| 148 | _array__swap(&(self)->size, &(self)->capacity, \ |
| 149 | &(other)->size, &(other)->capacity); \ |
| 150 | } while (0) |
| 151 | |
| 152 | /// Get the size of the array contents |
| 153 | #define array_elem_size(self) (sizeof *(self)->contents) |
| 154 | |
| 155 | /// Search a sorted array for a given `needle` value, using the given `compare` |
| 156 | /// callback to determine the order. |
| 157 | /// |
| 158 | /// If an existing element is found to be equal to `needle`, then the `index` |
| 159 | /// out-parameter is set to the existing value's index, and the `exists` |
| 160 | /// out-parameter is set to true. Otherwise, `index` is set to an index where |
| 161 | /// `needle` should be inserted in order to preserve the sorting, and `exists` |
| 162 | /// is set to false. |
| 163 | #define array_search_sorted_with(self, compare, needle, _index, _exists) \ |
| 164 | _array__search_sorted(self, 0, compare, , needle, _index, _exists) |
| 165 | |
| 166 | /// Search a sorted array for a given `needle` value, using integer comparisons |
| 167 | /// of a given struct field (specified with a leading dot) to determine the order. |
| 168 | /// |
| 169 | /// See also `array_search_sorted_with`. |
| 170 | #define array_search_sorted_by(self, field, needle, _index, _exists) \ |
| 171 | _array__search_sorted(self, 0, _compare_int, field, needle, _index, _exists) |
| 172 | |
| 173 | /// Insert a given `value` into a sorted array, using the given `compare` |
| 174 | /// callback to determine the order. |
| 175 | #define array_insert_sorted_with(self, compare, value) \ |
| 176 | do { \ |
| 177 | unsigned _index, _exists; \ |
| 178 | array_search_sorted_with(self, compare, &(value), &_index, &_exists); \ |
| 179 | if (!_exists) array_insert(self, _index, value); \ |
| 180 | } while (0) |
| 181 | |
| 182 | /// Insert a given `value` into a sorted array, using integer comparisons of |
| 183 | /// a given struct field (specified with a leading dot) to determine the order. |
| 184 | /// |
| 185 | /// See also `array_search_sorted_by`. |
| 186 | #define array_insert_sorted_by(self, field, value) \ |
| 187 | do { \ |
| 188 | unsigned _index, _exists; \ |
| 189 | array_search_sorted_by(self, field, (value) field, &_index, &_exists); \ |
| 190 | if (!_exists) array_insert(self, _index, value); \ |
| 191 | } while (0) |
| 192 | |
| 193 | // Private |
| 194 | |
| 195 | // Pointers to individual `Array` fields (rather than the entire `Array` itself) |
| 196 | // are passed to the various `_array__*` functions below to address strict aliasing |
| 197 | // violations that arises when the _entire_ `Array` struct is passed as `Array(void)*`. |
| 198 | // |
| 199 | // The `Array` type itself was not altered as a solution in order to avoid breakage |
| 200 | // with existing consumers (in particular, parsers with external scanners). |
| 201 | |
| 202 | /// This is not what you're looking for, see `array_erase`. |
| 203 | static inline void _array__erase(void* self_contents, uint32_t *size, |
| 204 | size_t element_size, uint32_t index) { |
| 205 | assert(index < *size); |
| 206 | char *contents = (char *)self_contents; |
| 207 | memmove(contents + index * element_size, contents + (index + 1) * element_size, |
| 208 | (*size - index - 1) * element_size); |
| 209 | (*size)--; |
| 210 | } |
| 211 | |
| 212 | /// This is not what you're looking for, see `array_reserve`. |
| 213 | static inline void *_array__reserve(void *contents, uint32_t *capacity, |
| 214 | size_t element_size, uint32_t new_capacity) { |
| 215 | void *new_contents = contents; |
| 216 | if (new_capacity > *capacity) { |
| 217 | if (contents) { |
| 218 | new_contents = ts_realloc(contents, new_capacity * element_size); |
| 219 | } else { |
| 220 | new_contents = ts_malloc(new_capacity * element_size); |
| 221 | } |
| 222 | *capacity = new_capacity; |
| 223 | } |
| 224 | return new_contents; |
| 225 | } |
| 226 | |
| 227 | /// This is not what you're looking for, see `array_assign`. |
| 228 | static inline void *_array__assign(void* self_contents, uint32_t *self_size, uint32_t *self_capacity, |
| 229 | const void *other_contents, uint32_t other_size, size_t element_size) { |
| 230 | void *new_contents = _array__reserve(self_contents, self_capacity, element_size, other_size); |
| 231 | *self_size = other_size; |
| 232 | memcpy(new_contents, other_contents, *self_size * element_size); |
| 233 | return new_contents; |
| 234 | } |
| 235 | |
| 236 | /// This is not what you're looking for, see `array_swap`. |
| 237 | static inline void _array__swap(uint32_t *self_size, uint32_t *self_capacity, |
| 238 | uint32_t *other_size, uint32_t *other_capacity) { |
| 239 | uint32_t tmp_size = *self_size; |
| 240 | uint32_t tmp_capacity = *self_capacity; |
| 241 | *self_size = *other_size; |
| 242 | *self_capacity = *other_capacity; |
| 243 | *other_size = tmp_size; |
| 244 | *other_capacity = tmp_capacity; |
| 245 | } |
| 246 | |
| 247 | /// This is not what you're looking for, see `array_push` or `array_grow_by`. |
| 248 | static inline void *_array__grow(void *contents, uint32_t size, uint32_t *capacity, |
| 249 | uint32_t count, size_t element_size) { |
| 250 | void *new_contents = contents; |
| 251 | uint32_t new_size = size + count; |
| 252 | if (new_size > *capacity) { |
| 253 | uint32_t new_capacity = *capacity * 2; |
| 254 | if (new_capacity < 8) new_capacity = 8; |
| 255 | if (new_capacity < new_size) new_capacity = new_size; |
| 256 | new_contents = _array__reserve(contents, capacity, element_size, new_capacity); |
| 257 | } |
| 258 | return new_contents; |
| 259 | } |
| 260 | |
| 261 | /// This is not what you're looking for, see `array_splice`. |
| 262 | static inline void *_array__splice(void *self_contents, uint32_t *size, uint32_t *capacity, |
| 263 | size_t element_size, |
| 264 | uint32_t index, uint32_t old_count, |
| 265 | uint32_t new_count, const void *elements) { |
| 266 | uint32_t new_size = *size + new_count - old_count; |
| 267 | uint32_t old_end = index + old_count; |
| 268 | uint32_t new_end = index + new_count; |
| 269 | assert(old_end <= *size); |
| 270 | |
| 271 | void *new_contents = _array__reserve(self_contents, capacity, element_size, new_size); |
| 272 | |
| 273 | char *contents = (char *)new_contents; |
| 274 | if (*size > old_end) { |
| 275 | memmove( |
| 276 | contents + new_end * element_size, |
| 277 | contents + old_end * element_size, |
| 278 | (*size - old_end) * element_size |
| 279 | ); |
| 280 | } |
| 281 | if (new_count > 0) { |
| 282 | if (elements) { |
| 283 | memcpy( |
| 284 | (contents + index * element_size), |
| 285 | elements, |
| 286 | new_count * element_size |
| 287 | ); |
| 288 | } else { |
| 289 | memset( |
| 290 | (contents + index * element_size), |
| 291 | 0, |
| 292 | new_count * element_size |
| 293 | ); |
| 294 | } |
| 295 | } |
| 296 | *size += new_count - old_count; |
| 297 | |
| 298 | return new_contents; |
| 299 | } |
| 300 | |
| 301 | /// A binary search routine, based on Rust's `std::slice::binary_search_by`. |
| 302 | /// This is not what you're looking for, see `array_search_sorted_with` or `array_search_sorted_by`. |
| 303 | #define _array__search_sorted(self, start, compare, suffix, needle, _index, _exists) \ |
| 304 | do { \ |
| 305 | *(_index) = start; \ |
| 306 | *(_exists) = false; \ |
| 307 | uint32_t size = (self)->size - *(_index); \ |
| 308 | if (size == 0) break; \ |
| 309 | int comparison; \ |
| 310 | while (size > 1) { \ |
| 311 | uint32_t half_size = size / 2; \ |
| 312 | uint32_t mid_index = *(_index) + half_size; \ |
| 313 | comparison = compare(&((self)->contents[mid_index] suffix), (needle)); \ |
| 314 | if (comparison <= 0) *(_index) = mid_index; \ |
| 315 | size -= half_size; \ |
| 316 | } \ |
| 317 | comparison = compare(&((self)->contents[*(_index)] suffix), (needle)); \ |
| 318 | if (comparison == 0) *(_exists) = true; \ |
| 319 | else if (comparison < 0) *(_index) += 1; \ |
| 320 | } while (0) |
| 321 | |
| 322 | /// Helper macro for the `_sorted_by` routines below. This takes the left (existing) |
| 323 | /// parameter by reference in order to work with the generic sorting function above. |
| 324 | #define _compare_int(a, b) ((int)*(a) - (int)(b)) |
| 325 | |
| 326 | #ifdef _MSC_VER |
| 327 | #pragma warning(pop) |
| 328 | #elif defined(__GNUC__) || defined(__clang__) |
| 329 | #pragma GCC diagnostic pop |
| 330 | #endif |
| 331 | |
| 332 | #ifdef __cplusplus |
| 333 | } |
| 334 | #endif |
| 335 | |
| 336 | #endif // TREE_SITTER_ARRAY_H_ |