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/container/queue/trait.h>
#include <derive-c/core/debug/gdb_marker.h>
#include <derive-c/core/debug/mutation_tracker.h>
#include <derive-c/core/prelude.h>
#include <derive-c/core/alloc/def.h>
#include <derive-c/core/self/def.h>
#include <derive-c/container/vector/dynamic/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
#define INVARIANT_CHECK(self)

Typedefs

typedef ITEMitem

Functions

static void item_delete (item_t *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 bool empty_item (ITEM *const *item)
static ITEMnext (ITER *iter)
static bool empty (ITER const *iter)
static ITER get_iter (SELF *self)
static bool empty_item (ITEM const *const *item)
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)
 TRAIT_QUEUE (SELF)

Macro Definition Documentation

◆ INTERNAL_NAME

#define INTERNAL_NAME   ITEM_VECTORS

Definition at line 56 of file template.h.

◆ INVARIANT_CHECK

#define INVARIANT_CHECK ( self)
Value:
ASSUME(self); \
ASSUME((self)->alloc);
#define ASSUME(expr,...)
Definition macros.h:62

Definition at line 72 of file template.h.

72#define INVARIANT_CHECK(self) \
73 ASSUME(self); \
74 ASSUME((self)->alloc);

◆ ITEM

#define ITEM   item_t

Definition at line 29 of file template.h.

◆ ITEM_CLONE

#define ITEM_CLONE   item_clone

Definition at line 34 of file template.h.

◆ ITEM_DELETE

#define ITEM_DELETE   item_delete

Definition at line 32 of file template.h.

◆ ITEM_VECTORS

#define ITEM_VECTORS   NS(NAME, item_vectors)

Definition at line 48 of file template.h.

Typedef Documentation

◆ item

typedef ITEM const* item

Definition at line 238 of file template.h.

Function Documentation

◆ clone()

SELF clone ( SELF const * other)
static

Definition at line 96 of file template.h.

96 {
97 INVARIANT_CHECK(other);
98 return (SELF){
99 .front = NS(ITEM_VECTORS, clone)(&other->front),
100 .back = NS(ITEM_VECTORS, clone)(&other->back),
101 .alloc = other->alloc,
102 .derive_c_deque = gdb_marker_new(),
103 .iterator_invalidation_tracker = mutation_tracker_new(),
104 };
105}
#define INVARIANT_CHECK(self)
Definition template.h:142
static SELF clone(SELF const *self)
Definition template.h:246
#define ITEM_VECTORS
Definition template.h:48
static gdb_marker gdb_marker_new()
Definition gdb_marker.h:7
static mutation_tracker mutation_tracker_new()
#define NS(pre, post)
Definition namespace.h:4
#define SELF
Definition def.h:52
Here is the call graph for this function:

◆ delete()

void delete ( SELF * self)
static

Definition at line 333 of file template.h.

333 {
334 INVARIANT_CHECK(self);
335 NS(ITEM_VECTORS, delete)(&self->front);
336 NS(ITEM_VECTORS, delete)(&self->back);
337}
ITEM_VECTORS back
Definition template.h:66
ITEM_VECTORS front
Definition template.h:65

◆ empty() [1/3]

bool empty ( ITER const * iter)
static

Definition at line 268 of file template.h.

268 {
269 mutation_version_check(&iter->version);
270 ASSUME(iter);
271 return iter->pos >=
272 NS(ITEM_VECTORS, size)(&iter->deque->front) + NS(ITEM_VECTORS, size)(&iter->deque->back);
273}
static INDEX_TYPE size(SELF const *self)
Definition template.h:275
static void mutation_version_check(mutation_version const *self)
Here is the call graph for this function:

◆ empty() [2/3]

bool empty ( ITER_CONST const * iter)
static

Definition at line 315 of file template.h.

315 {
316 ASSUME(iter);
317 mutation_version_check(&iter->version);
318 return iter->pos >=
319 NS(ITEM_VECTORS, size)(&iter->deque->front) + NS(ITEM_VECTORS, size)(&iter->deque->back);
320}
Here is the call graph for this function:

◆ empty() [3/3]

bool empty ( SELF const * self)
static

Definition at line 232 of file template.h.

232 {
233 INVARIANT_CHECK(self);
234 return NS(ITEM_VECTORS, size)(&self->front) == 0 && NS(ITEM_VECTORS, size)(&self->back) == 0;
235}
Here is the call graph for this function:

◆ empty_item() [1/2]

bool empty_item ( ITEM *const * item)
static

Definition at line 240 of file template.h.

240{ return *item == NULL; }
IV_PAIR const * item
Definition template.h:350
Here is the call graph for this function:

◆ empty_item() [2/2]

bool empty_item ( ITEM const *const * item)
static

Definition at line 287 of file template.h.

287{ return *item == NULL; }
Here is the call graph for this function:

◆ get_iter()

ITER get_iter ( SELF * self)
static

Definition at line 275 of file template.h.

275 {
276 ASSUME(self);
277 return (ITER){.deque = self,
278 .pos = 0,
280}
static mutation_version mutation_tracker_get(mutation_tracker const *self)
mutation_tracker iterator_invalidation_tracker
Definition template.h:139
Here is the call graph for this function:

◆ get_iter_const()

ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 322 of file template.h.

322 {
323 ASSUME(self);
324 return (ITER_CONST){
325 .deque = self,
326 .pos = 0,
327 .version = mutation_tracker_get(&self->iterator_invalidation_tracker),
328 };
329}
Here is the call graph for this function:

◆ item_clone()

item_t item_clone ( item_t const * a)
static

Definition at line 33 of file template.h.

33{ return *a; }

◆ item_delete()

void item_delete ( item_t * a)
static

Definition at line 31 of file template.h.

31{ (void)a; }

◆ new()

SELF new ( ALLOC * alloc)
static

Definition at line 76 of file template.h.

76 {
77 return (SELF){
78 .front = NS(ITEM_VECTORS, new)(alloc),
79 .back = NS(ITEM_VECTORS, new)(alloc),
80 .alloc = alloc,
81 .derive_c_deque = gdb_marker_new(),
82 .iterator_invalidation_tracker = mutation_tracker_new(),
83 };
84}
Here is the call graph for this function:

◆ new_with_capacity()

SELF new_with_capacity ( size_t front_and_back_capacity,
ALLOC * alloc )
static

Definition at line 86 of file template.h.

86 {
87 return (SELF){
88 .front = NS(ITEM_VECTORS, new_with_capacity)(front_and_back_capacity, alloc),
89 .back = NS(ITEM_VECTORS, new_with_capacity)(front_and_back_capacity, alloc),
90 .alloc = alloc,
91 .derive_c_deque = gdb_marker_new(),
92 .iterator_invalidation_tracker = mutation_tracker_new(),
93 };
94}
static SELF new_with_capacity(size_t front_and_back_capacity, ALLOC *alloc)
Definition template.h:86
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 248 of file template.h.

248 {
249 ASSUME(iter);
250 mutation_version_check(&iter->version);
251 size_t const front_size = NS(ITEM_VECTORS, size)(&iter->deque->front);
252 size_t const back_size = NS(ITEM_VECTORS, size)(&iter->deque->back);
253 size_t const total_size = front_size + back_size;
254
255 if (iter->pos < total_size) {
256 ITEM* item;
257 if (iter->pos < front_size) {
258 item = NS(ITEM_VECTORS, write)(&iter->deque->front, front_size - 1 - iter->pos);
259 } else {
260 item = NS(ITEM_VECTORS, write)(&iter->deque->back, iter->pos - front_size);
261 }
262 iter->pos++;
263 return item;
264 }
265 return NULL;
266}
#define ITEM
Definition template.h:58
static VALUE * write(SELF *self, INDEX index)
Definition template.h:222
Here is the call graph for this function:

◆ next() [2/2]

ITEM const * next ( ITER_CONST * iter)
static

Definition at line 295 of file template.h.

295 {
296 ASSUME(iter);
297 mutation_version_check(&iter->version);
298 size_t const front_size = NS(ITEM_VECTORS, size)(&iter->deque->front);
299 size_t const back_size = NS(ITEM_VECTORS, size)(&iter->deque->back);
300 size_t const total_size = front_size + back_size;
301
302 if (iter->pos < total_size) {
303 ITEM const* item;
304 if (iter->pos < front_size) {
305 item = NS(ITEM_VECTORS, read)(&iter->deque->front, front_size - 1 - iter->pos);
306 } else {
307 item = NS(ITEM_VECTORS, read)(&iter->deque->back, iter->pos - front_size);
308 }
309 iter->pos++;
310 return item;
311 }
312 return NULL;
313}
static VALUE const * read(SELF const *self, INDEX index)
Definition template.h:240
Here is the call graph for this function:

◆ peek_back_read()

ITEM const * peek_back_read ( SELF const * self)
static

Definition at line 163 of file template.h.

163 {
164 INVARIANT_CHECK(self);
165
166 size_t const back_size = NS(ITEM_VECTORS, size)(&self->back);
167 if (back_size > 0) {
168 return NS(ITEM_VECTORS, read)(&self->back, back_size - 1);
169 }
170
171 return NS(ITEM_VECTORS, try_read)(&self->front, 0);
172}
static VALUE const * try_read(SELF const *self, INDEX index)
Definition template.h:228
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 174 of file template.h.

174 {
175 INVARIANT_CHECK(self);
176
177 size_t const back_size = NS(ITEM_VECTORS, size)(&self->back);
178 if (back_size > 0) {
179 return NS(ITEM_VECTORS, write)(&self->back, back_size - 1);
180 }
181
182 return NS(ITEM_VECTORS, try_write)(&self->front, 0);
183}
static VALUE * try_write(SELF *self, INDEX index)
Definition template.h:210
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 141 of file template.h.

141 {
142 INVARIANT_CHECK(self);
143
144 size_t const front_size = NS(ITEM_VECTORS, size)(&self->front);
145 if (front_size > 0) {
146 return NS(ITEM_VECTORS, read)(&self->front, front_size - 1);
147 }
148
149 return NS(ITEM_VECTORS, try_read)(&self->back, 0);
150}
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 152 of file template.h.

152 {
153 INVARIANT_CHECK(self);
154
155 size_t const front_size = NS(ITEM_VECTORS, size)(&self->front);
156 if (front_size > 0) {
157 return NS(ITEM_VECTORS, write)(&self->front, front_size - 1);
158 }
159
160 return NS(ITEM_VECTORS, try_write)(&self->back, 0);
161}
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 213 of file template.h.

213 {
214 INVARIANT_CHECK(self);
216 if (NS(ITEM_VECTORS, size)(&self->back) > 0) {
217 ITEM result = NS(ITEM_VECTORS, pop)(&self->back);
218 NS(SELF, rebalance)(self);
219 return result;
220 }
221
222 ITEM result = NS(ITEM_VECTORS, pop_front)(&self->front);
223 NS(SELF, rebalance)(self);
224 return result;
225}
static ITEM pop_front(SELF *self)
Definition template.h:216
static void rebalance(SELF *self)
Definition template.h:107
static ITEM pop(SELF *self)
Definition template.h:264
static void mutation_tracker_mutate(mutation_tracker *self)
Here is the call graph for this function:

◆ pop_front()

ITEM pop_front ( SELF * self)
static

Definition at line 199 of file template.h.

199 {
200 INVARIANT_CHECK(self);
202 if (NS(ITEM_VECTORS, size)(&self->front) > 0) {
203 ITEM result = NS(ITEM_VECTORS, pop)(&self->front);
204 NS(SELF, rebalance)(self);
205 return result;
206 }
207
208 ITEM result = NS(ITEM_VECTORS, pop_front)(&self->back);
209 NS(SELF, rebalance)(self);
210 return result;
211}
Here is the call graph for this function:

◆ push_back()

void push_back ( SELF * self,
ITEM item )
static

Definition at line 192 of file template.h.

192 {
193 INVARIANT_CHECK(self);
195 NS(ITEM_VECTORS, push)(&self->back, item);
196 NS(SELF, rebalance)(self);
197}
static ITEM * push(SELF *self, ITEM item)
Definition template.h:214
Here is the call graph for this function:

◆ push_front()

void push_front ( SELF * self,
ITEM item )
static

Definition at line 185 of file template.h.

185 {
186 INVARIANT_CHECK(self);
188 NS(ITEM_VECTORS, push)(&self->front, item);
189 NS(SELF, rebalance)(self);
190}
Here is the call graph for this function:

◆ rebalance()

void rebalance ( SELF * self)
static

Definition at line 107 of file template.h.

107 {
108 INVARIANT_CHECK(self);
110
111 size_t const front_size = NS(ITEM_VECTORS, size)(&self->front);
112 size_t const back_size = NS(ITEM_VECTORS, size)(&self->back);
113 size_t const total_size = front_size + back_size;
114
115 if (!deque_rebalance_policy(total_size, front_size)) {
116 return;
117 }
118
119 ITEM_VECTORS* source;
120 ITEM_VECTORS* target;
121 size_t source_size;
122 size_t target_size;
123
124 if (front_size > back_size + 1) {
125 source = &self->front;
126 target = &self->back;
127 source_size = front_size;
128 target_size = back_size;
129 } else {
130 ASSUME(back_size > front_size + 1);
131 source = &self->back;
132 target = &self->front;
133 source_size = back_size;
134 target_size = front_size;
135 }
136
137 size_t const to_move = (source_size - target_size) / 2;
138 NS(ITEM_VECTORS, transfer_reverse)(source, target, to_move);
139}
static void transfer_reverse(SELF *source, SELF *target, size_t to_move)
Definition template.h:321
static bool deque_rebalance_policy(size_t total_size, size_t front_size)
Definition utils.h:6
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 227 of file template.h.

227 {
228 INVARIANT_CHECK(self);
229 return NS(ITEM_VECTORS, size)(&self->front) + NS(ITEM_VECTORS, size)(&self->back);
230}
Here is the call graph for this function:

◆ TRAIT_QUEUE()

TRAIT_QUEUE ( SELF )