Derive-C
Loading...
Searching...
No Matches
template.h File Reference

Go to the source code of this file.

Data Structures

struct  SELF
 An allocator that prints to stdout when it allocates or frees memory. More...
struct  ITER_CONST
struct  ITER

Macros

#define EXCLUSIVE_END_INDEX   32
 A simple bitset for indexes [0, EXCLUSIVE_END_INDEX), statically allocated.
#define INDICES_CAPACITY   EXCLUSIVE_END_INDEX
#define INVARIANT_CHECK(self)
#define ITER_INDEX_TYPE   INDEX_LARGER_TYPE
#define ITER_CONST   NS(SELF, iter_const)
#define ITER   NS(SELF, iter)

Typedefs

typedef INDEX_TYPE index_t
typedef ITER_INDEX_TYPE item

Functions

 DC_STATIC_ASSERT (EXCLUSIVE_END_INDEX > 0, DC_EXPAND_STRING(SELF) " EXCLUSIVE_END_INDEX must be larger than 0 for nonempty bitset")
static DC_PUBLIC SELF new ()
static DC_PUBLIC bool try_set (SELF *self, INDEX_TYPE index, bool value)
static DC_PUBLIC void set (SELF *self, INDEX_TYPE index, bool value)
static DC_PUBLIC bool get (SELF const *self, INDEX_TYPE index)
static DC_PUBLIC void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC SELF clone (SELF const *self)
static DC_PUBLIC size_t size (SELF const *self)
static DC_PUBLIC void delete (SELF *self)
static DC_PUBLIC bool empty_item (ITER_INDEX_TYPE const *item)
static DC_PUBLIC ITER_INDEX_TYPE next (ITER_CONST *iter)
static DC_PUBLIC bool empty (ITER_CONST const *iter)
static DC_PUBLIC ITER_CONST get_iter_const (SELF const *self)
static DC_PUBLIC ITER_INDEX_TYPE next (ITER *iter)
static DC_PUBLIC bool empty (ITER const *iter)
static DC_PUBLIC ITER get_iter (SELF *self)
 DC_TRAIT_BITSET (SELF)

Variables

DC_STATIC_CONSTANT INDEX_TYPE max_index = EXCLUSIVE_END_INDEX - 1
DC_STATIC_CONSTANT INDEX_TYPE min_index = 0

Macro Definition Documentation

◆ EXCLUSIVE_END_INDEX

#define EXCLUSIVE_END_INDEX   32

A simple bitset for indexes [0, EXCLUSIVE_END_INDEX), statically allocated.

Definition at line 14 of file template.h.

◆ INDICES_CAPACITY

#define INDICES_CAPACITY   EXCLUSIVE_END_INDEX

Definition at line 21 of file template.h.

◆ INVARIANT_CHECK

#define INVARIANT_CHECK ( self)
Value:
DC_ASSUME(self);
#define DC_ASSUME(expr,...)
Definition panic.h:57

Definition at line 37 of file template.h.

◆ ITER

#define ITER   NS(SELF, iter)

Definition at line 199 of file template.h.

◆ ITER_CONST

#define ITER_CONST   NS(SELF, iter_const)

Definition at line 147 of file template.h.

◆ ITER_INDEX_TYPE

#define ITER_INDEX_TYPE   INDEX_LARGER_TYPE

Definition at line 144 of file template.h.

Typedef Documentation

◆ index_t

typedef INDEX_TYPE index_t

Definition at line 26 of file template.h.

◆ item

Definition at line 153 of file template.h.

Function Documentation

◆ clone()

DC_PUBLIC SELF clone ( SELF const * self)
static

Definition at line 115 of file template.h.

115 {
116 INVARIANT_CHECK(self);
117 SELF new_self = (SELF){
118 .derive_c_bitset_static = dc_gdb_marker_new(),
119 .iterator_invalidation_tracker = mutation_tracker_new(),
120 };
121 memcpy(&new_self.bits, &self->bits, sizeof(self->bits));
122 return new_self;
123}
#define INVARIANT_CHECK(self)
Definition template.h:90
#define SELF
Definition def.h:52
static DC_PUBLIC dc_gdb_marker dc_gdb_marker_new()
Definition gdb_marker.h:8
static DC_PUBLIC mutation_tracker mutation_tracker_new()
uint8_t bits[DC_BITSET_STATIC_CAPACITY_TO_BYTES(EXCLUSIVE_END_INDEX)]
Definition template.h:32

◆ DC_STATIC_ASSERT()

DC_STATIC_ASSERT ( EXCLUSIVE_END_INDEX ,
0 ,
DC_EXPAND_STRING(SELF) " EXCLUSIVE_END_INDEX must be larger than 0 for nonempty bitset"  )

◆ DC_TRAIT_BITSET()

DC_TRAIT_BITSET ( SELF )

◆ debug()

DC_PUBLIC void debug ( SELF const * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 95 of file template.h.

95 {
96 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p {\n", (void*)self);
97 fmt = dc_debug_fmt_scope_begin(fmt);
98
99 dc_debug_fmt_print(fmt, stream, "blocks: [\n");
100 fmt = dc_debug_fmt_scope_begin(fmt);
101 for (size_t index = 0; index < EXCLUSIVE_END_INDEX; index++) {
102 if (NS(SELF, get)(self, (INDEX_TYPE)index)) {
103 dc_debug_fmt_print(fmt, stream, "{ byte: %lu, offset: %lu, index: %lu},\n",
106 }
107 }
108 fmt = dc_debug_fmt_scope_end(fmt);
109 dc_debug_fmt_print(fmt, stream, "],\n");
110
111 fmt = dc_debug_fmt_scope_end(fmt);
112 dc_debug_fmt_print(fmt, stream, "}");
113}
#define DC_BITSET_STATIC_INDEX_TO_OFFSET(INDEX)
Definition utils.h:5
#define DC_BITSET_STATIC_INDEX_TO_BYTES(INDEX)
Definition utils.h:4
static DC_PUBLIC bool get(SELF const *self, INDEX_TYPE index)
Definition template.h:80
#define EXCLUSIVE_END_INDEX
A simple bitset for indexes [0, EXCLUSIVE_END_INDEX), statically allocated.
Definition template.h:14
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define NS(pre, post)
Definition namespace.h:14
#define DC_EXPAND_STRING(NAME)
Definition namespace.h:6
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ delete()

DC_PUBLIC void delete ( SELF * self)
static

Definition at line 136 of file template.h.

136{ INVARIANT_CHECK(self); }

◆ empty() [1/2]

DC_PUBLIC bool empty ( ITER const * iter)
static

Definition at line 228 of file template.h.

228 {
229 DC_ASSUME(iter);
230 mutation_version_check(&iter->version);
231 return iter->next_index >= EXCLUSIVE_END_INDEX;
232}
static DC_PUBLIC void mutation_version_check(mutation_version const *self)

◆ empty() [2/2]

DC_PUBLIC bool empty ( ITER_CONST const * iter)
static

Definition at line 176 of file template.h.

176 {
177 DC_ASSUME(iter);
178 mutation_version_check(&iter->version);
179 return iter->next_index >= EXCLUSIVE_END_INDEX;
180}

◆ empty_item()

DC_PUBLIC bool empty_item ( ITER_INDEX_TYPE const * item)
static

Definition at line 155 of file template.h.

155 {
156 return *item == EXCLUSIVE_END_INDEX;
157}
IV_PAIR item
Definition template.h:281

◆ get()

DC_PUBLIC bool get ( SELF const * self,
INDEX_TYPE index )
static

Definition at line 80 of file template.h.

80 {
81 INVARIANT_CHECK(self);
82
83#if EXCLUSIVE_END_INDEX <= MAX_INDEX
85 "Index out of bounds {index=%lu, exclusive_end_index=%d}", (size_t)index,
87#endif
88
89 INDEX_TYPE byte = DC_BITSET_STATIC_INDEX_TO_BYTES(index);
90 INDEX_TYPE offset = DC_BITSET_STATIC_INDEX_TO_OFFSET(index);
91 uint8_t mask = DC_BITSET_STATIC_OFFSET_TO_MASK(offset);
92 return (self->bits[byte] & mask) != 0;
93}
#define DC_BITSET_STATIC_OFFSET_TO_MASK(OFFSET)
Definition utils.h:6
#define DC_ASSERT(expr,...)
Definition panic.h:37

◆ get_iter()

DC_PUBLIC ITER get_iter ( SELF * self)
static

Definition at line 234 of file template.h.

234 {
235 INVARIANT_CHECK(self);
236
237 ITER_INDEX_TYPE next_index = 0;
238 while (next_index < EXCLUSIVE_END_INDEX && !NS(SELF, get)(self, (INDEX_TYPE)next_index)) {
239 next_index++;
240 }
241
242 return (ITER){
243 .bitset = self,
244 .next_index = next_index,
246 };
247}
#define ITER
Definition template.h:322
#define ITER_INDEX_TYPE
Definition template.h:144
static DC_PUBLIC mutation_version mutation_tracker_get(mutation_tracker const *self)
mutation_tracker iterator_invalidation_tracker
Definition template.h:87

◆ get_iter_const()

DC_PUBLIC ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 182 of file template.h.

182 {
183 INVARIANT_CHECK(self);
184
185 ITER_INDEX_TYPE next_index = 0;
186 while (next_index < EXCLUSIVE_END_INDEX && !NS(SELF, get)(self, (INDEX_TYPE)next_index)) {
187 next_index++;
188 }
189
190 return (ITER_CONST){
191 .bitset = self,
192 .next_index = next_index,
193 .version = mutation_tracker_get(&self->iterator_invalidation_tracker),
194 };
195}
#define ITER_CONST
Definition template.h:411

◆ new()

DC_PUBLIC SELF new ( )
static

Definition at line 39 of file template.h.

39 {
40 return (SELF){
41 .bits = {},
42 .derive_c_bitset_static = dc_gdb_marker_new(),
43 .iterator_invalidation_tracker = mutation_tracker_new(),
44 };
45}

◆ next() [1/2]

DC_PUBLIC ITER_INDEX_TYPE next ( ITER * iter)
static

Definition at line 211 of file template.h.

211 {
212 DC_ASSUME(iter);
214
215 if (iter->next_index == EXCLUSIVE_END_INDEX) {
216 return EXCLUSIVE_END_INDEX;
217 }
218
219 ITER_INDEX_TYPE next_index = iter->next_index;
220 iter->next_index++;
221 while (iter->next_index < EXCLUSIVE_END_INDEX &&
222 !NS(SELF, get)(iter->bitset, (INDEX_TYPE)iter->next_index)) {
223 iter->next_index++;
224 }
225 return next_index;
226}
INDEX_TYPE next_index
Definition template.h:327
mutation_version version
Definition template.h:328
SELF * bitset
Definition template.h:201

◆ next() [2/2]

DC_PUBLIC ITER_INDEX_TYPE next ( ITER_CONST * iter)
static

Definition at line 159 of file template.h.

159 {
160 DC_ASSUME(iter);
162
163 if (iter->next_index == EXCLUSIVE_END_INDEX) {
164 return EXCLUSIVE_END_INDEX;
165 }
166
167 ITER_INDEX_TYPE next_index = iter->next_index;
168 iter->next_index++;
169 while (iter->next_index < EXCLUSIVE_END_INDEX &&
170 !NS(SELF, get)(iter->bitset, (INDEX_TYPE)iter->next_index)) {
171 iter->next_index++;
172 }
173 return next_index;
174}
INDEX_TYPE next_index
Definition template.h:416
SELF const * bitset
Definition template.h:149
mutation_version version
Definition template.h:417

◆ set()

DC_PUBLIC void set ( SELF * self,
INDEX_TYPE index,
bool value )
static
Examples
container/set.c.

Definition at line 73 of file template.h.

73 {
74 INVARIANT_CHECK(self);
75 DC_ASSERT(NS(SELF, try_set)(self, index, value),
76 "Failed to set index {index=%lu, value=%d, exclusive_end_index=%d}", (size_t)index,
77 value, EXCLUSIVE_END_INDEX);
78}
static DC_PUBLIC bool try_set(SELF *self, INDEX_TYPE index, bool value)
Definition template.h:47

◆ size()

DC_PUBLIC size_t size ( SELF const * self)
static

Definition at line 125 of file template.h.

125 {
126 size_t size = 0;
127
128 for (INDEX_TYPE byte = 0;
129 byte < (INDEX_TYPE)DC_BITSET_STATIC_CAPACITY_TO_BYTES(EXCLUSIVE_END_INDEX); byte++) {
130 size += (uint32_t)__builtin_popcount((unsigned int)self->bits[byte]);
131 }
132
133 return size;
134}
#define DC_BITSET_STATIC_CAPACITY_TO_BYTES(CAPACITY)
Definition utils.h:3
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252

◆ try_set()

DC_PUBLIC bool try_set ( SELF * self,
INDEX_TYPE index,
bool value )
static

Definition at line 47 of file template.h.

47 {
48 INVARIANT_CHECK(self);
50
51 // JUSTIFY: Only checking if the end index could be smaller than max
52 // - If exclusive end is 1 larger than the max index, this check does not work.
53 // e.g. exclusive end is 256. So index is uint8_t, which means we can never represent an
54 // incorrect index, so no check.
55#if EXCLUSIVE_END_INDEX <= MAX_INDEX
56 if (index >= EXCLUSIVE_END_INDEX) {
57 return false;
58 }
59#endif
60
61 INDEX_TYPE byte = DC_BITSET_STATIC_INDEX_TO_BYTES(index);
62 INDEX_TYPE offset = DC_BITSET_STATIC_INDEX_TO_OFFSET(index);
63 uint8_t mask = DC_BITSET_STATIC_OFFSET_TO_MASK(offset);
64
65 if (value) {
66 self->bits[byte] = self->bits[byte] | mask;
67 } else {
68 self->bits[byte] = self->bits[byte] & (~mask);
69 }
70 return true;
71}
static DC_PUBLIC void mutation_tracker_mutate(mutation_tracker *self)

Variable Documentation

◆ max_index

DC_STATIC_CONSTANT INDEX_TYPE max_index = EXCLUSIVE_END_INDEX - 1

Definition at line 28 of file template.h.

◆ min_index

DC_STATIC_CONSTANT INDEX_TYPE min_index = 0

Definition at line 29 of file template.h.