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 INVARIANT_CHECK(self)
#define ITER   NS(SELF, iter)
#define ITER_CONST   NS(SELF, iter_const)

Typedefs

typedef size_t index_t
typedef ITEMitem

Functions

static void ITEM_DELETE (item_t *)
static item_t ITEM_CLONE (item_t const *self)
static void ITEM_DEBUG (ITEM const *, dc_debug_fmt, FILE *)
static DC_PUBLIC SELF new (ref alloc_ref)
static DC_PUBLIC SELF new_with_capacity (size_t capacity, ref alloc_ref)
static DC_PUBLIC SELF new_with_defaults (size_t size, ITEM default_item, ref alloc_ref)
static DC_PUBLIC void reserve (SELF *self, size_t new_capacity)
static DC_PUBLIC SELF clone (SELF const *self)
static DC_PUBLIC ITEM const * try_read (SELF const *self, size_t index)
static DC_PUBLIC ITEM const * read (SELF const *self, size_t index)
static DC_PUBLIC ITEMtry_write (SELF *self, size_t index)
static DC_PUBLIC ITEMwrite (SELF *self, size_t index)
static DC_PUBLIC ITEMtry_insert_at (SELF *self, size_t at, ITEM const *items, size_t count)
static DC_PUBLIC void remove_at (SELF *self, size_t at, size_t count)
static DC_PUBLIC ITEMtry_push (SELF *self, ITEM item)
static DC_PUBLIC ITEMpush (SELF *self, ITEM item)
static DC_PUBLIC bool try_pop (SELF *self, ITEM *destination)
static DC_PUBLIC ITEMdata (SELF *self)
static DC_PUBLIC ITEM pop (SELF *self)
static DC_PUBLIC ITEM pop_front (SELF *self)
static DC_PUBLIC size_t size (SELF const *self)
static DC_PUBLIC void delete (SELF *self)
static DC_PUBLIC void transfer_reverse (SELF *source, SELF *target, size_t to_move)
static DC_PUBLIC DC_INLINE bool empty_item (ITEM *const *item)
static DC_PUBLIC DC_INLINE ITEMnext (ITER *DC_RESTRICT iter)
static DC_PUBLIC DC_INLINE size_t position (ITER const *DC_RESTRICT iter)
static DC_PUBLIC DC_INLINE bool empty (ITER const *DC_RESTRICT iter)
static DC_PUBLIC DC_INLINE ITER get_iter (SELF *self)
static DC_PUBLIC DC_INLINE bool empty_item (ITEM const *const *item)
static DC_PUBLIC DC_INLINE ITEM const * next (ITER_CONST *DC_RESTRICT iter)
static DC_PUBLIC DC_INLINE size_t position (ITER_CONST const *DC_RESTRICT iter)
static DC_PUBLIC DC_INLINE bool empty (ITER_CONST const *DC_RESTRICT iter)
static DC_PUBLIC DC_INLINE ITER_CONST get_iter_const (SELF const *self)
static DC_PUBLIC void debug (SELF const *self, dc_debug_fmt fmt, FILE *stream)
 DC_TRAIT_VECTOR (SELF)

Variables

DC_STATIC_CONSTANT size_t max_size = SIZE_MAX

Macro Definition Documentation

◆ INVARIANT_CHECK

#define INVARIANT_CHECK ( self)
Value:
DC_ASSUME(self); \
DC_ASSUME((self)->size <= (self)->capacity); \
DC_ASSUME(DC_WHEN(!((self)->data), (self)->capacity == 0 && (self)->size == 0));
static DC_PUBLIC size_t size(SELF const *self)
Definition template.h:252
static DC_PUBLIC ITEM * data(SELF *self)
Definition template.h:284
#define DC_WHEN(cond, expr)
Definition panic.h:52
#define DC_ASSUME(expr,...)
Definition panic.h:57

Definition at line 51 of file template.h.

51#define INVARIANT_CHECK(self) \
52 DC_ASSUME(self); \
53 DC_ASSUME((self)->size <= (self)->capacity); \
54 DC_ASSUME(DC_WHEN(!((self)->data), (self)->capacity == 0 && (self)->size == 0));

◆ ITEM

#define ITEM   item_t

Definition at line 18 of file template.h.

◆ ITEM_CLONE

#define ITEM_CLONE   item_clone

Definition at line 21 of file template.h.

◆ ITEM_DEBUG

#define ITEM_DEBUG   item_debug

Definition at line 23 of file template.h.

◆ ITEM_DELETE

#define ITEM_DELETE   item_delete

Definition at line 19 of file template.h.

◆ ITER

#define ITER   NS(SELF, iter)

Definition at line 370 of file template.h.

◆ ITER_CONST

#define ITER_CONST   NS(SELF, iter_const)

Definition at line 417 of file template.h.

Typedef Documentation

◆ index_t

typedef size_t index_t

Definition at line 39 of file template.h.

◆ item

typedef ITEM const* item

Definition at line 371 of file template.h.

Function Documentation

◆ clone()

DC_PUBLIC SELF clone ( SELF const * self)
static

Definition at line 141 of file template.h.

141 {
142 INVARIANT_CHECK(self);
143 ITEM* data = (ITEM*)NS(ALLOC, allocate_uninit)(self->alloc_ref, self->capacity * sizeof(ITEM));
144
145 for (size_t index = 0; index < self->size; index++) {
146 data[index] = ITEM_CLONE(&self->data[index]);
147 }
149 &data[self->size], (self->capacity - self->size) * sizeof(ITEM));
150 return (SELF){
151 .size = self->size,
152 .capacity = self->capacity,
153 .data = data,
154 .alloc_ref = self->alloc_ref,
155 .derive_c_vector_marker = dc_gdb_marker_new(),
156 .iterator_invalidation_tracker = mutation_tracker_new(),
157 };
158}
#define ITEM
Definition template.h:36
static DC_PUBLIC void * allocate_uninit(SELF *self, size_t size)
Definition template.h:92
#define ALLOC
Definition template.h:31
#define INVARIANT_CHECK(self)
Definition template.h:90
#define ITEM_CLONE
Definition template.h:22
#define SELF
Definition def.h:52
static DC_PUBLIC dc_gdb_marker dc_gdb_marker_new()
Definition gdb_marker.h:8
static DC_PUBLIC void dc_memory_tracker_set(dc_memory_tracker_level level, dc_memory_tracker_capability cap, const volatile void *addr, size_t size)
@ DC_MEMORY_TRACKER_LVL_CONTAINER
@ DC_MEMORY_TRACKER_CAP_NONE
static DC_PUBLIC mutation_tracker mutation_tracker_new()
#define NS(pre, post)
Definition namespace.h:14

◆ data()

DC_PUBLIC ITEM * data ( SELF * self)
static
Examples
complex/employees.c.

Definition at line 284 of file template.h.

284 {
285 INVARIANT_CHECK(self);
286 return self->data;
287}
ITEM * data
Definition template.h:44

◆ DC_TRAIT_VECTOR()

DC_TRAIT_VECTOR ( SELF )

◆ debug()

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

Definition at line 464 of file template.h.

464 {
465 fprintf(stream, DC_EXPAND_STRING(SELF) "@%p {\n", (void*)self);
466 fmt = dc_debug_fmt_scope_begin(fmt);
467 dc_debug_fmt_print(fmt, stream, "size: %lu,\n", self->size);
468 dc_debug_fmt_print(fmt, stream, "capacity: %lu,\n", self->capacity);
469
470 dc_debug_fmt_print(fmt, stream, "alloc: ");
471 NS(ALLOC, debug)(NS(NS(ALLOC, ref), deref)(self->alloc_ref), fmt, stream);
472 fprintf(stream, ",\n");
473
474 dc_debug_fmt_print(fmt, stream, "items: @%p [\n", (void*)self->data);
475 fmt = dc_debug_fmt_scope_begin(fmt);
476
477 ITER_CONST iter = NS(SELF, get_iter_const)(self);
478 ITEM const* item;
479 while ((item = NS(ITER_CONST, next)(&iter))) {
481 ITEM_DEBUG(item, fmt, stream);
482 fprintf(stream, ",\n");
483 }
484 fmt = dc_debug_fmt_scope_end(fmt);
485 dc_debug_fmt_print(fmt, stream, "],\n");
486 fmt = dc_debug_fmt_scope_end(fmt);
487 dc_debug_fmt_print(fmt, stream, "}");
488}
static DC_PUBLIC void debug(SELF const *self, dc_debug_fmt fmt, FILE *stream)
Definition template.h:212
static DC_PUBLIC IV_PAIR next(ITER *iter)
Definition template.h:355
#define ITER_CONST
Definition template.h:411
static DC_PUBLIC ITER_CONST get_iter_const(SELF const *self)
Definition template.h:464
IV_PAIR item
Definition template.h:281
#define ITEM_DEBUG
Definition template.h:24
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC void dc_debug_fmt_print_indents(dc_debug_fmt fmt, FILE *stream)
Definition fmt.h:20
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
#define DC_EXPAND_STRING(NAME)
Definition namespace.h:6
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108

◆ delete()

DC_PUBLIC void delete ( SELF * self)
static

Definition at line 313 of file template.h.

313 {
314 INVARIANT_CHECK(self);
315 if (self->data) {
316 for (size_t i = 0; i < self->size; i++) {
317 ITEM_DELETE(&self->data[i]);
318 // JUSTIFY: Setting items as inaccessible
319 // - Incase a destructor of one item accesses the memory of another.
321 &self->data[i], sizeof(ITEM));
322 }
323
324 // JUSTIFY: Return to write level before passing to allocator
325 // - Is uninitialised, but still valid memory
326 size_t const size = self->capacity * sizeof(ITEM);
328 self->data, size);
329 NS(ALLOC, deallocate)(self->alloc_ref, self->data, size);
330 }
331}
static DC_PUBLIC void deallocate(SELF *self, void *ptr, size_t size)
Definition template.h:127
#define ITEM_DELETE
Definition template.h:20
@ DC_MEMORY_TRACKER_CAP_WRITE
size_t capacity
Definition template.h:72
size_t size
Definition template.h:75
ref alloc_ref
Definition template.h:49

◆ empty() [1/2]

DC_PUBLIC DC_INLINE bool empty ( ITER const *DC_RESTRICT iter)
static

Definition at line 400 of file template.h.

400 {
401 DC_ASSUME(iter);
402 mutation_version_check(&iter->version);
403 return iter->pos >= iter->size;
404}
static DC_PUBLIC void mutation_version_check(mutation_version const *self)

◆ empty() [2/2]

DC_PUBLIC DC_INLINE bool empty ( ITER_CONST const *DC_RESTRICT iter)
static

Definition at line 448 of file template.h.

448 {
449 DC_ASSUME(iter);
450 mutation_version_check(&iter->vec_version);
451 return iter->pos >= iter->size;
452}

◆ empty_item() [1/2]

DC_PUBLIC DC_INLINE bool empty_item ( ITEM *const * item)
static

Definition at line 373 of file template.h.

373{ return *item == NULL; }

◆ empty_item() [2/2]

DC_PUBLIC DC_INLINE bool empty_item ( ITEM const *const * item)
static

Definition at line 420 of file template.h.

420 {
421 return *item == NULL;
422}

◆ get_iter()

DC_PUBLIC DC_INLINE ITER get_iter ( SELF * self)
static

Definition at line 406 of file template.h.

406 {
407 DC_ASSUME(self);
408 return (ITER){
409 .data = self->data,
410 .pos = 0,
411 .size = self->size,
413 };
414}
#define ITER
Definition template.h:322
static DC_PUBLIC mutation_version mutation_tracker_get(mutation_tracker const *self)
mutation_tracker iterator_invalidation_tracker
Definition template.h:87

◆ get_iter_const()

DC_PUBLIC DC_INLINE ITER_CONST get_iter_const ( SELF const * self)
static

Definition at line 454 of file template.h.

454 {
455 DC_ASSUME(self);
456 return (ITER_CONST){
457 .data = self->data,
458 .pos = 0,
459 .size = self->size,
460 .vec_version = mutation_tracker_get(&self->iterator_invalidation_tracker),
461 };
462}

◆ ITEM_CLONE()

item_t ITEM_CLONE ( item_t const * self)
static

Definition at line 22 of file template.h.

22{ return *self; }

◆ ITEM_DEBUG()

void ITEM_DEBUG ( ITEM const * ,
dc_debug_fmt ,
FILE *  )
static

Definition at line 24 of file template.h.

24{}

◆ ITEM_DELETE()

void ITEM_DELETE ( item_t * )
static

Definition at line 20 of file template.h.

20{}

◆ new()

DC_PUBLIC SELF new ( ref alloc_ref)
static

Definition at line 58 of file template.h.

58 {
59 SELF self = (SELF){
60 .size = 0,
61 .capacity = 0,
62 .data = NULL,
63 .alloc_ref = alloc_ref,
64 .derive_c_vector_marker = dc_gdb_marker_new(),
65 .iterator_invalidation_tracker = mutation_tracker_new(),
66 };
67 return self;
68}

◆ new_with_capacity()

DC_PUBLIC SELF new_with_capacity ( size_t capacity,
ref alloc_ref )
static

Definition at line 70 of file template.h.

70 {
71 if (capacity == 0) {
72 return NS(SELF, new)(alloc_ref);
73 }
74
75 ITEM* data = (ITEM*)NS(ALLOC, allocate_uninit)(alloc_ref, capacity * sizeof(ITEM));
77 capacity * sizeof(ITEM));
78 return (SELF){
79 .size = 0,
80 .capacity = capacity,
81 .data = data,
82 .alloc_ref = alloc_ref,
83 .derive_c_vector_marker = dc_gdb_marker_new(),
84 .iterator_invalidation_tracker = mutation_tracker_new(),
85 };
86}

◆ new_with_defaults()

DC_PUBLIC SELF new_with_defaults ( size_t size,
ITEM default_item,
ref alloc_ref )
static

Definition at line 88 of file template.h.

89 {
90 ITEM* data = (ITEM*)NS(ALLOC, allocate_uninit)(alloc_ref, size * sizeof(ITEM));
91 if (size > 0) {
92 // JUSTIFY: We only need to copy size-1 entries - can move the first as default.
93 data[0] = default_item;
94 for (size_t i = 1; i < size; i++) {
95 data[i] = ITEM_CLONE(&default_item);
96 }
97 }
98 return (SELF){
99 .size = size,
100 .capacity = size,
101 .data = data,
102 .alloc_ref = alloc_ref,
103 .derive_c_vector_marker = dc_gdb_marker_new(),
104 .iterator_invalidation_tracker = mutation_tracker_new(),
105 };
106}

◆ next() [1/2]

DC_PUBLIC DC_INLINE ITEM * next ( ITER *DC_RESTRICT iter)
static

Definition at line 382 of file template.h.

382 {
383 DC_ASSUME(iter);
385
386 if (iter->pos < iter->size) {
387 ITEM* item = &iter->data[iter->pos];
388 iter->pos++;
389 return item;
390 }
391 return NULL;
392}
mutation_version version
Definition template.h:328
size_t size
Definition template.h:378
ITEM * data
Definition template.h:376
size_t pos
Definition template.h:233

◆ next() [2/2]

DC_PUBLIC DC_INLINE ITEM const * next ( ITER_CONST *DC_RESTRICT iter)
static

Definition at line 431 of file template.h.

431 {
432 DC_ASSUME(iter);
434 if (iter->pos < iter->size) {
435 ITEM const* item = &iter->data[iter->pos];
436 iter->pos++;
437 return item;
438 }
439 return NULL;
440}
size_t pos
Definition template.h:280
ITEM const * data
Definition template.h:425
mutation_version vec_version
Definition template.h:428
size_t size
Definition template.h:427

◆ pop()

DC_PUBLIC ITEM pop ( SELF * self)
static

Definition at line 289 of file template.h.

289 {
290 ITEM entry;
291 DC_ASSERT(NS(SELF, try_pop)(self, &entry), "Cannot pop, already empty {size=%lu}",
292 (size_t)self->size);
293 return entry;
294}
static DC_PUBLIC bool try_pop(SELF *self, ITEM *destination)
Definition template.h:270
#define DC_ASSERT(expr,...)
Definition panic.h:37

◆ pop_front()

DC_PUBLIC ITEM pop_front ( SELF * self)
static

Definition at line 296 of file template.h.

296 {
297 INVARIANT_CHECK(self);
299 DC_ASSERT(self->size > 0, "Cannot pop front, already empty {size=%lu}", (size_t)self->size);
300 ITEM entry = self->data[0];
301 memmove(&self->data[0], &self->data[1], (self->size - 1) * sizeof(ITEM));
302 self->size--;
304 &self->data[self->size], sizeof(ITEM));
305 return entry;
306}
static DC_PUBLIC void mutation_tracker_mutate(mutation_tracker *self)

◆ position() [1/2]

DC_PUBLIC DC_INLINE size_t position ( ITER const *DC_RESTRICT iter)
static

Definition at line 394 of file template.h.

394 {
395 DC_ASSUME(iter);
396 mutation_version_check(&iter->version);
397 return iter->pos;
398}

◆ position() [2/2]

DC_PUBLIC DC_INLINE size_t position ( ITER_CONST const *DC_RESTRICT iter)
static

Definition at line 442 of file template.h.

442 {
443 DC_ASSUME(iter);
444 mutation_version_check(&iter->vec_version);
445 return iter->pos;
446}

◆ push()

DC_PUBLIC ITEM * push ( SELF * self,
ITEM item )
static

Definition at line 263 of file template.h.

263 {
264 ITEM* entry = NS(SELF, try_push)(self, item);
265 DC_ASSERT(entry != NULL, "Cannot push, already at max capacity {capacity=%lu, item=%s}",
266 (size_t)self->capacity, DC_DEBUG(ITEM_DEBUG, &item));
267 return entry;
268}
#define ITEM_DEBUG
Definition template.h:39
static DC_PUBLIC ITEM * try_push(SELF *self, ITEM item)
Definition template.h:232
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92

◆ read()

DC_PUBLIC ITEM const * read ( SELF const * self,
size_t index )
static

Definition at line 168 of file template.h.

168 {
169 ITEM const* item = NS(SELF, try_read)(self, index);
170 DC_ASSERT(item, "Cannot read, index out of bounds {index=%lu, size=%lu}", (size_t)index,
171 (size_t)self->size);
172 return item;
173}
static DC_PUBLIC VALUE const * try_read(SELF const *self, INDEX index)
Definition template.h:180

◆ remove_at()

DC_PUBLIC void remove_at ( SELF * self,
size_t at,
size_t count )
static

Definition at line 211 of file template.h.

211 {
212 INVARIANT_CHECK(self);
213 DC_ASSERT(at + count <= self->size,
214 "Cannot remove at, index out of bounds {at=%lu, count=%lu, size=%lu}", (size_t)at,
215 (size_t)count, (size_t)self->size);
217
218 if (count == 0) {
219 return;
220 }
221
222 for (size_t i = at; i < at + count; i++) {
223 ITEM_DELETE(&self->data[i]);
224 }
225
226 memmove(&self->data[at], &self->data[at + count], (self->size - (at + count)) * sizeof(ITEM));
227 self->size -= count;
229 &self->data[self->size], count * sizeof(ITEM));
230}

◆ reserve()

DC_PUBLIC void reserve ( SELF * self,
size_t new_capacity )
static

Definition at line 108 of file template.h.

108 {
109 INVARIANT_CHECK(self);
110 if (new_capacity > self->capacity) {
111 if (self->data == NULL) {
112 DC_ASSUME(self->capacity == 0);
113
114 ITEM* new_data =
115 (ITEM*)NS(ALLOC, allocate_uninit)(self->alloc_ref, new_capacity * sizeof(ITEM));
116 self->data = new_data;
117 self->capacity = new_capacity;
119 self->data, self->capacity * sizeof(ITEM));
120 } else {
121 const size_t capacity_increase = new_capacity - self->capacity;
122 const size_t uninit_elements = self->capacity - self->size;
123
125 &self->data[self->size], uninit_elements * sizeof(ITEM));
126
127 size_t old_size = self->capacity * sizeof(ITEM);
128 size_t new_size = new_capacity * sizeof(ITEM);
129 ITEM* new_data =
130 (ITEM*)NS(ALLOC, reallocate)(self->alloc_ref, self->data, old_size, new_size);
131
132 self->data = new_data;
134 &self->data[self->size],
135 (uninit_elements + capacity_increase) * sizeof(ITEM));
136 self->capacity = new_capacity;
137 }
138 }
139}
static DC_PUBLIC void * reallocate(SELF *self, void *ptr, size_t old_size, size_t new_size)
Definition template.h:137

◆ size()

DC_PUBLIC size_t size ( SELF const * self)
static

Definition at line 308 of file template.h.

308 {
309 INVARIANT_CHECK(self);
310 return self->size;
311}

◆ transfer_reverse()

DC_PUBLIC void transfer_reverse ( SELF * source,
SELF * target,
size_t to_move )
static

Moves to_move items from the beginning of source, to the end of target, shuffling elements in source forward appropriately.

  • Used for deque rebalancing
index: 0 1 2 3 4 5
source: [5, 4, 3, 2, 1, 0]
target: [6, 7, 8, 9]

Becomes:

index: 0 1 2 3 4 5
source: [3, 2, 1, 0]
target: [4, 5, 6, 7, 8, 9]

Definition at line 348 of file template.h.

348 {
349 INVARIANT_CHECK(source);
350 INVARIANT_CHECK(target);
351
352 NS(SELF, reserve)(target, target->size + to_move);
353
355 &target->data[target->size], to_move * sizeof(ITEM));
356 memmove(&target->data[to_move], target->data, target->size * sizeof(ITEM));
357
358 for (size_t i = 0; i < to_move; i++) {
359 target->data[to_move - 1 - i] = source->data[i];
360 }
361
362 memmove(source->data, &source->data[to_move], (source->size - to_move) * sizeof(ITEM));
363
364 source->size -= to_move;
365 target->size += to_move;
367 &source->data[source->size], to_move * sizeof(ITEM));
368}
static DC_PUBLIC void reserve(SELF *self, size_t new_capacity_for)
Definition template.h:130

◆ try_insert_at()

DC_PUBLIC ITEM * try_insert_at ( SELF * self,
size_t at,
ITEM const * items,
size_t count )
static

Definition at line 190 of file template.h.

191 {
192 INVARIANT_CHECK(self);
193 DC_ASSUME(items);
194 DC_ASSERT(at <= self->size, "Cannot insert at, index out of bounds {at=%lu, size=%lu}",
195 (size_t)at, (size_t)self->size);
197
198 if (count == 0) {
199 return NULL;
200 }
201
202 NS(SELF, reserve)(self, self->size + count);
204 &self->data[self->size], count * sizeof(ITEM));
205 memmove(&self->data[at + count], &self->data[at], (self->size - at) * sizeof(ITEM));
206 memcpy(&self->data[at], items, count * sizeof(ITEM));
207 self->size += count;
208 return &self->data[at];
209}

◆ try_pop()

DC_PUBLIC bool try_pop ( SELF * self,
ITEM * destination )
static

Definition at line 270 of file template.h.

270 {
271 INVARIANT_CHECK(self);
273
274 if (DC_LIKELY(self->size > 0)) {
275 self->size--;
276 *destination = self->data[self->size];
278 &self->data[self->size], sizeof(ITEM));
279 return true;
280 }
281 return false;
282}
#define DC_LIKELY(x)
Definition panic.h:48

◆ try_push()

DC_PUBLIC ITEM * try_push ( SELF * self,
ITEM item )
static

Definition at line 232 of file template.h.

232 {
233 INVARIANT_CHECK(self);
235
236 if (self->size == self->capacity) {
237 size_t new_capacity;
238 if (self->data == NULL) {
239 DC_ASSUME(self->capacity == 0);
240 // JUSTIFY: Allocating capacity of 4
241 // - Avoid repeat reallocations on growing a vector from
242 // size sero (from new)
243 // Otherwise an arbitrary choice (given we do not know the size of T)
244 new_capacity = 8;
245 } else {
246 // JUSTIFY: Growth factor of 2
247 // - Simple arithmetic (for debugging)
248 // - Same as used by GCC's std::vector implementation
249 new_capacity = self->capacity * 2;
250 }
251 NS(SELF, reserve)(self, new_capacity);
252 }
253
255 &self->data[self->size], sizeof(ITEM));
256
257 ITEM* entry = &self->data[self->size];
258 *entry = item;
259 self->size++;
260 return entry;
261}

◆ try_read()

DC_PUBLIC ITEM const * try_read ( SELF const * self,
size_t index )
static

Definition at line 160 of file template.h.

160 {
161 INVARIANT_CHECK(self);
162 if (DC_LIKELY(index < self->size)) {
163 return &self->data[index];
164 }
165 return NULL;
166}

◆ try_write()

DC_PUBLIC ITEM * try_write ( SELF * self,
size_t index )
static

Definition at line 175 of file template.h.

175 {
176 INVARIANT_CHECK(self);
177 if (DC_LIKELY(index < self->size)) {
178 return &self->data[index];
179 }
180 return NULL;
181}

◆ write()

DC_PUBLIC ITEM * write ( SELF * self,
size_t index )
static

Definition at line 183 of file template.h.

183 {
184 ITEM* item = NS(SELF, try_write)(self, index);
185 DC_ASSERT(item, "Cannot write, index out of bounds {index=%lu, size=%lu}", (size_t)index,
186 (size_t)self->size);
187 return item;
188}
static DC_PUBLIC VALUE * try_write(SELF *self, INDEX index)
Definition template.h:205

Variable Documentation

◆ max_size

DC_STATIC_CONSTANT size_t max_size = SIZE_MAX

Definition at line 56 of file template.h.