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/derives/std.h>
#include <derive-c/macros/iterators.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 79 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 80 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 114 of file hashmap.c.

114 {
115 return memcmp(str1->value, str2->value, sizeof(str1->value)) == 0;
116}

◆ fixed_string_example()

void fixed_string_example ( )
Examples
structures/hashmap.c.

Definition at line 129 of file hashmap.c.

129 {
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}
#define ITER_ENUMERATE_LOOP(ITER_TYPE, ITER_NAME, VALUE_TYPE, VALUE_NAME, COUNTER_TYPE, COUNTER_NAME)
Definition iterators.h:7
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 118 of file hashmap.c.

118 {
119 return hash_murmurhash_string_4(str->value);
120}

◆ id_to_name_example()

void id_to_name_example ( )
Examples
structures/hashmap.c.

Definition at line 32 of file hashmap.c.

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

◆ main()

int main ( )

Definition at line 154 of file hashmap.c.

154 {
158}
void id_to_name_example()
Definition hashmap.c:32
void report_map_example()
Definition hashmap.c:84
void fixed_string_example()
Definition hashmap.c:129
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 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}
Here is the caller graph for this function:

◆ report_delete()

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

Definition at line 73 of file hashmap.c.

73{ free(self->description); }
char * description
Definition hashmap.c:69
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 66 of file hashmap.c.

66{ free(self->name); }
char * name
Definition hashmap.c:53

◆ 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 57 of file hashmap.c.

57 {
58 return strcmp(report_1->name, report_2->name) == 0 && report_1->section == report_2->section;
59}

◆ report_id_hash()

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

Definition at line 61 of file hashmap.c.

61 {
63 hash_id_uint32_t(&report_id->section));
64}
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
uint32_t section
Definition hashmap.c:54
Here is the call graph for this function:

◆ report_map_example()

void report_map_example ( )
Examples
structures/hashmap.c.

Definition at line 84 of file hashmap.c.

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