2 * Copyright © 2009-2012 Intel Corporation
3 * Copyright © 1988-2004 Keith Packard and Bart Massey.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Except as contained in this notice, the names of the authors
25 * or their institutions shall not be used in advertising or
26 * otherwise to promote the sale, use or other dealings in this
27 * Software without prior written authorization from the
31 * Eric Anholt <eric@anholt.net>
32 * Keith Packard <keithp@keithp.com>
39 #include "hash_table.h"
45 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
46 * p and p-2 are both prime. These tables are sized to have an extra 10%
47 * free to avoid exponential performance degradation as the hash table fills
50 static const uint32_t deleted_key_value
;
51 static const void *deleted_key
= &deleted_key_value
;
54 uint32_t max_entries
, size
, rehash
;
69 { 16384, 18043, 18041 },
70 { 32768, 36109, 36107 },
71 { 65536, 72091, 72089 },
72 { 131072, 144409, 144407 },
73 { 262144, 288361, 288359 },
74 { 524288, 576883, 576881 },
75 { 1048576, 1153459, 1153457 },
76 { 2097152, 2307163, 2307161 },
77 { 4194304, 4613893, 4613891 },
78 { 8388608, 9227641, 9227639 },
79 { 16777216, 18455029, 18455027 },
80 { 33554432, 36911011, 36911009 },
81 { 67108864, 73819861, 73819859 },
82 { 134217728, 147639589, 147639587 },
83 { 268435456, 295279081, 295279079 },
84 { 536870912, 590559793, 590559791 },
85 { 1073741824, 1181116273, 1181116271 },
86 { 2147483648ul, 2362232233ul, 2362232231ul }
90 entry_is_free(struct set_entry
*entry
)
92 return entry
->key
== NULL
;
96 entry_is_deleted(struct set_entry
*entry
)
98 return entry
->key
== deleted_key
;
102 entry_is_present(struct set_entry
*entry
)
104 return entry
->key
!= NULL
&& entry
->key
!= deleted_key
;
108 _mesa_set_create(void *mem_ctx
,
109 uint32_t (*key_hash_function
)(const void *key
),
110 bool (*key_equals_function
)(const void *a
,
115 ht
= ralloc(mem_ctx
, struct set
);
120 ht
->size
= hash_sizes
[ht
->size_index
].size
;
121 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
122 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
123 ht
->key_hash_function
= key_hash_function
;
124 ht
->key_equals_function
= key_equals_function
;
125 ht
->table
= rzalloc_array(ht
, struct set_entry
, ht
->size
);
127 ht
->deleted_entries
= 0;
129 if (ht
->table
== NULL
) {
138 _mesa_set_clone(struct set
*set
, void *dst_mem_ctx
)
142 clone
= ralloc(dst_mem_ctx
, struct set
);
146 memcpy(clone
, set
, sizeof(struct set
));
148 clone
->table
= ralloc_array(clone
, struct set_entry
, clone
->size
);
149 if (clone
->table
== NULL
) {
154 memcpy(clone
->table
, set
->table
, clone
->size
* sizeof(struct set_entry
));
160 * Frees the given set.
162 * If delete_function is passed, it gets called on each entry present before
166 _mesa_set_destroy(struct set
*ht
, void (*delete_function
)(struct set_entry
*entry
))
171 if (delete_function
) {
172 set_foreach (ht
, entry
) {
173 delete_function(entry
);
176 ralloc_free(ht
->table
);
181 * Clears all values from the given set.
183 * If delete_function is passed, it gets called on each entry present before
184 * the set is cleared.
187 _mesa_set_clear(struct set
*set
, void (*delete_function
)(struct set_entry
*entry
))
192 set_foreach (set
, entry
) {
194 delete_function(entry
);
195 entry
->key
= deleted_key
;
198 set
->entries
= set
->deleted_entries
= 0;
202 * Finds a set entry with the given key and hash of that key.
204 * Returns NULL if no entry is found.
206 static struct set_entry
*
207 set_search(const struct set
*ht
, uint32_t hash
, const void *key
)
209 uint32_t hash_address
;
211 hash_address
= hash
% ht
->size
;
213 uint32_t double_hash
;
215 struct set_entry
*entry
= ht
->table
+ hash_address
;
217 if (entry_is_free(entry
)) {
219 } else if (entry_is_present(entry
) && entry
->hash
== hash
) {
220 if (ht
->key_equals_function(key
, entry
->key
)) {
225 double_hash
= 1 + hash
% ht
->rehash
;
227 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
228 } while (hash_address
!= hash
% ht
->size
);
234 _mesa_set_search(const struct set
*set
, const void *key
)
236 assert(set
->key_hash_function
);
237 return set_search(set
, set
->key_hash_function(key
), key
);
241 _mesa_set_search_pre_hashed(const struct set
*set
, uint32_t hash
,
244 assert(set
->key_hash_function
== NULL
||
245 hash
== set
->key_hash_function(key
));
246 return set_search(set
, hash
, key
);
249 static struct set_entry
*
250 set_add(struct set
*ht
, uint32_t hash
, const void *key
);
253 set_rehash(struct set
*ht
, unsigned new_size_index
)
256 struct set_entry
*table
;
258 if (new_size_index
>= ARRAY_SIZE(hash_sizes
))
261 table
= rzalloc_array(ht
, struct set_entry
,
262 hash_sizes
[new_size_index
].size
);
269 ht
->size_index
= new_size_index
;
270 ht
->size
= hash_sizes
[ht
->size_index
].size
;
271 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
272 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
274 ht
->deleted_entries
= 0;
276 set_foreach(&old_ht
, entry
) {
277 set_add(ht
, entry
->hash
, entry
->key
);
280 ralloc_free(old_ht
.table
);
284 * Inserts the key with the given hash into the table.
286 * Note that insertion may rearrange the table on a resize or rehash,
287 * so previously found hash_entries are no longer valid after this function.
289 static struct set_entry
*
290 set_add(struct set
*ht
, uint32_t hash
, const void *key
)
292 uint32_t hash_address
;
293 struct set_entry
*available_entry
= NULL
;
295 if (ht
->entries
>= ht
->max_entries
) {
296 set_rehash(ht
, ht
->size_index
+ 1);
297 } else if (ht
->deleted_entries
+ ht
->entries
>= ht
->max_entries
) {
298 set_rehash(ht
, ht
->size_index
);
301 hash_address
= hash
% ht
->size
;
303 struct set_entry
*entry
= ht
->table
+ hash_address
;
304 uint32_t double_hash
;
306 if (!entry_is_present(entry
)) {
307 /* Stash the first available entry we find */
308 if (available_entry
== NULL
)
309 available_entry
= entry
;
310 if (entry_is_free(entry
))
314 /* Implement replacement when another insert happens
315 * with a matching key. This is a relatively common
316 * feature of hash tables, with the alternative
317 * generally being "insert the new value as well, and
318 * return it first when the key is searched for".
320 * Note that the hash table doesn't have a delete callback.
321 * If freeing of old keys is required to avoid memory leaks,
322 * perform a search before inserting.
324 if (!entry_is_deleted(entry
) &&
325 entry
->hash
== hash
&&
326 ht
->key_equals_function(key
, entry
->key
)) {
331 double_hash
= 1 + hash
% ht
->rehash
;
333 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
334 } while (hash_address
!= hash
% ht
->size
);
336 if (available_entry
) {
337 if (entry_is_deleted(available_entry
))
338 ht
->deleted_entries
--;
339 available_entry
->hash
= hash
;
340 available_entry
->key
= key
;
342 return available_entry
;
345 /* We could hit here if a required resize failed. An unchecked-malloc
346 * application could ignore this result.
352 _mesa_set_add(struct set
*set
, const void *key
)
354 assert(set
->key_hash_function
);
355 return set_add(set
, set
->key_hash_function(key
), key
);
359 _mesa_set_add_pre_hashed(struct set
*set
, uint32_t hash
, const void *key
)
361 assert(set
->key_hash_function
== NULL
||
362 hash
== set
->key_hash_function(key
));
363 return set_add(set
, hash
, key
);
367 * This function deletes the given hash table entry.
369 * Note that deletion doesn't otherwise modify the table, so an iteration over
370 * the table deleting entries is safe.
373 _mesa_set_remove(struct set
*ht
, struct set_entry
*entry
)
378 entry
->key
= deleted_key
;
380 ht
->deleted_entries
++;
384 * Removes the entry with the corresponding key, if exists.
387 _mesa_set_remove_key(struct set
*set
, const void *key
)
389 _mesa_set_remove(set
, _mesa_set_search(set
, key
));
393 * This function is an iterator over the hash table.
395 * Pass in NULL for the first entry, as in the start of a for loop. Note that
396 * an iteration over the table is O(table_size) not O(entries).
399 _mesa_set_next_entry(const struct set
*ht
, struct set_entry
*entry
)
406 for (; entry
!= ht
->table
+ ht
->size
; entry
++) {
407 if (entry_is_present(entry
)) {
416 _mesa_set_random_entry(struct set
*ht
,
417 int (*predicate
)(struct set_entry
*entry
))
419 struct set_entry
*entry
;
420 uint32_t i
= rand() % ht
->size
;
422 if (ht
->entries
== 0)
425 for (entry
= ht
->table
+ i
; entry
!= ht
->table
+ ht
->size
; entry
++) {
426 if (entry_is_present(entry
) &&
427 (!predicate
|| predicate(entry
))) {
432 for (entry
= ht
->table
; entry
!= ht
->table
+ i
; entry
++) {
433 if (entry_is_present(entry
) &&
434 (!predicate
|| predicate(entry
))) {
443 * Helper to create a set with pointer keys.
446 _mesa_pointer_set_create(void *mem_ctx
)
448 return _mesa_set_create(mem_ctx
, _mesa_hash_pointer
,
449 _mesa_key_pointer_equal
);