#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/core.h>
#include <derive-c/panic.h>
#include <derive-c/self.h>
#include <derive-c/allocs/std.h>
Go to the source code of this file.
|
static SELF | new_with_capacity_for (INDEX_TYPE items, ALLOC *alloc) |
static INDEX | insert (SELF *self, V value) |
static V * | try_write (SELF *self, INDEX index) |
static V * | write (SELF *self, INDEX index) |
static V const * | try_read (SELF const *self, INDEX index) |
static V const * | read (SELF const *self, INDEX index) |
static SELF | shallow_clone (SELF const *self) |
static INDEX_TYPE | size (SELF const *self) |
static bool | full (SELF const *self) |
static bool | try_remove (SELF *self, INDEX index, V *destination) |
static V | remove (SELF *self, INDEX index) |
static bool | delete_entry (SELF *self, INDEX index) |
static bool | empty (ITER const *iter) |
static IV_PAIR const * | next (ITER *iter) |
static size_t | position (ITER const *iter) |
static ITER | get_iter (SELF *self) |
static void | delete (SELF *self) |
static bool | empty (ITER_CONST const *iter) |
static IV_PAIR_CONST const * | next (ITER_CONST *iter) |
static size_t | position (ITER_CONST const *iter) |
static ITER_CONST | get_iter_const (SELF const *self) |
◆ ALLOC
A vector-backed arena, with support for small indices.
Definition at line 15 of file template.h.
◆ CHECK_ACCESS_INDEX
#define CHECK_ACCESS_INDEX |
( |
| self, |
|
|
| index ) |
Value:((index).index < (self)->exclusive_end)
Definition at line 66 of file template.h.
◆ INDEX
◆ INDEX_BITS
◆ IV_PAIR
◆ IV_PAIR_CONST
#define IV_PAIR_CONST NAME(SELF, iv_const) |
◆ RESIZE_FACTOR
◆ SLOT
#define V derive_c_parameter_value |
◆ V_DELETE
#define V_DELETE derive_c_parameter_value_delete |
◆ delete()
void delete |
( |
SELF * | self | ) |
|
|
static |
Definition at line 327 of file template.h.
327 {
331 while ((entry =
NAME(ITER,
next)(&iter))) {
333 }
334
336}
#define ALLOC
An allocator that prints to stdout when it allocates or frees memory.
static void free(SELF *self, void *ptr)
#define DEBUG_ASSERT(expr)
static ITER get_iter(SELF *self)
static IV_PAIR const * next(ITER *iter)
◆ delete_entry()
bool delete_entry |
( |
SELF * | self, |
|
|
INDEX | index ) |
|
static |
Definition at line 249 of file template.h.
249 {
252 return false;
253 }
254
262 return true;
263 }
264 return false;
265}
#define CHECK_ACCESS_INDEX(self, index)
◆ empty() [1/2]
bool empty |
( |
ITER const * | iter | ) |
|
|
static |
Definition at line 281 of file template.h.
281 {
283
284
285
286 return iter->next_index == INDEX_NONE || iter->next_index >= iter->arena->exclusive_end;
287}
◆ empty() [2/2]
bool empty |
( |
ITER_CONST const * | iter | ) |
|
|
static |
Definition at line 360 of file template.h.
360 {
362 return iter->next_index == INDEX_NONE || iter->next_index >= iter->arena->exclusive_end;
363}
◆ full()
bool full |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 212 of file template.h.
212 {
215 if (self->free_list == INDEX_NONE) {
216 return true;
217 }
218 }
219 return false;
220}
◆ get_iter()
ITER get_iter |
( |
SELF * | self | ) |
|
|
static |
Definition at line 312 of file template.h.
312 {
314 INDEX_TYPE index = 0;
315 while (index < INDEX_NONE && index < self->exclusive_end && !self->
slots[index].
present) {
316 index++;
317 }
318
319 return (ITER){
320 .arena = self,
321 .next_index = index,
322 .pos = 0,
323 .curr = (
IV_PAIR){.index = (
INDEX){.index = INDEX_NONE}, .value = NULL},
324 };
325}
◆ get_iter_const()
ITER_CONST get_iter_const |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 389 of file template.h.
389 {
391 INDEX_TYPE index = 0;
392 while (index < INDEX_NONE && index < self->exclusive_end && !self->slots[index].present) {
393 index++;
394 }
395
396 return (ITER_CONST){
397 .arena = self,
398 .next_index = index,
399 .pos = 0,
401 };
402}
◆ insert()
Definition at line 126 of file template.h.
126 {
136 return (
INDEX){.index = free_index};
137 }
138
144 self->
slots = new_alloc;
145 }
146
153 return (
INDEX){.index = new_index};
154}
static void * realloc(SELF *self, void *ptr, size_t size)
◆ new_with_capacity_for()
SELF new_with_capacity_for |
( |
INDEX_TYPE | items, |
|
|
ALLOC * | alloc ) |
|
static |
Definition at line 110 of file template.h.
110 {
117 .slots = slots,
118 .capacity = (INDEX_TYPE)capacity,
119 .free_list = INDEX_NONE,
120 .exclusive_end = 0,
121 .count = 0,
122 .alloc = alloc,
123 };
124}
static void * calloc(SELF *self, size_t count, size_t size)
static size_t next_power_of_2(size_t x)
◆ next() [1/2]
IV_PAIR const * next |
( |
ITER * | iter | ) |
|
|
static |
Definition at line 289 of file template.h.
289 {
291
293 return NULL;
294 }
295 iter->curr = (
IV_PAIR){.index = (
INDEX){.index = iter->next_index},
296 .value = &iter->arena->slots[iter->next_index].value};
297 iter->next_index++;
298 while (iter->next_index < INDEX_NONE && iter->next_index < iter->arena->exclusive_end &&
299 !iter->arena->slots[iter->next_index].present) {
300 iter->next_index++;
301 }
302
303 iter->pos++;
304 return &iter->curr;
305}
static bool empty(ITER const *iter)
◆ next() [2/2]
Definition at line 365 of file template.h.
365 {
367
369 return NULL;
370 }
371
373 .value = &iter->arena->slots[iter->next_index].value};
374 iter->next_index++;
375 while (iter->next_index != INDEX_NONE && iter->next_index < iter->arena->exclusive_end &&
376 !iter->arena->slots[iter->next_index].present) {
377 iter->next_index++;
378 }
379
380 iter->pos++;
381 return &iter->curr;
382}
◆ position() [1/2]
size_t position |
( |
ITER const * | iter | ) |
|
|
static |
Definition at line 307 of file template.h.
307 {
309 return iter->pos;
310}
◆ position() [2/2]
size_t position |
( |
ITER_CONST const * | iter | ) |
|
|
static |
Definition at line 384 of file template.h.
384 {
386 return iter->pos;
387}
◆ read()
Definition at line 186 of file template.h.
186 {
189 return value;
190}
static V const * try_read(SELF const *self, INDEX index)
◆ remove()
Definition at line 243 of file template.h.
243 {
246 return value;
247}
static bool try_remove(SELF *self, INDEX index, V *destination)
◆ shallow_clone()
Definition at line 192 of file template.h.
192 {
196 memcpy(slots, self->slots, self->exclusive_end *
sizeof(
SLOT));
198 .slots = slots,
199 .capacity = self->capacity,
200 .free_list = self->free_list,
201 .exclusive_end = self->exclusive_end,
202 .count = self->count,
203 .alloc = self->alloc,
204 };
205}
◆ size()
INDEX_TYPE size |
( |
SELF const * | self | ) |
|
|
static |
◆ try_read()
V const * try_read |
( |
SELF const * | self, |
|
|
INDEX | index ) |
|
static |
Definition at line 174 of file template.h.
174 {
177 return NULL;
178 }
181 return NULL;
182 }
184}
◆ try_remove()
bool try_remove |
( |
SELF * | self, |
|
|
INDEX | index, |
|
|
V * | destination ) |
|
static |
Definition at line 225 of file template.h.
225 {
228 return false;
229 }
230
233 *destination = entry->
value;
238 return true;
239 }
240 return false;
241}
◆ try_write()
Definition at line 156 of file template.h.
156 {
159 return NULL;
160 }
163 return NULL;
164 }
166}
◆ write()
Definition at line 168 of file template.h.
168 {
171 return value;
172}
static V * try_write(SELF *self, INDEX index)
◆ iv_const_empty
Initial value:= {
.index = {.index = INDEX_NONE},
.value = NULL,
}
Definition at line 347 of file template.h.
347 {
348 .index = {.index = INDEX_NONE},
349 .value = NULL,
350};
◆ max_capacity
◆ max_index
size_t max_index = MAX_INDEX |
|
static |