summaryrefslogtreecommitdiff
path: root/hash.c
blob: 2c2e9827d4af1d3dc02f7053487b78bfe12df94c (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
334
335
336
337
338
339
340
341
342
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */
/*  Libnul - Another Utility Library
 *  Copyright (C) 2008  Søren Sandmann
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA  02111-1307, USA.
 */
#include "libnul.h"

typedef struct hash_entry_t hash_entry_t;

struct hash_entry_t
{
    nul_ptr_t	key;
    nul_ptr_t	value;
};

/* Expand the table when more than 1/HIGH_OCCUPATION is in use.
 * Shrink the table when less than 1/LOW_OCCUPATION is in use
 * When resizing the table, size it such that 1/DEFAULT_OCCUPATION is in use
 */
#define HIGH_WATER(hash)	((((hash)->mask + 1) * 5) / 8)
#define LOW_WATER(hash)		(((hash)->mask + 1) / 8)
#define DEFAULT_OCCUPATION	4
#define MIN_SIZE		128

struct nul_hash_t
{
    hash_entry_t *		entries;

    nul_hash_func_t		hash_func;
    nul_hash_equal_func_t	equal_func;

    nul_free_func_t		value_free_func;
    nul_free_func_t		key_free_func;

    size_t			n_dead;
    size_t			n_live;
    size_t			mask;

    nul_ptr_t free_marker;	/* entry->value has this value when it's not in use */
    nul_ptr_t dead_marker;	/* entry->value has this value when it's a tombstone */
};

static nul_ptr_t
random_pointer (void)
{
    size_t ptr = 0;
    int i;

    for (i = 0; i < sizeof (ptr) * 8; ++i)
    {
	uint32_t bit = nul_random_boolean();

	ptr |= (bit << i);
    }

    return (nul_ptr_t)ptr;
}

static inline void
kill_entry (nul_hash_t *hash, hash_entry_t *entry)
{
    if (hash->key_free_func)
	hash->key_free_func (entry->key);

    if (hash->value_free_func)
	hash->value_free_func (entry->value);

    entry->value = hash->dead_marker;
    hash->n_live--;
    hash->n_dead++;
}

/* FIXME: consider double hashing instead of this
 */
static inline size_t
next_probe (size_t index, size_t mask)
{
    return (index + 1) & mask;
}

static inline void
insert_internal (nul_hash_t *hash, nul_ptr_t key, nul_ptr_t value)
{
    size_t index = hash->hash_func (key) & hash->mask;
    hash_entry_t *entry;

    entry = &(hash->entries[index]);

    while (entry->value != hash->free_marker && entry->value != hash->dead_marker)
    {
	if (hash->equal_func (entry->key, key))
	{
	    kill_entry (hash, entry);
	    break;
	}

	index = next_probe (index, hash->mask);

	entry = &(hash->entries[index]);
    }

    hash->n_live++;
    entry->key = key;
    entry->value = value;
}

static void
rehash (nul_hash_t *hash)
{
    hash_entry_t *old_entries;
    size_t new_size;
    size_t t;

    new_size = 1;
    while (new_size < MAX (hash->n_live * DEFAULT_OCCUPATION, MIN_SIZE))
	new_size *= 2;

    old_entries = hash->entries;

    hash->entries = nul_array_new (hash_entry_t);
    hash->entries = nul_array_set_size (hash->entries, new_size);
    hash->mask = new_size - 1;
    hash->n_live = 0;
    hash->n_dead = 0;

    for (t = 0; t < new_size; ++t)
    {
	hash_entry_t *entry = &(hash->entries[t]);

	entry->key = NULL;
	entry->value = hash->free_marker;
    }

    if (old_entries)
    {
	for (t = 0; t < nul_array_len (old_entries); ++t)
	{
	    hash_entry_t *entry = &(old_entries[t]);

	    if (entry->value != hash->free_marker && entry->value != hash->dead_marker)
		insert_internal (hash, entry->key, entry->value);
	}

	nul_array_free (old_entries);
    }
}

static void
regenerate_markers (nul_hash_t *hash, nul_ptr_t value)
{
    nul_ptr_t new_free_marker, old_free_marker;
    nul_ptr_t new_dead_marker, old_dead_marker;
    int i;

    old_dead_marker = hash->dead_marker;
    old_free_marker = hash->free_marker;

retry:
    new_free_marker = random_pointer();
    new_dead_marker = random_pointer();

    /* Check for collisions */
    if (new_free_marker == value			||
	new_free_marker == old_free_marker		||
	new_free_marker == old_dead_marker		||
	new_dead_marker == value			||
	new_dead_marker == old_free_marker		||
	new_dead_marker == old_dead_marker)
    {
	goto retry;
    }

    for (i = 0; i < nul_array_len (hash->entries); ++i)
    {
	hash_entry_t *entry = &(hash->entries[i]);

	if (entry->value == new_free_marker	||
	    entry->value == new_dead_marker)
	{
	    goto retry;
	}
    }

    /* Update entries */
    for (i = 0; i < nul_array_len (hash->entries); ++i)
    {
	hash_entry_t *entry = &(hash->entries[i]);

	if (entry->value == old_free_marker)
	    entry->value = new_free_marker;

	if (entry->value == old_dead_marker)
	    entry->value = new_dead_marker;
    }

    hash->dead_marker = new_dead_marker;
    hash->free_marker = new_free_marker;
}

nul_hash_t *
nul_hash_new (nul_hash_func_t        hash_func,
	      nul_hash_equal_func_t  equal_func,
	      nul_free_func_t        key_free_func,
	      nul_free_func_t        value_free_func)
{
    nul_hash_t *hash = g_new (nul_hash_t, 1);

    hash->hash_func = hash_func;
    hash->equal_func = equal_func;
    hash->key_free_func = key_free_func;
    hash->value_free_func = value_free_func;
    hash->free_marker = random_pointer();
    hash->dead_marker = random_pointer();

    hash->entries = NULL;
    hash->n_live = 0;
    hash->n_dead = 0;
    hash->mask = 0;

    rehash (hash);

    return hash;
}

static inline nul_bool_t
need_rehash (nul_hash_t *hash)
{
    if (hash->n_dead + hash->n_live > HIGH_WATER (hash))
	return TRUE;

    if (hash->mask + 1 == MIN_SIZE)
	return FALSE;

    if (hash->n_live < LOW_WATER (hash))
	return TRUE;

    return FALSE;
}

void
nul_hash_insert (nul_hash_t            *hash,
		 nul_ptr_t              key,
		 nul_ptr_t              value)
{
    if (G_UNLIKELY (value == hash->free_marker || value == hash->dead_marker))
	regenerate_markers (hash, value);

    insert_internal (hash, key, value);

    if (need_rehash (hash))
	rehash (hash);
}

static inline hash_entry_t *
find_entry (nul_hash_t *hash, nul_ptr_t key)
{
    size_t index = hash->hash_func (key) & hash->mask;
    hash_entry_t *entry;

    entry = &(hash->entries[index]);

    while (entry->value != hash->free_marker)
    {
	if (entry->value != hash->dead_marker && hash->equal_func (entry->key, key))
	    return entry;

	index = next_probe (index, hash->mask);
	entry = &(hash->entries[index]);
    }

    return NULL;
}

nul_ptr_t
nul_hash_lookup  (nul_hash_t            *hash,
		  nul_ptr_t              key)
{
    hash_entry_t *entry = find_entry (hash, key);

    if (entry)
	return entry->value;

    return NULL;
}

nul_bool_t
nul_hash_remove (nul_hash_t *hash,
		 nul_ptr_t key)
{
    hash_entry_t *entry = find_entry (hash, key);

    if (entry)
    {
	kill_entry (hash, entry);

	if (need_rehash (hash))
	    rehash (hash);

	return TRUE;
    }

    return FALSE;
}

nul_bool_t
nul_hash_has_key (nul_hash_t            *hash,
		  nul_ptr_t              key)
{
    return !!find_entry (hash, key);
}

void
nul_hash_free (nul_hash_t *hash)
{
    size_t i;

    for (i = 0; i < nul_array_len (hash->entries); ++i)
    {
	hash_entry_t *entry = &(hash->entries[i]);

	if (entry->value != hash->free_marker && entry->value != hash->dead_marker)
	    kill_entry (hash, entry);
    }

    nul_array_free (hash->entries);

    g_free (hash);
}