C Container Collection (CCC)
Loading...
Searching...
No Matches
private_flat_bitset.h
1
16#ifndef CCC_PRIVATE_BITSET
17#define CCC_PRIVATE_BITSET
18
20#include <limits.h>
21#include <stddef.h>
22#include <stdint.h>
25#include "../configuration.h"
26#include "../types.h"
27
36 unsigned *blocks;
38 size_t count;
40 size_t capacity;
41};
42
43enum : size_t {
45 CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS
46 = (sizeof(*(struct CCC_Flat_bitset){}.blocks) * CHAR_BIT),
47};
48
49/*========================= Private Interface =========================*/
50
51CCC_Result CCC_private_flat_bitset_reserve(
52 struct CCC_Flat_bitset *, size_t, CCC_Allocator const *
53);
55CCC_private_flat_bitset_set(struct CCC_Flat_bitset *, size_t, CCC_Tribool);
56
57/*================================ Macros ===========================*/
58
61#define CCC_private_flat_bitset_block_count(private_bit_capacity) \
62 (((private_bit_capacity) + (CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS - 1)) \
63 / CCC_PRIVATE_FLAT_BITSET_BLOCK_BITS)
64
66#define CCC_private_flat_bitset_block_bytes(private_bit_capacity) \
67 (sizeof(*(struct CCC_Flat_bitset){}.blocks) \
68 * CCC_private_flat_bitset_block_count(private_bit_capacity))
69
71#define CCC_private_flat_bitset_default() \
72 (struct CCC_Flat_bitset) { \
73 }
74
76#define CCC_private_flat_bitset_non_CCC_private_flat_bitset_default_size( \
77 private_cap, ... \
78) \
79 __VA_ARGS__
81#define CCC_private_flat_bitset_default_size(private_cap, ...) private_cap
83#define CCC_private_flat_bitset_optional_size(private_cap, ...) \
84 __VA_OPT__(CCC_private_flat_bitset_non_) \
85 ##CCC_private_flat_bitset_default_size(private_cap, __VA_ARGS__)
86
89#define CCC_private_flat_bitset_for( \
90 private_cap, private_count, private_bitblock_pointer \
91) \
92 (struct CCC_Flat_bitset) { \
93 .blocks = memset( \
94 (private_bitblock_pointer), \
95 0, \
96 CCC_private_flat_bitset_block_bytes(private_cap) \
97 ), \
98 .count = (private_count), .capacity = (private_cap), \
99 }
100
103#define CCC_private_flat_bitset_from( \
104 private_allocator, \
105 private_start_index, \
106 private_count, \
107 private_on_char, \
108 private_string, \
109 ... \
110) \
111 (struct { struct CCC_Flat_bitset private; }){(__extension__({ \
112 struct CCC_Flat_bitset private_bitset \
113 = CCC_private_flat_bitset_default(); \
114 size_t const private_cap = CCC_private_flat_bitset_optional_size( \
115 (private_count), __VA_ARGS__ \
116 ); \
117 size_t private_index = (private_start_index); \
118 if (CCC_private_flat_bitset_reserve( \
119 &private_bitset, \
120 private_cap < private_count ? private_count : private_cap, \
121 &private_allocator \
122 ) \
123 == CCC_RESULT_OK) { \
124 private_bitset.count = private_count; \
125 while (private_index < private_count \
126 && private_string[private_index]) { \
127 (void)CCC_private_flat_bitset_set( \
128 &private_bitset, \
129 private_index, \
130 (CCC_Tribool)(private_string[private_index] \
131 == private_on_char) \
132 ); \
133 ++private_index; \
134 } \
135 private_bitset.count = private_index; \
136 } \
137 private_bitset; \
138 }))}.private
139
141#define CCC_private_flat_bitset_with_capacity( \
142 private_allocate, private_cap, ... \
143) \
144 (struct { struct CCC_Flat_bitset private; }){(__extension__({ \
145 struct CCC_Flat_bitset private_bitset \
146 = CCC_private_flat_bitset_default(); \
147 size_t const private_count = CCC_private_flat_bitset_optional_size( \
148 (private_cap), __VA_ARGS__ \
149 ); \
150 if (CCC_private_flat_bitset_reserve( \
151 &private_bitset, private_cap, &private_allocate \
152 ) \
153 == CCC_RESULT_OK) { \
154 private_bitset.count = private_count; \
155 } \
156 private_bitset; \
157 }))}.private
158
163#if defined(__clang__) || defined(__llvm__)
167# define CCC_private_flat_bitset_count_check_storage_for( \
168 private_count, \
169 private_bit_compound_literal, \
170 private_optional_storage_specifier... \
171 ) \
172 (private_optional_storage_specifier struct { \
173 static_assert( \
174 sizeof(private_bit_compound_literal), \
175 "Specify non-zero capacity of bits." \
176 ); \
177 static_assert( \
178 sizeof(*(private_bit_compound_literal)) == sizeof(CCC_Bit), \
179 "CCC_flat_bitset_storage_for and " \
180 "CCC_flat_bitset_with_storage only " \
181 "accept " \
182 "a (CCC_Bit[N]){} compound literal array as an argument. Do " \
183 "not " \
184 "use CCC_flat_bitset_storage_for as an argument to " \
185 "CCC_flat_bitset_with_storage." \
186 ); \
187 static_assert( \
188 (private_count) <= sizeof(private_bit_compound_literal), \
189 "Bit count is less than or equal to capacity." \
190 ); \
191 typeof(*(struct CCC_Flat_bitset){}.blocks) private \
192 [CCC_private_flat_bitset_block_count( \
193 sizeof(private_bit_compound_literal) \
194 )]; \
195 }){} \
196 .private
197
199# define CCC_private_flat_bitset_storage_for( \
200 private_bit_compound_literal, private_optional_storage_specifier... \
201 ) \
202 CCC_private_flat_bitset_count_check_storage_for( \
203 0, \
204 private_bit_compound_literal, \
205 private_optional_storage_specifier \
206 )
207
208#else
210# define CCC_private_flat_bitset_count_check_storage_for( \
211 private_count, \
212 private_bit_compound_literal, \
213 private_optional_storage_specifier... \
214 ) \
215 (typeof ( \
216 *(struct CCC_Flat_bitset){}.blocks \
217 )[CCC_private_flat_bitset_block_count( \
218 sizeof(private_bit_compound_literal) \
219 )]) { \
220 }
221
223# define CCC_private_flat_bitset_storage_for( \
224 private_bit_compound_literal, private_optional_storage_specifier... \
225 ) \
226 CCC_private_flat_bitset_count_check_storage_for( \
227 0, \
228 private_bit_compound_literal, \
229 private_optional_storage_specifier \
230 )
231#endif
232
234#define CCC_private_flat_bitset_with_storage( \
235 private_count, \
236 private_compound_literal_array, \
237 private_optional_storage_specifier... \
238) \
239 (struct CCC_Flat_bitset) { \
240 .blocks = CCC_private_flat_bitset_count_check_storage_for( \
241 private_count, \
242 private_compound_literal_array, \
243 private_optional_storage_specifier \
244 ), \
245 .count = (private_count), \
246 .capacity = CCC_private_flat_bitset_block_count( \
247 sizeof(private_compound_literal_array) \
248 ) \
249 * sizeof(*(struct CCC_Flat_bitset){}.blocks) * CHAR_BIT, \
250 }
251
252#endif /* CCC_PRIVATE_BITSET */
The type passed by reference to any container function that may need to allocate memory....
Definition: types.h:369
Definition: private_flat_bitset.h:34
size_t capacity
Definition: private_flat_bitset.h:40
unsigned * blocks
Definition: private_flat_bitset.h:36
size_t count
Definition: private_flat_bitset.h:38
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