Derive-C
Loading...
Searching...
No Matches
template.h File Reference
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <derive-c/core/helpers.h>
#include <derive-c/core/panic.h>
#include <derive-c/core/self/def.h>
#include <derive-c/core/self/undef.h>
Include dependency graph for template.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  item_t
 A queue comprised of an extendable circular buffer. More...
struct  SELF
 An allocator that prints to stdout when it allocates or frees memory. More...

Macros

#define ITEM   item_t
#define ITEM_DELETE   item_delete
#define ITEM_CLONE   item_clone
#define INPLACE_CAPACITY   8
#define INPLACE_TYPE   uint8_t

Functions

static void item_delete (item_t *UNUSED(self))
static item_t item_clone (item_t const *self)
static SELF new ()
static SELF clone (SELF const *self)
static ITEM const * try_read (SELF const *self, INPLACE_TYPE index)
static ITEM const * read (SELF const *self, INPLACE_TYPE index)
static ITEMtry_write (SELF *self, INPLACE_TYPE index)
static ITEMwrite (SELF *self, INPLACE_TYPE index)
static ITEMtry_push (SELF *self, ITEM item)
static bool try_insert_at (SELF *self, size_t at, ITEM const *items, size_t count)
static void remove_at (SELF *self, size_t at, size_t count)
static ITEMpush (SELF *self, ITEM item)
static bool try_pop (SELF *self, ITEM *destination)
static ITEM pop (SELF *self)
static size_t size (SELF const *self)
static void delete (SELF *self)
static ITEMnext (ITER *iter)
static size_t position (ITER const *iter)
static bool empty (ITER const *iter)
static ITER get_iter (SELF *self)
static ITEM const * next (ITER_CONST *iter)
static size_t position (ITER_CONST const *iter)
static bool empty (ITER_CONST const *iter)
static ITER_CONST get_iter_const (SELF const *self)

Variables

static const INPLACE_TYPE max_size = INPLACE_CAPACITY

Macro Definition Documentation

◆ INPLACE_CAPACITY

#define INPLACE_CAPACITY   8

Definition at line 39 of file template.h.

◆ INPLACE_TYPE

#define INPLACE_TYPE   uint8_t

Definition at line 43 of file template.h.

◆ ITEM

#define ITEM   item_t

Definition at line 20 of file template.h.

◆ ITEM_CLONE

#define ITEM_CLONE   item_clone

Definition at line 24 of file template.h.

◆ ITEM_DELETE

#define ITEM_DELETE   item_delete

Definition at line 22 of file template.h.

Function Documentation

◆ clone()

SELF clone ( SELF const * self)
static

Definition at line 61 of file template.h.

61 {
62 SELF new_self = NS(SELF, new)();
63 new_self.size = self->size;
64 for (INPLACE_TYPE i = 0; i < self->size; i++) {
65 new_self.data[i] = ITEM_CLONE(&self->data[i]);
66 }
67 return new_self;
68}
#define NS(pre, post)
Definition helpers.h:6
#define SELF
Definition def.h:51
ITEM * data
Definition template.h:38
INPLACE_TYPE size
Definition template.h:52
#define ITEM_CLONE
Definition template.h:26
#define INPLACE_TYPE
Definition template.h:43
Here is the call graph for this function:

◆ delete()

void delete ( SELF * self)
static

Definition at line 167 of file template.h.

167 {
168 DEBUG_ASSERT(self);
169 for (INPLACE_TYPE i = 0; i < self->size; i++) {
170 ITEM_DELETE(&self->data[i]);
171 }
172}
#define DEBUG_ASSERT(expr)
Definition panic.h:34
#define ITEM_DELETE
Definition template.h:24

◆ empty() [1/2]

bool empty ( ITER const * iter)
static

Definition at line 196 of file template.h.

196 {
197 DEBUG_ASSERT(iter);
198 return iter->pos >= iter->vec->size;
199}
Here is the call graph for this function:

◆ empty() [2/2]

bool empty ( ITER_CONST const * iter)
static

Definition at line 233 of file template.h.

233 {
234 DEBUG_ASSERT(iter);
235 return iter->pos >= iter->vec->size;
236}
Here is the call graph for this function:

◆ get_iter()

ITER get_iter ( SELF * self)
static

Definition at line 201 of file template.h.

201 {
202 DEBUG_ASSERT(self);
203 return (ITER){
204 .vec = self,
205 .pos = 0,
206 };
207}
Here is the call graph for this function:

◆ get_iter_const()

ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 238 of file template.h.

238 {
239 DEBUG_ASSERT(self);
240 return (ITER_CONST){
241 .vec = self,
242 .pos = 0,
243 };
244}
Here is the call graph for this function:

◆ item_clone()

item_t item_clone ( item_t const * self)
static

Definition at line 23 of file template.h.

23{ return *self; }

◆ item_delete()

void item_delete ( item_t * UNUSEDself)
static

Definition at line 21 of file template.h.

21{}

◆ new()

SELF new ( )
static

Definition at line 57 of file template.h.

57{ return (SELF){.size = 0}; }

◆ next() [1/2]

ITEM * next ( ITER * iter)
static

Definition at line 181 of file template.h.

181 {
182 DEBUG_ASSERT(iter);
183 if (iter->pos < iter->vec->size) {
184 ITEM* item = &iter->vec->data[iter->pos];
185 iter->pos++;
186 return item;
187 }
188 return NULL;
189}
#define ITEM
Definition template.h:55
Here is the call graph for this function:

◆ next() [2/2]

ITEM const * next ( ITER_CONST * iter)
static

Definition at line 218 of file template.h.

218 {
219 DEBUG_ASSERT(iter);
220 if (iter->pos < iter->vec->size) {
221 ITEM const* item = &iter->vec->data[iter->pos];
222 iter->pos++;
223 return item;
224 }
225 return NULL;
226}
Here is the call graph for this function:

◆ pop()

ITEM pop ( SELF * self)
static

Definition at line 156 of file template.h.

156 {
157 ITEM entry;
158 ASSERT(NS(SELF, try_pop)(self, &entry));
159 return entry;
160}
#define ASSERT(expr,...)
Definition panic.h:15
static bool try_pop(SELF *self, ITEM *destination)
Definition template.h:146
Here is the call graph for this function:
Here is the caller graph for this function:

◆ position() [1/2]

size_t position ( ITER const * iter)
static

Definition at line 191 of file template.h.

191 {
192 DEBUG_ASSERT(iter);
193 return iter->pos;
194}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ position() [2/2]

size_t position ( ITER_CONST const * iter)
static

Definition at line 228 of file template.h.

228 {
229 DEBUG_ASSERT(iter);
230 return iter->pos;
231}
Here is the call graph for this function:

◆ push()

ITEM * push ( SELF * self,
ITEM item )
static

Definition at line 140 of file template.h.

140 {
141 ITEM* slot = NS(SELF, try_push)(self, item);
142 ASSERT(slot);
143 return slot;
144}
static ITEM * try_push(SELF *self, ITEM item)
Definition template.h:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read()

ITEM const * read ( SELF const * self,
INPLACE_TYPE index )
static

Definition at line 78 of file template.h.

78 {
79 ITEM const* value = NS(SELF, try_read)(self, index);
80 ASSERT(value);
81 return value;
82}
static VALUE const * try_read(SELF const *self, INDEX index)
Definition template.h:177
Here is the call graph for this function:

◆ remove_at()

void remove_at ( SELF * self,
size_t at,
size_t count )
static

Definition at line 124 of file template.h.

124 {
125 DEBUG_ASSERT(self);
126 ASSERT(at + count <= self->size);
127
128 if (count == 0) {
129 return;
130 }
131
132 for (size_t i = at; i < at + count; i++) {
133 ITEM_DELETE(&self->data[i]);
134 }
135
136 memmove(&self->data[at], &self->data[at + count], (self->size - (at + count)) * sizeof(ITEM));
137 self->size -= count;
138}
static INDEX_TYPE size(SELF const *self)
Definition template.h:220
Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

size_t size ( SELF const * self)
static

Definition at line 162 of file template.h.

162 {
163 DEBUG_ASSERT(self);
164 return self->size;
165}
Here is the call graph for this function:

◆ try_insert_at()

bool try_insert_at ( SELF * self,
size_t at,
ITEM const * items,
size_t count )
static

Definition at line 109 of file template.h.

109 {
110 DEBUG_ASSERT(self);
111 DEBUG_ASSERT(items);
112 ASSERT(at <= self->size);
113
114 if (self->size + count > INPLACE_CAPACITY) {
115 return false;
116 }
117
118 memmove(&self->data[at + count], &self->data[at], (self->size - at) * sizeof(ITEM));
119 memcpy(&self->data[at], items, count * sizeof(ITEM));
120 self->size += count;
121 return true;
122}
#define INPLACE_CAPACITY
Definition template.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_pop()

bool try_pop ( SELF * self,
ITEM * destination )
static

Definition at line 146 of file template.h.

146 {
147 DEBUG_ASSERT(self);
148 if (LIKELY(self->size > 0)) {
149 self->size--;
150 *destination = self->data[self->size];
151 return true;
152 }
153 return false;
154}
#define LIKELY(x)
Definition helpers.h:18
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_push()

ITEM * try_push ( SELF * self,
ITEM item )
static

Definition at line 98 of file template.h.

98 {
99 DEBUG_ASSERT(self);
100 if (self->size < INPLACE_CAPACITY) {
101 ITEM* slot = &self->data[self->size];
102 *slot = item;
103 self->size++;
104 return slot;
105 }
106 return NULL;
107}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read()

ITEM const * try_read ( SELF const * self,
INPLACE_TYPE index )
static

Definition at line 70 of file template.h.

70 {
71 DEBUG_ASSERT(self);
72 if (LIKELY(index < self->size)) {
73 return &self->data[index];
74 }
75 return NULL;
76}
Here is the call graph for this function:

◆ try_write()

ITEM * try_write ( SELF * self,
INPLACE_TYPE index )
static

Definition at line 84 of file template.h.

84 {
85 DEBUG_ASSERT(self);
86 if (LIKELY(index < self->size)) {
87 return &self->data[index];
88 }
89 return NULL;
90}
Here is the call graph for this function:

◆ write()

ITEM * write ( SELF * self,
INPLACE_TYPE index )
static

Definition at line 92 of file template.h.

92 {
93 ITEM* value = NS(SELF, try_write)(self, index);
94 ASSERT(value);
95 return value;
96}
static VALUE * try_write(SELF *self, INDEX index)
Definition template.h:159
Here is the call graph for this function:

Variable Documentation

◆ max_size

const INPLACE_TYPE max_size = INPLACE_CAPACITY
static

Definition at line 59 of file template.h.