Graphviz 16.0.1~dev.20260815.2250
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1
34
35#pragma once
36
37#include <assert.h>
38#include <stdint.h>
39#include <string.h>
40#include <util/list-private.h>
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46static_assert(
47 offsetof(list_t_, base) == 0,
48 "LIST(<type>).base and LIST(<type>).impl.base will not alias each other");
49
55#define LIST(type) \
56 struct { \
57 union { \
58 type *base; \
59 list_t_ impl; \
60 }; \
61 void (*dtor)(type); \
62 type scratch; \
63 }
64
70#define LIST_DTOR_FREE ((void *)1)
71
80#define LIST_SIZE(list) gv_list_size_((list)->impl)
81
90#define LIST_IS_EMPTY(list) (LIST_SIZE(list) == 0)
91
101#define LIST_TRY_APPEND(list, item) \
102 gv_list_try_append_(&(list)->impl, \
103 ((list)->scratch = (item), &(list)->scratch), \
104 sizeof((list)->base[0]))
105
124#define LIST_APPEND(list, ...) \
125 do { \
126 (list)->scratch = (__VA_ARGS__); \
127 const size_t slot_ = \
128 gv_list_append_slot_(&(list)->impl, sizeof((list)->base[0])); \
129 (list)->base[slot_] = (list)->scratch; \
130 } while (0)
131
142#define LIST_PREPEND(list, item) \
143 do { \
144 (list)->scratch = (item); \
145 const size_t slot_ = \
146 gv_list_prepend_slot_(&(list)->impl, sizeof((list)->base[0])); \
147 (list)->base[slot_] = (list)->scratch; \
148 } while (0)
149
159#define LIST_GET(list, index) \
160 ((list)->base[gv_list_get_((list)->impl, (index))])
161
172#define LIST_AT(list, index) \
173 (&(list)->base[gv_list_get_((list)->impl, (index))])
174
184#define LIST_FRONT(list) LIST_AT((list), 0)
185
195#define LIST_BACK(list) LIST_AT((list), LIST_SIZE(list) - 1)
196
206#define LIST_SET(list, index, item) \
207 do { \
208 (list)->scratch = (item); \
209 const size_t slot_ = gv_list_get_((list)->impl, (index)); \
210 LIST_DTOR_((list), slot_); \
211 (list)->base[slot_] = (list)->scratch; \
212 } while (0)
213
222#define LIST_REMOVE(list, item) \
223 do { \
224 /* get something we can take the address of */ \
225 (list)->scratch = (item); \
226 \
227 const size_t found_ = gv_list_find_((list)->impl, &(list)->scratch, \
228 sizeof((list)->base[0])); \
229 if (found_ == SIZE_MAX) { /* not found */ \
230 break; \
231 } \
232 \
233 LIST_DTOR_((list), found_); \
234 gv_list_remove_(&(list)->impl, found_, sizeof((list)->base[0])); \
235 } while (0)
236
244#define LIST_CLEAR(list) \
245 do { \
246 for (size_t i_ = 0; i_ < LIST_SIZE(list); ++i_) { \
247 const size_t slot_ = gv_list_get_((list)->impl, i_); \
248 LIST_DTOR_((list), slot_); \
249 } \
250 gv_list_clear_(&(list)->impl, sizeof((list)->base[0])); \
251 } while (0)
252
261#define LIST_RESERVE(list, capacity) \
262 gv_list_reserve_(&(list)->impl, capacity, sizeof((list)->base[0]))
263
277#define LIST_CONTAINS(list, needle) \
278 gv_list_contains_((list)->impl, \
279 ((void)((list)->base == &(needle)), &(needle)), \
280 sizeof((list)->base[0]))
281
293#define LIST_COPY(list_type, src) \
294 ((list_type){.impl = gv_list_copy_((src)->impl, sizeof((src)->base[0])), \
295 .dtor = (src)->dtor})
296
307#define LIST_SYNC(list) gv_list_sync_(&(list)->impl, sizeof((list)->base[0]))
308
318#define LIST_SORT(list, cmp) \
319 gv_list_sort_(&(list)->impl, (cmp), sizeof((list)->base[0]))
320
328#define LIST_REVERSE(list) \
329 gv_list_reverse_(&(list)->impl, sizeof((list)->base[0]))
330
338#define LIST_SHRINK_TO_FIT(list) \
339 gv_list_shrink_to_fit_(&(list)->impl, sizeof((list)->base[0]))
340
350#define LIST_FREE(list) \
351 do { \
352 LIST_CLEAR(list); \
353 gv_list_free_(&(list)->impl); \
354 } while (0)
355
368#define LIST_PUSH_BACK(list, ...) LIST_APPEND((list), (__VA_ARGS__))
369
378#define LIST_POP_FRONT(list) \
379 (gv_list_pop_front_(&(list)->impl, &(list)->scratch, \
380 sizeof((list)->base[0])), \
381 (list)->scratch)
382
391#define LIST_POP_BACK(list) \
392 (gv_list_pop_back_(&(list)->impl, &(list)->scratch, \
393 sizeof((list)->base[0])), \
394 (list)->scratch)
395
406#define LIST_DROP_BACK(list) \
407 do { \
408 const size_t slot_ = gv_list_get_((list)->impl, LIST_SIZE(list) - 1); \
409 LIST_DTOR_((list), slot_); \
410 gv_list_pop_back_(&(list)->impl, &(list)->scratch, \
411 sizeof((list)->base[0])); \
412 } while (0)
413
427#define LIST_DETACH(list, datap, sizep) \
428 gv_list_detach_(&(list)->impl, ((void)(&(list)->base == (datap)), (datap)), \
429 (sizep), sizeof((list)->base[0]))
430
431#ifdef __cplusplus
432}
433#endif
internal implementation details of list.h