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
13#include <derive-c/allocs/std.h>
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
28 id_to_name_kv_const const* entry = NULL;
29 size_t pos = 0;
30 while ((entry = id_to_name_iter_const_next(&iter))) {
31 printf("position: %zu key: %" PRIu32 " string: %s\n", pos, *entry->key, *entry->value);
32 pos++;
33 }
34}
35
37 printf("Id to Name Map Example:\n");
38 id_to_name map = id_to_name_new(stdalloc_get());
39
40 id_to_name_insert(&map, 23, "hello");
41 id_to_name_insert(&map, 10, "bob");
42 id_to_name_insert(&map, 42, "meaning");
43 ASSERT(strcmp(*id_to_name_read(&map, 42), "meaning") == 0);
44
45 print_map(&map);
46
47 char const** entry = id_to_name_write(&map, 23);
48 ASSERT(entry);
49 *entry = "a different string!";
50
51 print_map(&map);
52
53 id_to_name_delete(&map);
54}
55
56struct report_id {
57 char* name;
58 uint32_t section;
59};
60
61bool report_id_equality(struct report_id const* report_1, struct report_id const* report_2) {
62 return strcmp(report_1->name, report_2->name) == 0 && report_1->section == report_2->section;
63}
64
65size_t report_id_hash(struct report_id const* report_id) {
67 hash_id_uint32_t(&report_id->section));
68}
69
70void report_id_delete(struct report_id* self) { free(self->name); }
71
72struct report {
74 int value;
75};
76
77void report_delete(struct report* self) { free(self->description); }
78
79#define K struct report_id
80#define V struct report
81#define EQ report_id_equality
82#define HASH report_id_hash
83#define K_DELETE report_id_delete
84#define V_DELETE report_delete
85#define SELF report_map
87
89 printf("Report Map Example:\n");
90 report_map map = report_map_new(stdalloc_get());
91
92 struct report_id id1 = {.name = strdup("Report A"), .section = 1};
93 struct report_id id2 = {.name = strdup("Report B"), .section = 2};
94
95 report_map_insert(&map, id1,
96 (struct report){.description = strdup("Description A"), .value = 100});
97 report_map_insert(&map, id2,
98 (struct report){.description = strdup("Description B"), .value = 200});
99
100 assert(strcmp(report_map_read(&map, id1)->description, "Description A") == 0);
101
102 {
103 report_map_iter_const iter = report_map_get_iter_const(&map);
104 report_map_kv_const const* entry = NULL;
105 size_t pos = 0;
106 while ((entry = report_map_iter_const_next(&iter))) {
107 printf("Position: %zu Key: %s Section: %u Value: %d\n", pos, entry->key->name,
108 entry->key->section, entry->value->value);
109 pos++;
110 }
111 }
112
113 struct report entry = report_map_remove(&map, id1);
114 report_delete(&entry);
115
116 report_map_delete(&map);
117}
118
120 char value[4];
121};
122
123bool fixed_string_eq(struct fixed_string const* str1, struct fixed_string const* str2) {
124 return memcmp(str1->value, str2->value, sizeof(str1->value)) == 0;
125}
126
127size_t fixed_string_hash(struct fixed_string const* str) {
128 return hash_murmurhash_string_4(str->value);
129}
130
131#define K struct fixed_string
132#define V uint32_t
133#define EQ fixed_string_eq
134#define HASH fixed_string_hash
135#define SELF fixed_string_map
137
139 printf("Fixed Strings Example:\n");
140 fixed_string_map map = fixed_string_map_new(stdalloc_get());
141
142 struct fixed_string key1 = {.value = "abc"};
143 struct fixed_string key2 = {.value = "def"};
144 struct fixed_string key3 = {.value = "ghi"};
145
146 fixed_string_map_insert(&map, key1, 123);
147 fixed_string_map_insert(&map, key2, 456);
148 fixed_string_map_insert(&map, key3, 789);
149
150 assert(*fixed_string_map_read(&map, key1) == 123);
151 assert(*fixed_string_map_read(&map, key2) == 456);
152 assert(*fixed_string_map_read(&map, key3) == 789);
153
154 fixed_string_map_iter_const iter = fixed_string_map_get_iter_const(&map);
155
156 fixed_string_map_kv_const const* entry = NULL;
157 size_t pos = 0;
158 while ((entry = fixed_string_map_iter_const_next(&iter))) {
159 printf("Position: %zu Key: %.3s Value: %u\n", pos, entry->key->value, *entry->value);
160 pos++;
161 }
162
163 fixed_string_map_delete(&map);
164}
165
static void free(SELF *self, void *ptr)
Definition template.h:62
size_t hash_murmurhash_string(const char *str)
Definition hashers.h:59
static size_t hash_combine(size_t seed, size_t h)
Definition hashers.h:85
void id_to_name_example()
Definition hashmap.c:36
void print_map(id_to_name const *map)
Definition hashmap.c:24
void report_delete(struct report *self)
Definition hashmap.c:77
void report_map_example()
Definition hashmap.c:88
bool report_id_equality(struct report_id const *report_1, struct report_id const *report_2)
Definition hashmap.c:61
void fixed_string_example()
Definition hashmap.c:138
bool fixed_string_eq(struct fixed_string const *str1, struct fixed_string const *str2)
Definition hashmap.c:123
void report_id_delete(struct report_id *self)
Definition hashmap.c:70
int main()
Definition hashmap.c:166
size_t report_id_hash(struct report_id const *report_id)
Definition hashmap.c:65
size_t fixed_string_hash(struct fixed_string const *str)
Definition hashmap.c:127
#define ASSERT(expr,...)
Definition panic.h:16
char value[4]
Definition hashmap.c:120
char * name
Definition hashmap.c:57
uint32_t section
Definition hashmap.c:58
char * description
Definition hashmap.c:73
int value
Definition hashmap.c:74