C Container Collection (CCC)
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
impl_doubly_linked_list.h
1
16#ifndef CCC_IMPL_DOUBLY_LINKED_LIST_H
17#define CCC_IMPL_DOUBLY_LINKED_LIST_H
18
20#include <assert.h>
21#include <stddef.h>
24#include "../types.h"
25
26/* NOLINTBEGIN(readability-identifier-naming) */
27
33struct ccc_dll_elem
34{
36 struct ccc_dll_elem *n;
38 struct ccc_dll_elem *p;
39};
40
87struct ccc_dll
88{
90 struct ccc_dll_elem nil;
92 size_t count;
94 size_t sizeof_type;
96 size_t dll_elem_offset;
100 ccc_any_alloc_fn *alloc;
102 void *aux;
103};
104
105/*======================= Private Interface ===========================*/
106
108void ccc_impl_dll_push_back(struct ccc_dll *, struct ccc_dll_elem *);
110void ccc_impl_dll_push_front(struct ccc_dll *, struct ccc_dll_elem *);
112struct ccc_dll_elem *ccc_impl_dll_elem_in(struct ccc_dll const *,
113 void const *any_struct);
114
115/*======================= Macro Implementations =======================*/
116
119#define ccc_impl_dll_init(impl_dll_name, impl_struct_name, \
120 impl_dll_elem_field, impl_cmp_fn, impl_alloc_fn, \
121 impl_aux_data) \
122 { \
123 .nil.n = &(impl_dll_name).nil, \
124 .nil.p = &(impl_dll_name).nil, \
125 .sizeof_type = sizeof(impl_struct_name), \
126 .dll_elem_offset = offsetof(impl_struct_name, impl_dll_elem_field), \
127 .count = 0, \
128 .alloc = (impl_alloc_fn), \
129 .cmp = (impl_cmp_fn), \
130 .aux = (impl_aux_data), \
131 }
132
134#define ccc_impl_dll_emplace_back(dll_ptr, struct_initializer...) \
135 (__extension__({ \
136 typeof(struct_initializer) *impl_dll_res = NULL; \
137 struct ccc_dll *impl_dll = (dll_ptr); \
138 if (impl_dll) \
139 { \
140 if (impl_dll->alloc) \
141 { \
142 impl_dll_res = impl_dll->alloc(NULL, impl_dll->sizeof_type, \
143 impl_dll->aux); \
144 if (impl_dll_res) \
145 { \
146 *impl_dll_res = (typeof(*impl_dll_res))struct_initializer; \
147 ccc_impl_dll_push_back( \
148 impl_dll, \
149 ccc_impl_dll_elem_in(impl_dll, impl_dll_res)); \
150 } \
151 } \
152 } \
153 impl_dll_res; \
154 }))
155
157#define ccc_impl_dll_emplace_front(dll_ptr, struct_initializer...) \
158 (__extension__({ \
159 typeof(struct_initializer) *impl_dll_res = NULL; \
160 struct ccc_dll *impl_dll = (dll_ptr); \
161 if (!impl_dll->alloc) \
162 { \
163 impl_dll_res = NULL; \
164 } \
165 else \
166 { \
167 impl_dll_res \
168 = impl_dll->alloc(NULL, impl_dll->sizeof_type, impl_dll->aux); \
169 if (impl_dll_res) \
170 { \
171 *impl_dll_res = struct_initializer; \
172 ccc_impl_dll_push_front( \
173 impl_dll, ccc_impl_dll_elem_in(impl_dll, impl_dll_res)); \
174 } \
175 } \
176 impl_dll_res; \
177 }))
178
179/* NOLINTEND(readability-identifier-naming) */
180
181#endif /* CCC_IMPL_DOUBLY_LINKED_LIST_H */
struct ccc_dll_elem ccc_dll_elem
A doubly linked list intrusive element to embedded in a user type.
Definition: doubly_linked_list.h:70
ccc_threeway_cmp ccc_any_type_cmp_fn(ccc_any_type_cmp)
A callback function for comparing two elements in a container.
Definition: types.h:320
void * ccc_any_alloc_fn(void *ptr, size_t size, void *aux)
An allocation function at the core of all containers.
Definition: types.h:312