-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhashmap.inl
231 lines (205 loc) · 5.79 KB
/
hashmap.inl
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Generic hash map.
*
* Usage: Define HashmapKey, HashmapValue and HashmapPrefix. Then include
* hashmap.inl to define a hash map with the corresponding key and value types.
* You can include hashmap.inl multiple times to define different Hash maps.
*
* If HashmapName is not defined, the type name will be [prefix]Hashmap
*
* By default, the keys are compared by value with ==. Define the KeyCompareFunc
* preprocessor macro to implement your own key comparison. There is also the
* HashFunction define to specify a hash function. See Example 2.
*
* See the rest of the defines at the beginning of hashmap.inl to see what
* knobs can be tweaked.
*
* This is designed to be used in a single-translation-unit build. It will need
* modification for this technique to work on a more traditional build style.
*
* The generated hashmap uses the Arena structure defined in memory.c
*
*
* Example 1: Hashmap mapping int to bool
* This will define a type called test_Hashmap.
* --------------------------------------
*
* `
* ` #define HashmapName MyMap
* ` #define HashmapPrefix test
* ` #define HashmapKey int
* ` #define HashmapVal bool
* ` #include "hashmap.inl"
* `
* ` // Now test_Hashmap is defined. Insert elements with test_hmInsert and
* ` // get them with test_hmGet
* ` MyMap hm = {0};
* ` testInsert(&hm, 42, TRUE);
* ` bool v = testGet(&hm, 42); // TRUE
* `
*
* Example 2: Hashmap mapping char* to int. This will define a hash map named
* str_Hashmap which maps char* keys to int values.
* --------------------------------------
*
* `
* ` b32 myKeyCompare(char* a, char* b) {
* ` b32 result = false;
* ` 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;
* ` }
* ` u64 myHash (char** str) {
* ` size_t len = strlen(*str);
* ` return hashStr(*str, len);
* ` }
* ` #define HashmapPrefix str
* ` #define HashmapKey char*
* ` #define HashmapValue int
* ` #define HashFunction myHash
* ` #define KeyCompareFunc myKeyCompare
* ` #include "hashmap.inl"
* `
* ` int example_func() {
* ` str_Hashmap hm = {0};
* ` hm.arena = my_arena; // You can specify the arena to control how the allocation is done.
* ` strInsert(&hm, "hello world", 42);
* ` }
*
*
*
*
*
*
**/
#ifndef HashmapInitSize
#define HashmapInitSize 1024
#endif
#ifndef HashmapPrefix
#define HashmapPrefix HashmapKey ## _ ## HashmapValue
#endif
#ifndef HashmapKey
#warning "HashmapKey not defined. Default to int."
#define HashmapKey int
#endif
#ifndef HashmapValue
#warning "HashmapValue not defined. Default to int."
#define HashmapValue int
#endif
// Default hash functions.
u64 hashStr(char* str, size_t size);
u64 hashStrPtr (char** str);
b32 compareStringKey (char* a, char* b);
#ifndef HashPointer
#define HashPointer(ptr) (hashStr((char*)(ptr), sizeof(*(ptr))))
#endif
#ifndef HashFunction
#define HashFunction HashPointer
#endif
#if !defined(HashmapName)
#define HashmapName Generic(Hashmap)
#endif
// #if !defined(HashmapIterName)
#define HashmapIterName(postfix) HashmapIterNameEx(HashmapName, postfix)
#define HashmapIterNameEx(hmName, postfix) GenericExEx(hmName, postfix)
#define HashmapIterNameExEx(hmName, postfix) hmName ## postfix
//#undef HashmapIterNameEx
// #endif
#if !defined(HashmapError)
#define HashmapError(msg) Assert(!(msg))
#endif
#define Generic(name) GenericEx(HashmapPrefix, name)
#define GenericEx(pref, name) GenericExEx(pref, name)
#define GenericExEx(pref, name) pref ## name
typedef struct Generic(HashmapKeyVal_s) Generic(HashmapKeyVal);
struct Generic(HashmapKeyVal_s) {
HashmapKey key;
HashmapValue val;
Generic(HashmapKeyVal)* next;
};
typedef struct {
Arena arena;
Generic(HashmapKeyVal) *keyvals;
u32 n_keyvals;
} HashmapName;
typedef struct {
int foo;
} HashmapIterName(KeyIter);
void
Generic(__maybeInit) (HashmapName* hm) {
if (!hm->keyvals) {
hm->n_keyvals = HashmapInitSize;
hm->keyvals = AllocArray(&hm->arena, Generic(HashmapKeyVal), hm->n_keyvals);
}
}
HashmapValue*
Generic(Insert) (HashmapName* hm, HashmapKey key, HashmapValue val) {
Generic(__maybeInit(hm));
u64 hash = HashFunction(&key);
Generic(HashmapKeyVal) *kv = &hm->keyvals[hash % hm->n_keyvals];
while (true) {
// Using the first element as a sentinel.
#if defined(KeyCompareFunc)
if (KeyCompareFunc(key, kv->key)) {
#else
if (key == kv->key) {
#endif
HashmapError("Duplicate key!");
}
if (kv->next) {
kv = kv->next;
}
else {
break;
}
} while(kv->next);
kv->next = AllocType(&hm->arena, Generic(HashmapKeyVal));
kv = kv->next;
kv->key = key;
kv->val = val;
return &kv->val;
}
HashmapValue*
Generic(Get) (HashmapName* hm, HashmapKey key) {
Generic(__maybeInit(hm));
u64 hash = HashFunction(&key);
Generic(HashmapKeyVal)* kv = hm->keyvals[hash % hm->n_keyvals].next;
HashmapValue* result = NULL;
while (kv) {
#if defined(KeyCompareFunc)
if (KeyCompareFunc(key, kv->key)) {
#else
if (key == kv->key) {
#endif
result = &kv->val;
break;
}
kv = kv->next;
}
return result;
}
void
Generic(KeyIterBegin) (HashmapName*hm, HashmapIterName(KeyIter)* iter) {
}
#ifdef KeyCompareFunc
#undef KeyCompareFunc
#endif
#undef Generic
#undef GenericEx
#undef GenericExEx
#undef HashmapName
#undef HashmapKey
#undef HashmapPrefix
#undef HashmapInitSize
#undef HashmapValue
#undef HashFunction
#undef HashmapIterName
#undef HashmapIterNameEx
#undef HashmapIterNameExEx