-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrjd_strhash.h
69 lines (53 loc) · 1.66 KB
/
rjd_strhash.h
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
#define RJD_STRHASH_H 1
struct rjd_strhash
{
struct rjd_hash64 hash;
struct rjd_strref* debug_string;
};
void rjd_strhash_global_init(struct rjd_mem_allocator* debug_allocator, uint32_t initial_capacity);
void rjd_strhash_global_destroy(void);
struct rjd_strhash rjd_strhash_init(const char* str);
bool rjd_strhash_isequal(struct rjd_strhash a, struct rjd_strhash b);
int rjd_strhash_compare(const struct rjd_strhash* a, const struct rjd_strhash* b);
#define RJD_STRHASH(string) (rjd_strhash_init(string))
#if RJD_IMPL
struct rjd_strpool* g_strhash_strpool;
void rjd_strhash_global_init(struct rjd_mem_allocator* debug_allocator, uint32_t initial_capacity)
{
if (initial_capacity == 0) {
initial_capacity = 128;
}
g_strhash_strpool = rjd_mem_alloc(struct rjd_strpool, debug_allocator);
*g_strhash_strpool = rjd_strpool_init(debug_allocator, initial_capacity);
}
void rjd_strhash_global_destroy(void)
{
rjd_strpool_free(g_strhash_strpool);
rjd_mem_free(g_strhash_strpool);
g_strhash_strpool = NULL;
}
struct rjd_strhash rjd_strhash_init(const char* str)
{
struct rjd_hash64 hash = rjd_hash64_str(str);
struct rjd_strref* debug_string = NULL;
if (g_strhash_strpool && hash.value != 0)
{
// TODO make threadsafe
debug_string = rjd_strpool_add(g_strhash_strpool, str);
}
struct rjd_strhash strhash = {
.debug_string = debug_string,
.hash = hash,
};
return strhash;
}
bool rjd_strhash_isequal(struct rjd_strhash a, struct rjd_strhash b)
{
return a.hash.value == b.hash.value;
}
int rjd_strhash_compare(const struct rjd_strhash* a, const struct rjd_strhash* b)
{
return a->hash.value < b->hash.value;
}
#endif // RJD_IMPL