Line data Source code
1 : /*
2 : MIT License
3 :
4 : Copyright (c) 2026 - A bunch of nerds
5 :
6 : Permission is hereby granted, free of charge, to any person obtaining a copy
7 : of this software and associated documentation files (the "Software"), to deal
8 : in the Software without restriction, including without limitation the rights
9 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : copies of the Software, and to permit persons to whom the Software is
11 : furnished to do so, subject to the following conditions:
12 :
13 : The above copyright notice and this permission notice shall be included in all
14 : copies or substantial portions of the Software.
15 :
16 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 : SOFTWARE.
23 : */
24 :
25 : #pragma once
26 :
27 : // The one iteration API of every container. cbstmap.h, chashmap.h and
28 : // cvector.h include it for you. ccol_begin() starts an iteration,
29 : // ccol_iter_next() moves on, ccol_iter_key_ptr() and ccol_iter_val_ptr() give
30 : // typed pointers, ccol_iter_destroy() stops early, and ccol_for_each() wraps
31 : // the whole loop.
32 :
33 : #include "common.h"
34 :
35 : // Everything from here to the matching pop is the public ABI and the shared
36 : // library exports it. The library builds with -fvisibility=hidden, so anything
37 : // outside these blocks stays internal.
38 : #pragma GCC visibility push(default)
39 :
40 : // Incomplete types are enough for _Generic and for the prototypes below. The
41 : // begin_iter prototypes live only here because ccol_begin() names all three,
42 : // and a second declaration in a container header fails -Wredundant-decls.
43 : struct cvector;
44 : struct chashmap;
45 : struct cbstmap;
46 :
47 : // Creates an iterator at the first element of v, for walking a vector without
48 : // the typed macros. The key is the element's index (a size_t) and the value
49 : // points straight into the buffer. An empty or NULL v gives NULL with *err set
50 : // to NULL, so a vector that was never created iterates as an empty one; a
51 : // failed allocation gives NULL with a library-owned error string in *err.
52 : // Changing the vector invalidates the iterator. ccol_iter_next() frees it
53 : // after the last element; call ccol_iter_destroy() to stop early.
54 : cmap_iterator *cvector_begin_iter(struct cvector *v, char **err);
55 :
56 : // Creates an iterator at the first entry of chm, for walking a hash map
57 : // without the typed macros. An empty or NULL map gives NULL with *err set to
58 : // NULL; a failed allocation gives NULL with a library-owned error string in
59 : // *err. The order isn't sorted and depends on the key and value types, as
60 : // chashmap_begin_iter(3) describes. Changing the map invalidates the
61 : // iterator. ccol_iter_next() frees it after the last entry; call
62 : // ccol_iter_destroy() to stop early.
63 : cmap_iterator *chashmap_begin_iter(struct chashmap *chm, char **err);
64 :
65 : // Creates an iterator at the smallest key of cbm, for walking an ordered map in
66 : // ascending key order without the typed macros. An empty or NULL map gives
67 : // NULL with *err set to NULL; a failed allocation gives NULL with a
68 : // library-owned error string in *err. Changing the map invalidates the
69 : // iterator. ccol_iter_next() frees it after the last entry; call
70 : // ccol_iter_destroy() to stop early.
71 : cmap_iterator *cbstmap_begin_iter(struct cbstmap *cbm, char **err);
72 :
73 : // The unified iterator API
74 :
75 : // Internal: Frees *it and sets it to NULL; the cleanup handler of
76 : // ccol_iter_declare(), so an iterator left by an early break isn't leaked. It
77 : // frees through the iterator's own free function, so the right allocator is
78 : // used.
79 13 : static inline void ccol__iterator_destroy_cleanup(cmap_iterator **it) {
80 13 : if (it && *it) {
81 9 : (*it)->ccol__free_fn(*it);
82 9 : *it = NULL;
83 : }
84 2 : }
85 :
86 : // Declares the iterator variable it, set to NULL, for a cvec, chmap or cbmap
87 : // container that a _declare, _construct or _redeclare macro declared, so that
88 : // ccol_iter_key_ptr() and ccol_iter_val_ptr() give pointers of the right
89 : // types. An iterator left in it when it goes out of scope is freed, so a
90 : // break out of the loop leaks nothing. It's a declaration.
91 : //
92 : // ccol_iter_declare(vec, it);
93 : // for (it = ccol_begin(vec); it; it = ccol_iter_next(it))
94 : // printf("%d\n", *ccol_iter_val_ptr(it));
95 : #define ccol_iter_declare(container, it) \
96 : __typeof__(*container##__ccol_key_type_var) *it##__ccol_iter_key_type_var \
97 : __attribute__((unused)) = NULL; \
98 : __typeof__(*container##__ccol_val_type_var) *it##__ccol_iter_val_type_var \
99 : __attribute__((unused)) = NULL; \
100 : cmap_iterator *it ccol__destructor(ccol__iterator_destroy_cleanup) = NULL
101 :
102 : // Internal: Calls the iterator's own next function and gives its result; the
103 : // body of ccol_iter_next(), written as a function so the argument is evaluated
104 : // once.
105 1632267 : static inline cmap_iterator *ccol__iter_next(cmap_iterator *it) {
106 1632267 : return it->ccol__next_fn(it);
107 : }
108 :
109 : // Moves the iterator it to the next element and evaluates to it, for the step
110 : // of a loop; it is evaluated once. After the last element it frees the
111 : // iterator and gives NULL, so assign the result back: It = ccol_iter_next(it).
112 : #define ccol_iter_next(it) ccol__iter_next(it)
113 :
114 : // Evaluates to a const pointer to the current key, typed after the key type of
115 : // the container: A const size_t * to the index for a cvec, a const KeyT * for
116 : // a map. For a char * key it points to the stored char *, so *ptr is the
117 : // string. it must come from ccol_iter_declare(). Valid until the iterator
118 : // moves or the container changes.
119 : #define ccol_iter_key_ptr(it) \
120 : ({ \
121 : const __typeof__(*it##__ccol_iter_key_type_var) *ccol__iter_k; \
122 : if ((it)->ccol__direct_ptr || \
123 : !ccol__is_char_ptr(*it##__ccol_iter_key_type_var)) { \
124 : ccol__iter_k = (__typeof__(ccol__iter_k))((it)->key_pair->ptr); \
125 : } else { \
126 : ccol__iter_k = (__typeof__(ccol__iter_k))(&(it)->key_pair->ptr); \
127 : } \
128 : ccol__iter_k; \
129 : })
130 :
131 : // Internal: Converts p to the result type of ccol_iter_val_ptr(). For a
132 : // character-pointer value type it adds const, because the iterator names the
133 : // accessor slot whose size describes the stored string; you can edit the
134 : // string in place but not store a new pointer the map didn't size. Every
135 : // other type comes back as is.
136 : #define ccol__iter_value_ptr_result(it, p) \
137 : _Generic(*it##__ccol_iter_val_type_var, \
138 : char *: (__typeof__(*it##__ccol_iter_val_type_var) const *)(p), \
139 : const char *: (__typeof__(*it##__ccol_iter_val_type_var) const *)(p), \
140 : signed char *: (__typeof__(*it##__ccol_iter_val_type_var) const *)(p), \
141 : const signed char *: (__typeof__(*it##__ccol_iter_val_type_var) \
142 : const *)(p), \
143 : unsigned char *: (__typeof__(*it##__ccol_iter_val_type_var) const *)(p), \
144 : const unsigned char *: (__typeof__(*it##__ccol_iter_val_type_var) \
145 : const *)(p), \
146 : default: (p))
147 :
148 : // Internal: Evaluates to the address of the current value for
149 : // ccol_iter_val_ptr(). For a character-pointer value type it's the accessor
150 : // slot &val_pair->ptr, otherwise val_pair->ptr. No branch casts const away,
151 : // so ccol_iter_val_ptr() is clean under -Wcast-qual.
152 : #define ccol__iter_val_slot(it) \
153 : _Generic(*it##__ccol_iter_val_type_var, \
154 : char *: &(it)->val_pair->ptr, \
155 : const char *: &(it)->val_pair->ptr, \
156 : signed char *: &(it)->val_pair->ptr, \
157 : const signed char *: &(it)->val_pair->ptr, \
158 : unsigned char *: &(it)->val_pair->ptr, \
159 : const unsigned char *: &(it)->val_pair->ptr, \
160 : default: (it)->val_pair->ptr)
161 :
162 : // Evaluates to a ValT * to the current value, for reading or changing it in
163 : // place; writing through it changes the container. For a character-pointer
164 : // value type it's ValT const *, so *ptr is the stored string: Edit its bytes
165 : // in place, or use the container's insert to store another one. it must come
166 : // from ccol_iter_declare(). Valid until the iterator moves or the container
167 : // changes.
168 : #define ccol_iter_val_ptr(it) \
169 : ({ \
170 : __typeof__(ccol__iter_value_ptr_result( \
171 : it, (__typeof__(*it##__ccol_iter_val_type_var) *)0)) ccol__iter_v; \
172 : if ((it)->ccol__direct_ptr || \
173 : !ccol__is_char_ptr(*it##__ccol_iter_val_type_var)) { \
174 : ccol__iter_v = (__typeof__(ccol__iter_v))((it)->val_pair->ptr); \
175 : } else { \
176 : ccol__iter_v = (__typeof__(ccol__iter_v))(ccol__iter_val_slot(it)); \
177 : } \
178 : ccol__iter_v; \
179 : })
180 :
181 : // Frees the iterator and sets it to NULL, for a loop you leave before the end.
182 : // it must be a modifiable lvalue and is evaluated once; a NULL it does
183 : // nothing. Don't call it after ccol_iter_next() gave NULL, since the iterator
184 : // is already freed then. It's a statement.
185 : #define ccol_iter_destroy(it) \
186 : ccol__iter_destroy_impl(it, ccol__uniq(ccol__iter_destroy_slot, __COUNTER__))
187 :
188 : // Internal: The body of ccol_iter_destroy(). It takes the address of it once
189 : // into slot, a name from ccol__uniq(), so destroy macros can nest without
190 : // -Wshadow warnings.
191 : #define ccol__iter_destroy_impl(it, slot) \
192 : do { \
193 : __typeof__(it) *slot = &(it); \
194 : if (*slot) { \
195 : (*slot)->ccol__free_fn(*slot); \
196 : *slot = NULL; \
197 : } \
198 : } while (0)
199 :
200 : // The value of an iterator past the end (NULL), for code that compares an
201 : // iterator against it.
202 : #define ccol_end NULL
203 :
204 : // ccol_begin and ccol_for_each
205 :
206 : // Internal: Call the begin function of each container; ccol_begin() selects
207 : // one with _Generic. They take void * so every branch type-checks for any
208 : // container.
209 10 : static inline __attribute__((always_inline)) cmap_iterator *ccol__cvec_begin(
210 : void *v, char **err) {
211 10 : return cvector_begin_iter((struct cvector *)v, err);
212 : }
213 5507 : static inline __attribute__((always_inline)) cmap_iterator *ccol__chmap_begin(
214 : void *v, char **err) {
215 5510 : return chashmap_begin_iter((struct chashmap *)v, err);
216 : }
217 2042 : static inline __attribute__((always_inline)) cmap_iterator *ccol__cbmap_begin(
218 : void *v, char **err) {
219 2042 : return cbstmap_begin_iter((struct cbstmap *)v, err);
220 : }
221 :
222 : // Starts an iteration over a cvec, chmap or cbmap; one spelling works for
223 : // every container. Evaluates to an iterator at the first element, or NULL for
224 : // an empty or NULL container. A failed allocation prints the reason and ends
225 : // the program. Any other handle type is a compile error. Assign the result to
226 : // an iterator from ccol_iter_declare(), and step with ccol_iter_next().
227 : #define ccol_begin(container) \
228 : ({ \
229 : char *ccol__err = NULL; \
230 : cmap_iterator *ccol__it = _Generic((container), \
231 : struct cvector *: ccol__cvec_begin, \
232 : struct chashmap *: ccol__chmap_begin, \
233 : struct cbstmap *: ccol__cbmap_begin)((container), &ccol__err); \
234 : if (!ccol__it && ccol__err) { \
235 : ccol__fatal_err("ccol_begin('%s'): %s", #container, ccol__err); \
236 : } \
237 : ccol__it; \
238 : })
239 :
240 : // Declares the iterator it and runs the body (the last argument) once for
241 : // every element of a cvec, chmap or cbmap, the shortest way to write a whole
242 : // loop. Inside the body use ccol_iter_key_ptr(it) and ccol_iter_val_ptr(it).
243 : // Commas in the body are fine, and a break leaks nothing. Don't change the
244 : // container inside the body. A failed allocation at the start prints the
245 : // reason and ends the program.
246 : //
247 : // ccol_for_each(my_vec, it, {
248 : // printf("[%zu] = %d\n", *ccol_iter_key_ptr(it), *ccol_iter_val_ptr(it));
249 : // });
250 : #define ccol_for_each(container, it, ...) \
251 : do { \
252 : ccol_iter_declare(container, it); \
253 : for (it = ccol_begin(container); it != NULL; it = ccol_iter_next(it)) { \
254 : __VA_ARGS__ \
255 : } \
256 : } while (0)
257 :
258 : #pragma GCC visibility pop
|