Derive-C
Loading...
Searching...
No Matches
static.c File Reference
#include <stdint.h>
#include <stdio.h>
#include <derive-c/core/prelude.h>
#include <derive-c/container/vector/static/template.h>

Go to the source code of this file.

Macros

#define MAX_CAPACITY   8
#define ITEM   unsigned char
#define INPLACE_CAPACITY   MAX_CAPACITY
#define NAME   staticvec_chars

Functions

void push_example ()
void iter_example ()
int main ()

Macro Definition Documentation

◆ INPLACE_CAPACITY

#define INPLACE_CAPACITY   MAX_CAPACITY

Definition at line 13 of file static.c.

◆ ITEM

#define ITEM   unsigned char

Definition at line 12 of file static.c.

◆ MAX_CAPACITY

#define MAX_CAPACITY   8
Examples
container/vector/static.c.

Definition at line 10 of file static.c.

◆ NAME

#define NAME   staticvec_chars

Definition at line 14 of file static.c.

Function Documentation

◆ iter_example()

void iter_example ( )
Examples
container/vector/static.c.

Definition at line 42 of file static.c.

42 {
43 staticvec_chars vec = staticvec_chars_new();
44
45 // Push characters into the static vector
46 for (unsigned char i = 0; i < MAX_CAPACITY; i++) {
47 staticvec_chars_push(&vec, 'a' + i);
48 }
49
50 // Iterate over the vector and print the items
51 staticvec_chars_iter_const iter = staticvec_chars_get_iter_const(&vec);
52 unsigned char const* item = NULL;
53 while (item = staticvec_chars_iter_const_next(&iter), item != NULL) {
54 printf("%u ", *item);
55 }
56 printf("\n");
57
58 staticvec_chars_debug(&vec, dc_debug_fmt_new(), stdout);
59
60 staticvec_chars_delete(&vec);
61}
IV_PAIR item
Definition template.h:283
static dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:14
#define MAX_CAPACITY
Definition static.c:10

◆ main()

int main ( )

Definition at line 63 of file static.c.

63 {
66}
void iter_example()
Definition static.c:42
void push_example()
Definition static.c:17

◆ push_example()

void push_example ( )
Examples
container/vector/static.c.

Definition at line 17 of file static.c.

17 {
18 staticvec_chars vec = staticvec_chars_new();
19
20 // Push characters into the static vector
21 for (unsigned char i = 0; i < MAX_CAPACITY; i++) {
22 staticvec_chars_push(&vec, i);
23 }
24
25 // Cannot push past the in-place capacity
26 DC_ASSERT(!staticvec_chars_try_push(&vec, 8));
27
28 // Check that the first 8 characters are in place
29 for (unsigned char i = 0; i < MAX_CAPACITY; i++) {
30 DC_ASSERT(*staticvec_chars_read(&vec, i) == i);
31 }
32
33 // The next two should be NULL since they exceed the in-place capacity
34 DC_ASSERT(staticvec_chars_try_read(&vec, MAX_CAPACITY) == NULL);
35 DC_ASSERT(staticvec_chars_try_read(&vec, MAX_CAPACITY + 1) == NULL);
36
37 staticvec_chars_debug(&vec, dc_debug_fmt_new(), stdout);
38
39 staticvec_chars_delete(&vec);
40}
#define DC_ASSERT(expr,...)
Definition panic.h:36