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

Functions

static SELF new ()
 
static SELF new_with_capacity (size_t capacity)
 
static SELF new_with_defaults (size_t size, T default_value)
 
static SELF shallow_clone (SELF const *self)
 
static T const * try_read (SELF const *self, size_t index)
 
static T const * read (SELF const *self, size_t index)
 
static Ttry_write (SELF *self, size_t index)
 
static Twrite (SELF *self, size_t index)
 
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

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

static void delete ( SELF * self)
static

Definition at line 161 of file template.h.

161 {
162 DEBUG_ASSERT(self);
163 for (size_t i = 0; i < self->size; i++) {
164 T_DELETE(&self->data[i]);
165 }
166 free(self->data);
167}
#define T_DELETE
Definition template.h:18
#define DEBUG_ASSERT(expr)
Definition panic.h:24
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 192 of file template.h.

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

◆ empty() [2/2]

static bool empty ( ITER_CONST const * iter)
static

Definition at line 229 of file template.h.

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

◆ get_iter()

static ITER get_iter ( SELF * self)
static

Definition at line 197 of file template.h.

197 {
198 DEBUG_ASSERT(self);
199 return (ITER){
200 .vec = self,
201 .pos = 0,
202 };
203}
Here is the call graph for this function:

◆ get_iter_const()

static ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 234 of file template.h.

234 {
235 DEBUG_ASSERT(self);
236 return (ITER_CONST){
237 .vec = self,
238 .pos = 0,
239 };
240}
Here is the call graph for this function:

◆ new()

static SELF new ( )
static

Definition at line 34 of file template.h.

34 {
35 SELF temp = (SELF){
36 .size = 0,
37 .capacity = 0,
38 .data = NULL,
39 };
40 return temp;
41}
#define SELF
Definition self.h:4

◆ new_with_capacity()

static SELF new_with_capacity ( size_t capacity)
static

Definition at line 43 of file template.h.

43 {
44 DEBUG_ASSERT(capacity > 0);
45 T* data = (T*)malloc(capacity * sizeof(T));
46 ASSERT(LIKELY(data));
47 return (SELF){
48 .size = 0,
49 .capacity = capacity,
50 .data = data,
51 };
52}
#define LIKELY(x)
Definition core.h:7
#define T
Definition template.h:16
#define ASSERT(expr,...)
Definition panic.h:15
Here is the call graph for this function:
Here is the caller graph for this function:

◆ new_with_defaults()

static SELF new_with_defaults ( size_t size,
T default_value )
static

Definition at line 54 of file template.h.

54 {
55 T* data = (T*)malloc(size * sizeof(T));
56 for (size_t i = 0; i < size; i++) {
57 data[i] = default_value;
58 }
59 ASSERT(LIKELY(data));
60 return (SELF){
61 .size = size,
62 .capacity = size,
63 .data = data,
64 };
65}
static INDEX_TYPE size(SELF const *self)
Definition template.h:194
Here is the call graph for this function:
Here is the caller graph for this function:

◆ next() [1/2]

static T * next ( ITER * iter)
static

Definition at line 176 of file template.h.

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

◆ next() [2/2]

static T const * next ( ITER_CONST * iter)
static

Definition at line 213 of file template.h.

213 {
214 DEBUG_ASSERT(iter);
215 if (iter->pos < iter->vec->size) {
216 T const* item = &iter->vec->data[iter->pos];
217 iter->pos++;
218 return item;
219 } else {
220 return NULL;
221 }
222}
Here is the call graph for this function:

◆ pop()

static T pop ( SELF * self)
static

Definition at line 150 of file template.h.

150 {
151 T entry;
152 ASSERT(NAME(SELF, try_pop)(self, &entry));
153 return entry;
154}
#define NAME(pre, post)
Definition core.h:5
static bool try_pop(SELF *self, T *destination)
Definition template.h:106
Here is the call graph for this function:

◆ position() [1/2]

static size_t position ( ITER const * iter)
static

Definition at line 187 of file template.h.

187 {
188 DEBUG_ASSERT(iter);
189 return iter->pos;
190}
Here is the call graph for this function:

◆ position() [2/2]

static size_t position ( ITER_CONST const * iter)
static

Definition at line 224 of file template.h.

224 {
225 DEBUG_ASSERT(iter);
226 return iter->pos;
227}
Here is the call graph for this function:

◆ push()

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

Definition at line 109 of file template.h.

109 {
110 DEBUG_ASSERT(self);
111 if (self->size == self->capacity) {
112 T* new_data;
113 size_t new_capacity;
114 if (self->data == NULL) {
115 DEBUG_ASSERT(self->capacity == 0);
116 // JUSTIFY: Allocating capacity of 4
117 // - Avoid repeat reallocations on growing a vector from
118 // size sero (from new)
119 // Otherwise an arbitrary choice (given we do not know the size of T)
120 new_capacity = 8;
121 new_data = (T*)malloc(new_capacity * sizeof(T));
122 } else {
123 // JUSTIFY: Growth factor of 2
124 // - Simple arithmetic (for debugging)
125 // - Same as used by GCC's std::vector implementation
126 new_capacity = self->capacity * 2;
127 new_data = (T*)realloc(self->data, new_capacity * sizeof(T));
128 }
129 ASSERT(new_data);
130 self->capacity = new_capacity;
131 self->data = new_data;
132 }
133 T* entry = &self->data[self->size];
134 *entry = value;
135 self->size++;
136 return entry;
137}
size_t capacity
Definition template.h:85
Here is the call graph for this function:

◆ read()

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

Definition at line 88 of file template.h.

88 {
89 T const* value = NAME(SELF, try_read)(self, index);
90 ASSERT(value);
91 return value;
92}
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 67 of file template.h.

67 {
68 DEBUG_ASSERT(self);
69 T* data = (T*)malloc(self->capacity * sizeof(T));
70 ASSERT(data);
71 memcpy(data, self->data, self->size * sizeof(T));
72 return (SELF){
73 .size = self->size,
74 .capacity = self->capacity,
75 .data = data,
76 };
77}
Here is the call graph for this function:

◆ size()

static size_t size ( SELF const * self)
static

Definition at line 156 of file template.h.

156 {
157 DEBUG_ASSERT(self);
158 return self->size;
159}
Here is the call graph for this function:

◆ try_pop()

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

Definition at line 139 of file template.h.

139 {
140 DEBUG_ASSERT(self);
141 if (LIKELY(self->size > 0)) {
142 self->size--;
143 *destination = self->data[self->size];
144 return true;
145 } else {
146 return false;
147 }
148}
Here is the call graph for this function:

◆ try_read()

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

Definition at line 79 of file template.h.

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

◆ try_write()

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

Definition at line 94 of file template.h.

94 {
95 DEBUG_ASSERT(self);
96 if (LIKELY(index < self->size)) {
97 return &self->data[index];
98 } else {
99 return NULL;
100 }
101}
Here is the call graph for this function:

◆ write()

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

Definition at line 103 of file template.h.

103 {
104 T* value = NAME(SELF, try_write)(self, index);
105 ASSERT(value);
106 return value;
107}
static V * try_write(SELF *self, INDEX index)
Definition template.h:144
Here is the call graph for this function: