Derive-C
Loading...
Searching...
No Matches
template.h File Reference
#include <stdint.h>
#include <derive-c/core.h>
#include <derive-c/panic.h>
#include <derive-c/self.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  SELF
 

Macros

#define T   derive_c_parameter_t
 
#define T_DELETE   derive_c_parameter_t_delete
 
#define INPLACE_CAPACITY   8
 
#define INPLACE_TYPE   uint8_t
 

Functions

static SELF new ()
 
static SELF shallow_clone (SELF const *self)
 
static T const * try_read (SELF const *self, INPLACE_TYPE index)
 
static T const * read (SELF const *self, INPLACE_TYPE index)
 
static Ttry_write (SELF *self, INPLACE_TYPE index)
 
static Twrite (SELF *self, INPLACE_TYPE index)
 
static Ttry_push (SELF *self, T value)
 
static Tpush (SELF *self, T value)
 
static bool try_pop (SELF *self, T *destination)
 
static T pop (SELF *self)
 
static size_t size (SELF const *self)
 
static void delete (SELF *self)
 
static Tnext (ITER *iter)
 
static size_t position (ITER const *iter)
 
static bool empty (ITER const *iter)
 
static ITER get_iter (SELF *self)
 
static T 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)
 

Macro Definition Documentation

◆ INPLACE_CAPACITY

#define INPLACE_CAPACITY   8

Definition at line 26 of file template.h.

◆ INPLACE_TYPE

#define INPLACE_TYPE   uint8_t

Definition at line 30 of file template.h.

◆ T

#define T   derive_c_parameter_t

Definition at line 15 of file template.h.

◆ T_DELETE

#define T_DELETE   derive_c_parameter_t_delete

Definition at line 17 of file template.h.

Function Documentation

◆ delete()

static void delete ( SELF * self)
static

Definition at line 128 of file template.h.

128 {
129 DEBUG_ASSERT(self);
130 for (INPLACE_TYPE i = 0; i < self->size; i++) {
131 T_DELETE(&self->data[i]);
132 }
133}
#define T_DELETE
Definition template.h:18
#define DEBUG_ASSERT(expr)
Definition panic.h:24
#define INPLACE_TYPE
Definition template.h:30
T data[INPLACE_CAPACITY]
Definition template.h:40
INPLACE_TYPE size
Definition template.h:39

◆ empty() [1/2]

static bool empty ( ITER const * iter)
static

Definition at line 158 of file template.h.

158 {
159 DEBUG_ASSERT(iter);
160 return iter->pos >= iter->vec->size;
161}
Here is the call graph for this function:

◆ empty() [2/2]

static bool empty ( ITER_CONST 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:

◆ get_iter()

static ITER get_iter ( SELF * self)
static

Definition at line 163 of file template.h.

163 {
164 DEBUG_ASSERT(self);
165 return (ITER){
166 .vec = self,
167 .pos = 0,
168 };
169}
Here is the call graph for this function:

◆ get_iter_const()

static ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 201 of file template.h.

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

◆ new()

static SELF new ( )
static

Definition at line 44 of file template.h.

44{ return (SELF){.size = 0}; }
#define SELF
Definition self.h:4

◆ next() [1/2]

static T * next ( ITER * iter)
static

Definition at line 142 of file template.h.

142 {
143 DEBUG_ASSERT(iter);
144 if (iter->pos < iter->vec->size) {
145 T* item = &iter->vec->data[iter->pos];
146 iter->pos++;
147 return item;
148 } else {
149 return NULL;
150 }
151}
#define T
Definition template.h:16
Here is the call graph for this function:

◆ next() [2/2]

static T const * next ( ITER_CONST * iter)
static

Definition at line 180 of file template.h.

180 {
181 DEBUG_ASSERT(iter);
182 if (iter->pos < iter->vec->size) {
183 T const* item = &iter->vec->data[iter->pos];
184 iter->pos++;
185 return item;
186 } else {
187 return NULL;
188 }
189}
Here is the call graph for this function:

◆ pop()

static T pop ( SELF * self)
static

Definition at line 117 of file template.h.

117 {
118 T entry;
119 ASSERT(NAME(SELF, try_pop)(self, &entry));
120 return entry;
121}
#define NAME(pre, post)
Definition core.h:5
#define ASSERT(expr,...)
Definition panic.h:15
static bool try_pop(SELF *self, T *destination)
Definition template.h:106
Here is the call graph for this function:
Here is the caller graph for this function:

◆ position() [1/2]

static size_t position ( ITER const * iter)
static

Definition at line 153 of file template.h.

153 {
154 DEBUG_ASSERT(iter);
155 return iter->pos;
156}
Here is the call graph for this function:

◆ position() [2/2]

static size_t position ( ITER_CONST 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:

◆ push()

static T * push ( SELF * self,
T value )
static

Definition at line 100 of file template.h.

100 {
101 T* slot = NAME(SELF, try_push)(self, value);
102 ASSERT(slot);
103 return slot;
104}
static T * try_push(SELF *self, T value)
Definition template.h:88
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read()

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

Definition at line 67 of file template.h.

67 {
68 T const* value = NAME(SELF, try_read)(self, index);
69 ASSERT(value);
70 return value;
71}
static V const * try_read(SELF const *self, INDEX index)
Definition template.h:162
Here is the call graph for this function:

◆ shallow_clone()

static SELF shallow_clone ( SELF const * self)
static

Definition at line 46 of file template.h.

46 {
47 SELF new_self = NAME(SELF, new)();
48 new_self.size = self->size;
49 // TODO(oliverkillane): premature optimization, only copy the data needed.
50 // If this is already a very small vector, may be better to just
51 // `return *self` and copy the entire buffer
52 for (INPLACE_TYPE i = 0; i < self->size; i++) {
53 new_self.data[i] = self->data[i];
54 }
55 return new_self;
56}
Here is the call graph for this function:

◆ size()

static size_t size ( SELF const * self)
static

Definition at line 123 of file template.h.

123 {
124 DEBUG_ASSERT(self);
125 return self->size;
126}
Here is the call graph for this function:

◆ try_pop()

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

Definition at line 106 of file template.h.

106 {
107 DEBUG_ASSERT(self);
108 if (LIKELY(self->size > 0)) {
109 self->size--;
110 *destination = self->data[self->size];
111 return true;
112 } else {
113 return false;
114 }
115}
#define LIKELY(x)
Definition core.h:7
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_push()

static T * try_push ( SELF * self,
T value )
static

Definition at line 88 of file template.h.

88 {
89 DEBUG_ASSERT(self);
90 if (self->size < INPLACE_CAPACITY) {
91 T* slot = &self->data[self->size];
92 *slot = value;
93 self->size++;
94 return slot;
95 } else {
96 return NULL;
97 }
98}
#define INPLACE_CAPACITY
Definition template.h:26
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_read()

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

Definition at line 58 of file template.h.

58 {
59 DEBUG_ASSERT(self);
60 if (LIKELY(index < self->size)) {
61 return &self->data[index];
62 } else {
63 return NULL;
64 }
65}
static INDEX_TYPE size(SELF const *self)
Definition template.h:194
Here is the call graph for this function:

◆ try_write()

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

Definition at line 73 of file template.h.

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

◆ write()

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

Definition at line 82 of file template.h.

82 {
83 T* value = NAME(SELF, try_write)(self, index);
84 ASSERT(value);
85 return value;
86}
static V * try_write(SELF *self, INDEX index)
Definition template.h:144
Here is the call graph for this function: