Derive-C
Loading...
Searching...
No Matches
set.c
Go to the documentation of this file.
1
4
9#include <derive-c/prelude.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#define ITEM int
15#define ITEM_HASH DC_DEFAULT_HASH
16#define NAME int_set
18
19static void example_integer_set(DC_LOGGER* parent) {
20 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
21 DC_SCOPED(int_set) set = int_set_new(stdalloc_get_ref());
22
23 DC_LOG(log, DC_INFO, "adding integers 0-9");
24 for (int i = 0; i < 10; i++) {
25 int_set_add(&set, i);
26 }
27
28 DC_FOR_CONST(int_set, &set, iter, item) { DC_LOG(log, DC_INFO, "item: %d", *item); }
29
30 DC_LOG(log, DC_INFO, "set: %s", DC_DEBUG(int_set_debug, &set));
31}
32
34 bool flag;
35 char* name;
36 int value;
37};
38
39static void complex_data_delete(struct complex_data* self) { free(self->name); }
40
41static bool complex_data_eq(struct complex_data const* a, struct complex_data const* b) {
42 return a->flag == b->flag && strcmp(a->name, b->name) == 0 && a->value == b->value;
43}
44
45static size_t complex_data_hash(struct complex_data const* self) {
46 const char* name_ptr = self->name;
47 size_t hash = dc_fnv1a_str_const(&name_ptr);
48 int32_t value = (int32_t)self->value;
49 hash = dc_hash_combine(hash, int32_t_hash_id(&value));
50 hash = dc_hash_combine(hash, (size_t)self->flag);
51 return hash;
52}
53
54static void complex_data_debug(struct complex_data const* self, dc_debug_fmt fmt, FILE* stream) {
55 fprintf(stream, "complex_data@%p {\n", (void*)self);
56 fmt = dc_debug_fmt_scope_begin(fmt);
57 dc_debug_fmt_print(fmt, stream, "flag: %s,\n", self->flag ? "true" : "false");
58 dc_debug_fmt_print(fmt, stream, "name: %s,\n", self->name);
59 dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
60 fmt = dc_debug_fmt_scope_end(fmt);
61 dc_debug_fmt_print(fmt, stream, "}");
62}
63
64#define ITEM struct complex_data
65#define ITEM_HASH complex_data_hash
66#define ITEM_EQ complex_data_eq
67#define ITEM_DELETE complex_data_delete
68#define ITEM_DEBUG complex_data_debug
69#define NAME complex_set
71
72static void example_complex_set(DC_LOGGER* parent) {
73 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
74 DC_SCOPED(complex_set) set = complex_set_new(stdalloc_get_ref());
75
76 DC_LOG(log, DC_INFO, "adding two complex items");
77 struct complex_data item1 = {.flag = true, .name = strdup("first"), .value = 42};
78 struct complex_data item2 = {.flag = false, .name = strdup("second"), .value = 100};
79
80 complex_set_add(&set, item1);
81 complex_set_add(&set, item2);
82
83 struct complex_data lookup = {.flag = true, .name = strdup("first"), .value = 42};
84 DC_LOG(log, DC_INFO, "checking if lookup item exists");
85 DC_ASSERT(complex_set_contains(&set, lookup));
86 free(lookup.name);
87
88 DC_LOG(log, DC_INFO, "set: %s", DC_DEBUG(complex_set_debug, &set));
89}
90
91int main() {
93 root = NS(DC_LOGGER, new_global)(
94 (NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true}, (dc_log_id){"set"});
95
98 return 0;
99}
static DC_PUBLIC size_t dc_hash_combine(size_t seed, size_t h)
Definition combine.h:7
IV_PAIR item
Definition template.h:281
static DC_PUBLIC void set(SELF *self, INDEX_TYPE index, bool value)
Definition template.h:73
#define DC_DEBUG(DEBUG_FN, DEBUG_PTR)
Definition dump.h:92
#define DC_LOGGER
Definition file.h:168
static DC_PUBLIC void dc_debug_fmt_print(dc_debug_fmt fmt, FILE *stream, const char *format,...)
Definition fmt.h:32
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_end(dc_debug_fmt fmt)
Definition fmt.h:57
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_scope_begin(dc_debug_fmt fmt)
Definition fmt.h:50
static DC_PUBLIC uint64_t dc_fnv1a_str_const(const char *const *s)
Definition fnv1a.h:26
#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
static void example_complex_set(DC_LOGGER *parent)
Definition set.c:72
static size_t complex_data_hash(struct complex_data const *self)
Definition set.c:45
static void example_integer_set(DC_LOGGER *parent)
Definition set.c:19
static void complex_data_debug(struct complex_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition set.c:54
int main()
Definition set.c:91
static bool complex_data_eq(struct complex_data const *a, struct complex_data const *b)
Definition set.c:41
static void complex_data_delete(struct complex_data *self)
Definition set.c:39
bool flag
Definition set.c:34
int value
Definition set.c:36
char * name
Definition set.c:35
Debug format helpers for debug printin data structures.
Definition fmt.h:11
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108