Derive-C
Loading...
Searching...
No Matches
hashmap.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/allocs/std.h>
#include <derive-c/derives/std.h>
#include <derive-c/structures/hashmap/hashers.h>
#include <derive-c/structures/hashmap/template.h>
Include dependency graph for hashmap.c:

Go to the source code of this file.

Classes

struct  report_id
struct  report
struct  fixed_string

Macros

#define K   uint32_t
#define V   char const*
#define EQ   uint32_t_eq
#define HASH   hash_id_uint32_t
#define SELF   id_to_name
#define K   struct report_id
#define V   struct report
#define EQ   report_id_equality
#define HASH   report_id_hash
#define K_DELETE   report_id_delete
#define V_DELETE   report_delete
#define SELF   report_map
#define K   struct fixed_string
#define V   uint32_t
#define EQ   fixed_string_eq
#define HASH   fixed_string_hash
#define SELF   fixed_string_map

Functions

void print_map (id_to_name const *map)
void id_to_name_example ()
bool report_id_equality (struct report_id const *report_1, struct report_id const *report_2)
size_t report_id_hash (struct report_id const *report_id)
void report_id_delete (struct report_id *self)
void report_delete (struct report *self)
void report_map_example ()
bool fixed_string_eq (struct fixed_string const *str1, struct fixed_string const *str2)
size_t fixed_string_hash (struct fixed_string const *str)
void fixed_string_example ()
int main ()

Macro Definition Documentation

◆ EQ [1/3]

#define EQ   uint32_t_eq

Definition at line 19 of file hashmap.c.

◆ EQ [2/3]

#define EQ   report_id_equality

Definition at line 19 of file hashmap.c.

◆ EQ [3/3]

#define EQ   fixed_string_eq

Definition at line 19 of file hashmap.c.

◆ HASH [1/3]

#define HASH   hash_id_uint32_t

Definition at line 20 of file hashmap.c.

◆ HASH [2/3]

#define HASH   report_id_hash

Definition at line 20 of file hashmap.c.

◆ HASH [3/3]

#define HASH   fixed_string_hash

Definition at line 20 of file hashmap.c.

◆ K [1/3]

#define K   uint32_t

Definition at line 17 of file hashmap.c.

◆ K [2/3]

#define K   struct report_id

Definition at line 17 of file hashmap.c.

◆ K [3/3]

#define K   struct fixed_string

Definition at line 17 of file hashmap.c.

◆ K_DELETE

#define K_DELETE   report_id_delete

Definition at line 83 of file hashmap.c.

◆ SELF [1/3]

#define SELF   id_to_name

Definition at line 21 of file hashmap.c.

◆ SELF [2/3]

#define SELF   report_map

Definition at line 21 of file hashmap.c.

◆ SELF [3/3]

#define SELF   fixed_string_map

Definition at line 21 of file hashmap.c.

◆ V [1/3]

#define V   char const*

Definition at line 18 of file hashmap.c.

◆ V [2/3]

#define V   struct report

Definition at line 18 of file hashmap.c.

◆ V [3/3]

#define V   uint32_t

Definition at line 18 of file hashmap.c.

◆ V_DELETE

#define V_DELETE   report_delete

Definition at line 84 of file hashmap.c.

Function Documentation

◆ fixed_string_eq()

bool fixed_string_eq ( struct fixed_string const * str1,
struct fixed_string const * str2 )
Examples
structures/hashmap.c.

Definition at line 123 of file hashmap.c.

123 {
124 return memcmp(str1->value, str2->value, sizeof(str1->value)) == 0;
125}

◆ fixed_string_example()

void fixed_string_example ( )
Examples
structures/hashmap.c.

Definition at line 138 of file hashmap.c.

138 {
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}
Here is the caller graph for this function:

◆ fixed_string_hash()

size_t fixed_string_hash ( struct fixed_string const * str)
Examples
structures/hashmap.c.

Definition at line 127 of file hashmap.c.

127 {
128 return hash_murmurhash_string_4(str->value);
129}

◆ id_to_name_example()

void id_to_name_example ( )
Examples
structures/hashmap.c.

Definition at line 36 of file hashmap.c.

36 {
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}
void print_map(id_to_name const *map)
Definition hashmap.c:24
#define ASSERT(expr,...)
Definition panic.h:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 166 of file hashmap.c.

166 {
170}
void id_to_name_example()
Definition hashmap.c:36
void report_map_example()
Definition hashmap.c:88
void fixed_string_example()
Definition hashmap.c:138
Here is the call graph for this function:

◆ print_map()

void print_map ( id_to_name const * map)
Examples
structures/hashmap.c.

Definition at line 24 of file hashmap.c.

24 {
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}
Here is the caller graph for this function:

◆ report_delete()

void report_delete ( struct report * self)
Examples
structures/hashmap.c.

Definition at line 77 of file hashmap.c.

77{ free(self->description); }
static void free(SELF *self, void *ptr)
Definition template.h:62
char * description
Definition hashmap.c:73
Here is the call graph for this function:
Here is the caller graph for this function:

◆ report_id_delete()

void report_id_delete ( struct report_id * self)
Examples
structures/hashmap.c.

Definition at line 70 of file hashmap.c.

70{ free(self->name); }
char * name
Definition hashmap.c:57
Here is the call graph for this function:

◆ report_id_equality()

bool report_id_equality ( struct report_id const * report_1,
struct report_id const * report_2 )
Examples
structures/hashmap.c.

Definition at line 61 of file hashmap.c.

61 {
62 return strcmp(report_1->name, report_2->name) == 0 && report_1->section == report_2->section;
63}

◆ report_id_hash()

size_t report_id_hash ( struct report_id const * report_id)
Examples
structures/hashmap.c.

Definition at line 65 of file hashmap.c.

65 {
67 hash_id_uint32_t(&report_id->section));
68}
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
uint32_t section
Definition hashmap.c:58
Here is the call graph for this function:

◆ report_map_example()

void report_map_example ( )
Examples
structures/hashmap.c.

Definition at line 88 of file hashmap.c.

88 {
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}
void report_delete(struct report *self)
Definition hashmap.c:77
Here is the call graph for this function:
Here is the caller graph for this function: