Derive-C
Loading...
Searching...
No Matches
template.h File Reference
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "utils.h"
#include <derive-c/core/helpers.h>
#include <derive-c/core/panic.h>
#include <derive-c/core/alloc/def.h>
#include <derive-c/core/self/def.h>
#include <derive-c/structures/vector/template.h>
#include <derive-c/core/alloc/undef.h>
#include <derive-c/core/self/undef.h>
Include dependency graph for template.h:

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 ITEM_VECTORS   NS(NAME, item_vectors)
#define INTERNAL_NAME   ITEM_VECTORS

Functions

static void item_delete (item_t *UNUSED(a))
static item_t item_clone (item_t const *a)
static SELF new (ALLOC *alloc)
static SELF new_with_capacity (size_t front_and_back_capacity, ALLOC *alloc)
static SELF clone (SELF const *other)
static void rebalance (SELF *self)
static ITEM const * peek_front_read (SELF const *self)
static ITEMpeek_front_write (SELF *self)
static ITEM const * peek_back_read (SELF const *self)
static ITEMpeek_back_write (SELF *self)
static void push_front (SELF *self, ITEM item)
static void push_back (SELF *self, ITEM item)
static ITEM pop_front (SELF *self)
static ITEM pop_back (SELF *self)
static size_t size (SELF const *self)
static bool empty (SELF const *self)
static ITEMnext (ITER *iter)
static bool empty (ITER const *iter)
static ITER get_iter (SELF *self)
static ITEM const * next (ITER_CONST *iter)
static bool empty (ITER_CONST const *iter)
static ITER_CONST get_iter_const (SELF const *self)
static void delete (SELF *self)

Macro Definition Documentation

◆ INTERNAL_NAME

#define INTERNAL_NAME   ITEM_VECTORS

Definition at line 48 of file template.h.

◆ ITEM

#define ITEM   item_t

Definition at line 24 of file template.h.

◆ ITEM_CLONE

#define ITEM_CLONE   item_clone

Definition at line 29 of file template.h.

◆ ITEM_DELETE

#define ITEM_DELETE   item_delete

Definition at line 27 of file template.h.

◆ ITEM_VECTORS

#define ITEM_VECTORS   NS(NAME, item_vectors)

Definition at line 40 of file template.h.

Function Documentation

◆ clone()

SELF clone ( SELF const * other)
static

Definition at line 77 of file template.h.

77 {
78 DEBUG_ASSERT(other);
79 return (SELF){.front = NS(ITEM_VECTORS, clone)(&other->front),
80 .back = NS(ITEM_VECTORS, clone)(&other->back),
81 .alloc = other->alloc,
82 .derive_c_deque = (gdb_marker){}};
83}
#define NS(pre, post)
Definition helpers.h:6
#define DEBUG_ASSERT(expr)
Definition panic.h:34
#define SELF
Definition def.h:51
static SELF clone(SELF const *self)
Definition template.h:195
#define ITEM_VECTORS
Definition template.h:40
Here is the call graph for this function:

◆ delete()

void delete ( SELF * self)
static

Definition at line 309 of file template.h.

309 {
310 DEBUG_ASSERT(self);
311 NS(ITEM_VECTORS, delete)(&self->front);
312 NS(ITEM_VECTORS, delete)(&self->back);
313}
ITEM_VECTORS back
Definition template.h:58
ITEM_VECTORS front
Definition template.h:57

◆ empty() [1/3]

bool empty ( ITER const * iter)
static

Definition at line 251 of file template.h.

251 {
252 DEBUG_ASSERT(iter);
253 return iter->pos >=
254 NS(ITEM_VECTORS, size)(&iter->deque->front) + NS(ITEM_VECTORS, size)(&iter->deque->back);
255}
static INDEX_TYPE size(SELF const *self)
Definition template.h:220
Here is the call graph for this function:

◆ empty() [2/3]

bool empty ( ITER_CONST const * iter)
static

Definition at line 293 of file template.h.

293 {
294 DEBUG_ASSERT(iter);
295 return iter->pos >=
296 NS(ITEM_VECTORS, size)(&iter->deque->front) + NS(ITEM_VECTORS, size)(&iter->deque->back);
297}
Here is the call graph for this function:

◆ empty() [3/3]

bool empty ( SELF const * self)
static

Definition at line 220 of file template.h.

220 {
221 DEBUG_ASSERT(self);
222 return NS(ITEM_VECTORS, size)(&self->front) == 0 && NS(ITEM_VECTORS, size)(&self->back) == 0;
223}
Here is the call graph for this function:

◆ get_iter()

ITER get_iter ( SELF * self)
static

Definition at line 257 of file template.h.

257 {
258 DEBUG_ASSERT(self);
259 return (ITER){
260 .deque = self,
261 .pos = 0,
262 };
263}
Here is the call graph for this function:

◆ get_iter_const()

ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 299 of file template.h.

299 {
300 DEBUG_ASSERT(self);
301 return (ITER_CONST){
302 .deque = self,
303 .pos = 0,
304 };
305}
Here is the call graph for this function:

◆ item_clone()

item_t item_clone ( item_t const * a)
static

Definition at line 28 of file template.h.

28{ return *a; }

◆ item_delete()

void item_delete ( item_t * UNUSEDa)
static

Definition at line 26 of file template.h.

26{}

◆ new()

SELF new ( ALLOC * alloc)
static

Definition at line 63 of file template.h.

63 {
64 return (SELF){.front = NS(ITEM_VECTORS, new)(alloc),
65 .back = NS(ITEM_VECTORS, new)(alloc),
66 .alloc = alloc,
67 .derive_c_deque = (gdb_marker){}};
68}

◆ new_with_capacity()

SELF new_with_capacity ( size_t front_and_back_capacity,
ALLOC * alloc )
static

Definition at line 70 of file template.h.

70 {
71 return (SELF){.front = NS(ITEM_VECTORS, new_with_capacity)(front_and_back_capacity, alloc),
72 .back = NS(ITEM_VECTORS, new_with_capacity)(front_and_back_capacity, alloc),
73 .alloc = alloc,
74 .derive_c_deque = (gdb_marker){}};
75}
static SELF new_with_capacity(size_t front_and_back_capacity, ALLOC *alloc)
Definition template.h:70
Here is the call graph for this function:
Here is the caller graph for this function:

◆ next() [1/2]

ITEM * next ( ITER * iter)
static

Definition at line 232 of file template.h.

232 {
233 DEBUG_ASSERT(iter);
234 size_t front_size = NS(ITEM_VECTORS, size)(&iter->deque->front);
235 size_t back_size = NS(ITEM_VECTORS, size)(&iter->deque->back);
236 size_t total_size = front_size + back_size;
237
238 if (iter->pos < total_size) {
239 ITEM* item;
240 if (iter->pos < front_size) {
241 item = NS(ITEM_VECTORS, write)(&iter->deque->front, front_size - 1 - iter->pos);
242 } else {
243 item = NS(ITEM_VECTORS, write)(&iter->deque->back, iter->pos - front_size);
244 }
245 iter->pos++;
246 return item;
247 }
248 return NULL;
249}
#define ITEM
Definition template.h:55
static VALUE * write(SELF *self, INDEX index)
Definition template.h:171
Here is the call graph for this function:

◆ next() [2/2]

ITEM const * next ( ITER_CONST * iter)
static

Definition at line 274 of file template.h.

274 {
275 DEBUG_ASSERT(iter);
276 size_t front_size = NS(ITEM_VECTORS, size)(&iter->deque->front);
277 size_t back_size = NS(ITEM_VECTORS, size)(&iter->deque->back);
278 size_t total_size = front_size + back_size;
279
280 if (iter->pos < total_size) {
281 ITEM const* item;
282 if (iter->pos < front_size) {
283 item = NS(ITEM_VECTORS, read)(&iter->deque->front, front_size - 1 - iter->pos);
284 } else {
285 item = NS(ITEM_VECTORS, read)(&iter->deque->back, iter->pos - front_size);
286 }
287 iter->pos++;
288 return item;
289 }
290 return NULL;
291}
static VALUE const * read(SELF const *self, INDEX index)
Definition template.h:189
Here is the call graph for this function:

◆ peek_back_read()

ITEM const * peek_back_read ( SELF const * self)
static

Definition at line 155 of file template.h.

155 {
156 DEBUG_ASSERT(self);
157
158 size_t back_size = NS(ITEM_VECTORS, size)(&self->back);
159 if (back_size > 0) {
160 return NS(ITEM_VECTORS, read)(&self->back, back_size - 1);
161 }
162
163 return NS(ITEM_VECTORS, try_read)(&self->front, 0);
164}
static VALUE const * try_read(SELF const *self, INDEX index)
Definition template.h:177
Here is the call graph for this function:
Here is the caller graph for this function:

◆ peek_back_write()

ITEM * peek_back_write ( SELF * self)
static

Definition at line 166 of file template.h.

166 {
167 DEBUG_ASSERT(self);
168
169 size_t back_size = NS(ITEM_VECTORS, size)(&self->back);
170 if (back_size > 0) {
171 return NS(ITEM_VECTORS, write)(&self->back, back_size - 1);
172 }
173
174 return NS(ITEM_VECTORS, try_write)(&self->front, 0);
175}
static VALUE * try_write(SELF *self, INDEX index)
Definition template.h:159
Here is the call graph for this function:
Here is the caller graph for this function:

◆ peek_front_read()

ITEM const * peek_front_read ( SELF const * self)
static

Definition at line 133 of file template.h.

133 {
134 DEBUG_ASSERT(self);
135
136 size_t front_size = NS(ITEM_VECTORS, size)(&self->front);
137 if (front_size > 0) {
138 return NS(ITEM_VECTORS, read)(&self->front, front_size - 1);
139 }
140
141 return NS(ITEM_VECTORS, try_read)(&self->back, 0);
142}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ peek_front_write()

ITEM * peek_front_write ( SELF * self)
static

Definition at line 144 of file template.h.

144 {
145 DEBUG_ASSERT(self);
146
147 size_t front_size = NS(ITEM_VECTORS, size)(&self->front);
148 if (front_size > 0) {
149 return NS(ITEM_VECTORS, write)(&self->front, front_size - 1);
150 }
151
152 return NS(ITEM_VECTORS, try_write)(&self->back, 0);
153}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pop_back()

ITEM pop_back ( SELF * self)
static

Definition at line 202 of file template.h.

202 {
203 DEBUG_ASSERT(self);
204 if (NS(ITEM_VECTORS, size)(&self->back) > 0) {
205 ITEM result = NS(ITEM_VECTORS, pop)(&self->back);
206 NS(SELF, rebalance)(self);
207 return result;
208 }
209
210 ITEM result = NS(ITEM_VECTORS, pop_front)(&self->front);
211 NS(SELF, rebalance)(self);
212 return result;
213}
static ITEM pop_front(SELF *self)
Definition template.h:161
static void rebalance(SELF *self)
Definition template.h:85
static ITEM pop(SELF *self)
Definition template.h:156
Here is the call graph for this function:

◆ pop_front()

ITEM pop_front ( SELF * self)
static

Definition at line 189 of file template.h.

189 {
190 DEBUG_ASSERT(self);
191 if (NS(ITEM_VECTORS, size)(&self->front) > 0) {
192 ITEM result = NS(ITEM_VECTORS, pop)(&self->front);
193 NS(SELF, rebalance)(self);
194 return result;
195 }
196
197 ITEM result = NS(ITEM_VECTORS, pop_front)(&self->back);
198 NS(SELF, rebalance)(self);
199 return result;
200}
Here is the call graph for this function:

◆ push_back()

void push_back ( SELF * self,
ITEM item )
static

Definition at line 183 of file template.h.

183 {
184 DEBUG_ASSERT(self);
185 NS(ITEM_VECTORS, push)(&self->back, item);
186 NS(SELF, rebalance)(self);
187}
static ITEM * push(SELF *self, ITEM item)
Definition template.h:140
Here is the call graph for this function:

◆ push_front()

void push_front ( SELF * self,
ITEM item )
static

Definition at line 177 of file template.h.

177 {
178 DEBUG_ASSERT(self);
179 NS(ITEM_VECTORS, push)(&self->front, item);
180 NS(SELF, rebalance)(self);
181}
Here is the call graph for this function:

◆ rebalance()

void rebalance ( SELF * self)
static

Definition at line 85 of file template.h.

85 {
86 DEBUG_ASSERT(self);
87
88 size_t front_size = NS(ITEM_VECTORS, size)(&self->front);
89 size_t back_size = NS(ITEM_VECTORS, size)(&self->back);
90 size_t total_size = front_size + back_size;
91
92 if (!deque_rebalance_policy(total_size, front_size)) {
93 return;
94 }
95
96 ITEM_VECTORS* source;
97 ITEM_VECTORS* target;
98 size_t source_size;
99 size_t target_size;
100
101 if (front_size > back_size + 1) {
102 source = &self->front;
103 target = &self->back;
104 source_size = front_size;
105 target_size = back_size;
106 } else {
107 DEBUG_ASSERT(back_size > front_size + 1)
108 source = &self->back;
109 target = &self->front;
110 source_size = back_size;
111 target_size = front_size;
112 }
113
114 size_t to_move = (source_size - target_size) / 2;
115
116 NS(ITEM_VECTORS, reserve)(target, target_size + to_move);
117
118 ITEM* source_data = NS(ITEM_VECTORS, data)(source);
119 ITEM* target_data = NS(ITEM_VECTORS, data)(target);
120
121 memmove(&target_data[to_move], target_data, target_size * sizeof(ITEM));
122
123 for (size_t i = 0; i < to_move; i++) {
124 target_data[to_move - 1 - i] = source_data[i];
125 }
126
127 memmove(source_data, &source_data[to_move], (source_size - to_move) * sizeof(ITEM));
128
129 target->size += to_move;
130 source->size -= to_move;
131}
static bool deque_rebalance_policy(size_t total_size, size_t front_size)
Definition utils.h:6
static void reserve(SELF *self, size_t new_capacity_for)
Definition template.h:97
static ITEM * data(SELF *self)
Definition template.h:215
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 215 of file template.h.

215 {
216 DEBUG_ASSERT(self);
217 return NS(ITEM_VECTORS, size)(&self->front) + NS(ITEM_VECTORS, size)(&self->back);
218}
Here is the call graph for this function: