array.h 10 KB

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