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
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
 * @version $Id$
 *
 *
 * 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
 *                             them on demand from the datavalues. (usefull if
 *                             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
78
#endif /* JUMP */
Matthias Braun's avatar
Matthias Braun committed
79
80
81

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

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

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

#ifdef ID_HASH
Matthias Braun's avatar
Matthias Braun committed
103
104
105
#define InsertReturnValue                 int
#define GetInsertReturnValue(entry,found) (found)
#define NullReturnValue                   0
Christian Würdig's avatar
Christian Würdig committed
106
#else /* ! ID_HASH */
Matthias Braun's avatar
Matthias Braun committed
107
108
109
110
111
112
113
114
115
#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
116
#endif /* ID_HASH */
Matthias Braun's avatar
Matthias Braun committed
117
118
119
120

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

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

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

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

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

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

#define ILLEGAL_POS       ((size_t)-1)

Christian Würdig's avatar
Christian Würdig committed
173
/* check that all needed functions are defined */
Matthias Braun's avatar
Matthias Braun committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#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
195
196

#ifndef NO_ITERATOR
Matthias Braun's avatar
Matthias Braun committed
197
198
199
200
201
202
203
204
205
#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
206
#endif
Matthias Braun's avatar
Matthias Braun committed
207
208
209
210

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

/**
 * 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
 */
Andreas Schösser's avatar
Andreas Schösser committed
222
static INLINE
223
InsertReturnValue insert_nogrow(HashSet *self, KeyType key)
Matthias Braun's avatar
Matthias Braun committed
224
{
Matthias Braun's avatar
Matthias Braun committed
225
226
227
228
229
230
	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
231
232
233
234

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

	while(1) {
235
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
236
237
238
239
240
241
242
243
244
245
246

		if(EntryIsEmpty(*entry)) {
			size_t p;
			HashSetEntry *nentry;

			if(insert_pos != ILLEGAL_POS) {
				p = insert_pos;
			} else {
				p = bucknum;
			}

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

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

Michael Beck's avatar
Michael Beck committed
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
 * calculate shrink and enlarge limits
 * @internal
 */
static INLINE
void reset_thresholds(HashSet *self)
{
	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
282
283
284
285
286
287
/**
 * 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
 */
static
288
void insert_new(HashSet *self, unsigned hash, ValueType value)
Matthias Braun's avatar
Matthias Braun committed
289
{
Matthias Braun's avatar
Matthias Braun committed
290
	size_t num_probes  = 0;
291
	size_t num_buckets = self->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
292
293
294
	size_t hashmask    = num_buckets - 1;
	size_t bucknum     = hash & hashmask;
	size_t insert_pos  = ILLEGAL_POS;
Matthias Braun's avatar
Matthias Braun committed
295

Matthias Braun's avatar
Matthias Braun committed
296
	//assert(value != NullValue);
Matthias Braun's avatar
Matthias Braun committed
297
298

	while(1) {
299
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
300
301

		if(EntryIsEmpty(*entry)) {
302
			size_t        p;
Matthias Braun's avatar
Matthias Braun committed
303
304
305
306
307
308
309
			HashSetEntry *nentry;

			if(insert_pos != ILLEGAL_POS) {
				p = insert_pos;
			} else {
				p = bucknum;
			}
310
			nentry = &self->entries[p];
Matthias Braun's avatar
Matthias Braun committed
311
312
313

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

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

/**
 * Resize the hashset
 * @internal
 */
Andreas Schösser's avatar
Andreas Schösser committed
329
static INLINE
330
void resize(HashSet *self, size_t new_size)
Matthias Braun's avatar
Matthias Braun committed
331
{
332
	size_t num_buckets = self->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
333
	size_t i;
334
	HashSetEntry *old_entries = self->entries;
Matthias Braun's avatar
Matthias Braun committed
335
336
337
338
339
340
341
	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
342
343
	self->entries      = new_entries;
	self->num_buckets  = new_size;
344
	self->num_elements = 0;
Matthias Braun's avatar
Matthias Braun committed
345
	self->num_deleted  = 0;
Matthias Braun's avatar
Matthias Braun committed
346
#ifndef NDEBUG
347
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
348
#endif
349
	reset_thresholds(self);
Matthias Braun's avatar
Matthias Braun committed
350
351
352
353
354
355
356

	/* reinsert all elements */
	for(i = 0; i < num_buckets; ++i) {
		HashSetEntry *entry = & old_entries[i];
		if(EntryIsEmpty(*entry) || EntryIsDeleted(*entry))
			continue;

357
		insert_new(self, EntryGetHash(self, *entry), EntryGetValue(*entry));
Matthias Braun's avatar
Matthias Braun committed
358
359
360
361
362
	}

	/* now we can free the old array */
	Free(old_entries);
}
363
364
365
#else

/* resize must be defined outside */
366
static INLINE void resize(HashSet *self, size_t new_size);
367
368

#endif
Matthias Braun's avatar
Matthias Braun committed
369
370
371
372
373

/**
 * grow the hashset if adding 1 more elements would make it too crowded
 * @internal
 */
Andreas Schösser's avatar
Andreas Schösser committed
374
static INLINE
375
void maybe_grow(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
376
377
378
{
	size_t resize_to;

379
	if(LIKELY(self->num_elements + 1 <= self->enlarge_threshold))
Matthias Braun's avatar
Matthias Braun committed
380
381
382
		return;

	/* double table size */
383
384
	resize_to = self->num_buckets * 2;
	resize(self, resize_to);
Matthias Braun's avatar
Matthias Braun committed
385
386
387
388
389
390
}

/**
 * shrink the hashset if it is only sparsely filled
 * @internal
 */
Andreas Schösser's avatar
Andreas Schösser committed
391
static INLINE
392
void maybe_shrink(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
393
394
395
396
{
	size_t size;
	size_t resize_to;

397
	if(!self->consider_shrink)
Matthias Braun's avatar
Matthias Braun committed
398
399
		return;

400
	self->consider_shrink = 0;
401
	size                  = hashset_size(self);
402
403
404
	if(size <= HT_MIN_BUCKETS)
		return;

405
	if(LIKELY(size > self->shrink_threshold))
Matthias Braun's avatar
Matthias Braun committed
406
407
408
409
410
411
412
		return;

	resize_to = ceil_po2(size);

	if(resize_to < 4)
		resize_to = 4;

413
	resize(self, resize_to);
Matthias Braun's avatar
Matthias Braun committed
414
415
416
}

/**
Michael Beck's avatar
Michael Beck committed
417
 * Insert an element into the hashset. If no element with the given key exists yet,
Matthias Braun's avatar
Matthias Braun committed
418
 * then a new one is created and initialized with the InitData function.
Michael Beck's avatar
Michael Beck committed
419
 * Otherwise the existing element is returned (for hashs where key is equal to
Matthias Braun's avatar
Matthias Braun committed
420
421
 * value, nothing is returned.)
 *
422
 * @param self   the hashset
Matthias Braun's avatar
Matthias Braun committed
423
424
425
 * @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)
 */
426
InsertReturnValue hashset_insert(HashSet *self, KeyType key)
Matthias Braun's avatar
Matthias Braun committed
427
428
{
#ifndef NDEBUG
429
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
430
431
#endif

432
433
434
	maybe_shrink(self);
	maybe_grow(self);
	return insert_nogrow(self, key);
Matthias Braun's avatar
Matthias Braun committed
435
436
437
}

/**
Michael Beck's avatar
Michael Beck committed
438
 * Searches for an element with key @p key.
Matthias Braun's avatar
Matthias Braun committed
439
 *
440
 * @param self      the hashset
Matthias Braun's avatar
Matthias Braun committed
441
442
443
 * @param key       the key to search for
 * @returns         the found value or NullValue if nothing was found
 */
Matthias Braun's avatar
Matthias Braun committed
444
InsertReturnValue hashset_find(const HashSet *self, ConstKeyType key)
Matthias Braun's avatar
Matthias Braun committed
445
{
Matthias Braun's avatar
Matthias Braun committed
446
447
448
449
450
	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
451
452

	while(1) {
453
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
454
455

		if(EntryIsEmpty(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
456
			return NullReturnValue;
Matthias Braun's avatar
Matthias Braun committed
457
458
459
		}
		if(EntryIsDeleted(*entry)) {
			// value is deleted
460
461
		} else if(EntryGetHash(self, *entry) == hash) {
			if(KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
462
				// found the value
Matthias Braun's avatar
Matthias Braun committed
463
				return GetInsertReturnValue(*entry, 1);
Matthias Braun's avatar
Matthias Braun committed
464
465
466
467
468
469
470
471
472
473
474
475
476
			}
		}

		++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.
 *
477
 * @param self    the hashset
Matthias Braun's avatar
Matthias Braun committed
478
479
 * @param key     key that identifies the data to remove
 */
480
void hashset_remove(HashSet *self, ConstKeyType key)
Matthias Braun's avatar
Matthias Braun committed
481
{
Matthias Braun's avatar
Matthias Braun committed
482
483
484
485
486
	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
487
488

#ifndef NDEBUG
489
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
490
491
492
#endif

	while(1) {
493
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
494
495
496
497
498
499

		if(EntryIsEmpty(*entry)) {
			return;
		}
		if(EntryIsDeleted(*entry)) {
			// entry is deleted
500
501
		} else if(EntryGetHash(self, *entry) == hash) {
			if(KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
502
				EntrySetDeleted(*entry);
503
504
				self->num_deleted++;
				self->consider_shrink = 1;
Matthias Braun's avatar
Matthias Braun committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
				return;
			}
		}

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

/**
 * Initializes hashset with a specific size
 * @internal
 */
Andreas Schösser's avatar
Andreas Schösser committed
519
static INLINE
520
void init_size(HashSet *self, size_t initial_size)
Matthias Braun's avatar
Matthias Braun committed
521
522
523
524
{
	if(initial_size < 4)
		initial_size = 4;

Matthias Braun's avatar
Matthias Braun committed
525
	self->entries         = Alloc(initial_size);
526
	SetRangeEmpty(self->entries, initial_size);
Matthias Braun's avatar
Matthias Braun committed
527
	self->num_buckets     = initial_size;
528
	self->consider_shrink = 0;
Matthias Braun's avatar
Matthias Braun committed
529
530
	self->num_elements    = 0;
	self->num_deleted     = 0;
Matthias Braun's avatar
Matthias Braun committed
531
#ifndef NDEBUG
532
	self->entries_version = 0;
Matthias Braun's avatar
Matthias Braun committed
533
#endif
Michael Beck's avatar
Michael Beck committed
534
535
536
#ifdef ADDITIONAL_INIT
	ADDITIONAL_INIT
#endif
Matthias Braun's avatar
Matthias Braun committed
537

538
	reset_thresholds(self);
Matthias Braun's avatar
Matthias Braun committed
539
540
541
}

/**
Michael Beck's avatar
Michael Beck committed
542
 * Initializes a hashset with the default size. The memory for the set has to
Matthias Braun's avatar
Matthias Braun committed
543
544
 * already allocated.
 */
545
void hashset_init(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
546
{
547
	init_size(self, HT_MIN_BUCKETS);
Matthias Braun's avatar
Matthias Braun committed
548
549
550
551
552
553
}

/**
 * Destroys a hashset, freeing all used memory (except the memory for the
 * HashSet struct itself).
 */
554
void hashset_destroy(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
555
{
Michael Beck's avatar
Michael Beck committed
556
557
558
#ifdef ADDITIONAL_TERM
	ADDITIONAL_TERM
#endif
559
	Free(self->entries);
Matthias Braun's avatar
Matthias Braun committed
560
#ifndef NDEBUG
561
	self->entries = NULL;
Matthias Braun's avatar
Matthias Braun committed
562
563
564
565
#endif
}

/**
Michael Beck's avatar
Michael Beck committed
566
 * Initializes a hashset expecting expected_element size.
Matthias Braun's avatar
Matthias Braun committed
567
 */
568
void hashset_init_size(HashSet *self, size_t expected_elements)
Matthias Braun's avatar
Matthias Braun committed
569
570
571
572
573
574
575
576
{
	size_t needed_size;
	size_t po2size;

	if(expected_elements >= UINT_MAX/2) {
		abort();
	}

577
	needed_size = expected_elements * HT_1_DIV_OCCUPANCY_FLT;
Matthias Braun's avatar
Matthias Braun committed
578
	po2size     = ceil_po2(needed_size);
579
	init_size(self, po2size);
Matthias Braun's avatar
Matthias Braun committed
580
581
}

Michael Beck's avatar
Michael Beck committed
582
#ifndef NO_ITERATOR
Matthias Braun's avatar
Matthias Braun committed
583
584
585
586
587
/**
 * 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
 */
588
void hashset_iterator_init(HashSetIterator *self, const HashSet *hashset)
Matthias Braun's avatar
Matthias Braun committed
589
{
590
	self->current_bucket = hashset->entries - 1;
Matthias Braun's avatar
Matthias Braun committed
591
	self->end            = hashset->entries + hashset->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
592
#ifndef NDEBUG
Matthias Braun's avatar
Matthias Braun committed
593
	self->set             = hashset;
594
	self->entries_version = hashset->entries_version;
Matthias Braun's avatar
Matthias Braun committed
595
596
597
598
599
600
601
602
#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
 */
603
ValueType hashset_iterator_next(HashSetIterator *self)
Matthias Braun's avatar
Matthias Braun committed
604
{
605
	HashSetEntry *current_bucket = self->current_bucket;
Matthias Braun's avatar
Matthias Braun committed
606
	HashSetEntry *end            = self->end;
Matthias Braun's avatar
Matthias Braun committed
607
608

	/* using hashset_insert or hashset_remove is not allowed while iterating */
609
	assert(self->entries_version == self->set->entries_version);
Matthias Braun's avatar
Matthias Braun committed
610
611
612

	do {
		current_bucket++;
613
614
615
		if(current_bucket >= end)
			return NullValue;
	} while(EntryIsEmpty(*current_bucket) || EntryIsDeleted(*current_bucket));
Matthias Braun's avatar
Matthias Braun committed
616

617
	self->current_bucket = current_bucket;
Matthias Braun's avatar
Matthias Braun committed
618
619
620
621
622
623
624
	return EntryGetValue(*current_bucket);
}

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

	/* iterator_next needs to have been called at least once */
630
	assert(entry >= self->entries);
Matthias Braun's avatar
Matthias Braun committed
631
	/* needs to be on a valid element */
632
	assert(entry < self->entries + self->num_buckets);
Matthias Braun's avatar
Matthias Braun committed
633
634
635
636
637

	if(EntryIsDeleted(*entry))
		return;

	EntrySetDeleted(*entry);
638
639
	self->num_deleted++;
	self->consider_shrink = 1;
Matthias Braun's avatar
Matthias Braun committed
640
}
Michael Beck's avatar
Michael Beck committed
641
#endif /* NO_ITERATOR */
Matthias Braun's avatar
Matthias Braun committed
642

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