#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.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/core/alloc/undef.h>
#include <derive-c/core/self/undef.h>
Go to the source code of this file.
|
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...
|
◆ ITEM
◆ ITEM_CLONE
◆ ITEM_DELETE
◆ clone()
Definition at line 295 of file template.h.
295 {
297 ITEM* new_data = NULL;
298 size_t tail = 0;
299 size_t new_capacity = 0;
301
302 if (old_size > 0) {
306
309 while ((item =
NS(ITER_CONST,
next)(&iter))) {
311 tail++;
312 }
313 tail--;
314 }
316 .data = new_data,
317 .capacity = new_capacity,
318 .head = 0,
319 .tail = tail,
320 .empty = self->empty,
321 .alloc = self->alloc,
323 };
324}
static void * malloc(SELF *self, size_t size)
static size_t next_power_of_2(size_t x)
#define DEBUG_ASSERT(expr)
static ITER_CONST get_iter_const(SELF const *self)
static INDEX_TYPE size(SELF const *self)
static IV_PAIR const * next(ITER *iter)
◆ delete()
void delete |
( |
SELF * | self | ) |
|
|
static |
Definition at line 252 of file template.h.
252 {
257 while ((item =
NS(ITER,
next)(&iter))) {
259 }
261 }
262}
static void free(SELF *self, void *ptr)
static ITER get_iter(SELF *self)
◆ empty() [1/3]
bool empty |
( |
ITER const * | iter | ) |
|
|
static |
Definition at line 229 of file template.h.
229 {
232}
static ITEM const * try_read_from_front(SELF const *self, size_t index)
◆ empty() [2/3]
bool empty |
( |
ITER_CONST const * | iter | ) |
|
|
static |
◆ empty() [3/3]
bool empty |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 76 of file template.h.
76 {
78 if (self->empty) {
80 }
81 return self->empty;
82}
◆ get_iter()
ITER get_iter |
( |
SELF * | self | ) |
|
|
static |
Definition at line 244 of file template.h.
244 {
246 return (ITER){
247 .circular = self,
248 .position = 0,
249 };
250}
◆ get_iter_const()
ITER_CONST get_iter_const |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 287 of file template.h.
287 {
289 return (ITER_CONST){
290 .circular = self,
291 .position = 0,
292 };
293}
◆ item_clone()
◆ item_delete()
void item_delete |
( |
item_t * | UNUSEDself | ) |
|
|
static |
◆ new()
Definition at line 47 of file template.h.
47 {
48 return (
SELF){.data = NULL,
49 .head = 0,
50 .tail = 0,
51 .empty = true,
52 .alloc = alloc,
54}
◆ new_with_capacity_for()
SELF new_with_capacity_for |
( |
size_t | capacity_for, |
|
|
ALLOC * | alloc ) |
|
static |
Definition at line 56 of file template.h.
56 {
57 if (capacity_for == 0) {
58 return NS(
SELF,
new)(alloc);
59 }
64
67 .capacity = capacity,
68 .head = 0,
69 .tail = 0,
70 .empty = true,
71 .alloc = alloc,
73 };
74}
static bool is_power_of_2(size_t x)
static ITEM * data(SELF *self)
◆ next() [1/2]
ITEM * next |
( |
ITER * | iter | ) |
|
|
static |
Definition at line 234 of file template.h.
234 {
237 if (!item) {
238 return NULL;
239 }
240 iter->position++;
241 return item;
242}
static ITEM * try_write_from_front(SELF *self, size_t index)
◆ next() [2/2]
ITEM const * next |
( |
ITER_CONST * | iter | ) |
|
|
static |
Definition at line 277 of file template.h.
277 {
280 if (!item) {
281 return NULL;
282 }
283 iter->position++;
284 return item;
285}
◆ pop_back()
Definition at line 174 of file template.h.
174 {
177
181 } else {
182 if (self->
tail == 0) {
184 } else {
186 }
187 }
188 return value;
189}
static bool empty(ITER const *iter)
◆ pop_front()
Definition at line 161 of file template.h.
161 {
164
168 } else {
170 }
171 return value;
172}
static size_t modulus_power_of_2_capacity(size_t index, size_t capacity)
◆ push_back()
void push_back |
( |
SELF * | self, |
|
|
ITEM | item ) |
|
static |
Definition at line 135 of file template.h.
135 {
138
141 }
144}
static void reserve(SELF *self, size_t new_capacity_for)
◆ push_front()
void push_front |
( |
SELF * | self, |
|
|
ITEM | item ) |
|
static |
Definition at line 146 of file template.h.
146 {
149
151 if (self->
head == 0) {
153 } else {
155 }
156 }
159}
◆ read_from_front()
ITEM const * read_from_front |
( |
SELF const * | self, |
|
|
size_t | index ) |
|
static |
Definition at line 200 of file template.h.
200 {
204 return item;
205}
◆ reserve()
void reserve |
( |
SELF * | self, |
|
|
size_t | new_capacity_for ) |
|
static |
Definition at line 97 of file template.h.
97 {
99
100 if (new_capacity_for > self->
capacity) {
106
107
108
109
110 size_t old_capacity = self->
capacity;
111 size_t additional_capacity = new_capacity - old_capacity;
112 size_t front_tail_items = self->
tail + 1;
113 size_t back_head_items = old_capacity - self->
head;
114
115 if (front_tail_items > back_head_items) {
116 size_t new_head = self->
head + additional_capacity;
117 memmove(&new_data[new_head], &new_data[self->
head], back_head_items *
sizeof(
ITEM));
118 self->
head = new_head;
119 } else {
120
121
122
123
125
126 memcpy(&new_data[old_capacity], &new_data[0], front_tail_items *
sizeof(
ITEM));
127 self->
tail = old_capacity + front_tail_items - 1;
128 }
129 }
131 self->
data = new_data;
132 }
133}
static void * realloc(SELF *self, void *ptr, size_t size)
◆ size()
size_t size |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 84 of file template.h.
84 {
86 if (self->empty) {
88 return 0;
89 }
90
91 if (self->head <= self->tail) {
92 return (self->tail - self->head) + 1;
93 }
94 return (self->capacity - self->head) + self->tail + 1;
95}
◆ try_read_from_front()
ITEM const * try_read_from_front |
( |
SELF const * | self, |
|
|
size_t | index ) |
|
static |
Definition at line 191 of file template.h.
191 {
195 return &self->data[real_index];
196 }
197 return NULL;
198}
◆ try_write_from_front()
ITEM * try_write_from_front |
( |
SELF * | self, |
|
|
size_t | index ) |
|
static |
Definition at line 207 of file template.h.
207 {
211 return &self->
data[real_index];
212 }
213 return NULL;
214}
◆ write_from_front()
ITEM * write_from_front |
( |
SELF * | self, |
|
|
size_t | index ) |
|
static |
Definition at line 216 of file template.h.
216 {
220 return value;
221}