summaryrefslogtreecommitdiff
path: root/util/htab.c
blob: eafb6637968d4efa933761b99559bb0101114561 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <assert.h>
#include <limits.h>
#include <string.h>

#include "util.h"

#define Initsz 16
#define Seed 2928213749

/* Creates a new empty hash table, using 'hash' as the
 * hash funciton, and 'cmp' to verify that there are no
 * hash collisions. */
Htab *
mkht(ulong (*hash)(void *key), int (*cmp)(void *k1, void *k2))
{
	Htab *ht;

	ht = xalloc(sizeof(Htab));
	ht->nelt = 0;
	ht->ndead = 0;
	ht->sz = Initsz;
	ht->hash = hash;
	ht->cmp = cmp;
	ht->keys = zalloc(Initsz * sizeof(void *));
	ht->vals = zalloc(Initsz * sizeof(void *));
	ht->hashes = zalloc(Initsz * sizeof(void *));
	ht->dead = zalloc(Initsz * sizeof(char));

	return ht;
}

/* Frees a hash table. Passing this function
 * NULL is a no-op. */
void
htfree(Htab *ht)
{
	if (!ht)
		return;
	free(ht->keys);
	free(ht->vals);
	free(ht->hashes);
	free(ht->dead);
	free(ht);
}

/* Offsets the hash so that '0' can be
 * used as a 'no valid value */
static ulong
hash(Htab *ht, void *k)
{
	ulong h;
	h = ht->hash(k);
	if (h == 0)
		return 1;
	else
		return h;
}

/* Resizes the hash table by copying all
 * the old keys into the right slots in a
 * new table. */
static void
grow(Htab *ht, int sz)
{
	void **oldk;
	void **oldv;
	ulong *oldh;
	char *oldd;
	int oldsz;
	int i;

	oldk = ht->keys;
	oldv = ht->vals;
	oldh = ht->hashes;
	oldd = ht->dead;
	oldsz = ht->sz;

	ht->nelt = 0;
	ht->ndead = 0;
	ht->sz = sz;
	ht->keys = zalloc(sz * sizeof(void *));
	ht->vals = zalloc(sz * sizeof(void *));
	ht->hashes = zalloc(sz * sizeof(void *));
	ht->dead = zalloc(sz * sizeof(void *));

	for (i = 0; i < oldsz; i++)
		if (oldh[i] && !oldd[i])
			htput(ht, oldk[i], oldv[i]);

	free(oldh);
	free(oldk);
	free(oldv);
	free(oldd);
}

/* Inserts 'k' into the hash table, possibly
 * killing any previous key that compare
 * as equal. */
int
htput(Htab *ht, void *k, void *v)
{
	int i;
	ulong h;
	int di;

	di = 0;
	h = hash(ht, k);
	i = h & (ht->sz - 1);
	while (ht->hashes[i] && !ht->dead[i]) {
		/* second insertion overwrites first. nb, we shouldn't touch the
		 * keys for dead values */
		if (ht->hashes[i] == h) {
			if (ht->dead[i])
				break;
			else if (ht->cmp(ht->keys[i], k))
				goto conflicted;
		}
		di++;
		i = (h + di) & (ht->sz - 1);
	}
	ht->nelt++;
conflicted
:
	if (ht->dead[i])
		ht->ndead--;
	ht->hashes[i] = h;
	ht->keys[i] = k;
	ht->vals[i] = v;
	ht->dead[i] = 0;

	if (ht->sz < ht->nelt * 2)
		grow(ht, ht->sz * 2);
	if (ht->sz < ht->ndead * 4)
		grow(ht, ht->sz);
	return 1;
}

/* Finds the index that we would insert
 * the key into */
static ssize_t
htidx(Htab *ht, void *k)
{
	ssize_t i;
	ulong h;
	int di;

	di = 0;
	h = hash(ht, k);
	i = h & (ht->sz - 1);
	while (ht->hashes[i] && !ht->dead[i] && ht->hashes[i] != h) {
searchmore
:
		di++;
		i = (h + di) & (ht->sz - 1);
	}
	if (!ht->hashes[i])
		return -1;
	if ((ht->hashes[i] == h && ht->dead[i]) || !ht->cmp(ht->keys[i], k))
		goto searchmore; /* collision */
	return i;
}

/* Looks up a key, returning NULL if
 * the value is not present. Note,
 * if NULL is a valid value, you need
 * to check with hthas() to see if it'
 * not there */
void *
htget(Htab *ht, void *k)
{
	ssize_t i;

	i = htidx(ht, k);
	if (i < 0)
		return NULL;
	else
		return ht->vals[i];
}

void
htdel(Htab *ht, void *k)
{
	ssize_t i;

	i = htidx(ht, k);
	if (i < 0)
		return;
	assert(!ht->dead[i]);
	assert(ht->hashes[i]);
	ht->dead[i] = 1;
	ht->nelt--;
	ht->ndead++;
}

/* Tests for 'k's presence in 'ht' */
int
hthas(Htab *ht, void *k) { return htidx(ht, k) >= 0; }

/* Returns a list of all keys in the hash
 * table, storing the size of the returned
 * array in 'nkeys'. NB: the value returned
 * is allocated on the heap, and it is the
 * job of the caller to free it */
void **
htkeys(Htab *ht, size_t *nkeys)
{
	void **k;
	size_t i, j;

	j = 0;
	k = xalloc(sizeof(void *) * ht->nelt);
	for (i = 0; i < ht->sz; i++)
		if (ht->hashes[i] && !ht->dead[i])
			k[j++] = ht->keys[i];
	*nkeys = ht->nelt;
	return k;
}

ulong
strhash(void *_s)
{
	char *s;

	s = _s;
	if (!s)
		return Seed;
	return murmurhash2(_s, strlen(_s));
}

int
streq(void *a, void *b)
{
	if (a == b)
		return 1;
	if (a == NULL || b == NULL)
		return 0;
	return !strcmp(a, b);
}

ulong
strlithash(void *_s)
{
	Str *s;

	s = _s;
	if (!s)
		return Seed;
	return murmurhash2(s->buf, s->len);
}

int
strliteq(void *_a, void *_b)
{
	Str *a, *b;

	a = _a;
	b = _b;
	if (a == b)
		return 1;
	if (a == NULL || b == NULL)
		return 0;
	if (a->len != b->len)
		return 0;
	return !memcmp(a->buf, b->buf, a->len);
}

ulong
ptrhash(void *key)
{
	return inthash((uintptr_t)key);
}

ulong
inthash(uint64_t key)
{
	return murmurhash2(&key, sizeof key);
}

int
inteq(uint64_t a, uint64_t b)
{
	return a == b;
}

int
ptreq(void *a, void *b)
{
	return a == b;
}

ulong
murmurhash2 (void *ptr, size_t len)
{
	uint32_t m = 0x5bd1e995;
	uint32_t r = 24;
	uint32_t h, k, n;
	uint8_t *p, *end;
	
	h = Seed ^ len;
	n = len & ~0x3ull;
	end = ptr;
	end += n;
	for (p = ptr; p != end; p += 4) {
		k = (p[0] <<  0) |
			(p[1] <<  8) |
			(p[2] << 16) |
			(p[3] << 24);

		k *= m;
		k ^= k >> r;
		k *= m;

		h *= m;
		h ^= k;
	}

	switch (len & 0x3) {
	case 3:	h ^= p[2] << 16;
	case 2:	h ^= p[1] << 8;
	case 1:	h ^= p[0] << 0;
		h *= m;
	}

	h ^= h >> 13;
	h *= m;
	h ^= h >> 15;

	return h;
}