Derive-C
Loading...
Searching...
No Matches
vector.c
Go to the documentation of this file.
1
4
5#include <assert.h>
6#include <stdint.h>
7
9
10#define T int32_t
11#define SELF vec_ints
13
15 vec_ints ints = vec_ints_new_with_capacity(10);
16 const int32_t upto = 100;
17
18 for (int32_t i = 0; i < upto; i++) {
19 vec_ints_push(&ints, i);
20 }
21 assert(vec_ints_size(&ints) == upto);
22
23 for (int32_t i = 0; i < upto; i++) {
24 int* value = vec_ints_write(&ints, i);
25 *value += 1; // Increment each value by 1
26 }
27
28 for (int32_t i = 0; i < upto; i++) {
29 assert(*vec_ints_read(&ints, i) == i + 1);
30 }
31
32 // Pop the last value
33 int32_t last_value = vec_ints_pop(&ints);
34 assert(last_value == upto); // Last value should be 99 + 1
35 assert(vec_ints_size(&ints) == upto - 1);
36
37 vec_ints_delete(&ints);
38}
39
40struct complex {
42 size_t score;
43};
44
45void complex_delete(struct complex* self) { free(self->description); }
46
47#define T struct complex
48#define T_DELETE complex_delete
49#define SELF vec_complex
51
53 vec_complex vec = vec_complex_new_with_capacity(5);
54 size_t entries = 5;
55 for (size_t i = 0; i < entries; i++) {
56 struct complex item = {.description = strdup("Complex item"), .score = i * 10};
57 vec_complex_push(&vec, item);
58 }
59
60 assert(vec_complex_size(&vec) == entries);
61
62 struct complex* first_item = vec_complex_write(&vec, 0);
63 first_item->score += 5;
64
65 assert(vec_complex_read(&vec, 0)->score == 5);
66
67 struct complex popped = vec_complex_pop(&vec);
68 assert(popped.score == 40); // Last item's score should be 40
69
70 vec_complex_delete(&vec);
71 complex_delete(&popped);
72}
73
74#define T char
75#define SELF char_vec
77
79 char_vec vec = char_vec_new();
80 char_vec_push(&vec, 'H');
81 char_vec_push(&vec, 'e');
82 char_vec_push(&vec, 'l');
83 char_vec_push(&vec, 'l');
84 char_vec_push(&vec, 'o');
85 char_vec_push(&vec, ' ');
86 char_vec_push(&vec, 'W');
87 char_vec_push(&vec, 'o');
88 char_vec_push(&vec, 'r');
89 char_vec_push(&vec, 'l');
90 char_vec_push(&vec, 'd');
91
92 assert(char_vec_size(&vec) == 11);
93 {
94 // Iterate over the vector and print the items
95 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
96 char const* item = NULL;
97 while (item = char_vec_iter_const_next(&iter), item != NULL) {
98 printf("%c", *item);
99 }
100 printf("\n");
101 }
102
103 {
104 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
105 ITER_LOOP(char_vec_iter_const, iter, char const*, foo) { printf("%c", *foo); }
106 printf("\n");
107 }
108
109 char_vec_delete(&vec);
110}
111
112int main() {
113 ints_example();
116}
#define ITER_LOOP(ITER_TYPE, ITER_NAME, VALUE_TYPE, VALUE_NAME)
Definition iterators.h:2
char * description
Definition vector.c:41
size_t score
Definition vector.c:42
Definition arena.c:39
void ints_example()
Definition vector.c:14
void complex_example()
Definition vector.c:52
void complex_delete(struct complex *self)
Definition vector.c:45
void iterate_example()
Definition vector.c:78
int main()
Definition vector.c:112