Graphviz 14.1.5~dev.20260407.0703
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
318#define LIST_IS_CONTIGUOUS(list) gv_list_is_contiguous_((list)->impl);
319
330#define LIST_SYNC(list) gv_list_sync_(&(list)->impl, sizeof((list)->base[0]))
331
341#define LIST_SORT(list, cmp) \
342 gv_list_sort_(&(list)->impl, (cmp), sizeof((list)->base[0]))
343
351#define LIST_REVERSE(list) \
352 gv_list_reverse_(&(list)->impl, sizeof((list)->base[0]))
353
361#define LIST_SHRINK_TO_FIT(list) \
362 gv_list_shrink_to_fit_(&(list)->impl, sizeof((list)->base[0]))
363
373#define LIST_FREE(list) \
374 do { \
375 LIST_CLEAR(list); \
376 gv_list_free_(&(list)->impl); \
377 } while (0)
378
391#define LIST_PUSH_BACK(list, ...) LIST_APPEND((list), (__VA_ARGS__))
392
401#define LIST_POP_FRONT(list) \
402 (gv_list_pop_front_(&(list)->impl, &(list)->scratch, \
403 sizeof((list)->base[0])), \
404 (list)->scratch)
405
414#define LIST_POP_BACK(list) \
415 (gv_list_pop_back_(&(list)->impl, &(list)->scratch, \
416 sizeof((list)->base[0])), \
417 (list)->scratch)
418
429#define LIST_DROP_BACK(list) \
430 do { \
431 const size_t slot_ = gv_list_get_((list)->impl, LIST_SIZE(list) - 1); \
432 LIST_DTOR_((list), slot_); \
433 gv_list_pop_back_(&(list)->impl, &(list)->scratch, \
434 sizeof((list)->base[0])); \
435 } while (0)
436
450#define LIST_DETACH(list, datap, sizep) \
451 gv_list_detach_(&(list)->impl, ((void)(&(list)->base == (datap)), (datap)), \
452 (sizep), sizeof((list)->base[0]))
453
454#ifdef __cplusplus
455}
456#endif
internal implementation details of list.h