Derive-C
Loading...
Searching...
No Matches
hashmap.c
Go to the documentation of this file.
1
4
5#include <assert.h>
6#include <inttypes.h>
7#include <stdbool.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
16
17#define K uint32_t
18#define V char const*
19#define EQ uint32_t_eq
20#define HASH hash_id_uint32_t
21#define SELF id_to_name
23
24void print_map(id_to_name const* map) {
25 printf("Map has items:\n");
26 id_to_name_iter_const iter = id_to_name_get_iter_const(map);
27 ITER_ENUMERATE_LOOP(id_to_name_iter_const, iter, id_to_name_kv_const, entry, size_t, pos) {
28 printf("position: %zu key: %" PRIu32 " string: %s\n", pos, *entry.key, *entry.value);
29 }
30}
31
33 printf("Id to Name Map Example:\n");
34 id_to_name map = id_to_name_new();
35
36 id_to_name_insert(&map, 23, "hello");
37 id_to_name_insert(&map, 10, "bob");
38 id_to_name_insert(&map, 42, "meaning");
39 ASSERT(strcmp(*id_to_name_read(&map, 42), "meaning") == 0);
40
41 print_map(&map);
42
43 char const** entry;
44 ASSERT((entry = id_to_name_write(&map, 23)));
45 *entry = "a different string!";
46
47 print_map(&map);
48
49 id_to_name_delete(&map);
50}
51
52struct report_id {
53 char* name;
54 uint32_t section;
55};
56
57bool report_id_equality(struct report_id const* report_1, struct report_id const* report_2) {
58 return strcmp(report_1->name, report_2->name) == 0 && report_1->section == report_2->section;
59}
60
61size_t report_id_hash(struct report_id const* report_id) {
63 hash_id_uint32_t(&report_id->section));
64}
65
66void report_id_delete(struct report_id* self) { free(self->name); }
67
68struct report {
70 int value;
71};
72
73void report_delete(struct report* self) { free(self->description); }
74
75#define K struct report_id
76#define V struct report
77#define EQ report_id_equality
78#define HASH report_id_hash
79#define K_DELETE report_id_delete
80#define V_DELETE report_delete
81#define SELF report_map
83
85 printf("Report Map Example:\n");
86 report_map map = report_map_new();
87
88 struct report_id id1 = {.name = strdup("Report A"), .section = 1};
89 struct report_id id2 = {.name = strdup("Report B"), .section = 2};
90
91 report_map_insert(&map, id1,
92 (struct report){.description = strdup("Description A"), .value = 100});
93 report_map_insert(&map, id2,
94 (struct report){.description = strdup("Description B"), .value = 200});
95
96 assert(strcmp(report_map_read(&map, id1)->description, "Description A") == 0);
97
98 report_map_iter_const iter = report_map_get_iter_const(&map);
99 ITER_ENUMERATE_LOOP(report_map_iter_const, iter, report_map_kv_const, entry, size_t, pos) {
100 printf("Position: %zu Key: %s Section: %u Value: %d\n", pos, entry.key->name,
101 entry.key->section, entry.value->value);
102 }
103
104 struct report entry = report_map_remove(&map, id1);
105 report_delete(&entry);
106
107 report_map_delete(&map);
108}
109
111 char value[4];
112};
113
114bool fixed_string_eq(struct fixed_string const* str1, struct fixed_string const* str2) {
115 return memcmp(str1->value, str2->value, sizeof(str1->value)) == 0;
116}
117
118size_t fixed_string_hash(struct fixed_string const* str) {
119 return hash_murmurhash_string_4(str->value);
120}
121
122#define K struct fixed_string
123#define V uint32_t
124#define EQ fixed_string_eq
125#define HASH fixed_string_hash
126#define SELF fixed_string_map
128
130 printf("Fixed Strings Example:\n");
131 fixed_string_map map = fixed_string_map_new();
132
133 struct fixed_string key1 = {.value = "abc"};
134 struct fixed_string key2 = {.value = "def"};
135 struct fixed_string key3 = {.value = "ghi"};
136
137 fixed_string_map_insert(&map, key1, 123);
138 fixed_string_map_insert(&map, key2, 456);
139 fixed_string_map_insert(&map, key3, 789);
140
141 assert(*fixed_string_map_read(&map, key1) == 123);
142 assert(*fixed_string_map_read(&map, key2) == 456);
143 assert(*fixed_string_map_read(&map, key3) == 789);
144
145 fixed_string_map_iter_const iter = fixed_string_map_get_iter_const(&map);
146 ITER_ENUMERATE_LOOP(fixed_string_map_iter_const, iter, fixed_string_map_kv_const, entry, size_t,
147 pos) {
148 printf("Position: %zu Key: %.3s Value: %u\n", pos, entry.key->value, *entry.value);
149 }
150
151 fixed_string_map_delete(&map);
152}
153
size_t hash_murmurhash_string(const char *str)
Definition hashers.h:58
static size_t hash_combine(size_t seed, size_t h)
Definition hashers.h:83
void id_to_name_example()
Definition hashmap.c:32
void print_map(id_to_name const *map)
Definition hashmap.c:24
void report_delete(struct report *self)
Definition hashmap.c:73
void report_map_example()
Definition hashmap.c:84
bool report_id_equality(struct report_id const *report_1, struct report_id const *report_2)
Definition hashmap.c:57
void fixed_string_example()
Definition hashmap.c:129
bool fixed_string_eq(struct fixed_string const *str1, struct fixed_string const *str2)
Definition hashmap.c:114
void report_id_delete(struct report_id *self)
Definition hashmap.c:66
int main()
Definition hashmap.c:154
size_t report_id_hash(struct report_id const *report_id)
Definition hashmap.c:61
size_t fixed_string_hash(struct fixed_string const *str)
Definition hashmap.c:118
#define ITER_ENUMERATE_LOOP(ITER_TYPE, ITER_NAME, VALUE_TYPE, VALUE_NAME, COUNTER_TYPE, COUNTER_NAME)
Definition iterators.h:7
#define ASSERT(expr,...)
Definition panic.h:15
char value[4]
Definition hashmap.c:111
char * name
Definition hashmap.c:53
uint32_t section
Definition hashmap.c:54
char * description
Definition hashmap.c:69
int value
Definition hashmap.c:70