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

Examples for using set containers.

Examples for using set containers.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ITEM int
#define ITEM_HASH DC_DEFAULT_HASH
#define NAME int_set
static void example_integer_set() {
DC_SCOPED(int_set) set = int_set_new(stdalloc_get_ref());
for (int i = 0; i < 10; i++) {
int_set_add(&set, i);
}
DC_FOR_CONST(int_set, &set, iter, item) { printf("%d ", *item); }
printf("\n");
int_set_debug(&set, dc_debug_fmt_new(), stdout);
}
struct complex_data {
bool flag;
char* name;
int value;
};
static void complex_data_delete(struct complex_data* self) { free(self->name); }
static bool complex_data_eq(struct complex_data const* a, struct complex_data const* b) {
return a->flag == b->flag && strcmp(a->name, b->name) == 0 && a->value == b->value;
}
static size_t complex_data_hash(struct complex_data const* self) {
const char* name_ptr = self->name;
size_t hash = dc_fnv1a_str_const(&name_ptr);
int32_t value = (int32_t)self->value;
hash = dc_hash_combine(hash, int32_t_hash_id(&value));
hash = dc_hash_combine(hash, (size_t)self->flag);
return hash;
}
static void complex_data_debug(struct complex_data const* self, dc_debug_fmt fmt, FILE* stream) {
fprintf(stream, "complex_data@%p {\n", (void*)self);
dc_debug_fmt_print(fmt, stream, "flag: %s,\n", self->flag ? "true" : "false");
dc_debug_fmt_print(fmt, stream, "name: %s,\n", self->name);
dc_debug_fmt_print(fmt, stream, "value: %d,\n", self->value);
}
#define ITEM struct complex_data
#define ITEM_HASH complex_data_hash
#define ITEM_EQ complex_data_eq
#define ITEM_DELETE complex_data_delete
#define ITEM_DEBUG complex_data_debug
#define NAME complex_set
static void example_complex_set() {
DC_SCOPED(complex_set) set = complex_set_new(stdalloc_get_ref());
struct complex_data item1 = {.flag = true, .name = strdup("first"), .value = 42};
struct complex_data item2 = {.flag = false, .name = strdup("second"), .value = 100};
complex_set_add(&set, item1);
complex_set_add(&set, item2);
struct complex_data lookup = {.flag = true, .name = strdup("first"), .value = 42};
DC_ASSERT(complex_set_contains(&set, lookup));
free(lookup.name);
complex_set_debug(&set, dc_debug_fmt_new(), stdout);
}
int main() {
return 0;
}
int main()
Definition alloc.c:109
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
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_new()
Definition fmt.h:15
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 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()
Definition set.c:72
static size_t complex_data_hash(struct complex_data const *self)
Definition set.c:45
static void example_integer_set()
Definition set.c:19
static void complex_data_debug(struct complex_data const *self, dc_debug_fmt fmt, FILE *stream)
Definition set.c:54
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_DEBUG_TRACE
Definition debug.h:17
static DC_PUBLIC FILE * stream(SELF *self)
Definition template.h:108