C Container Collection (CCC)
Loading...
Searching...
No Matches
private_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 "../types.h"
26#include "configuration.h"
27
34struct CCC_Bitset {
36 unsigned *blocks;
38 size_t count;
40 size_t capacity;
41};
42
43enum : size_t {
45 CCC_PRIVATE_BITSET_BLOCK_BITS
46 = (sizeof(*(struct CCC_Bitset){}.blocks) * CHAR_BIT),
47};
48
49/*========================= Private Interface =========================*/
50
52CCC_private_bitset_reserve(struct CCC_Bitset *, size_t, CCC_Allocator const *);
53CCC_Tribool CCC_private_bitset_set(struct CCC_Bitset *, size_t, CCC_Tribool);
54
55/*================================ Macros ===========================*/
56
59#define CCC_private_bitset_block_count(private_bit_capacity) \
60 (((private_bit_capacity) + (CCC_PRIVATE_BITSET_BLOCK_BITS - 1)) \
61 / CCC_PRIVATE_BITSET_BLOCK_BITS)
62
64#define CCC_private_bitset_block_bytes(private_bit_capacity) \
65 (sizeof(*(struct CCC_Bitset){}.blocks) \
66 * CCC_private_bitset_block_count(private_bit_capacity))
67
69#define CCC_private_bitset_default() \
70 (struct CCC_Bitset) { \
71 }
72
74#define CCC_private_bitset_non_CCC_private_bitset_default_size( \
75 private_cap, ... \
76) \
77 __VA_ARGS__
79#define CCC_private_bitset_default_size(private_cap, ...) private_cap
81#define CCC_private_bitset_optional_size(private_cap, ...) \
82 __VA_OPT__(CCC_private_bitset_non_) \
83 ##CCC_private_bitset_default_size(private_cap, __VA_ARGS__)
84
87#define CCC_private_bitset_for( \
88 private_cap, private_count, private_bitblock_pointer \
89) \
90 (struct CCC_Bitset) { \
91 .blocks = memset( \
92 (private_bitblock_pointer), \
93 0, \
94 CCC_private_bitset_block_bytes(private_cap) \
95 ), \
96 .count = (private_count), .capacity = (private_cap), \
97 }
98
101#define CCC_private_bitset_from( \
102 private_allocator, \
103 private_start_index, \
104 private_count, \
105 private_on_char, \
106 private_string, \
107 ... \
108) \
109 (struct { struct CCC_Bitset private; }){(__extension__({ \
110 struct CCC_Bitset private_bitset = CCC_private_bitset_default(); \
111 size_t const private_cap \
112 = CCC_private_bitset_optional_size((private_count), __VA_ARGS__); \
113 size_t private_index = (private_start_index); \
114 if (CCC_private_bitset_reserve( \
115 &private_bitset, \
116 private_cap < private_count ? private_count : private_cap, \
117 &private_allocator \
118 ) \
119 == CCC_RESULT_OK) { \
120 private_bitset.count = private_count; \
121 while (private_index < private_count \
122 && private_string[private_index]) { \
123 (void)CCC_private_bitset_set( \
124 &private_bitset, \
125 private_index, \
126 private_string[private_index] == private_on_char \
127 ); \
128 ++private_index; \
129 } \
130 private_bitset.count = private_index; \
131 } \
132 private_bitset; \
133 }))}.private
134
136#define CCC_private_bitset_with_capacity(private_allocate, private_cap, ...) \
137 (struct { struct CCC_Bitset private; }){(__extension__({ \
138 struct CCC_Bitset private_bitset = CCC_private_bitset_default(); \
139 size_t const private_count \
140 = CCC_private_bitset_optional_size((private_cap), __VA_ARGS__); \
141 if (CCC_private_bitset_reserve( \
142 &private_bitset, private_cap, &private_allocate \
143 ) \
144 == CCC_RESULT_OK) { \
145 private_bitset.count = private_count; \
146 } \
147 private_bitset; \
148 }))}.private
149
154#if defined(__clang__) || defined(__llvm__)
158# define CCC_private_bitset_count_check_storage_for( \
159 private_count, \
160 private_bit_compound_literal, \
161 private_optional_storage_specifier... \
162 ) \
163 (private_optional_storage_specifier struct { \
164 static_assert( \
165 sizeof(private_bit_compound_literal), \
166 "Specify non-zero capacity of bits." \
167 ); \
168 static_assert( \
169 sizeof(*(private_bit_compound_literal)) == sizeof(CCC_Bit), \
170 "CCC_bitset_storage_for and CCC_bitset_with_storage only " \
171 "accept " \
172 "a (CCC_Bit[N]){} compound literal array as an argument. Do " \
173 "not " \
174 "use CCC_bitset_storage_for as an argument to " \
175 "CCC_bitset_with_storage." \
176 ); \
177 static_assert( \
178 (private_count) <= sizeof(private_bit_compound_literal), \
179 "Bit count is less than or equal to capacity." \
180 ); \
181 typeof(*(struct CCC_Bitset){}.blocks) private \
182 [CCC_private_bitset_block_count( \
183 sizeof(private_bit_compound_literal) \
184 )]; \
185 }){} \
186 .private
187
189# define CCC_private_bitset_storage_for( \
190 private_bit_compound_literal, private_optional_storage_specifier... \
191 ) \
192 CCC_private_bitset_count_check_storage_for( \
193 0, \
194 private_bit_compound_literal, \
195 private_optional_storage_specifier \
196 )
197
198#else
200# define CCC_private_bitset_count_check_storage_for( \
201 private_count, \
202 private_bit_compound_literal, \
203 private_optional_storage_specifier... \
204 ) \
205 (typeof ( \
206 *(struct CCC_Bitset){}.blocks \
207 )[CCC_private_bitset_block_count( \
208 sizeof(private_bit_compound_literal) \
209 )]) { \
210 }
211
213# define CCC_private_bitset_storage_for( \
214 private_bit_compound_literal, private_optional_storage_specifier... \
215 ) \
216 CCC_private_bitset_count_check_storage_for( \
217 0, \
218 private_bit_compound_literal, \
219 private_optional_storage_specifier \
220 )
221#endif
222
224#define CCC_private_bitset_with_storage( \
225 private_count, \
226 private_compound_literal_array, \
227 private_optional_storage_specifier... \
228) \
229 (struct CCC_Bitset) { \
230 .blocks = CCC_private_bitset_count_check_storage_for( \
231 private_count, \
232 private_compound_literal_array, \
233 private_optional_storage_specifier \
234 ), \
235 .count = (private_count), \
236 .capacity = CCC_private_bitset_block_count( \
237 sizeof(private_compound_literal_array) \
238 ) \
239 * sizeof(*(struct CCC_Bitset){}.blocks) * CHAR_BIT, \
240 }
241
242#endif /* CCC_PRIVATE_BITSET */
The C Container Collection Configuration Header. For full download and install instructions using a u...
The type passed by reference to any container function that may need to allocate memory....
Definition: types.h:376
Definition: private_bitset.h:34
unsigned * blocks
Definition: private_bitset.h:36
size_t count
Definition: private_bitset.h:38
size_t capacity
Definition: private_bitset.h:40
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