Debian Codegraph @

src/utils/dynarr.h

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #pragma once
5
6 #include <stddef.h>
7
8 #include "misc.h"
9
10 /// Dynamic array implementation, design is similar to sds [0].
11 ///
12 /// Note this is not very type safe, be sure to annotate wherever you use this. And the
13 /// array won't have a fixed address in memory.
14 ///
15 /// [0]: https://github.com/antirez/sds
16
17 struct dynarr_header {
18 size_t len;
19 size_t cap;
20 };
21
22 static inline void *dynarr_new_impl(size_t size, size_t nelem) {
23 struct dynarr_header *ret = calloc(1, sizeof(struct dynarr_header) + size * nelem);
24 allocchk(ret);
25 ret->len = 0;
26 ret->cap = nelem;
27 return ret + 1;
28 }
29
30 static inline void *dynarr_reserve_impl(size_t size, void *arr, size_t nelem) {
31 struct dynarr_header *hdr = (struct dynarr_header *)arr - 1;
32 if (hdr->len + nelem > hdr->cap) {
33 hdr->cap = max2(max2(1, hdr->cap * 2), hdr->len + nelem);
34 auto new_hdr = realloc(hdr, sizeof(struct dynarr_header) + hdr->cap * size);
35 allocchk(new_hdr);
36 hdr = new_hdr;
37 }
38 return hdr + 1;
39 }
40
41 static inline void *dynarr_shrink_to_fit_impl(size_t size, void *arr) {
42 struct dynarr_header *hdr = (struct dynarr_header *)arr - 1;
43 if (hdr->len < hdr->cap) {
44 hdr->cap = hdr->len;
45 auto new_hdr = realloc(hdr, sizeof(struct dynarr_header) + hdr->cap * size);
46 allocchk(new_hdr);
47 hdr = new_hdr;
48 }
49 return hdr + 1;
50 }
51
52 static inline void dynarr_remove_impl(size_t size, void *arr, size_t idx) {
53 struct dynarr_header *hdr = (struct dynarr_header *)arr - 1;
54 BUG_ON(idx >= hdr->len);
55 memmove((char *)arr + idx * size, (char *)arr + (idx + 1) * size,
56 (hdr->len - idx - 1) * size);
57 hdr->len--;
58 }
59
60 static inline void dynarr_remove_swap_impl(size_t size, void *arr, size_t idx) {
61 struct dynarr_header *hdr = (struct dynarr_header *)arr - 1;
62 BUG_ON(idx >= hdr->len);
63 hdr->len--;
64 if (idx != hdr->len) {
65 memcpy((char *)arr + idx * size, (char *)arr + hdr->len * size, size);
66 }
67 }
68
69 /// Create a new dynamic array with capacity `cap` for type `type`.
70 #define dynarr_new(type, cap) ((type *)dynarr_new_impl(sizeof(type), (cap)))
71 /// Free a dynamic array, destructing each element with `dtor`.
72 #define dynarr_free(arr, dtor) \
73 do { \
74 dynarr_clear(arr, dtor); \
75 free((struct dynarr_header *)(arr) - 1); \
76 (arr) = NULL; \
77 } while (0)
78 #define dynarr_free_pod(arr) dynarr_free(arr, (void (*)(typeof(arr)))NULL)
79
80 /// Expand the capacity of the array so it can hold at least `nelem` more elements
81 #define dynarr_reserve(arr, nelem) \
82 ((arr) = dynarr_reserve_impl(sizeof(typeof(*(arr))), (void *)(arr), (nelem)))
83 /// Resize the array to `newlen`. If `newlen` is greater than the current length, the
84 /// new elements will be initialized with `init`. If `newlen` is less than the current
85 /// length, the excess elements will be destructed with `dtor`.
86 #define dynarr_resize(arr, newlen, init, dtor) \
87 do { \
88 BUG_ON((arr) == NULL); \
89 dynarr_reserve((arr), (newlen) - dynarr_len(arr)); \
90 if ((init) != NULL) { \
91 for (size_t i = dynarr_len(arr); i < (newlen); i++) { \
92 (init)((arr) + i); \
93 } \
94 } \
95 if ((dtor) != NULL) { \
96 for (size_t i = (newlen); i < dynarr_len(arr); i++) { \
97 (dtor)((arr) + i); \
98 } \
99 } \
100 dynarr_len(arr) = (newlen); \
101 } while (0)
102 /// Resize the array to `newlen`, for types that can be trivially constructed and
103 /// destructed.
104 #define dynarr_resize_pod(arr, newlen) \
105 dynarr_resize(arr, newlen, (void (*)(typeof(arr)))NULL, (void (*)(typeof(arr)))NULL)
106 /// Shrink the capacity of the array to its length.
107 #define dynarr_shrink_to_fit(arr) \
108 ((arr) = dynarr_shrink_to_fit_impl(sizeof(typeof(*(arr))), (void *)(arr)))
109 /// Push an element to the end of the array
110 #define dynarr_push(arr, item) \
111 do { \
112 dynarr_reserve(arr, 1); \
113 (arr)[dynarr_len(arr)++] = (item); \
114 } while (0)
115 /// Pop an element from the end of the array
116 #define dynarr_pop(arr) ((arr)[--dynarr_len(arr)])
117 /// Remove an element from the array by shifting the rest of the array forward.
118 #define dynarr_remove(arr, idx) \
119 dynarr_remove_impl(sizeof(typeof(*(arr))), (void *)(arr), idx)
120 /// Remove an element from the array by swapping it with the last element.
121 #define dynarr_remove_swap(arr, idx) \
122 dynarr_remove_swap_impl(sizeof(typeof(*(arr))), (void *)(arr), idx)
123 /// Return the length of the array
124 #define dynarr_len(arr) (((struct dynarr_header *)(arr) - 1)->len)
125 /// Return the capacity of the array
126 #define dynarr_cap(arr) (((struct dynarr_header *)(arr) - 1)->cap)
127 /// Return the last element of the array
128 #define dynarr_last(arr) ((arr)[dynarr_len(arr) - 1])
129 /// Return the pointer just past the last element of the array
130 #define dynarr_end(arr) ((arr) + dynarr_len(arr))
131 /// Return whether the array is empty
132 #define dynarr_is_empty(arr) (dynarr_len(arr) == 0)
133
134 /// Reduce the length of the array to `n`, destructing each element with `dtor`. If `n`
135 /// is greater than the current length, this does nothing.
136 #define dynarr_truncate(arr, n, dtor) \
137 do { \
138 if ((dtor) != NULL) { \
139 for (size_t __dynarr_i = n; __dynarr_i < dynarr_len(arr); \
140 __dynarr_i++) { \
141 (dtor)((arr) + __dynarr_i); \
142 } \
143 } \
144 dynarr_len(arr) = n; \
145 } while (0)
146 #define dynarr_truncate_pod(arr, n) dynarr_truncate(arr, n, (void (*)(typeof(arr)))NULL)
147
148 /// Clear the array, destructing each element with `dtor`.
149 #define dynarr_clear(arr, dtor) dynarr_truncate(arr, 0, dtor)
150 #define dynarr_clear_pod(arr) dynarr_truncate_pod(arr, 0)
151
152 /// Extend the array by copying `n` elements from `other`
153 #define dynarr_extend_from(arr, other, n) \
154 do { \
155 if ((n) > 0) { \
156 dynarr_reserve(arr, n); \
157 memcpy(dynarr_end(arr), other, sizeof(typeof(*(arr))[(n)])); \
158 dynarr_len(arr) += (n); \
159 } \
160 } while (0)
161 /// Extend the array by `n` elements, initializing them with `init`
162 #define dynarr_extend_with(arr, init, n) \
163 do { \
164 if ((n) > 0) { \
165 dynarr_resize(arr, dynarr_len(arr) + (n), init, \
166 (void (*)(typeof(arr)))NULL); \
167 } \
168 } while (0)
169
170 #define dynarr_foreach(arr, i) for (typeof(arr)(i) = (arr); (i) < dynarr_end(arr); (i)++)
171 #define dynarr_foreach_rev(arr, i) \
172 for (typeof(arr)(i) = dynarr_end(arr) - 1; (i) >= (arr); (i)--)
173
174 /// Find the index of the first appearance of an element in the array by using trivial
175 /// comparison, returns -1 if not found.
176 #define dynarr_find_pod(arr, needle) \
177 ({ \
178 ptrdiff_t dynarr_find_ret = -1; \
179 dynarr_foreach(arr, dynarr_find_i) { \
180 if (*dynarr_find_i == (needle)) { \
181 dynarr_find_ret = dynarr_find_i - (arr); \
182 break; \
183 } \
184 } \
185 dynarr_find_ret; \
186 })
187
188 /// Concatenate a dynarr of strings into a single string, separated by `sep`. The array
189 /// will be freed by this function.
190 char *dynarr_join(char **arr, const char *sep);