Derive-C
Loading...
Searching...
No Matches
dynamic.c
Go to the documentation of this file.
1
4
5#include <stddef.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include <derive-c/alloc/std.h>
13
14#define ITEM int32_t
15#define NAME vec_ints
17
19 vec_ints ints = vec_ints_new_with_capacity(10, stdalloc_get());
20 const int32_t upto = 100;
21
22 for (int32_t i = 0; i < upto; i++) {
23 vec_ints_push(&ints, i);
24 }
25 DC_ASSERT(vec_ints_size(&ints) == upto);
26
27 for (int32_t i = 0; i < upto; i++) {
28 int* value = vec_ints_write(&ints, i);
29 *value += 1; // Increment each value by 1
30 }
31
32 for (int32_t i = 0; i < upto; i++) {
33 DC_ASSERT(*vec_ints_read(&ints, i) == i + 1);
34 }
35
36 // Pop the last value
37 int32_t last_value = vec_ints_pop(&ints);
38 DC_ASSERT(last_value == upto); // Last value should be 99 + 1
39 DC_ASSERT(vec_ints_size(&ints) == upto - 1);
40
41 vec_ints_debug(&ints, dc_debug_fmt_new(), stdout);
42
43 vec_ints_delete(&ints);
44}
45
46struct complex_data {
47 char* description;
48 size_t score;
49};
50
51void complex_data_debug(struct complex_data const* self, dc_debug_fmt fmt, FILE* stream) {
52 fprintf(stream, "complex_data@%p {\n", self);
53 fmt = dc_debug_fmt_scope_begin(fmt);
54 dc_debug_fmt_print(fmt, stream, "description: %s,\n", self->description);
55 dc_debug_fmt_print(fmt, stream, "score: %lu,\n", self->score);
56 fmt = dc_debug_fmt_scope_end(fmt);
57 dc_debug_fmt_print(fmt, stream, "}");
58}
59
60void complex_data_delete(struct complex_data* self) { free(self->description); }
61
62#define ITEM struct complex_data
63#define ITEM_DELETE complex_data_delete
64#define ITEM_DEBUG complex_data_debug
65#define NAME vec_complex_data
67
69 vec_complex_data vec = vec_complex_data_new_with_capacity(5, stdalloc_get());
70 size_t entries = 5;
71 for (size_t i = 0; i < entries; i++) {
72 struct complex_data item = {.description = strdup("Complex item"), .score = i * 10};
73 vec_complex_data_push(&vec, item);
74 }
75
76 DC_ASSERT(vec_complex_data_size(&vec) == entries);
77
78 struct complex_data* first_item = vec_complex_data_write(&vec, 0);
79 first_item->score += 5;
80
81 DC_ASSERT(vec_complex_data_read(&vec, 0)->score == 5);
82
83 struct complex_data popped = vec_complex_data_pop(&vec);
84 DC_ASSERT(popped.score == 40); // Last item's score should be 40
85
86 vec_complex_data_debug(&vec, dc_debug_fmt_new(), stdout);
87
88 vec_complex_data_delete(&vec);
89 complex_data_delete(&popped);
90}
91
92#define ITEM char
93#define NAME char_vec
95
97 char_vec vec = char_vec_new(stdalloc_get());
98 char_vec_push(&vec, 'H');
99 char_vec_push(&vec, 'e');
100 char_vec_push(&vec, 'l');
101 char_vec_push(&vec, 'l');
102 char_vec_push(&vec, 'o');
103 char_vec_push(&vec, ' ');
104 char_vec_push(&vec, 'W');
105 char_vec_push(&vec, 'o');
106 char_vec_push(&vec, 'r');
107 char_vec_push(&vec, 'l');
108 char_vec_push(&vec, 'd');
109
110 DC_ASSERT(char_vec_size(&vec) == 11);
111 {
112 // Iterate over the vector and print the items
113 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
114 char const* item = NULL;
115 while (item = char_vec_iter_const_next(&iter), item != NULL) {
116 printf("%c", *item);
117 }
118 printf("\n");
119 }
120
121 {
122 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
123
124 char const* c = NULL;
125 size_t index = 0;
126 while ((c = char_vec_iter_const_next(&iter))) {
127 printf("entry for '%c' at index %zu\n", *c, index);
128 index++;
129 }
130 }
131
132 char_vec_delete(&vec);
133}
134
135int main() {
136 ints_example();
139}
static void free(SELF *self, void *ptr)
Definition template.h:56
IV_PAIR item
Definition template.h:283
void complex_data_debug(struct complex_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition dynamic.c:51
void ints_example()
Definition dynamic.c:18
void complex_data_delete(struct complex_data *self)
Definition dynamic.c:60
void complex_data_example()
Definition dynamic.c:68
void iterate_example()
Definition dynamic.c:96
int main()
Definition dynamic.c:135
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
static dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:14
#define DC_ASSERT(expr,...)
Definition panic.h:36
size_t score
Definition dynamic.c:48
char * description
Definition option.c:13
Debug format helpers for debug printin data structures.
Definition fmt.h:10
static FILE * stream(SELF *self)
Opens a file for.
Definition template.h:107