Examples for using vector containers.
Examples for using vector containers.
#include <stdlib.h>
#define ITEM int
#define NAME basic_vec
DC_SCOPED(basic_vec) vec = basic_vec_new(stdalloc_get_ref());
for (int i = 0; i < 10; i++) {
basic_vec_push(&vec, i);
}
basic_vec_pop(&vec);
}
#define ITEM int
#define NAME dynamic_vec
DC_SCOPED(dynamic_vec) vec = dynamic_vec_new(stdalloc_get_ref());
for (int i = 0; i < 10; i++) {
dynamic_vec_push(&vec, i);
}
}
#define ITEM int
#define CAPACITY 3
#define NAME static_vec
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_LOG(log,
DC_INFO,
"try_push returned NULL as expected (capacity full)");
}
#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
DC_SCOPED(str_to_vec_map) map = str_to_vec_map_new(stdalloc_get_ref());
char_vec empty_vec = char_vec_new(stdalloc_get_ref());
char* key = strdup("key1");
str_to_vec_map_insert(&map, key, empty_vec);
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"));
}
new_global)((
NS(
DC_LOGGER, global_config)){.stream = stdout, .ansi_colours =
true},
return 0;
}
static void example_basic(DC_LOGGER *parent)
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
#define DC_FOR_CONST(TYPE, INSTANCE, ITER, ITEM)
#define DC_ASSERT(expr,...)
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
#define DC_LOGGER_NEW(...)
static void example_static(DC_LOGGER *parent)
static void example_map(DC_LOGGER *parent)
static void example_dynamic(DC_LOGGER *parent)