hashset.c 16.6 KB
Newer Older
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Matthias Braun's avatar
Matthias Braun committed
20
21
/**
 * @file
22
 * @brief   Generic hashset implementation
Matthias Braun's avatar
Matthias Braun committed
23
24
 * @author  Matthias Braun, inspiration from densehash from google sparsehash
 *          package
25
 * @date    17.03.2007
Matthias Braun's avatar
Matthias Braun committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 *
 *
 * You have to specialize this file by defining:
 *
 * <ul>
 *  <li><b>HashSet</b>         The name of the hashset type</li>
 *  <li><b>HashSetIterator</b> The name of the hashset iterator type</li>
 *  <li><b>ValueType</b>       The type of the stored data values</li>
 *  <li><b>NullValue</b>       A special value representing no values</li>
 *  <li><b>DeletedValue</b>    A special value representing deleted entries</li>
 *  <li><b>Hash(hashset,key)</b> calculates the hash value for a given key</li>
 * </ul>
 *
 * Note that by default it is assumed that the data values themselfes are used
 * as keys. However you can change that with additional defines:
 *
 * <ul>
 *  <li><b>KeyType</b>         The type of the keys identifying data values.
 *                             Defining this implies, that a data value contains
 *                             more than just the key.</li>
 *  <li><b>GetKey(value)</b>   Extracts the key from a data value</li>
 *  <li><b>KeysEqual(hashset,key1,key2)</b>  Tests wether 2 keys are equal</li>
 *  <li><b>DO_REHASH</b>       Instead of storing the hash-values, recalculate
Christoph Mallon's avatar
Christoph Mallon committed
49
 *                             them on demand from the datavalues. (useful if
Matthias Braun's avatar
Matthias Braun committed
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
 *                             calculating the hash-values takes less time than
 *                             a memory access)</li>
 * </ul>
 *
 * You can further fine tune your hashset by defining the following:
 *
 * <ul>
 *  <li><b>JUMP(num_probes)</b> The probing method</li>
 *  <li><b>Alloc(count)</b>     Allocates count hashset entries (NOT bytes)</li>
 *  <li><b>Free(ptr)</b>        Frees a block of memory allocated by Alloc</li>
 *  <li><b>SetRangeEmpty(ptr,count)</b> Efficiently sets a range of elements to
 *                                      the Null value</li>
 *  <li><b>ADDITIONAL_DATA<b>   Additional fields appended to the hashset struct</li>
 * </ul>
 */
#ifdef HashSet

#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "bitfiddle.h"
#include "util.h"

/* quadratic probing */
#ifndef JUMP
#define JUMP(num_probes)      (num_probes)
Christian Würdig's avatar
Christian Würdig committed
77
#endif /* JUMP */
Matthias Braun's avatar
Matthias Braun committed
78
79
80

#ifndef Hash
#define ID_HASH
Michael Beck's avatar
Michael Beck committed
81
#define Hash(self,key)        ((unsigned)(((char *)key) - (char *)0))
Christian Würdig's avatar
Christian Würdig committed
82
#endif /* Hash */
Matthias Braun's avatar
Matthias Braun committed
83
84
85

#ifdef DO_REHASH
#define HashSetEntry                   ValueType
86
#define EntrySetHash(entry,new_hash)   (void)0
Matthias Braun's avatar
Matthias Braun committed
87
#define EntryGetHash(self,entry)       Hash(self, GetKey(entry))
Matthias Braun's avatar
Matthias Braun committed
88
#define EntryGetValue(entry)           (entry)
Christian Würdig's avatar
Christian Würdig committed
89
#else /* ! DO_REHASH */
90
#define EntryGetHash(self,entry)       (entry).hash
Matthias Braun's avatar
Matthias Braun committed
91
92
#define EntrySetHash(entry,new_hash)   (entry).hash = (new_hash)
#define EntryGetValue(entry)           (entry).data
Christian Würdig's avatar
Christian Würdig committed
93
#endif /* DO_REHASH */
Matthias Braun's avatar
Matthias Braun committed
94
95
96

#ifndef Alloc
#include "xmalloc.h"
97
#define Alloc(size) XMALLOCN(HashSetEntry, (size))
Matthias Braun's avatar
Matthias Braun committed
98
#define Free(ptr)      free(ptr)
Christian Würdig's avatar
Christian Würdig committed
99
#endif /* Alloc */
Matthias Braun's avatar
Matthias Braun committed
100
101

#ifdef ID_HASH
Matthias Braun's avatar
Matthias Braun committed
102
103
104
#define InsertReturnValue                 int
#define GetInsertReturnValue(entry,found) (found)
#define NullReturnValue                   0
Christian Würdig's avatar
Christian Würdig committed
105
#else /* ! ID_HASH */
Matthias Braun's avatar
Matthias Braun committed
106
107
108
109
110
111
112
113
114
#ifdef SCALAR_RETURN
#define InsertReturnValue                 ValueType
#define GetInsertReturnValue(entry,found) EntryGetValue(entry)
#define NullReturnValue                   NullValue
#else
#define InsertReturnValue                 ValueType*
#define GetInsertReturnValue(entry,found) & EntryGetValue(entry)
#define NullReturnValue                   & NullValue
#endif
Christian Würdig's avatar
Christian Würdig committed
115
#endif /* ID_HASH */
Matthias Braun's avatar
Matthias Braun committed
116
117
118
119

#ifndef KeyType
#define KeyType                  ValueType
#define GetKey(value)            (value)
120
#define InitData(self,value,key) (value) = (key)
Christian Würdig's avatar
Christian Würdig committed
121
#endif /* KeyType */
Matthias Braun's avatar
Matthias Braun committed
122
123
124

#ifndef ConstKeyType
#define ConstKeyType             const KeyType
Christian Würdig's avatar
Christian Würdig committed
125
#endif /* ConstKeyType */
Matthias Braun's avatar
Matthias Braun committed
126
127
128

#ifndef EntrySetEmpty
#define EntrySetEmpty(entry)    EntryGetValue(entry) = NullValue
Christian Würdig's avatar
Christian Würdig committed
129
#endif /* EntrySetEmpty */
Matthias Braun's avatar
Matthias Braun committed
130
131
#ifndef EntrySetDeleted
#define EntrySetDeleted(entry)  EntryGetValue(entry) = DeletedValue
Christian Würdig's avatar
Christian Würdig committed
132
#endif /* EntrySetDeleted */
Matthias Braun's avatar
Matthias Braun committed
133
134
#ifndef EntryIsEmpty
#define EntryIsEmpty(entry)     (EntryGetValue(entry) == NullValue)
Christian Würdig's avatar
Christian Würdig committed
135
#endif /* EntryIsEmpty */
Matthias Braun's avatar
Matthias Braun committed
136
137
#ifndef EntryIsDeleted
#define EntryIsDeleted(entry)   (EntryGetValue(entry) == DeletedValue)
Christian Würdig's avatar
Christian Würdig committed
138
#endif /* EntryIsDeleted */
Matthias Braun's avatar
Matthias Braun committed
139
140
141
142
143
144
#ifndef SetRangeEmpty
#define SetRangeEmpty(ptr,size)                \
{                                              \
	size_t _i;                                 \
	size_t _size = (size);                     \
	HashSetEntry *entries = (ptr);             \
145
	for (_i = 0; _i < _size; ++_i) {            \
Matthias Braun's avatar
Matthias Braun committed
146
147
148
149
		HashSetEntry *entry = & entries[_i];   \
		EntrySetEmpty(*entry);                 \
	}                                          \
}
Christian Würdig's avatar
Christian Würdig committed
150
#endif /* SetRangeEmpty */
Matthias Braun's avatar
Matthias Braun committed
151
152
153

#ifndef HT_OCCUPANCY_FLT
/** how full before we double size */
154
#define HT_OCCUPANCY_FLT(x) ((x)/2)
Christian Würdig's avatar
Christian Würdig committed
155
#endif /* HT_OCCUPANCY_FLT */
156
157
158
#ifndef HT_1_DIV_OCCUPANCY_FLT
#define HT_1_DIV_OCCUPANCY_FLT 2
#endif
Matthias Braun's avatar
Matthias Braun committed
159
160
161

#ifndef HT_EMPTY_FLT
/** how empty before we half size */
162
#define HT_EMPTY_FLT(x)     ((x)/5)
Christian Würdig's avatar
Christian Würdig committed
163
#endif /* HT_EMPTY_FLT */
Matthias Braun's avatar
Matthias Braun committed
164
165
166
167

#ifndef HT_MIN_BUCKETS
/** default smallest bucket size */
#define HT_MIN_BUCKETS    32
Christian Würdig's avatar
Christian Würdig committed
168
#endif /* HT_MIN_BUCKETS */
Matthias Braun's avatar
Matthias Braun committed
169
170
171

#define ILLEGAL_POS       ((size_t)-1)

Christian Würdig's avatar
Christian Würdig committed
172
/* check that all needed functions are defined */
Matthias Braun's avatar
Matthias Braun committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef hashset_init
#error You have to redefine hashset_init
#endif
#ifndef hashset_init_size
#error You have to redefine hashset_init_size
#endif
#ifndef hashset_destroy
#error You have to redefine hashset_destroy
#endif
#ifndef hashset_insert
#error You have to redefine hashset_insert
#endif
#ifndef hashset_remove
#error You have to redefine hashset_remove
#endif
#ifndef hashset_find
#error You have to redefine hashset_find
#endif
#ifndef hashset_size
#error You have to redefine hashset_size
#endif
Michael Beck's avatar
Michael Beck committed
194
195

#ifndef NO_ITERATOR
Matthias Braun's avatar
Matthias Braun committed
196
197
198
199
200
201
202
203
204
#ifndef hashset_iterator_init
#error You have to redefine hashset_iterator_init
#endif
#ifndef hashset_iterator_next
#error You have to redefine hashset_iterator_next
#endif
#ifndef hashset_remove_iterator
#error You have to redefine hashset_remove_iterator
#endif
Matthias Braun's avatar
Matthias Braun committed
205
#endif
Matthias Braun's avatar
Matthias Braun committed
206
207
208
209

/**
 * Returns the number of elements in the hashset
 */
210
size_t hashset_size(const HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
211
{
212
	return self->num_elements - self->num_deleted;
Matthias Braun's avatar
Matthias Braun committed
213
214
215
216
217
218
219
220
}

/**
 * Inserts an element into a hashset without growing the set (you have to make
 * sure there's enough room for that.
 * @note also see comments for hashset_insert()
 * @internal
 */
221
static inline InsertReturnValue insert_nogrow(HashSet *self, KeyType key)
Matthias Braun's avatar
Matthias Braun committed
222
{
Matthias Braun's avatar
Matthias Braun committed
223
224
225
226
227
228
	size_t   num_probes  = 0;
	size_t   num_buckets = self->num_buckets;
	size_t   hashmask    = num_buckets - 1;
	unsigned hash        = Hash(self, key);
	size_t   bucknum     = hash & hashmask;
	size_t   insert_pos  = ILLEGAL_POS;
Matthias Braun's avatar
Matthias Braun committed
229
230
231

	assert((num_buckets & (num_buckets - 1)) == 0);

232
	for (;;) {
233
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
234

235
		if (EntryIsEmpty(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
236
237
238
			size_t p;
			HashSetEntry *nentry;

239
			if (insert_pos != ILLEGAL_POS) {
Matthias Braun's avatar
Matthias Braun committed
240
241
242
243
244
				p = insert_pos;
			} else {
				p = bucknum;
			}

245
246
			nentry = &self->entries[p];
			InitData(self, EntryGetValue(*nentry), key);
Matthias Braun's avatar
Matthias Braun committed
247
			EntrySetHash(*nentry, hash);
248
			self->num_elements++;
Matthias Braun's avatar
Matthias Braun committed
249
			return GetInsertReturnValue(*nentry, 0);
Matthias Braun's avatar
Matthias Braun committed
250
		}
251
252
		if (EntryIsDeleted(*entry)) {
			if (insert_pos == ILLEGAL_POS)
Matthias Braun's avatar
Matthias Braun committed
253
				insert_pos = bucknum;
254
255
		} else if (EntryGetHash(self, *entry) == hash) {
			if (KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
256
				// Value already in the set, return it
Matthias Braun's avatar
Matthias Braun committed
257
				return GetInsertReturnValue(*entry, 1);
Matthias Braun's avatar
Matthias Braun committed
258
259
260
261
262
263
264
265
266
			}
		}

		++num_probes;
		bucknum = (bucknum + JUMP(num_probes)) & hashmask;
		assert(num_probes < num_buckets);
	}
}

Michael Beck's avatar
Michael Beck committed
267
268
269
270
/**
 * calculate shrink and enlarge limits
 * @internal
 */
271
static inline void reset_thresholds(HashSet *self)
Michael Beck's avatar
Michael Beck committed
272
273
274
275
276
277
278
{
	self->enlarge_threshold = (size_t) HT_OCCUPANCY_FLT(self->num_buckets);
	self->shrink_threshold  = (size_t) HT_EMPTY_FLT(self->num_buckets);
	self->consider_shrink   = 0;
}

#ifndef HAVE_OWN_RESIZE
Matthias Braun's avatar
Matthias Braun committed
279
280
281
282
283
/**
 * Inserts an element into a hashset under the assumption that the hashset
 * contains no deleted entries and the element doesn't exist in the hashset yet.
 * @internal
 */
284
static void insert_new(HashSet *self, unsigned hash, ValueType value)
Matthias Braun's avatar
Matthias Braun committed
285
{
Matthias Braun's avatar
Matthias Braun committed
286
	size_t num_probes  = 0;
287
	size_t num_buckets = self->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
288
289
290
	size_t hashmask    = num_buckets - 1;
	size_t bucknum     = hash & hashmask;
	size_t insert_pos  = ILLEGAL_POS;
Matthias Braun's avatar
Matthias Braun committed
291

Matthias Braun's avatar
Matthias Braun committed
292
	//assert(value != NullValue);
Matthias Braun's avatar
Matthias Braun committed
293

294
	for (;;) {
295
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
296

297
		if (EntryIsEmpty(*entry)) {
298
			size_t        p;
Matthias Braun's avatar
Matthias Braun committed
299
300
			HashSetEntry *nentry;

301
			if (insert_pos != ILLEGAL_POS) {
Matthias Braun's avatar
Matthias Braun committed
302
303
304
305
				p = insert_pos;
			} else {
				p = bucknum;
			}
306
			nentry = &self->entries[p];
Matthias Braun's avatar
Matthias Braun committed
307
308
309

			EntryGetValue(*nentry) = value;
			EntrySetHash(*nentry, hash);
310
			self->num_elements++;
Matthias Braun's avatar
Matthias Braun committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
			return;
		}
		assert(!EntryIsDeleted(*entry));

		++num_probes;
		bucknum = (bucknum + JUMP(num_probes)) & hashmask;
		assert(num_probes < num_buckets);
	}
}

/**
 * Resize the hashset
 * @internal
 */
325
static inline void resize(HashSet *self, size_t new_size)
Matthias Braun's avatar
Matthias Braun committed
326
{
327
	size_t num_buckets = self->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
328
	size_t i;
329
	HashSetEntry *old_entries = self->entries;
Matthias Braun's avatar
Matthias Braun committed
330
331
332
333
334
335
336
	HashSetEntry *new_entries;

	/* allocate a new array with double size */
	new_entries = Alloc(new_size);
	SetRangeEmpty(new_entries, new_size);

	/* use the new array */
Matthias Braun's avatar
Matthias Braun committed
337
338
	self->entries      = new_entries;
	self->num_buckets  = new_size;
339
	self->num_elements = 0;
Matthias Braun's avatar
Matthias Braun committed
340
	self->num_deleted  = 0;
Matthias Braun's avatar
Matthias Braun committed
341
#ifndef NDEBUG
342
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
343
#endif
344
	reset_thresholds(self);
Matthias Braun's avatar
Matthias Braun committed
345
346

	/* reinsert all elements */
347
	for (i = 0; i < num_buckets; ++i) {
Matthias Braun's avatar
Matthias Braun committed
348
		HashSetEntry *entry = & old_entries[i];
349
		if (EntryIsEmpty(*entry) || EntryIsDeleted(*entry))
Matthias Braun's avatar
Matthias Braun committed
350
351
			continue;

352
		insert_new(self, EntryGetHash(self, *entry), EntryGetValue(*entry));
Matthias Braun's avatar
Matthias Braun committed
353
354
355
356
357
	}

	/* now we can free the old array */
	Free(old_entries);
}
358
359
360
#else

/* resize must be defined outside */
361
static inline void resize(HashSet *self, size_t new_size);
362
363

#endif
Matthias Braun's avatar
Matthias Braun committed
364
365
366
367
368

/**
 * grow the hashset if adding 1 more elements would make it too crowded
 * @internal
 */
369
static inline void maybe_grow(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
370
371
372
{
	size_t resize_to;

373
	if (LIKELY(self->num_elements + 1 <= self->enlarge_threshold))
Matthias Braun's avatar
Matthias Braun committed
374
375
376
		return;

	/* double table size */
377
378
	resize_to = self->num_buckets * 2;
	resize(self, resize_to);
Matthias Braun's avatar
Matthias Braun committed
379
380
381
382
383
384
}

/**
 * shrink the hashset if it is only sparsely filled
 * @internal
 */
385
static inline void maybe_shrink(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
386
387
388
389
{
	size_t size;
	size_t resize_to;

390
	if (!self->consider_shrink)
Matthias Braun's avatar
Matthias Braun committed
391
392
		return;

393
	self->consider_shrink = 0;
394
	size                  = hashset_size(self);
395
	if (size <= HT_MIN_BUCKETS)
396
397
		return;

398
	if (LIKELY(size > self->shrink_threshold))
Matthias Braun's avatar
Matthias Braun committed
399
400
401
402
		return;

	resize_to = ceil_po2(size);

403
	if (resize_to < 4)
Matthias Braun's avatar
Matthias Braun committed
404
405
		resize_to = 4;

406
	resize(self, resize_to);
Matthias Braun's avatar
Matthias Braun committed
407
408
409
}

/**
Michael Beck's avatar
Michael Beck committed
410
 * Insert an element into the hashset. If no element with the given key exists yet,
Matthias Braun's avatar
Matthias Braun committed
411
 * then a new one is created and initialized with the InitData function.
Michael Beck's avatar
Michael Beck committed
412
 * Otherwise the existing element is returned (for hashs where key is equal to
Matthias Braun's avatar
Matthias Braun committed
413
414
 * value, nothing is returned.)
 *
415
 * @param self   the hashset
Matthias Braun's avatar
Matthias Braun committed
416
417
418
 * @param key    the key that identifies the data
 * @returns      the existing or newly created data element (or nothing in case of hashs where keys are the while value)
 */
419
InsertReturnValue hashset_insert(HashSet *self, KeyType key)
Matthias Braun's avatar
Matthias Braun committed
420
421
{
#ifndef NDEBUG
422
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
423
424
#endif

425
426
427
	maybe_shrink(self);
	maybe_grow(self);
	return insert_nogrow(self, key);
Matthias Braun's avatar
Matthias Braun committed
428
429
430
}

/**
Michael Beck's avatar
Michael Beck committed
431
 * Searches for an element with key @p key.
Matthias Braun's avatar
Matthias Braun committed
432
 *
433
 * @param self      the hashset
Matthias Braun's avatar
Matthias Braun committed
434
435
436
 * @param key       the key to search for
 * @returns         the found value or NullValue if nothing was found
 */
Matthias Braun's avatar
Matthias Braun committed
437
InsertReturnValue hashset_find(const HashSet *self, ConstKeyType key)
Matthias Braun's avatar
Matthias Braun committed
438
{
Matthias Braun's avatar
Matthias Braun committed
439
440
441
442
443
	size_t   num_probes  = 0;
	size_t   num_buckets = self->num_buckets;
	size_t   hashmask    = num_buckets - 1;
	unsigned hash        = Hash(self, key);
	size_t   bucknum     = hash & hashmask;
Matthias Braun's avatar
Matthias Braun committed
444

445
	for (;;) {
446
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
447

448
		if (EntryIsEmpty(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
449
			return NullReturnValue;
Matthias Braun's avatar
Matthias Braun committed
450
		}
451
		if (EntryIsDeleted(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
452
			// value is deleted
453
454
		} else if (EntryGetHash(self, *entry) == hash) {
			if (KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
455
				// found the value
Matthias Braun's avatar
Matthias Braun committed
456
				return GetInsertReturnValue(*entry, 1);
Matthias Braun's avatar
Matthias Braun committed
457
458
459
460
461
462
463
464
465
466
467
468
469
			}
		}

		++num_probes;
		bucknum = (bucknum + JUMP(num_probes)) & hashmask;
		assert(num_probes < num_buckets);
	}
}

/**
 * Removes an element from a hashset. Does nothing if the set doesn't contain
 * the element.
 *
470
 * @param self    the hashset
Matthias Braun's avatar
Matthias Braun committed
471
472
 * @param key     key that identifies the data to remove
 */
473
void hashset_remove(HashSet *self, ConstKeyType key)
Matthias Braun's avatar
Matthias Braun committed
474
{
Matthias Braun's avatar
Matthias Braun committed
475
476
477
478
479
	size_t   num_probes  = 0;
	size_t   num_buckets = self->num_buckets;
	size_t   hashmask    = num_buckets - 1;
	unsigned hash        = Hash(self, key);
	size_t   bucknum     = hash & hashmask;
Matthias Braun's avatar
Matthias Braun committed
480
481

#ifndef NDEBUG
482
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
483
484
#endif

485
	for (;;) {
486
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
487

488
		if (EntryIsEmpty(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
489
490
			return;
		}
491
		if (EntryIsDeleted(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
492
			// entry is deleted
493
494
		} else if (EntryGetHash(self, *entry) == hash) {
			if (KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
495
				EntrySetDeleted(*entry);
496
497
				self->num_deleted++;
				self->consider_shrink = 1;
Matthias Braun's avatar
Matthias Braun committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
				return;
			}
		}

		++num_probes;
		bucknum = (bucknum + JUMP(num_probes)) & hashmask;
		assert(num_probes < num_buckets);
	}
}

/**
 * Initializes hashset with a specific size
 * @internal
 */
512
static inline void init_size(HashSet *self, size_t initial_size)
Matthias Braun's avatar
Matthias Braun committed
513
{
514
	if (initial_size < 4)
Matthias Braun's avatar
Matthias Braun committed
515
516
		initial_size = 4;

Matthias Braun's avatar
Matthias Braun committed
517
	self->entries         = Alloc(initial_size);
518
	SetRangeEmpty(self->entries, initial_size);
Matthias Braun's avatar
Matthias Braun committed
519
	self->num_buckets     = initial_size;
520
	self->consider_shrink = 0;
Matthias Braun's avatar
Matthias Braun committed
521
522
	self->num_elements    = 0;
	self->num_deleted     = 0;
Matthias Braun's avatar
Matthias Braun committed
523
#ifndef NDEBUG
524
	self->entries_version = 0;
Matthias Braun's avatar
Matthias Braun committed
525
#endif
Michael Beck's avatar
Michael Beck committed
526
527
528
#ifdef ADDITIONAL_INIT
	ADDITIONAL_INIT
#endif
Matthias Braun's avatar
Matthias Braun committed
529

530
	reset_thresholds(self);
Matthias Braun's avatar
Matthias Braun committed
531
532
533
}

/**
Michael Beck's avatar
Michael Beck committed
534
 * Initializes a hashset with the default size. The memory for the set has to
Matthias Braun's avatar
Matthias Braun committed
535
536
 * already allocated.
 */
537
void hashset_init(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
538
{
539
	init_size(self, HT_MIN_BUCKETS);
Matthias Braun's avatar
Matthias Braun committed
540
541
542
543
544
545
}

/**
 * Destroys a hashset, freeing all used memory (except the memory for the
 * HashSet struct itself).
 */
546
void hashset_destroy(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
547
{
Michael Beck's avatar
Michael Beck committed
548
549
550
#ifdef ADDITIONAL_TERM
	ADDITIONAL_TERM
#endif
551
	Free(self->entries);
Matthias Braun's avatar
Matthias Braun committed
552
#ifndef NDEBUG
553
	self->entries = NULL;
Matthias Braun's avatar
Matthias Braun committed
554
555
556
557
#endif
}

/**
Michael Beck's avatar
Michael Beck committed
558
 * Initializes a hashset expecting expected_element size.
Matthias Braun's avatar
Matthias Braun committed
559
 */
560
void hashset_init_size(HashSet *self, size_t expected_elements)
Matthias Braun's avatar
Matthias Braun committed
561
562
563
564
{
	size_t needed_size;
	size_t po2size;

565
	if (expected_elements >= UINT_MAX/2) {
Matthias Braun's avatar
Matthias Braun committed
566
567
568
		abort();
	}

569
	needed_size = expected_elements * HT_1_DIV_OCCUPANCY_FLT;
Matthias Braun's avatar
Matthias Braun committed
570
	po2size     = ceil_po2(needed_size);
571
	init_size(self, po2size);
Matthias Braun's avatar
Matthias Braun committed
572
573
}

Michael Beck's avatar
Michael Beck committed
574
#ifndef NO_ITERATOR
Matthias Braun's avatar
Matthias Braun committed
575
576
577
578
579
/**
 * Initializes a hashset iterator. The memory for the allocator has to be
 * already allocated.
 * @note it is not allowed to remove or insert elements while iterating
 */
580
void hashset_iterator_init(HashSetIterator *self, const HashSet *hashset)
Matthias Braun's avatar
Matthias Braun committed
581
{
582
	self->current_bucket = hashset->entries - 1;
Matthias Braun's avatar
Matthias Braun committed
583
	self->end            = hashset->entries + hashset->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
584
#ifndef NDEBUG
Matthias Braun's avatar
Matthias Braun committed
585
	self->set             = hashset;
586
	self->entries_version = hashset->entries_version;
Matthias Braun's avatar
Matthias Braun committed
587
588
589
590
591
592
593
594
#endif
}

/**
 * Returns the next value in the iterator or NULL if no value is left
 * in the hashset.
 * @note it is not allowed to remove or insert elements while iterating
 */
595
ValueType hashset_iterator_next(HashSetIterator *self)
Matthias Braun's avatar
Matthias Braun committed
596
{
597
	HashSetEntry *current_bucket = self->current_bucket;
Matthias Braun's avatar
Matthias Braun committed
598
	HashSetEntry *end            = self->end;
Matthias Braun's avatar
Matthias Braun committed
599
600

	/* using hashset_insert or hashset_remove is not allowed while iterating */
601
	assert(self->entries_version == self->set->entries_version);
Matthias Braun's avatar
Matthias Braun committed
602
603
604

	do {
		current_bucket++;
605
		if (current_bucket >= end)
606
			return NullValue;
607
	} while (EntryIsEmpty(*current_bucket) || EntryIsDeleted(*current_bucket));
Matthias Braun's avatar
Matthias Braun committed
608

609
	self->current_bucket = current_bucket;
Matthias Braun's avatar
Matthias Braun committed
610
611
612
613
614
615
616
	return EntryGetValue(*current_bucket);
}

/**
 * Removes the element the iterator points to. Removing an element a second time
 * has no result.
 */
617
void hashset_remove_iterator(HashSet *self, const HashSetIterator *iter)
Matthias Braun's avatar
Matthias Braun committed
618
619
620
621
{
	HashSetEntry *entry = iter->current_bucket;

	/* iterator_next needs to have been called at least once */
622
	assert(entry >= self->entries);
Matthias Braun's avatar
Matthias Braun committed
623
	/* needs to be on a valid element */
624
	assert(entry < self->entries + self->num_buckets);
Matthias Braun's avatar
Matthias Braun committed
625

626
	if (EntryIsDeleted(*entry))
Matthias Braun's avatar
Matthias Braun committed
627
628
629
		return;

	EntrySetDeleted(*entry);
630
631
	self->num_deleted++;
	self->consider_shrink = 1;
Matthias Braun's avatar
Matthias Braun committed
632
}
Michael Beck's avatar
Michael Beck committed
633
#endif /* NO_ITERATOR */
Matthias Braun's avatar
Matthias Braun committed
634

Michael Beck's avatar
Michael Beck committed
635
#endif /* HashSet */