Derive-C
Loading...
Searching...
No Matches
container/vector.c

Examples for using vector containers.

Examples for using vector containers.

#include <stdlib.h>
#include <string.h>
#define ITEM int
#define NAME basic_vec
static void example_basic(DC_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
DC_SCOPED(basic_vec) vec = basic_vec_new(stdalloc_get_ref());
DC_LOG(log, DC_INFO, "pushing 10 integers");
for (int i = 0; i < 10; i++) {
basic_vec_push(&vec, i);
}
DC_LOG(log, DC_INFO, "vector: %s", DC_DEBUG(basic_vec_debug, &vec));
basic_vec_pop(&vec);
DC_LOG(log, DC_INFO, "after pop: %s", DC_DEBUG(basic_vec_debug, &vec));
}
#define ITEM int
#define NAME dynamic_vec
static void example_dynamic(DC_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
DC_SCOPED(dynamic_vec) vec = dynamic_vec_new(stdalloc_get_ref());
DC_LOG(log, DC_INFO, "pushing 10 integers");
for (int i = 0; i < 10; i++) {
dynamic_vec_push(&vec, i);
}
DC_LOG(log, DC_INFO, "vector: %s", DC_DEBUG(dynamic_vec_debug, &vec));
}
#define ITEM int
#define CAPACITY 3
#define NAME static_vec
static void example_static(DC_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
DC_SCOPED(static_vec) vec = static_vec_new();
DC_LOG(log, DC_INFO, "pushing 3 integers to static vec (capacity 3)");
static_vec_push(&vec, 1);
static_vec_push(&vec, 2);
static_vec_push(&vec, 3);
int* result = static_vec_try_push(&vec, 4);
DC_ASSERT(result == NULL);
DC_LOG(log, DC_INFO, "try_push returned NULL as expected (capacity full)");
DC_FOR_CONST(static_vec, &vec, iter, item) { DC_LOG(log, DC_INFO, "item: %d", *item); }
}
#define ITEM char*
#define ITEM_DELETE(ptr_to_str) free(*ptr_to_str)
#define NAME char_vec
#define KEY char*
#define KEY_DELETE(ptr_to_str) free(*ptr_to_str)
#define KEY_HASH DC_DEFAULT_HASH
#define VALUE char_vec
#define VALUE_DELETE char_vec_delete
#define VALUE_DEBUG char_vec_debug
#define VALUE_CLONE char_vec_clone
#define NAME str_to_vec_map
static void example_map(DC_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
DC_SCOPED(str_to_vec_map) map = str_to_vec_map_new(stdalloc_get_ref());
DC_LOG(log, DC_INFO, "inserting key1 with empty vector");
char_vec empty_vec = char_vec_new(stdalloc_get_ref());
char* key = strdup("key1");
str_to_vec_map_insert(&map, key, empty_vec);
DC_LOG(log, DC_INFO, "pushing values to key1's vector");
char_vec* vec_ptr = str_to_vec_map_write(&map, key);
char_vec_push(vec_ptr, strdup("value1"));
char_vec_push(vec_ptr, strdup("value2"));
char_vec_push(vec_ptr, strdup("value3"));
DC_LOG(log, DC_INFO, "map: %s", DC_DEBUG(str_to_vec_map_debug, &map));
}
int main() {
root = NS(DC_LOGGER,
new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
(dc_log_id){"vector"});
example_basic(&root);
example_map(&root);
return 0;
}
int main()
Definition alloc.c:120
static void example_basic(DC_LOGGER *parent)
Definition bitset.c:11
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_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