CoolFace
Apppublic

aravagarwal/CodeCloakPII

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