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)) == sizeof(CCC_Bit), \
189 "CCC_flat_bitset_storage_for and " \
190 "CCC_flat_bitset_with_storage only " \
191 "accept " \
192 "a (CCC_Bit[N]){} compound literal array as an argument. Do " \
193 "not " \
194 "use CCC_flat_bitset_storage_for as an argument to " \
195 "CCC_flat_bitset_with_storage." \
196 ); \
197 static_assert( \
198 (private_count) <= sizeof(private_bit_compound_literal), \
199 "Bit count is less than or equal to capacity." \
200 ); \
201 typeof(*(struct CCC_Flat_bitset){}.blocks) private \
202 [CCC_private_flat_bitset_block_count( \
203 sizeof(private_bit_compound_literal) \
204 )]; \
205 }){} \
206 .private
207
209# define CCC_private_flat_bitset_storage_for( \
210 private_bit_compound_literal, private_optional_storage_specifier... \
211 ) \
212 CCC_private_flat_bitset_count_check_storage_for( \
213 0, \
214 private_bit_compound_literal, \
215 private_optional_storage_specifier \
216 )
217
218#else
220# define CCC_private_flat_bitset_count_check_storage_for( \
221 private_count, \
222 private_bit_compound_literal, \
223 private_optional_storage_specifier... \
224 ) \
225 (typeof ( \
226 *(struct CCC_Flat_bitset){}.blocks \
227 )[CCC_private_flat_bitset_block_count( \
228 sizeof(private_bit_compound_literal) \
229 )]) { \
230 }
231
233# define CCC_private_flat_bitset_storage_for( \
234 private_bit_compound_literal, private_optional_storage_specifier... \
235 ) \
236 CCC_private_flat_bitset_count_check_storage_for( \
237 0, \
238 private_bit_compound_literal, \
239 private_optional_storage_specifier \
240 )
241#endif
242
244#define CCC_private_flat_bitset_with_storage( \
245 private_count, \
246 private_compound_literal_array, \
247 private_optional_storage_specifier... \
248) \
249 (struct CCC_Flat_bitset) { \
250 .blocks = CCC_private_flat_bitset_count_check_storage_for( \
251 private_count, \
252 private_compound_literal_array, \
253 private_optional_storage_specifier \
254 ), \
255 .count = (private_count), \
256 .capacity = CCC_private_flat_bitset_block_count( \
257 sizeof(private_compound_literal_array) \
258 ) \
259 * sizeof(*(struct CCC_Flat_bitset){}.blocks) * CHAR_BIT, \
260 }
261
262#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