Derive-C
Loading...
Searching...
No Matches
template.h File Reference
#include <stddef.h>
#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 31 of file template.h.

◆ INPLACE_TYPE

#define INPLACE_TYPE   uint8_t

Definition at line 35 of file template.h.

◆ T

#define T   derive_c_parameter_t

Definition at line 18 of file template.h.

◆ T_DELETE

#define T_DELETE   derive_c_parameter_t_delete

Definition at line 20 of file template.h.

Function Documentation

◆ delete()

void delete ( SELF * self)
static

Definition at line 129 of file template.h.

129 {
130 DEBUG_ASSERT(self);
131 for (INPLACE_TYPE i = 0; i < self->size; i++) {
132 T_DELETE(&self->data[i]);
133 }
134}
#define DEBUG_ASSERT(expr)
Definition panic.h:23
T data[INPLACE_CAPACITY]
Definition template.h:45
INPLACE_TYPE size
Definition template.h:44
#define T_DELETE
Definition template.h:20
#define INPLACE_TYPE
Definition template.h:35

◆ empty() [1/2]

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]

bool empty ( ITER_CONST const * iter)
static

Definition at line 195 of file template.h.

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

◆ get_iter()

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()

ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 200 of file template.h.

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

◆ new()

SELF new ( )
static

Definition at line 49 of file template.h.

49{ return (SELF){.size = 0}; }
#define SELF
Definition template.h:70

◆ next() [1/2]

T * next ( ITER * iter)
static

Definition at line 143 of file template.h.

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

◆ next() [2/2]

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 }
187 return NULL;
188}
Here is the call graph for this function:

◆ pop()

T pop ( SELF * self)
static

Definition at line 118 of file template.h.

118 {
119 T entry;
120 ASSERT(NAME(SELF, try_pop)(self, &entry));
121 return entry;
122}
#define NAME(pre, post)
Definition core.h:5
#define ASSERT(expr,...)
Definition panic.h:16
static bool try_pop(SELF *self, T *destination)
Definition template.h:108
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 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]

size_t position ( ITER_CONST const * iter)
static

Definition at line 190 of file template.h.

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

◆ push()

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

Definition at line 102 of file template.h.

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

◆ read()

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

Definition at line 71 of file template.h.

71 {
72 T const* value = NAME(SELF, try_read)(self, index);
73 ASSERT(value);
74 return value;
75}
static V const * try_read(SELF const *self, INDEX index)
Definition template.h:174
Here is the call graph for this function:

◆ shallow_clone()

SELF shallow_clone ( SELF const * self)
static

Definition at line 51 of file template.h.

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

◆ size()

size_t size ( SELF const * self)
static

Definition at line 124 of file template.h.

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

◆ try_pop()

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

Definition at line 108 of file template.h.

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

◆ try_push()

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

Definition at line 91 of file template.h.

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

◆ try_read()

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

Definition at line 63 of file template.h.

63 {
64 DEBUG_ASSERT(self);
65 if (LIKELY(index < self->size)) {
66 return &self->data[index];
67 }
68 return NULL;
69}
static INDEX_TYPE size(SELF const *self)
Definition template.h:207
Here is the call graph for this function:

◆ try_write()

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

Definition at line 77 of file template.h.

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

◆ write()

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

Definition at line 85 of file template.h.

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