-
Notifications
You must be signed in to change notification settings - Fork 2
/
hashmap.c
44 lines (39 loc) · 813 Bytes
/
hashmap.c
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 HashPointer
#define HashPointer(ptr) (hashStr((char*)(ptr), sizeof(*(ptr))))
#endif
u64
hashStr(char* str, size_t size) {
u64 hash = 0;
u64 g = 0;
for (size_t i = 0; i < size; ++i) {
hash = (hash << 4) + str[i];
g = hash & 0xf000000000000000;
if (g) {
hash ^= g >> 24;
hash &= ~g;
}
}
return hash;
}
// Utility functions.
u64
hashStrPtr (char** str) {
size_t len = strlen(*str);
return hashStr(*str, len);
}
b32
compareStringKey (char* a, char* b) {
b32 result = false;
if (a && b) {
size_t sa = strlen(a);
size_t sb = strlen(b);
if (sa == sb) {
size_t i;
for (i = 0; i < sa && a[i] == b[i]; ++i) {}
if (sa == i) {
result = true;
}
}
}
return result;
}