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 + 1)
#define INVARIANT_CHECK(self)
#define ITER_CONST   NS(SELF, iter_const)
#define ITER   NS(SELF, iter)

Typedefs

typedef INDEX_TYPE index_t
typedef INDEX_TYPE item

Functions

static INDEX_TYPE max_index ()
static INDEX_TYPE min_index ()
static SELF new ()
static bool try_set (SELF *self, INDEX_TYPE index, bool value)
static void set (SELF *self, INDEX_TYPE index, bool value)
static bool get (SELF const *self, INDEX_TYPE index)
static void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
static SELF clone (SELF const *self)
static INDEX_TYPE size (SELF const *self)
static void delete (SELF *self)
static bool empty_item (INDEX_TYPE const *item)
static INDEX_TYPE next (ITER_CONST *iter)
static ITER_CONST get_iter_const (SELF const *self)
static INDEX_TYPE next (ITER *iter)
static ITER get_iter (SELF *self)
 DC_TRAIT_BITSET (SELF)

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 + 1)

Definition at line 27 of file template.h.

◆ INVARIANT_CHECK

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

Definition at line 44 of file template.h.

◆ ITER

#define ITER   NS(SELF, iter)

Definition at line 179 of file template.h.

◆ ITER_CONST

#define ITER_CONST   NS(SELF, iter_const)

Definition at line 133 of file template.h.

Typedef Documentation

◆ index_t

Definition at line 32 of file template.h.

◆ item

typedef INDEX_TYPE item

Definition at line 139 of file template.h.

Function Documentation

◆ clone()

SELF clone ( SELF const * self)
static

Definition at line 110 of file template.h.

110 {
111 INVARIANT_CHECK(self);
112 SELF new_self = (SELF){
113 .derive_c_bitset_static = dc_gdb_marker_new(),
114 .iterator_invalidation_tracker = mutation_tracker_new(),
115 };
116 memcpy(&new_self.bits, &self->bits, sizeof(self->bits));
117 return new_self;
118}
#define INVARIANT_CHECK(self)
Definition template.h:88
static dc_gdb_marker dc_gdb_marker_new()
Definition gdb_marker.h:7
static mutation_tracker mutation_tracker_new()
#define SELF
Definition def.h:52
uint8_t bits[DC_BITSET_STATIC_CAPACITY_TO_BYTES(EXCLUSIVE_END_INDEX)]
Definition template.h:39

◆ DC_TRAIT_BITSET()

DC_TRAIT_BITSET ( SELF )

◆ debug()

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

Definition at line 90 of file template.h.

90 {
91 fprintf(stream, EXPAND_STRING(SELF) "@%p {\n", self);
92 fmt = dc_debug_fmt_scope_begin(fmt);
93
94 dc_debug_fmt_print(fmt, stream, "blocks: [");
95 fmt = dc_debug_fmt_scope_begin(fmt);
96 for (INDEX_TYPE index = 0; index < EXCLUSIVE_END_INDEX; index++) {
97 if (NS(SELF, get)(self, index)) {
98 dc_debug_fmt_print(fmt, stream, "(byte: %lu, offset: %lu, index: %lu)",
100 (size_t)DC_BITSET_STATIC_INDEX_TO_OFFSET(index), (size_t)index);
101 }
102 }
103 fmt = dc_debug_fmt_scope_end(fmt);
104 dc_debug_fmt_print(fmt, stream, "],\n");
105
106 fmt = dc_debug_fmt_scope_end(fmt);
107 dc_debug_fmt_print(fmt, stream, "}");
108}
#define DC_BITSET_STATIC_INDEX_TO_OFFSET(INDEX)
Definition utils.h:5
#define DC_BITSET_STATIC_INDEX_TO_BYTES(INDEX)
Definition utils.h:4
#define EXCLUSIVE_END_INDEX
A simple bitset for indexes [0, EXCLUSIVE_END_INDEX), statically allocated.
Definition template.h:14
#define INDEX_TYPE
Definition template.h:74
dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:39
dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:33
static void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:22
#define EXPAND_STRING(NAME)
Definition namespace.h:8
#define NS(pre, post)
Definition namespace.h:4
static nullalloc get()
Definition null.h:14
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107

◆ delete()

void delete ( SELF * self)
static

Definition at line 131 of file template.h.

131{ INVARIANT_CHECK(self); }

◆ empty_item()

bool empty_item ( INDEX_TYPE const * item)
static

Definition at line 141 of file template.h.

141 {
142 return *item == EXCLUSIVE_END_INDEX;
143}
IV_PAIR item
Definition template.h:283

◆ get()

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

Definition at line 79 of file template.h.

79 {
80 INVARIANT_CHECK(self);
81
83
86 uint8_t mask = DC_BITSET_STATIC_OFFSET_TO_MASK(offset);
87 return (self->bits[byte] & mask) != 0;
88}
#define DC_BITSET_STATIC_OFFSET_TO_MASK(OFFSET)
Definition utils.h:6
#define DC_ASSERT(expr,...)
Definition panic.h:36

◆ get_iter()

ITER get_iter ( SELF * self)
static

Definition at line 206 of file template.h.

206 {
207 INVARIANT_CHECK(self);
208
209 INDEX_TYPE next_index = 0;
210 while (next_index < EXCLUSIVE_END_INDEX && !NS(SELF, get)(self, next_index)) {
211 next_index++;
212 }
213
214 return (ITER){
215 .bitset = self,
216 .next_index = next_index,
218 };
219}
#define ITER
Definition template.h:320
static mutation_version mutation_tracker_get(mutation_tracker const *self)
mutation_tracker iterator_invalidation_tracker
Definition template.h:85

◆ get_iter_const()

ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 162 of file template.h.

162 {
163 INVARIANT_CHECK(self);
164
165 INDEX_TYPE next_index = 0;
166 while (next_index < EXCLUSIVE_END_INDEX && !NS(SELF, get)(self, next_index)) {
167 next_index++;
168 }
169
170 return (ITER_CONST){
171 .bitset = self,
172 .next_index = next_index,
173 .version = mutation_tracker_get(&self->iterator_invalidation_tracker),
174 };
175}
#define ITER_CONST
Definition template.h:398

◆ max_index()

INDEX_TYPE max_index ( )
static

Definition at line 34 of file template.h.

34{ return EXCLUSIVE_END_INDEX - 1; }

◆ min_index()

INDEX_TYPE min_index ( )
static

Definition at line 36 of file template.h.

36{ return 0; }

◆ new()

SELF new ( )
static

Definition at line 46 of file template.h.

46 {
47 return (SELF){
48 .bits = {},
49 .derive_c_bitset_static = dc_gdb_marker_new(),
50 .iterator_invalidation_tracker = mutation_tracker_new(),
51 };
52}

◆ next() [1/2]

INDEX_TYPE next ( ITER * iter)
static

Definition at line 189 of file template.h.

189 {
190 DC_ASSUME(iter);
192
193 if (iter->next_index == EXCLUSIVE_END_INDEX) {
194 return EXCLUSIVE_END_INDEX;
195 }
196
197 INDEX_TYPE next_index = iter->next_index;
198 iter->next_index++;
199 while (iter->next_index < EXCLUSIVE_END_INDEX &&
200 !NS(SELF, get)(iter->bitset, iter->next_index)) {
201 iter->next_index++;
202 }
203 return next_index;
204}
static void mutation_version_check(mutation_version const *self)
INDEX_TYPE next_index
Definition template.h:325
mutation_version version
Definition template.h:326
SELF * bitset
Definition template.h:181

◆ next() [2/2]

INDEX_TYPE next ( ITER_CONST * iter)
static

Definition at line 145 of file template.h.

145 {
146 DC_ASSUME(iter);
148
149 if (iter->next_index == EXCLUSIVE_END_INDEX) {
150 return EXCLUSIVE_END_INDEX;
151 }
152
153 INDEX_TYPE next_index = iter->next_index;
154 iter->next_index++;
155 while (iter->next_index < EXCLUSIVE_END_INDEX &&
156 !NS(SELF, get)(iter->bitset, iter->next_index)) {
157 iter->next_index++;
158 }
159 return next_index;
160}
INDEX_TYPE next_index
Definition template.h:403
SELF const * bitset
Definition template.h:135
mutation_version version
Definition template.h:404

◆ set()

void set ( SELF * self,
INDEX_TYPE index,
bool value )
static

Definition at line 74 of file template.h.

74 {
75 INVARIANT_CHECK(self);
76 DC_ASSERT(NS(SELF, try_set)(self, index, value));
77}
static bool try_set(SELF *self, INDEX_TYPE index, bool value)
Definition template.h:54

◆ size()

INDEX_TYPE size ( SELF const * self)
static

Definition at line 120 of file template.h.

120 {
121 INDEX_TYPE size = 0;
122
123 for (INDEX_TYPE byte = 0;
125 size += __builtin_popcount((unsigned int)self->bits[byte]);
126 }
127
128 return size;
129}
#define DC_BITSET_STATIC_CAPACITY_TO_BYTES(CAPACITY)
Definition utils.h:3
static INDEX_TYPE size(SELF const *self)
Definition template.h:252

◆ try_set()

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

Definition at line 54 of file template.h.

54 {
55 INVARIANT_CHECK(self);
57
58 if (index >= EXCLUSIVE_END_INDEX) {
59 return false;
60 }
61
64 uint8_t mask = DC_BITSET_STATIC_OFFSET_TO_MASK(offset);
65
66 if (value) {
67 self->bits[byte] = self->bits[byte] | mask;
68 } else {
69 self->bits[byte] = self->bits[byte] & (~mask);
70 }
71 return true;
72}
static void mutation_tracker_mutate(mutation_tracker *self)