1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#ifndef AMALGAM_HASH_MAP_H
#define AMALGAM_HASH_MAP_H
#include "defs.h"
#include "misc.h"
#include "types.h"
#include "buffer.h"
#include "buffer_view.h"
typedef struct HashMap HashMap;
typedef int(*HashMapCompare)(const void *a, const void *b);
typedef usize(*HashMapHash)(const u8 *data, usize size);
/* TODO: Optimize small hash maps by using the members of the struct instead of allocating on heap */
struct HashMap {
ArenaAllocator *allocator; /* borrowed */
Buffer/*HashMapBucket*/ buckets;
usize value_type_size;
usize num_elements;
HashMapCompare compare_func;
HashMapHash hash_func;
};
#define HashMapType(key_type, value_type) __attribute__((annotate(#key_type", "#value_type))) HashMap
CHECK_RESULT int hash_map_init(HashMap *self, ArenaAllocator *allocator, usize value_type_size, HashMapCompare key_compare_func, HashMapHash key_hash_func);
/*
Not thread-safe.
Expected @value size to be @self->value_type_size.
*/
CHECK_RESULT int hash_map_insert(HashMap *self, BufferView key, void *value);
/*
Thread-safe unless @hash_map_insert is used in another thread at the same time.
Expected @value size to be @self->value_type_size.
If @value is NULL, then the value is not copied and the functions works the same as @hash_map_contains
*/
CHECK_RESULT bool hash_map_get(HashMap *self, BufferView key, void *value);
CHECK_RESULT bool hash_map_get_ref(HashMap *self, BufferView key, void **value);
CHECK_RESULT bool hash_map_contains(HashMap *self, BufferView key);
int hash_map_compare_string(const void *a, const void *b);
#endif
|