1
0

array.h 13 KB

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