Derive-C
Loading...
Searching...
No Matches
template.h File Reference

Go to the source code of this file.

Data Structures

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...
struct  ITER
struct  ITER_CONST

Macros

#define ITEM   item_t
#define ITEM_DELETE   item_delete
#define ITEM_CLONE   item_clone
#define ITEM_DEBUG   item_debug
#define ITEM_VECTORS   NS(NAME, item_vectors)
#define INTERNAL_NAME   ITEM_VECTORS
#define INVARIANT_CHECK(self)
#define ITER   NS(SELF, iter)
#define ITER_CONST   NS(SELF, iter_const)

Typedefs

typedef ITEMitem

Functions

static void ITEM_DELETE (item_t *self)
static item_t ITEM_CLONE (item_t const *self)
static void ITEM_DEBUG (ITEM const *self, dc_debug_fmt fmt, FILE *stream)
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 size_t size (SELF const *self)
static bool empty (SELF const *self)
static void rebalance (SELF *self)
static ITEM const * try_read_from_front (SELF const *self, size_t index)
static ITEM const * try_read_from_back (SELF const *self, size_t index)
static ITEMtry_write_from_front (SELF *self, size_t index)
static ITEMtry_write_from_back (SELF *self, size_t index)
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 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)
static void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
 DC_TRAIT_QUEUE (SELF)

Macro Definition Documentation

◆ INTERNAL_NAME

#define INTERNAL_NAME   ITEM_VECTORS

Definition at line 52 of file template.h.

◆ INVARIANT_CHECK

#define INVARIANT_CHECK ( self)
Value:
DC_ASSUME(self);
#define DC_ASSUME(expr,...)
Definition panic.h:56

Definition at line 67 of file template.h.

◆ ITEM

#define ITEM   item_t

Definition at line 19 of file template.h.

◆ ITEM_CLONE

#define ITEM_CLONE   item_clone

Definition at line 23 of file template.h.

◆ ITEM_DEBUG

#define ITEM_DEBUG   item_debug

Definition at line 25 of file template.h.

◆ ITEM_DELETE

#define ITEM_DELETE   item_delete

Definition at line 21 of file template.h.

◆ ITEM_VECTORS

#define ITEM_VECTORS   NS(NAME, item_vectors)

Definition at line 44 of file template.h.

◆ ITER

#define ITER   NS(SELF, iter)

Definition at line 207 of file template.h.

◆ ITER_CONST

#define ITER_CONST   NS(SELF, iter_const)

Definition at line 254 of file template.h.

Typedef Documentation

◆ item

typedef ITEM const* item

Definition at line 208 of file template.h.

Function Documentation

◆ clone()

SELF clone ( SELF const * other)
static

Definition at line 87 of file template.h.

87 {
88 INVARIANT_CHECK(other);
89 return (SELF){
90 .front = NS(ITEM_VECTORS, clone)(&other->front),
91 .back = NS(ITEM_VECTORS, clone)(&other->back),
92 .derive_c_deque = dc_gdb_marker_new(),
93 .iterator_invalidation_tracker = mutation_tracker_new(),
94 };
95}
#define INVARIANT_CHECK(self)
Definition template.h:88
static SELF clone(SELF const *self)
Definition template.h:215
#define ITEM_VECTORS
Definition template.h:44
static dc_gdb_marker dc_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

◆ DC_TRAIT_QUEUE()

DC_TRAIT_QUEUE ( SELF )

◆ debug()

void debug ( SELF const * self,
dc_debug_fmt fmt,
FILE * stream )
static

Definition at line 309 of file template.h.

309 {
310 fprintf(stream, EXPAND_STRING(SELF) "@%p {\n", self);
311 fmt = dc_debug_fmt_scope_begin(fmt);
312 dc_debug_fmt_print(fmt, stream, "size: %lu,\n", NS(SELF, size)(self));
313
314 dc_debug_fmt_print(fmt, stream, "front: ");
315 NS(ITEM_VECTORS, debug)(&self->front, fmt, stream);
316 fprintf(stream, ",\n");
317
318 dc_debug_fmt_print(fmt, stream, "back: ");
319 NS(ITEM_VECTORS, debug)(&self->back, fmt, stream);
320 fprintf(stream, ",\n");
321
322 fmt = dc_debug_fmt_scope_end(fmt);
323 dc_debug_fmt_print(fmt, stream, "}\n");
324}
static void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:62
static INDEX_TYPE size(SELF const *self)
Definition template.h:252
dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:39
dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:33
static void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:22
#define EXPAND_STRING(NAME)
Definition namespace.h:8
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107

◆ delete()

void delete ( SELF * self)
static

Definition at line 303 of file template.h.

303 {
304 INVARIANT_CHECK(self);
305 NS(ITEM_VECTORS, delete)(&self->front);
306 NS(ITEM_VECTORS, delete)(&self->back);
307}
ITEM_VECTORS back
Definition template.h:62
ITEM_VECTORS front
Definition template.h:61

◆ empty() [1/3]

bool empty ( ITER const * iter)
static

Definition at line 238 of file template.h.

238 {
239 mutation_version_check(&iter->version);
240 DC_ASSUME(iter);
241 return iter->pos >=
242 NS(ITEM_VECTORS, size)(&iter->deque->front) + NS(ITEM_VECTORS, size)(&iter->deque->back);
243}
static void mutation_version_check(mutation_version const *self)

◆ empty() [2/3]

bool empty ( ITER_CONST const * iter)
static

Definition at line 285 of file template.h.

285 {
286 DC_ASSUME(iter);
287 mutation_version_check(&iter->version);
288 return iter->pos >=
289 NS(ITEM_VECTORS, size)(&iter->deque->front) + NS(ITEM_VECTORS, size)(&iter->deque->back);
290}

◆ empty() [3/3]

bool empty ( SELF const * self)
static

Definition at line 102 of file template.h.

102 {
103 INVARIANT_CHECK(self);
104 return NS(ITEM_VECTORS, size)(&self->front) == 0 && NS(ITEM_VECTORS, size)(&self->back) == 0;
105}

◆ empty_item() [1/2]

bool empty_item ( ITEM *const * item)
static

Definition at line 210 of file template.h.

210{ return *item == NULL; }
IV_PAIR item
Definition template.h:283

◆ empty_item() [2/2]

bool empty_item ( ITEM const *const * item)
static

Definition at line 257 of file template.h.

257{ return *item == NULL; }

◆ get_iter()

ITER get_iter ( SELF * self)
static

Definition at line 245 of file template.h.

245 {
246 DC_ASSUME(self);
247 return (ITER){.deque = self,
248 .pos = 0,
250}
#define ITER
Definition template.h:320
static mutation_version mutation_tracker_get(mutation_tracker const *self)
mutation_tracker iterator_invalidation_tracker
Definition template.h:85

◆ get_iter_const()

ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 292 of file template.h.

292 {
293 DC_ASSUME(self);
294 return (ITER_CONST){
295 .deque = self,
296 .pos = 0,
297 .version = mutation_tracker_get(&self->iterator_invalidation_tracker),
298 };
299}
#define ITER_CONST
Definition template.h:398

◆ ITEM_CLONE()

item_t ITEM_CLONE ( item_t const * self)
static

◆ ITEM_DEBUG()

void ITEM_DEBUG ( ITEM const * self,
dc_debug_fmt fmt,
FILE * stream )
static

◆ ITEM_DELETE()

void ITEM_DELETE ( item_t * self)
static

◆ new()

SELF new ( ALLOC * alloc)
static

Definition at line 69 of file template.h.

69 {
70 return (SELF){
71 .front = NS(ITEM_VECTORS, new)(alloc),
72 .back = NS(ITEM_VECTORS, new)(alloc),
73 .derive_c_deque = dc_gdb_marker_new(),
74 .iterator_invalidation_tracker = mutation_tracker_new(),
75 };
76}

◆ new_with_capacity()

SELF new_with_capacity ( size_t front_and_back_capacity,
ALLOC * alloc )
static

Definition at line 78 of file template.h.

78 {
79 return (SELF){
80 .front = NS(ITEM_VECTORS, new_with_capacity)(front_and_back_capacity, alloc),
81 .back = NS(ITEM_VECTORS, new_with_capacity)(front_and_back_capacity, alloc),
82 .derive_c_deque = dc_gdb_marker_new(),
83 .iterator_invalidation_tracker = mutation_tracker_new(),
84 };
85}
static SELF new_with_capacity(size_t front_and_back_capacity, ALLOC *alloc)
Definition template.h:78

◆ next() [1/2]

ITEM * next ( ITER * iter)
static

Definition at line 218 of file template.h.

218 {
219 DC_ASSUME(iter);
221 size_t const front_size = NS(ITEM_VECTORS, size)(&iter->deque->front);
222 size_t const back_size = NS(ITEM_VECTORS, size)(&iter->deque->back);
223 size_t const total_size = front_size + back_size;
224
225 if (iter->pos < total_size) {
226 ITEM* item;
227 if (iter->pos < front_size) {
228 item = NS(ITEM_VECTORS, write)(&iter->deque->front, front_size - 1 - iter->pos);
229 } else {
230 item = NS(ITEM_VECTORS, write)(&iter->deque->back, iter->pos - front_size);
231 }
232 iter->pos++;
233 return item;
234 }
235 return NULL;
236}
#define ITEM
Definition template.h:63
static VALUE * write(SELF *self, INDEX index)
Definition template.h:209
mutation_version version
Definition template.h:326
SELF * deque
Definition template.h:213
size_t pos
Definition template.h:214

◆ next() [2/2]

ITEM const * next ( ITER_CONST * iter)
static

Definition at line 265 of file template.h.

265 {
266 DC_ASSUME(iter);
268 size_t const front_size = NS(ITEM_VECTORS, size)(&iter->deque->front);
269 size_t const back_size = NS(ITEM_VECTORS, size)(&iter->deque->back);
270 size_t const total_size = front_size + back_size;
271
272 if (iter->pos < total_size) {
273 ITEM const* item;
274 if (iter->pos < front_size) {
275 item = NS(ITEM_VECTORS, read)(&iter->deque->front, front_size - 1 - iter->pos);
276 } else {
277 item = NS(ITEM_VECTORS, read)(&iter->deque->back, iter->pos - front_size);
278 }
279 iter->pos++;
280 return item;
281 }
282 return NULL;
283}
static VALUE const * read(SELF const *self, INDEX index)
Definition template.h:199
size_t pos
Definition template.h:261
SELF const * deque
Definition template.h:260
mutation_version version
Definition template.h:404

◆ pop_back()

ITEM pop_back ( SELF * self)
static

Definition at line 193 of file template.h.

193 {
194 INVARIANT_CHECK(self);
196 if (NS(ITEM_VECTORS, size)(&self->back) > 0) {
197 ITEM result = NS(ITEM_VECTORS, pop)(&self->back);
198 NS(SELF, rebalance)(self);
199 return result;
200 }
201
202 ITEM result = NS(ITEM_VECTORS, pop_front)(&self->front);
203 NS(SELF, rebalance)(self);
204 return result;
205}
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:266
static void mutation_tracker_mutate(mutation_tracker *self)

◆ pop_front()

ITEM pop_front ( SELF * self)
static

Definition at line 179 of file template.h.

179 {
180 INVARIANT_CHECK(self);
182 if (NS(ITEM_VECTORS, size)(&self->front) > 0) {
183 ITEM result = NS(ITEM_VECTORS, pop)(&self->front);
184 NS(SELF, rebalance)(self);
185 return result;
186 }
187
188 ITEM result = NS(ITEM_VECTORS, pop_front)(&self->back);
189 NS(SELF, rebalance)(self);
190 return result;
191}

◆ push_back()

void push_back ( SELF * self,
ITEM item )
static

Definition at line 172 of file template.h.

172 {
173 INVARIANT_CHECK(self);
175 NS(ITEM_VECTORS, push)(&self->back, item);
176 NS(SELF, rebalance)(self);
177}
static ITEM * push(SELF *self, ITEM item)
Definition template.h:216

◆ push_front()

void push_front ( SELF * self,
ITEM item )
static

Definition at line 165 of file template.h.

165 {
166 INVARIANT_CHECK(self);
168 NS(ITEM_VECTORS, push)(&self->front, item);
169 NS(SELF, rebalance)(self);
170}

◆ 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 (!dc_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 DC_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:323
static bool dc_deque_rebalance_policy(size_t total_size, size_t front_size)
Definition utils.h:6

◆ size()

size_t size ( SELF const * self)
static

Definition at line 97 of file template.h.

97 {
98 INVARIANT_CHECK(self);
99 return NS(ITEM_VECTORS, size)(&self->front) + NS(ITEM_VECTORS, size)(&self->back);
100}

◆ try_read_from_back()

ITEM const * try_read_from_back ( SELF const * self,
size_t index )
static

Definition at line 153 of file template.h.

153 {
154 return NS(SELF, try_read_from_front)(self, NS(SELF, size)(self) - 1 - index);
155}
static ITEM const * try_read_from_front(SELF const *self, size_t index)
Definition template.h:255

◆ try_read_from_front()

ITEM const * try_read_from_front ( SELF const * self,
size_t index )
static

Definition at line 141 of file template.h.

141 {
142 INVARIANT_CHECK(self);
143
144 if (index < NS(ITEM_VECTORS, size)(&self->front)) {
145 size_t front_index = NS(ITEM_VECTORS, size)(&self->front) - 1 - index;
146 return NS(ITEM_VECTORS, read)(&self->front, front_index);
147 }
148
149 size_t back_index = index - NS(ITEM_VECTORS, size)(&self->front);
150 return NS(ITEM_VECTORS, try_read)(&self->back, back_index);
151}
static VALUE const * try_read(SELF const *self, INDEX index)
Definition template.h:180

◆ try_write_from_back()

ITEM * try_write_from_back ( SELF * self,
size_t index )
static

Definition at line 161 of file template.h.

161 {
162 return (ITEM*)NS(SELF, try_read_from_back)(self, index);
163}
static ITEM const * try_read_from_back(SELF const *self, size_t index)
Definition template.h:265

◆ try_write_from_front()

ITEM * try_write_from_front ( SELF * self,
size_t index )
static

Definition at line 157 of file template.h.

157 {
158 return (ITEM*)NS(SELF, try_read_from_front)(self, index);
159}