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/*========================= Private Interface =========================*/
57
60 struct CCC_Flat_bitset *, size_t, CCC_Allocator const *
61);
65
66/*================================ Macros ===========================*/
67
70#define CCC_private_flat_bitset_block_count(private_bit_capacity) \
71 (((private_bit_capacity) + (CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS - 1)) \
72 / CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS)
73
75#define CCC_private_flat_bitset_block_bytes(private_bit_capacity) \
76 (sizeof(*(struct CCC_Flat_bitset){}.blocks) \
77 * CCC_private_flat_bitset_block_count(private_bit_capacity))
78
80#define CCC_private_flat_bitset_default() \
81 (struct CCC_Flat_bitset) { \
82 }
83
85#define CCC_private_flat_bitset_non_CCC_private_flat_bitset_default_size( \
86 private_cap, ... \
87) \
88 __VA_ARGS__
90#define CCC_private_flat_bitset_default_size(private_cap, ...) private_cap
92#define CCC_private_flat_bitset_optional_size(private_cap, ...) \
93 __VA_OPT__(CCC_private_flat_bitset_non_) \
94 ##CCC_private_flat_bitset_default_size(private_cap, __VA_ARGS__)
95
98#define CCC_private_flat_bitset_for( \
99 private_cap, private_count, private_bitblock_pointer \
100) \
101 (struct CCC_Flat_bitset) { \
102 .blocks = memset( \
103 (private_bitblock_pointer), \
104 0, \
105 CCC_private_flat_bitset_block_bytes(private_cap) \
106 ), \
107 .count = (private_count), .capacity = (private_cap), \
108 }
109
112#define CCC_private_flat_bitset_from( \
113 private_allocator, \
114 private_start_index, \
115 private_count, \
116 private_on_char, \
117 private_string, \
118 ... \
119) \
120 (struct { struct CCC_Flat_bitset private; }){(__extension__({ \
121 struct CCC_Flat_bitset private_bitset \
122 = CCC_private_flat_bitset_default(); \
123 size_t const private_cap = CCC_private_flat_bitset_optional_size( \
124 (private_count), __VA_ARGS__ \
125 ); \
126 size_t private_index = (private_start_index); \
127 if (CCC_private_flat_bitset_reserve( \
128 &private_bitset, \
129 private_cap < private_count ? private_count : private_cap, \
130 &private_allocator \
131 ) \
132 == CCC_RESULT_OK) { \
133 private_bitset.count = private_count; \
134 while (private_index < private_count \
135 && private_string[private_index]) { \
136 (void)CCC_private_flat_bitset_set( \
137 &private_bitset, \
138 private_index, \
139 (CCC_Tribool)(private_string[private_index] \
140 == private_on_char) \
141 ); \
142 ++private_index; \
143 } \
144 private_bitset.count = private_index; \
145 } \
146 private_bitset; \
147 }))}.private
148
150#define CCC_private_flat_bitset_with_capacity( \
151 private_allocate, private_cap, ... \
152) \
153 (struct { struct CCC_Flat_bitset private; }){(__extension__({ \
154 struct CCC_Flat_bitset private_bitset \
155 = CCC_private_flat_bitset_default(); \
156 size_t const private_count = CCC_private_flat_bitset_optional_size( \
157 (private_cap), __VA_ARGS__ \
158 ); \
159 if (CCC_private_flat_bitset_reserve( \
160 &private_bitset, private_cap, &private_allocate \
161 ) \
162 == CCC_RESULT_OK) { \
163 private_bitset.count = private_count; \
164 } \
165 private_bitset; \
166 }))}.private
167
172#if defined(__clang__) || defined(__llvm__)
176# define CCC_private_flat_bitset_count_check_storage_for( \
177 private_count, \
178 private_bit_compound_literal, \
179 private_optional_storage_specifier... \
180 ) \
181 (private_optional_storage_specifier struct { \
182 static_assert( \
183 sizeof(private_bit_compound_literal), \
184 "Specify non-zero capacity of bits." \
185 ); \
186 static_assert( \
187 sizeof(*(private_bit_compound_literal)) == sizeof(CCC_Bit), \
188 "CCC_flat_bitset_storage_for and " \
189 "CCC_flat_bitset_with_storage only " \
190 "accept " \
191 "a (CCC_Bit[N]){} compound literal array as an argument. Do " \
192 "not " \
193 "use CCC_flat_bitset_storage_for as an argument to " \
194 "CCC_flat_bitset_with_storage." \
195 ); \
196 static_assert( \
197 (private_count) <= sizeof(private_bit_compound_literal), \
198 "Bit count is less than or equal to capacity." \
199 ); \
200 typeof(*(struct CCC_Flat_bitset){}.blocks) private \
201 [CCC_private_flat_bitset_block_count( \
202 sizeof(private_bit_compound_literal) \
203 )]; \
204 }){} \
205 .private
206
208# define CCC_private_flat_bitset_storage_for( \
209 private_bit_compound_literal, private_optional_storage_specifier... \
210 ) \
211 CCC_private_flat_bitset_count_check_storage_for( \
212 0, \
213 private_bit_compound_literal, \
214 private_optional_storage_specifier \
215 )
216
217#else
219# define CCC_private_flat_bitset_count_check_storage_for( \
220 private_count, \
221 private_bit_compound_literal, \
222 private_optional_storage_specifier... \
223 ) \
224 (typeof ( \
225 *(struct CCC_Flat_bitset){}.blocks \
226 )[CCC_private_flat_bitset_block_count( \
227 sizeof(private_bit_compound_literal) \
228 )]) { \
229 }
230
232# define CCC_private_flat_bitset_storage_for( \
233 private_bit_compound_literal, private_optional_storage_specifier... \
234 ) \
235 CCC_private_flat_bitset_count_check_storage_for( \
236 0, \
237 private_bit_compound_literal, \
238 private_optional_storage_specifier \
239 )
240#endif
241
243#define CCC_private_flat_bitset_with_storage( \
244 private_count, \
245 private_compound_literal_array, \
246 private_optional_storage_specifier... \
247) \
248 (struct CCC_Flat_bitset) { \
249 .blocks = CCC_private_flat_bitset_count_check_storage_for( \
250 private_count, \
251 private_compound_literal_array, \
252 private_optional_storage_specifier \
253 ), \
254 .count = (private_count), \
255 .capacity = CCC_private_flat_bitset_block_count( \
256 sizeof(private_compound_literal_array) \
257 ) \
258 * sizeof(*(struct CCC_Flat_bitset){}.blocks) * CHAR_BIT, \
259 }
260
261#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