array.h 10 KB

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