Derive-C
Loading...
Searching...
No Matches
vector.c
Go to the documentation of this file.
1
4
7#include <derive-c/prelude.h>
8#include <stdlib.h>
9#include <string.h>
10
11#define ITEM int
12#define NAME basic_vec
14
15static void example_basic(DC_LOGGER* parent) {
16 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
17 DC_SCOPED(basic_vec) vec = basic_vec_new(stdalloc_get_ref());
18
19 DC_LOG(log, DC_INFO, "pushing 10 integers");
20 for (int i = 0; i < 10; i++) {
21 basic_vec_push(&vec, i);
22 }
23
24 DC_LOG(log, DC_INFO, "vector: %s", DC_DEBUG(basic_vec_debug, &vec));
25 basic_vec_pop(&vec);
26 DC_LOG(log, DC_INFO, "after pop: %s", DC_DEBUG(basic_vec_debug, &vec));
27}
28
29#define ITEM int
30#define NAME dynamic_vec
32
33static void example_dynamic(DC_LOGGER* parent) {
34 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
35 DC_SCOPED(dynamic_vec) vec = dynamic_vec_new(stdalloc_get_ref());
36
37 DC_LOG(log, DC_INFO, "pushing 10 integers");
38 for (int i = 0; i < 10; i++) {
39 dynamic_vec_push(&vec, i);
40 }
41
42 DC_LOG(log, DC_INFO, "vector: %s", DC_DEBUG(dynamic_vec_debug, &vec));
43}
44
45#define ITEM int
46#define CAPACITY 3
47#define NAME static_vec
49
50static void example_static(DC_LOGGER* parent) {
51 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
52 DC_SCOPED(static_vec) vec = static_vec_new();
53
54 DC_LOG(log, DC_INFO, "pushing 3 integers to static vec (capacity 3)");
55 static_vec_push(&vec, 1);
56 static_vec_push(&vec, 2);
57 static_vec_push(&vec, 3);
58
59 int* result = static_vec_try_push(&vec, 4);
60 DC_ASSERT(result == NULL);
61 DC_LOG(log, DC_INFO, "try_push returned NULL as expected (capacity full)");
62
63 DC_FOR_CONST(static_vec, &vec, iter, item) { DC_LOG(log, DC_INFO, "item: %d", *item); }
64}
65
66#define ITEM char*
67#define ITEM_DELETE(ptr_to_str) free(*ptr_to_str)
68#define NAME char_vec
70
71#define KEY char*
72#define KEY_DELETE(ptr_to_str) free(*ptr_to_str)
73#define KEY_HASH DC_DEFAULT_HASH
74#define VALUE char_vec
75#define VALUE_DELETE char_vec_delete
76#define VALUE_DEBUG char_vec_debug
77#define VALUE_CLONE char_vec_clone
78#define NAME str_to_vec_map
80
81static void example_map(DC_LOGGER* parent) {
82 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
83 DC_SCOPED(str_to_vec_map) map = str_to_vec_map_new(stdalloc_get_ref());
84
85 DC_LOG(log, DC_INFO, "inserting key1 with empty vector");
86 char_vec empty_vec = char_vec_new(stdalloc_get_ref());
87 char* key = strdup("key1");
88 str_to_vec_map_insert(&map, key, empty_vec);
89
90 DC_LOG(log, DC_INFO, "pushing values to key1's vector");
91 char_vec* vec_ptr = str_to_vec_map_write(&map, key);
92 char_vec_push(vec_ptr, strdup("value1"));
93 char_vec_push(vec_ptr, strdup("value2"));
94 char_vec_push(vec_ptr, strdup("value3"));
95
96 DC_LOG(log, DC_INFO, "map: %s", DC_DEBUG(str_to_vec_map_debug, &map));
97}
98
99int main() {
101 root = NS(DC_LOGGER,
102 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
103 (dc_log_id){"vector"});
104
105 example_basic(&root);
106 example_dynamic(&root);
107 example_static(&root);
108 example_map(&root);
109 return 0;
110}
IV_PAIR item
Definition template.h:281
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
#define DC_FOR_CONST(TYPE, INSTANCE, ITER, ITEM)
Definition for.h:14
#define NS(pre, post)
Definition namespace.h:14
#define DC_ASSERT(expr,...)
Definition panic.h:37
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8
static void example_basic(DC_LOGGER *parent)
Definition vector.c:15
static void example_static(DC_LOGGER *parent)
Definition vector.c:50
static void example_map(DC_LOGGER *parent)
Definition vector.c:81
static void example_dynamic(DC_LOGGER *parent)
Definition vector.c:33
int main()
Definition vector.c:99