C Container Collection (CCC)
Loading...
Searching...
No Matches
private_flat_bitset.h
Go to the documentation of this file.
1
23#ifndef CCC_PRIVATE_FLAT_BITSET
24#define CCC_PRIVATE_FLAT_BITSET
25
27#include <limits.h>
28#include <stddef.h>
29#include <stdint.h>
32#include "../configuration.h" /* IWYU pragma: keep */
33#include "../types.h" /* IWYU pragma: keep */
34
43 unsigned *blocks;
45 size_t count;
47 size_t capacity;
48};
49
50enum : size_t {
53 sizeof(*(struct CCC_Flat_bitset){}.blocks) * CHAR_BIT
54 ),
55};
56
57/*========================= Private Interface =========================*/
58
61 struct CCC_Flat_bitset *, size_t, CCC_Allocator const *
62);
66
67/*================================ Macros ===========================*/
68
71#define CCC_private_flat_bitset_block_count(private_bit_capacity) \
72 (((private_bit_capacity) + (CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS - 1)) \
73 / CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS)
74
76#define CCC_private_flat_bitset_block_bytes(private_bit_capacity) \
77 (sizeof(*(struct CCC_Flat_bitset){}.blocks) \
78 * CCC_private_flat_bitset_block_count(private_bit_capacity))
79
81#define CCC_private_flat_bitset_default() \
82 (struct CCC_Flat_bitset) { \
83 }
84
86#define CCC_private_flat_bitset_non_CCC_private_flat_bitset_default_size( \
87 private_cap, ... \
88) \
89 __VA_ARGS__
91#define CCC_private_flat_bitset_default_size(private_cap, ...) private_cap
93#define CCC_private_flat_bitset_optional_size(private_cap, ...) \
94 __VA_OPT__(CCC_private_flat_bitset_non_) \
95 ##CCC_private_flat_bitset_default_size(private_cap, __VA_ARGS__)
96
99#define CCC_private_flat_bitset_for( \
100 private_cap, private_count, private_bitblock_pointer \
101) \
102 (struct CCC_Flat_bitset) { \
103 .blocks = memset( \
104 (private_bitblock_pointer), \
105 0, \
106 CCC_private_flat_bitset_block_bytes(private_cap) \
107 ), \
108 .count = (private_count), .capacity = (private_cap), \
109 }
110
113#define CCC_private_flat_bitset_from( \
114 private_allocator, \
115 private_start_index, \
116 private_count, \
117 private_on_char, \
118 private_string, \
119 ... \
120) \
121 (struct { struct CCC_Flat_bitset private; }){(__extension__({ \
122 struct CCC_Flat_bitset private_bitset \
123 = CCC_private_flat_bitset_default(); \
124 size_t const private_cap = CCC_private_flat_bitset_optional_size( \
125 (private_count), __VA_ARGS__ \
126 ); \
127 size_t private_index = (private_start_index); \
128 if (CCC_private_flat_bitset_reserve( \
129 &private_bitset, \
130 private_cap < private_count ? private_count : private_cap, \
131 &private_allocator \
132 ) \
133 == CCC_RESULT_OK) { \
134 private_bitset.count = private_count; \
135 while (private_index < private_count \
136 && private_string[private_index]) { \
137 (void)CCC_private_flat_bitset_set( \
138 &private_bitset, \
139 private_index, \
140 (CCC_Tribool)(private_string[private_index] \
141 == private_on_char) \
142 ); \
143 ++private_index; \
144 } \
145 private_bitset.count = private_index; \
146 } \
147 private_bitset; \
148 }))}.private
149
151#define CCC_private_flat_bitset_with_capacity( \
152 private_allocate, private_cap, ... \
153) \
154 (struct { struct CCC_Flat_bitset private; }){(__extension__({ \
155 struct CCC_Flat_bitset private_bitset \
156 = CCC_private_flat_bitset_default(); \
157 size_t const private_count = CCC_private_flat_bitset_optional_size( \
158 (private_cap), __VA_ARGS__ \
159 ); \
160 if (CCC_private_flat_bitset_reserve( \
161 &private_bitset, private_cap, &private_allocate \
162 ) \
163 == CCC_RESULT_OK) { \
164 private_bitset.count = private_count; \
165 } \
166 private_bitset; \
167 }))}.private
168
173#if defined(__clang__) || defined(__llvm__)
177# define CCC_private_flat_bitset_count_check_storage_for( \
178 private_count, \
179 private_bit_compound_literal, \
180 private_optional_storage_specifier... \
181 ) \
182 (private_optional_storage_specifier struct { \
183 static_assert( \
184 sizeof(private_bit_compound_literal), \
185 "Specify non-zero capacity of bits." \
186 ); \
187 static_assert( \
188 sizeof(*(private_bit_compound_literal)) \
189 == sizeof(CCC_Tribool), \
190 "CCC_flat_bitset_storage_for and " \
191 "CCC_flat_bitset_with_storage only " \
192 "accept " \
193 "a (CCC_Tribool[N]){} compound literal array as an argument. " \
194 "Do " \
195 "not " \
196 "use CCC_flat_bitset_storage_for as an argument to " \
197 "CCC_flat_bitset_with_storage." \
198 ); \
199 static_assert( \
200 (private_count) <= sizeof(private_bit_compound_literal), \
201 "Bit count is less than or equal to capacity." \
202 ); \
203 typeof(*(struct CCC_Flat_bitset){}.blocks) private \
204 [CCC_private_flat_bitset_block_count( \
205 sizeof(private_bit_compound_literal) \
206 )]; \
207 }){} \
208 .private
209
211# define CCC_private_flat_bitset_storage_for( \
212 private_bit_compound_literal, private_optional_storage_specifier... \
213 ) \
214 CCC_private_flat_bitset_count_check_storage_for( \
215 0, \
216 private_bit_compound_literal, \
217 private_optional_storage_specifier \
218 )
219
220#else
222# define CCC_private_flat_bitset_count_check_storage_for( \
223 private_count, \
224 private_bit_compound_literal, \
225 private_optional_storage_specifier... \
226 ) \
227 (typeof ( \
228 *(struct CCC_Flat_bitset){}.blocks \
229 )[CCC_private_flat_bitset_block_count( \
230 sizeof(private_bit_compound_literal) \
231 )]) { \
232 }
233
235# define CCC_private_flat_bitset_storage_for( \
236 private_bit_compound_literal, private_optional_storage_specifier... \
237 ) \
238 CCC_private_flat_bitset_count_check_storage_for( \
239 0, \
240 private_bit_compound_literal, \
241 private_optional_storage_specifier \
242 )
243#endif
244
246#define CCC_private_flat_bitset_with_storage( \
247 private_count, \
248 private_compound_literal_array, \
249 private_optional_storage_specifier... \
250) \
251 (struct CCC_Flat_bitset) { \
252 .blocks = CCC_private_flat_bitset_count_check_storage_for( \
253 private_count, \
254 private_compound_literal_array, \
255 private_optional_storage_specifier \
256 ), \
257 .count = (private_count), \
258 .capacity = CCC_private_flat_bitset_block_count( \
259 sizeof(private_compound_literal_array) \
260 ) \
261 * sizeof(*(struct CCC_Flat_bitset){}.blocks) * CHAR_BIT, \
262 }
263
264#endif /* CCC_PRIVATE_FLAT_BITSET */
CCC_Result CCC_private_flat_bitset_reserve(struct CCC_Flat_bitset *, size_t, CCC_Allocator const *)
CCC_Tribool CCC_private_flat_bitset_set(struct CCC_Flat_bitset *, size_t, CCC_Tribool)
@ CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS
Definition: private_flat_bitset.h:52
The type passed by reference to any container function that may need to allocate memory....
Definition: types.h:384
Definition: private_flat_bitset.h:41
size_t capacity
Definition: private_flat_bitset.h:47
unsigned * blocks
Definition: private_flat_bitset.h:43
size_t count
Definition: private_flat_bitset.h:45
CCC_Tribool
A three state boolean to allow for an error state. Error is -1, False is 0, and True is 1.
Definition: types.h:178
CCC_Result
A result of actions on containers.
Definition: types.h:192