hashset.c.inl 16.6 KB
Newer Older
1
/*
Matthias Braun's avatar
Matthias Braun committed
2
 * Copyright (C) 1995-2012 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
 *
 *
 * 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>
 *
Matthias Braun's avatar
Matthias Braun committed
39
 * Note that by default it is assumed that the data values themselves are used
Matthias Braun's avatar
Matthias Braun committed
40
41
42
43
44
45
46
47
48
 * 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
Matthias Braun's avatar
Matthias Braun committed
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
102
103
104
105
#define FindReturnValue                 bool
#define GetFindReturnValue(entry,found) (found)
#define NullReturnValue                 false
#define InsertReturnValue(findreturn)   !(findreturn)
Christian Würdig's avatar
Christian Würdig committed
106
#else /* ! ID_HASH */
Matthias Braun's avatar
Matthias Braun committed
107
#ifdef SCALAR_RETURN
108
109
110
#define FindReturnValue                 ValueType
#define GetFindReturnValue(entry,found) EntryGetValue(entry)
#define NullReturnValue                 NullValue
Matthias Braun's avatar
Matthias Braun committed
111
#else
112
113
114
#define FindReturnValue                 ValueType*
#define GetFindReturnValue(entry,found) & EntryGetValue(entry)
#define NullReturnValue                 & NullValue
Matthias Braun's avatar
Matthias Braun committed
115
#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
121
#ifndef InsertReturnValue
#define InsertReturnValue(findreturn)   findreturn
#endif

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

#ifndef ConstKeyType
#define ConstKeyType             const KeyType
Christian Würdig's avatar
Christian Würdig committed
130
#endif /* ConstKeyType */
Matthias Braun's avatar
Matthias Braun committed
131
132
133

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

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

#ifndef HT_EMPTY_FLT
/** how empty before we half size */
167
#define HT_EMPTY_FLT(x)     ((x)/5)
Christian Würdig's avatar
Christian Würdig committed
168
#endif /* HT_EMPTY_FLT */
Matthias Braun's avatar
Matthias Braun committed
169
170
171
172

#ifndef HT_MIN_BUCKETS
/** default smallest bucket size */
#define HT_MIN_BUCKETS    32
Christian Würdig's avatar
Christian Würdig committed
173
#endif /* HT_MIN_BUCKETS */
Matthias Braun's avatar
Matthias Braun committed
174
175
176

#define ILLEGAL_POS       ((size_t)-1)

Matthias Braun's avatar
Matthias Braun committed
177
#ifdef hashset_size
Matthias Braun's avatar
Matthias Braun committed
178
179
180
/**
 * Returns the number of elements in the hashset
 */
181
size_t hashset_size(const HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
182
{
183
	return self->num_elements - self->num_deleted;
Matthias Braun's avatar
Matthias Braun committed
184
}
Matthias Braun's avatar
Matthias Braun committed
185
186
187
188
189
190
#else
static inline size_t hashset_size(const HashSet *self)
{
	return self->num_elements - self->num_deleted;
}
#endif
Matthias Braun's avatar
Matthias Braun committed
191
192
193
194

/**
 * Inserts an element into a hashset without growing the set (you have to make
 * sure there's enough room for that.
195
 * @returns  previous value if found, NullValue otherwise
Matthias Braun's avatar
Matthias Braun committed
196
197
198
 * @note also see comments for hashset_insert()
 * @internal
 */
199
static inline FindReturnValue insert_nogrow(HashSet *self, KeyType key)
Matthias Braun's avatar
Matthias Braun committed
200
{
Matthias Braun's avatar
Matthias Braun committed
201
202
203
204
205
206
	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
207
208
209

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

210
	for (;;) {
211
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
212

213
		if (EntryIsEmpty(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
214
215
216
			size_t p;
			HashSetEntry *nentry;

217
			if (insert_pos != ILLEGAL_POS) {
Matthias Braun's avatar
Matthias Braun committed
218
219
220
221
222
				p = insert_pos;
			} else {
				p = bucknum;
			}

223
224
			nentry = &self->entries[p];
			InitData(self, EntryGetValue(*nentry), key);
Matthias Braun's avatar
Matthias Braun committed
225
			EntrySetHash(*nentry, hash);
226
			self->num_elements++;
227
			return GetFindReturnValue(*nentry, false);
Matthias Braun's avatar
Matthias Braun committed
228
		}
229
230
		if (EntryIsDeleted(*entry)) {
			if (insert_pos == ILLEGAL_POS)
Matthias Braun's avatar
Matthias Braun committed
231
				insert_pos = bucknum;
232
233
		} else if (EntryGetHash(self, *entry) == hash) {
			if (KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
234
				// Value already in the set, return it
235
				return GetFindReturnValue(*entry, true);
Matthias Braun's avatar
Matthias Braun committed
236
237
238
239
240
241
242
243
244
			}
		}

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

Michael Beck's avatar
Michael Beck committed
245
246
247
248
/**
 * calculate shrink and enlarge limits
 * @internal
 */
249
static inline void reset_thresholds(HashSet *self)
Michael Beck's avatar
Michael Beck committed
250
251
252
253
254
255
256
{
	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
257
258
259
260
261
/**
 * 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
 */
262
static void insert_new(HashSet *self, unsigned hash, ValueType value)
Matthias Braun's avatar
Matthias Braun committed
263
{
Matthias Braun's avatar
Matthias Braun committed
264
	size_t num_probes  = 0;
265
	size_t num_buckets = self->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
266
267
268
	size_t hashmask    = num_buckets - 1;
	size_t bucknum     = hash & hashmask;
	size_t insert_pos  = ILLEGAL_POS;
Matthias Braun's avatar
Matthias Braun committed
269

Matthias Braun's avatar
Matthias Braun committed
270
	//assert(value != NullValue);
Matthias Braun's avatar
Matthias Braun committed
271

272
	for (;;) {
273
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
274

275
		if (EntryIsEmpty(*entry)) {
276
			size_t        p;
Matthias Braun's avatar
Matthias Braun committed
277
278
			HashSetEntry *nentry;

279
			if (insert_pos != ILLEGAL_POS) {
Matthias Braun's avatar
Matthias Braun committed
280
281
282
283
				p = insert_pos;
			} else {
				p = bucknum;
			}
284
			nentry = &self->entries[p];
Matthias Braun's avatar
Matthias Braun committed
285
286
287

			EntryGetValue(*nentry) = value;
			EntrySetHash(*nentry, hash);
288
			self->num_elements++;
Matthias Braun's avatar
Matthias Braun committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
			return;
		}
		assert(!EntryIsDeleted(*entry));

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

/**
 * Resize the hashset
 * @internal
 */
303
static inline void resize(HashSet *self, size_t new_size)
Matthias Braun's avatar
Matthias Braun committed
304
{
305
	size_t num_buckets = self->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
306
	size_t i;
307
	HashSetEntry *old_entries = self->entries;
Matthias Braun's avatar
Matthias Braun committed
308
309
310
311
312
313
314
	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
315
316
	self->entries      = new_entries;
	self->num_buckets  = new_size;
317
	self->num_elements = 0;
Matthias Braun's avatar
Matthias Braun committed
318
	self->num_deleted  = 0;
Matthias Braun's avatar
Matthias Braun committed
319
#ifndef NDEBUG
320
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
321
#endif
322
	reset_thresholds(self);
Matthias Braun's avatar
Matthias Braun committed
323
324

	/* reinsert all elements */
325
	for (i = 0; i < num_buckets; ++i) {
Matthias Braun's avatar
Matthias Braun committed
326
		HashSetEntry *entry = & old_entries[i];
327
		if (EntryIsEmpty(*entry) || EntryIsDeleted(*entry))
Matthias Braun's avatar
Matthias Braun committed
328
329
			continue;

330
		insert_new(self, EntryGetHash(self, *entry), EntryGetValue(*entry));
Matthias Braun's avatar
Matthias Braun committed
331
332
333
334
335
	}

	/* now we can free the old array */
	Free(old_entries);
}
336
337
338
#else

/* resize must be defined outside */
339
static inline void resize(HashSet *self, size_t new_size);
340
341

#endif
Matthias Braun's avatar
Matthias Braun committed
342
343
344
345
346

/**
 * grow the hashset if adding 1 more elements would make it too crowded
 * @internal
 */
347
static inline void maybe_grow(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
348
{
349
	if (LIKELY(self->num_elements + 1 <= self->enlarge_threshold))
Matthias Braun's avatar
Matthias Braun committed
350
351
		return;

352
353
354
355
356
357
358
359
360
361
362
	size_t resize_to;
	if (self->num_elements - self->num_deleted + 2 > self->enlarge_threshold) {
		/* double table size */
		resize_to = self->num_buckets * 2;
		if (resize_to <= self->num_buckets) {
			abort();
		}
	} else {
		/* no need to resize, we just clean up the deleted entries */
		resize_to = self->num_buckets;
	}
363
	resize(self, resize_to);
Matthias Braun's avatar
Matthias Braun committed
364
365
366
367
368
369
}

/**
 * shrink the hashset if it is only sparsely filled
 * @internal
 */
370
static inline void maybe_shrink(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
371
372
373
374
{
	size_t size;
	size_t resize_to;

375
	if (!self->consider_shrink)
Matthias Braun's avatar
Matthias Braun committed
376
377
		return;

378
	self->consider_shrink = 0;
379
	size                  = hashset_size(self);
380
	if (size <= HT_MIN_BUCKETS)
381
382
		return;

383
	if (LIKELY(size > self->shrink_threshold))
Matthias Braun's avatar
Matthias Braun committed
384
385
386
387
		return;

	resize_to = ceil_po2(size);

388
	if (resize_to < 4)
Matthias Braun's avatar
Matthias Braun committed
389
390
		resize_to = 4;

391
	resize(self, resize_to);
Matthias Braun's avatar
Matthias Braun committed
392
393
}

Matthias Braun's avatar
Matthias Braun committed
394
#ifdef hashset_insert
Matthias Braun's avatar
Matthias Braun committed
395
/**
Michael Beck's avatar
Michael Beck committed
396
 * Insert an element into the hashset. If no element with the given key exists yet,
Matthias Braun's avatar
Matthias Braun committed
397
 * then a new one is created and initialized with the InitData function.
Michael Beck's avatar
Michael Beck committed
398
 * Otherwise the existing element is returned (for hashs where key is equal to
Matthias Braun's avatar
Matthias Braun committed
399
400
 * value, nothing is returned.)
 *
401
 * @param self   the hashset
Matthias Braun's avatar
Matthias Braun committed
402
403
404
 * @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)
 */
405
FindReturnValue hashset_insert(HashSet *self, KeyType key)
Matthias Braun's avatar
Matthias Braun committed
406
407
{
#ifndef NDEBUG
408
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
409
410
#endif

411
412
	maybe_shrink(self);
	maybe_grow(self);
413
	return InsertReturnValue(insert_nogrow(self, key));
Matthias Braun's avatar
Matthias Braun committed
414
}
Matthias Braun's avatar
Matthias Braun committed
415
#endif
Matthias Braun's avatar
Matthias Braun committed
416

Matthias Braun's avatar
Matthias Braun committed
417
#ifdef hashset_find
Matthias Braun's avatar
Matthias Braun committed
418
/**
Michael Beck's avatar
Michael Beck committed
419
 * Searches for an element with key @p key.
Matthias Braun's avatar
Matthias Braun committed
420
 *
421
 * @param self      the hashset
Matthias Braun's avatar
Matthias Braun committed
422
423
424
 * @param key       the key to search for
 * @returns         the found value or NullValue if nothing was found
 */
425
FindReturnValue hashset_find(const HashSet *self, ConstKeyType key)
Matthias Braun's avatar
Matthias Braun committed
426
{
Matthias Braun's avatar
Matthias Braun committed
427
428
429
430
431
	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
432

433
	for (;;) {
434
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
435

436
		if (EntryIsEmpty(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
437
			return NullReturnValue;
Matthias Braun's avatar
Matthias Braun committed
438
		}
439
		if (EntryIsDeleted(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
440
			// value is deleted
441
442
		} else if (EntryGetHash(self, *entry) == hash) {
			if (KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
443
				// found the value
444
				return GetFindReturnValue(*entry, true);
Matthias Braun's avatar
Matthias Braun committed
445
446
447
448
449
450
451
452
			}
		}

		++num_probes;
		bucknum = (bucknum + JUMP(num_probes)) & hashmask;
		assert(num_probes < num_buckets);
	}
}
Matthias Braun's avatar
Matthias Braun committed
453
#endif
Matthias Braun's avatar
Matthias Braun committed
454

Matthias Braun's avatar
Matthias Braun committed
455
#ifdef hashset_remove
Matthias Braun's avatar
Matthias Braun committed
456
457
458
459
/**
 * Removes an element from a hashset. Does nothing if the set doesn't contain
 * the element.
 *
460
 * @param self    the hashset
Matthias Braun's avatar
Matthias Braun committed
461
462
 * @param key     key that identifies the data to remove
 */
463
void hashset_remove(HashSet *self, ConstKeyType key)
Matthias Braun's avatar
Matthias Braun committed
464
{
Matthias Braun's avatar
Matthias Braun committed
465
466
467
468
469
	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
470
471

#ifndef NDEBUG
472
	self->entries_version++;
Matthias Braun's avatar
Matthias Braun committed
473
474
#endif

475
	for (;;) {
476
		HashSetEntry *entry = & self->entries[bucknum];
Matthias Braun's avatar
Matthias Braun committed
477

478
		if (EntryIsEmpty(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
479
480
			return;
		}
481
		if (EntryIsDeleted(*entry)) {
Matthias Braun's avatar
Matthias Braun committed
482
			// entry is deleted
483
484
		} else if (EntryGetHash(self, *entry) == hash) {
			if (KeysEqual(self, GetKey(EntryGetValue(*entry)), key)) {
Matthias Braun's avatar
Matthias Braun committed
485
				EntrySetDeleted(*entry);
486
487
				self->num_deleted++;
				self->consider_shrink = 1;
Matthias Braun's avatar
Matthias Braun committed
488
489
490
491
492
493
494
495
496
				return;
			}
		}

		++num_probes;
		bucknum = (bucknum + JUMP(num_probes)) & hashmask;
		assert(num_probes < num_buckets);
	}
}
Matthias Braun's avatar
Matthias Braun committed
497
#endif
Matthias Braun's avatar
Matthias Braun committed
498
499
500
501
502

/**
 * Initializes hashset with a specific size
 * @internal
 */
503
static inline void init_size(HashSet *self, size_t initial_size)
Matthias Braun's avatar
Matthias Braun committed
504
{
505
	if (initial_size < 4)
Matthias Braun's avatar
Matthias Braun committed
506
507
		initial_size = 4;

Matthias Braun's avatar
Matthias Braun committed
508
	self->entries         = Alloc(initial_size);
509
	SetRangeEmpty(self->entries, initial_size);
Matthias Braun's avatar
Matthias Braun committed
510
	self->num_buckets     = initial_size;
511
	self->consider_shrink = 0;
Matthias Braun's avatar
Matthias Braun committed
512
513
	self->num_elements    = 0;
	self->num_deleted     = 0;
Matthias Braun's avatar
Matthias Braun committed
514
#ifndef NDEBUG
515
	self->entries_version = 0;
Matthias Braun's avatar
Matthias Braun committed
516
#endif
Michael Beck's avatar
Michael Beck committed
517
518
519
#ifdef ADDITIONAL_INIT
	ADDITIONAL_INIT
#endif
Matthias Braun's avatar
Matthias Braun committed
520

521
	reset_thresholds(self);
Matthias Braun's avatar
Matthias Braun committed
522
523
}

Matthias Braun's avatar
Matthias Braun committed
524
#ifdef hashset_init
Matthias Braun's avatar
Matthias Braun committed
525
/**
Michael Beck's avatar
Michael Beck committed
526
 * Initializes a hashset with the default size. The memory for the set has to
Matthias Braun's avatar
Matthias Braun committed
527
528
 * already allocated.
 */
529
void hashset_init(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
530
{
531
	init_size(self, HT_MIN_BUCKETS);
Matthias Braun's avatar
Matthias Braun committed
532
}
Matthias Braun's avatar
Matthias Braun committed
533
#endif
Matthias Braun's avatar
Matthias Braun committed
534

Matthias Braun's avatar
Matthias Braun committed
535
#ifdef hashset_destroy
Matthias Braun's avatar
Matthias Braun committed
536
537
538
539
/**
 * Destroys a hashset, freeing all used memory (except the memory for the
 * HashSet struct itself).
 */
540
void hashset_destroy(HashSet *self)
Matthias Braun's avatar
Matthias Braun committed
541
{
Michael Beck's avatar
Michael Beck committed
542
543
544
#ifdef ADDITIONAL_TERM
	ADDITIONAL_TERM
#endif
545
	Free(self->entries);
Matthias Braun's avatar
Matthias Braun committed
546
#ifndef NDEBUG
547
	self->entries = NULL;
Matthias Braun's avatar
Matthias Braun committed
548
549
#endif
}
Matthias Braun's avatar
Matthias Braun committed
550
#endif
Matthias Braun's avatar
Matthias Braun committed
551

Matthias Braun's avatar
Matthias Braun committed
552
#ifdef hashset_init_size
Matthias Braun's avatar
Matthias Braun committed
553
/**
Michael Beck's avatar
Michael Beck committed
554
 * Initializes a hashset expecting expected_element size.
Matthias Braun's avatar
Matthias Braun committed
555
 */
556
void hashset_init_size(HashSet *self, size_t expected_elements)
Matthias Braun's avatar
Matthias Braun committed
557
558
559
560
{
	size_t needed_size;
	size_t po2size;

561
	if (expected_elements >= UINT_MAX/2) {
Matthias Braun's avatar
Matthias Braun committed
562
563
564
		abort();
	}

565
	needed_size = expected_elements * HT_1_DIV_OCCUPANCY_FLT;
Matthias Braun's avatar
Matthias Braun committed
566
	po2size     = ceil_po2(needed_size);
567
	init_size(self, po2size);
Matthias Braun's avatar
Matthias Braun committed
568
}
Matthias Braun's avatar
Matthias Braun committed
569
#endif
Matthias Braun's avatar
Matthias Braun committed
570

Matthias Braun's avatar
Matthias Braun committed
571
#ifdef hashset_iterator_init
Matthias Braun's avatar
Matthias Braun committed
572
573
574
575
576
/**
 * 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
 */
577
void hashset_iterator_init(HashSetIterator *self, const HashSet *hashset)
Matthias Braun's avatar
Matthias Braun committed
578
{
579
	self->current_bucket = hashset->entries - 1;
Matthias Braun's avatar
Matthias Braun committed
580
	self->end            = hashset->entries + hashset->num_buckets;
Matthias Braun's avatar
Matthias Braun committed
581
#ifndef NDEBUG
Matthias Braun's avatar
Matthias Braun committed
582
	self->set             = hashset;
583
	self->entries_version = hashset->entries_version;
Matthias Braun's avatar
Matthias Braun committed
584
585
#endif
}
Matthias Braun's avatar
Matthias Braun committed
586
#endif
Matthias Braun's avatar
Matthias Braun committed
587

Matthias Braun's avatar
Matthias Braun committed
588
#ifdef hashset_iterator_next
Matthias Braun's avatar
Matthias Braun committed
589
590
591
592
593
/**
 * 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
 */
594
ValueType hashset_iterator_next(HashSetIterator *self)
Matthias Braun's avatar
Matthias Braun committed
595
{
596
	HashSetEntry *current_bucket = self->current_bucket;
Matthias Braun's avatar
Matthias Braun committed
597
	HashSetEntry *end            = self->end;
Matthias Braun's avatar
Matthias Braun committed
598
599

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

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

608
	self->current_bucket = current_bucket;
Matthias Braun's avatar
Matthias Braun committed
609
610
	return EntryGetValue(*current_bucket);
}
Matthias Braun's avatar
Matthias Braun committed
611
#endif
Matthias Braun's avatar
Matthias Braun committed
612

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

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

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

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

Matthias Braun's avatar
Matthias Braun committed
636
#endif