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

Go to the source code of this file.

Data Structures

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

Macros

#define CAPACITY   32
 A statically allocated map that linearly looks up keys.
#define KEY   map_key_t
#define KEY_EQ   DC_MEM_EQ
#define KEY_DELETE   DC_NO_DELETE
#define KEY_CLONE   DC_COPY_CLONE
#define KEY_DEBUG   DC_DEFAULT_DEBUG
#define VALUE   value_t
#define VALUE_DELETE   DC_NO_DELETE
#define VALUE_CLONE   DC_COPY_CLONE
#define VALUE_DEBUG   DC_DEFAULT_DEBUG
#define ENTRY   NS(SELF, entry_t)
#define INVARIANT_CHECK(self)
#define ITER_CONST   NS(SELF, iter_const)
#define KV_PAIR_CONST   NS(ITER_CONST, item)
#define ITER   NS(SELF, iter)
#define KV_PAIR   NS(ITER, item)

Typedefs

typedef int KEY

Functions

 DC_STATIC_ASSERT (CAPACITY > 0, DC_EXPAND_STRING(SELF) " CAPACITY cannot be empty")
static DC_PUBLIC SELF new ()
static DC_PUBLIC VALUE const * try_read (SELF const *self, KEY key)
static DC_PUBLIC VALUE const * read (SELF const *self, KEY key)
static DC_PUBLIC VALUEtry_write (SELF *self, KEY key)
static DC_PUBLIC VALUEwrite (SELF *self, KEY key)
static DC_PUBLIC VALUEtry_insert (SELF *self, KEY key, VALUE value)
static DC_PUBLIC VALUEinsert (SELF *self, KEY key, VALUE value)
static DC_PUBLIC bool try_remove (SELF *self, KEY key, VALUE *dest)
static DC_PUBLIC VALUE remove (SELF *self, KEY key)
static DC_PUBLIC void delete_entry (SELF *self, KEY key)
static DC_PUBLIC size_t size (SELF const *self)
static DC_PUBLIC SELF clone (SELF const *self)
static DC_PUBLIC void delete (SELF *self)
static DC_PUBLIC void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
static DC_PUBLIC bool empty_item (KV_PAIR_CONST const *item)
static DC_PUBLIC KV_PAIR_CONST 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 bool empty_item (KV_PAIR const *item)
static DC_PUBLIC KV_PAIR next (ITER *iter)
static DC_PUBLIC bool empty (ITER const *iter)
static DC_PUBLIC ITER get_iter (SELF *self)
 DC_TRAIT_MAP (SELF)

Variables

DC_STATIC_CONSTANT size_t max_capacity = (size_t)CAPACITY

Macro Definition Documentation

◆ CAPACITY

#define CAPACITY   32

A statically allocated map that linearly looks up keys.

Definition at line 14 of file template.h.

◆ ENTRY

#define ENTRY   NS(SELF, entry_t)

Definition at line 68 of file template.h.

◆ INVARIANT_CHECK

#define INVARIANT_CHECK ( self)
Value:
DC_ASSUME(self); \
DC_ASSUME((self)->size <= CAPACITY);
#define CAPACITY
A hybrid of a bump allocator on a statically allocated buffer, and any other allocator.
Definition template.h:18
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
#define DC_ASSUME(expr,...)
Definition panic.h:57

Definition at line 84 of file template.h.

84#define INVARIANT_CHECK(self) \
85 DC_ASSUME(self); \
86 DC_ASSUME((self)->size <= CAPACITY);

◆ ITER

#define ITER   NS(SELF, iter)

Definition at line 280 of file template.h.

◆ ITER_CONST

#define ITER_CONST   NS(SELF, iter_const)

Definition at line 227 of file template.h.

◆ KEY

#define KEY   map_key_t

Definition at line 23 of file template.h.

◆ KEY_CLONE

#define KEY_CLONE   DC_COPY_CLONE

Definition at line 36 of file template.h.

◆ KEY_DEBUG

#define KEY_DEBUG   DC_DEFAULT_DEBUG

Definition at line 40 of file template.h.

◆ KEY_DELETE

#define KEY_DELETE   DC_NO_DELETE

Definition at line 32 of file template.h.

◆ KEY_EQ

#define KEY_EQ   DC_MEM_EQ

Definition at line 28 of file template.h.

◆ KV_PAIR

#define KV_PAIR   NS(ITER, item)

Definition at line 281 of file template.h.

◆ KV_PAIR_CONST

#define KV_PAIR_CONST   NS(ITER_CONST, item)

Definition at line 228 of file template.h.

◆ VALUE

#define VALUE   value_t

Definition at line 50 of file template.h.

◆ VALUE_CLONE

#define VALUE_CLONE   DC_COPY_CLONE

Definition at line 58 of file template.h.

◆ VALUE_DEBUG

#define VALUE_DEBUG   DC_DEFAULT_DEBUG

Definition at line 62 of file template.h.

◆ VALUE_DELETE

#define VALUE_DELETE   DC_NO_DELETE

Definition at line 54 of file template.h.

Typedef Documentation

◆ KEY

typedef int KEY

Definition at line 24 of file template.h.

Function Documentation

◆ clone()

DC_PUBLIC SELF clone ( SELF const * self)
static

Definition at line 182 of file template.h.

182 {
183 INVARIANT_CHECK(self);
184
185 SELF new_self = (SELF){
186 .size = self->size,
187 .derive_c_map_staticlinear = dc_gdb_marker_new(),
188 .iterator_invalidation_tracker = mutation_tracker_new(),
189 };
190 for (size_t index = 0; index < self->size; index++) {
191 new_self.entries[index].key = KEY_CLONE(&self->entries[index].key);
192 new_self.entries[index].value = VALUE_CLONE(&self->entries[index].value);
193 }
194 return new_self;
195}
#define INVARIANT_CHECK(self)
Definition template.h:90
#define VALUE_CLONE
Definition template.h:39
#define KEY_CLONE
Definition template.h:39
#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()
VALUE value
Definition template.h:71
KEY key
Definition template.h:70
ENTRY entries[CAPACITY]
Definition template.h:76

◆ DC_STATIC_ASSERT()

DC_STATIC_ASSERT ( CAPACITY ,
0 ,
DC_EXPAND_STRING(SELF) " CAPACITY cannot be empty"  )

◆ DC_TRAIT_MAP()

DC_TRAIT_MAP ( SELF )

◆ debug()

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

Definition at line 206 of file template.h.

206 {
207 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p {\n", (void*)self);
208 fmt = dc_debug_fmt_scope_begin(fmt);
209 dc_debug_fmt_print(fmt, stream, "capacity: %zu,\n", (size_t)CAPACITY);
210 dc_debug_fmt_print(fmt, stream, "size: %zu,\n", self->size);
211 dc_debug_fmt_print(fmt, stream, "entries: [\n");
212 fmt = dc_debug_fmt_scope_begin(fmt);
213 for (size_t index = 0; index < self->size; index++) {
214 dc_debug_fmt_print(fmt, stream, "{index: %lu, key: ", index);
215 KEY_DEBUG(&self->entries[index].key, fmt, stream);
216 fprintf(stream, ", value: ");
217 VALUE_DEBUG(&self->entries[index].value, fmt, stream);
218 fprintf(stream, "},\n");
219 }
220 fmt = dc_debug_fmt_scope_end(fmt);
221 dc_debug_fmt_print(fmt, stream, "],\n");
222
223 fmt = dc_debug_fmt_scope_end(fmt);
224 dc_debug_fmt_print(fmt, stream, "}");
225}
#define VALUE_DEBUG
Definition template.h:41
#define KEY_DEBUG
Definition template.h:43
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 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 197 of file template.h.

197 {
198 INVARIANT_CHECK(self);
199
200 for (size_t index = 0; index < self->size; index++) {
201 KEY_DELETE(&self->entries[index].key);
202 VALUE_DELETE(&self->entries[index].value);
203 }
204}
#define VALUE_DELETE
Definition template.h:37
#define KEY_DELETE
Definition template.h:35
size_t size
Definition template.h:75

◆ delete_entry()

DC_PUBLIC void delete_entry ( SELF * self,
KEY key )
static

Definition at line 175 of file template.h.

175 {
176 VALUE val = NS(SELF, remove)(self, key);
177 VALUE_DELETE(&val);
178}
#define VALUE
Definition template.h:35
static DC_PUBLIC VALUE remove(SELF *self, INDEX index)
Definition template.h:292
#define NS(pre, post)
Definition namespace.h:14

◆ empty() [1/2]

DC_PUBLIC bool empty ( ITER const * iter)
static

Definition at line 314 of file template.h.

314 {
315 DC_ASSUME(iter);
316 mutation_version_check(&iter->version);
317 return iter->next_index >= iter->map->size;
318}
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 261 of file template.h.

261 {
262 DC_ASSUME(iter);
263 mutation_version_check(&iter->version);
264 return iter->next_index >= iter->map->size;
265}

◆ empty_item() [1/2]

DC_PUBLIC bool empty_item ( KV_PAIR const * item)
static

Definition at line 294 of file template.h.

294 {
295 return item->key == NULL && item->value == NULL;
296}
IV_PAIR item
Definition template.h:281

◆ empty_item() [2/2]

DC_PUBLIC bool empty_item ( KV_PAIR_CONST const * item)
static

Definition at line 241 of file template.h.

241 {
242 return item->key == NULL && item->value == NULL;
243}

◆ get_iter()

DC_PUBLIC ITER get_iter ( SELF * self)
static

Definition at line 320 of file template.h.

320 {
321 INVARIANT_CHECK(self);
322
323 return (ITER){
324 .map = self,
325 .next_index = 0,
327 };
328}
#define ITER
Definition template.h:322
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 267 of file template.h.

267 {
268 INVARIANT_CHECK(self);
269
270 return (ITER_CONST){
271 .map = self,
272 .next_index = 0,
273 .version = mutation_tracker_get(&self->iterator_invalidation_tracker),
274 };
275}
#define ITER_CONST
Definition template.h:411

◆ insert()

DC_PUBLIC VALUE * insert ( SELF * self,
KEY key,
VALUE value )
static

Definition at line 142 of file template.h.

142 {
143 VALUE* placed = NS(SELF, try_insert)(self, key, value);
144 DC_ASSERT(placed, "Failed to insert item {key=%s, value=%s}", DC_DEBUG(KEY_DEBUG, &key),
145 DC_DEBUG(VALUE_DEBUG, &value));
146 return placed;
147}
#define KEY_DEBUG
Definition template.h:34
static DC_PUBLIC VALUE * try_insert(SELF *self, KEY key, VALUE value)
Definition template.h:318
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_ASSERT(expr,...)
Definition panic.h:37

◆ new()

DC_PUBLIC SELF new ( )
static

Definition at line 88 of file template.h.

88 {
89 return (SELF){
90 .size = 0,
91 .entries = {},
92 .derive_c_map_staticlinear = dc_gdb_marker_new(),
93 .iterator_invalidation_tracker = mutation_tracker_new(),
94 };
95}

◆ next() [1/2]

DC_PUBLIC KV_PAIR next ( ITER * iter)
static

Definition at line 298 of file template.h.

298 {
300 size_t const next_index = iter->next_index;
301
302 if (next_index >= iter->map->size) {
303 return (KV_PAIR){.key = NULL, .value = NULL};
304 }
305
306 iter->next_index++;
307
308 return (KV_PAIR){
309 .key = &iter->map->entries[next_index].key,
310 .value = &iter->map->entries[next_index].value,
311 };
312}
#define KV_PAIR
Definition template.h:585
INDEX_TYPE next_index
Definition template.h:327
mutation_version version
Definition template.h:328
SELF * map
Definition template.h:411

◆ next() [2/2]

DC_PUBLIC KV_PAIR_CONST next ( ITER_CONST * iter)
static

Definition at line 245 of file template.h.

245 {
247 size_t const next_index = iter->next_index;
248
249 if (next_index >= iter->map->size) {
250 return (KV_PAIR_CONST){.key = NULL, .value = NULL};
251 }
252
253 iter->next_index++;
254
255 return (KV_PAIR_CONST){
256 .key = &iter->map->entries[next_index].key,
257 .value = &iter->map->entries[next_index].value,
258 };
259}
#define KV_PAIR_CONST
Definition template.h:522
INDEX_TYPE next_index
Definition template.h:416
SELF const * map
Definition template.h:484
mutation_version version
Definition template.h:417

◆ read()

DC_PUBLIC VALUE const * read ( SELF const * self,
KEY key )
static

Definition at line 107 of file template.h.

107 {
108 VALUE const* value = NS(SELF, try_read)(self, key);
109 DC_ASSERT(value, "Cannot read item {key=%s}", DC_DEBUG(KEY_DEBUG, &key));
110 return value;
111}
static DC_PUBLIC VALUE const * try_read(SELF const *self, INDEX index)
Definition template.h:180

◆ remove()

DC_PUBLIC VALUE remove ( SELF * self,
KEY key )
static

Definition at line 168 of file template.h.

168 {
169 VALUE dest;
170 DC_ASSERT(NS(SELF, try_remove)(self, key, &dest), "Failed to remove item {key=%s}",
171 DC_DEBUG(KEY_DEBUG, &key));
172 return dest;
173}
static DC_PUBLIC bool try_remove(SELF *self, INDEX index, VALUE *destination)
Definition template.h:264

◆ size()

DC_PUBLIC size_t size ( SELF const * self)
static

Definition at line 180 of file template.h.

180{ return self->size; }

◆ try_insert()

DC_PUBLIC VALUE * try_insert ( SELF * self,
KEY key,
VALUE value )
static

Definition at line 123 of file template.h.

123 {
124 INVARIANT_CHECK(self);
126
127 if (self->size >= CAPACITY) {
128 return NULL;
129 }
130
131 VALUE const* existing_value = NS(SELF, try_read)(self, key);
132 if (existing_value) {
133 return NULL;
134 }
135
136 self->entries[self->size].key = key;
137 self->entries[self->size].value = value;
138 self->size++;
139 return &self->entries[self->size - 1].value;
140}
static DC_PUBLIC void mutation_tracker_mutate(mutation_tracker *self)

◆ try_read()

DC_PUBLIC VALUE const * try_read ( SELF const * self,
KEY key )
static

Definition at line 97 of file template.h.

97 {
98 INVARIANT_CHECK(self);
99 for (size_t index = 0; index < self->size; index++) {
100 if (KEY_EQ(&self->entries[index].key, &key)) {
101 return &self->entries[index].value;
102 }
103 }
104 return NULL;
105}
#define KEY_EQ
Definition template.h:31

◆ try_remove()

DC_PUBLIC bool try_remove ( SELF * self,
KEY key,
VALUE * dest )
static

Definition at line 149 of file template.h.

149 {
150 INVARIANT_CHECK(self);
152
153 for (size_t index = 0; index < self->size; index++) {
154 if (KEY_EQ(&self->entries[index].key, &key)) {
155 KEY_DELETE(&self->entries[index].key);
156 *dest = self->entries[index].value;
157 // Shift remaining entries down
158 for (size_t i = index; i < self->size - 1; i++) {
159 self->entries[i] = self->entries[i + 1];
160 }
161 self->size--;
162 return true;
163 }
164 }
165 return false;
166}

◆ try_write()

DC_PUBLIC VALUE * try_write ( SELF * self,
KEY key )
static

Definition at line 113 of file template.h.

113 {
114 return (VALUE*)(NS(SELF, try_read)(self, key));
115}

◆ write()

DC_PUBLIC VALUE * write ( SELF * self,
KEY key )
static

Definition at line 117 of file template.h.

117 {
118 VALUE* value = NS(SELF, try_write)(self, key);
119 DC_ASSERT(value, "Cannot write item {key=%s}", DC_DEBUG(KEY_DEBUG, &key));
120 return value;
121}
static DC_PUBLIC VALUE * try_write(SELF *self, INDEX index)
Definition template.h:205

Variable Documentation

◆ max_capacity

DC_STATIC_CONSTANT size_t max_capacity = (size_t)CAPACITY

Definition at line 82 of file template.h.