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>
24#include "../types.h"
25
33{
35 unsigned *blocks;
37 size_t count;
39 size_t capacity;
43 void *context;
44};
45
46enum : size_t
47{
49 CCC_PRIVATE_BITSET_BLOCK_BITS
50 = (sizeof(*(struct CCC_Bitset){}.blocks) * CHAR_BIT),
51};
52
53/*========================= Private Interface =========================*/
54
55CCC_Result CCC_private_bitset_reserve(struct CCC_Bitset *, size_t,
57CCC_Tribool CCC_private_bitset_set(struct CCC_Bitset *, size_t, CCC_Tribool);
58
59/*================================ Macros ===========================*/
60
63#define CCC_private_bitset_block_count(private_bit_cap) \
64 (((private_bit_cap) + (CCC_PRIVATE_BITSET_BLOCK_BITS - 1)) \
65 / CCC_PRIVATE_BITSET_BLOCK_BITS)
66
68#define CCC_private_bitset_block_bytes(private_bit_cap) \
69 (sizeof(*(struct CCC_Bitset){}.blocks) * (private_bit_cap))
70
74#define CCC_private_bitset_blocks(private_bit_cap, ...) \
75 (__VA_OPT__(__VA_ARGS__) typeof ( \
76 *(struct CCC_Bitset){} \
77 .blocks)[CCC_private_bitset_block_count(private_bit_cap)]) \
78 {}
79
81#define CCC_private_bitset_non_CCC_private_bitset_default_size(private_cap, \
82 ...) \
83 __VA_ARGS__
85#define CCC_private_bitset_default_size(private_cap, ...) private_cap
87#define CCC_private_bitset_optional_size(private_cap, ...) \
88 __VA_OPT__(CCC_private_bitset_non_) \
89 ##CCC_private_bitset_default_size(private_cap, __VA_ARGS__)
90
96#define CCC_private_bitset_initialize(private_bitblock_pointer, \
97 private_allocate, private_context, \
98 private_cap, ...) \
99 { \
100 .blocks = (private_bitblock_pointer), \
101 .count = CCC_private_bitset_optional_size((private_cap), __VA_ARGS__), \
102 .capacity = (private_cap), \
103 .allocate = (private_allocate), \
104 .context = (private_context), \
105 }
106
109static inline struct CCC_Bitset
110CCC_private_bitset_with_capacity_fn(CCC_Allocator *const private_allocate,
111 void *const private_context,
112 size_t const private_cap,
113 size_t const private_count)
114{
115 struct CCC_Bitset b = CCC_private_bitset_initialize(NULL, private_allocate,
116 private_context, 0);
117 if (CCC_private_bitset_reserve(&b, private_cap, private_allocate)
118 == CCC_RESULT_OK)
119 {
120 b.count = private_count;
121 }
122 return b;
123}
124
127#define CCC_private_bitset_from(private_allocate, private_context, \
128 private_start_index, private_count, \
129 private_on_char, private_string, ...) \
130 (__extension__({ \
131 struct CCC_Bitset private_bitset = CCC_private_bitset_initialize( \
132 NULL, private_allocate, private_context, 0); \
133 size_t const private_cap \
134 = CCC_private_bitset_optional_size((private_count), __VA_ARGS__); \
135 size_t private_index = (private_start_index); \
136 if (CCC_private_bitset_reserve( \
137 &private_bitset, \
138 private_cap < private_count ? private_count : private_cap, \
139 private_allocate) \
140 == CCC_RESULT_OK) \
141 { \
142 private_bitset.count = private_count; \
143 while (private_index < private_count \
144 && private_string[private_index]) \
145 { \
146 (void)CCC_private_bitset_set(&private_bitset, private_index, \
147 private_string[private_index] \
148 == private_on_char); \
149 ++private_index; \
150 } \
151 private_bitset.count = private_index; \
152 } \
153 private_bitset; \
154 }))
155
157#define CCC_private_bitset_with_capacity(private_allocate, private_context, \
158 private_cap, ...) \
159 (__extension__({ \
160 struct CCC_Bitset private_bitset = CCC_private_bitset_initialize( \
161 NULL, private_allocate, private_context, 0); \
162 size_t const private_count \
163 = CCC_private_bitset_optional_size((private_cap), __VA_ARGS__); \
164 if (CCC_private_bitset_reserve(&private_bitset, private_cap, \
165 private_allocate) \
166 == CCC_RESULT_OK) \
167 { \
168 private_bitset.count = private_count; \
169 } \
170 private_bitset; \
171 }))
172
174#define CCC_private_bitset_with_compound_literal( \
175 private_count, private_compound_literal_array) \
176 { \
177 .blocks = (private_compound_literal_array), \
178 .count = (private_count), \
179 .capacity = sizeof(private_compound_literal_array) * CHAR_BIT, \
180 .allocate = NULL, \
181 .context = NULL, \
182 }
183
185#define CCC_private_bitset_with_context_compound_literal( \
186 private_context, private_count, private_compound_literal_array) \
187 { \
188 .blocks = (private_compound_literal_array), \
189 .count = (private_count), \
190 .capacity = sizeof(private_compound_literal_array) * CHAR_BIT, \
191 .allocate = NULL, \
192 .context = (private_context), \
193 }
194
195#endif /* CCC_PRIVATE_BITSET */
Definition: private_bitset.h:33
void * context
Definition: private_bitset.h:43
unsigned * blocks
Definition: private_bitset.h:35
CCC_Allocator * allocate
Definition: private_bitset.h:41
size_t count
Definition: private_bitset.h:37
size_t capacity
Definition: private_bitset.h:39
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:133
CCC_Result
A result of actions on containers.
Definition: types.h:148
@ CCC_RESULT_OK
Definition: types.h:150
void * CCC_Allocator(CCC_Allocator_context)
An allocation function at the core of all containers.
Definition: types.h:340