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 _mesa_set_resize(struct set
*set
, uint32_t entries
)
286 /* You can't shrink a set below its number of entries */
287 if (set
->entries
> entries
)
288 entries
= set
->entries
;
290 unsigned size_index
= 0;
291 while (hash_sizes
[size_index
].max_entries
< entries
)
294 set_rehash(set
, size_index
);
298 * Find a matching entry for the given key, or insert it if it doesn't already
301 * Note that insertion may rearrange the table on a resize or rehash,
302 * so previously found hash_entries are no longer valid after this function.
304 static struct set_entry
*
305 set_search_or_add(struct set
*ht
, uint32_t hash
, const void *key
, bool *found
)
307 uint32_t hash_address
;
308 struct set_entry
*available_entry
= NULL
;
310 if (ht
->entries
>= ht
->max_entries
) {
311 set_rehash(ht
, ht
->size_index
+ 1);
312 } else if (ht
->deleted_entries
+ ht
->entries
>= ht
->max_entries
) {
313 set_rehash(ht
, ht
->size_index
);
316 hash_address
= hash
% ht
->size
;
318 struct set_entry
*entry
= ht
->table
+ hash_address
;
319 uint32_t double_hash
;
321 if (!entry_is_present(entry
)) {
322 /* Stash the first available entry we find */
323 if (available_entry
== NULL
)
324 available_entry
= entry
;
325 if (entry_is_free(entry
))
329 if (!entry_is_deleted(entry
) &&
330 entry
->hash
== hash
&&
331 ht
->key_equals_function(key
, entry
->key
)) {
337 double_hash
= 1 + hash
% ht
->rehash
;
339 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
340 } while (hash_address
!= hash
% ht
->size
);
342 if (available_entry
) {
343 /* There is no matching entry, create it. */
344 if (entry_is_deleted(available_entry
))
345 ht
->deleted_entries
--;
346 available_entry
->hash
= hash
;
347 available_entry
->key
= key
;
351 return available_entry
;
354 /* We could hit here if a required resize failed. An unchecked-malloc
355 * application could ignore this result.
361 * Inserts the key with the given hash into the table.
363 * Note that insertion may rearrange the table on a resize or rehash,
364 * so previously found hash_entries are no longer valid after this function.
366 static struct set_entry
*
367 set_add(struct set
*ht
, uint32_t hash
, const void *key
)
369 struct set_entry
*entry
= set_search_or_add(ht
, hash
, key
, NULL
);
371 if (unlikely(!entry
))
374 /* Note: If a matching entry already exists, this will replace it. This is
375 * a relatively common feature of hash tables, with the alternative
376 * generally being "insert the new value as well, and return it first when
377 * the key is searched for".
379 * Note that the hash table doesn't have a delete callback. If freeing of
380 * old keys is required to avoid memory leaks, use the alternative
381 * _mesa_set_search_or_add function and implement the replacement yourself.
388 _mesa_set_add(struct set
*set
, const void *key
)
390 assert(set
->key_hash_function
);
391 return set_add(set
, set
->key_hash_function(key
), key
);
395 _mesa_set_add_pre_hashed(struct set
*set
, uint32_t hash
, const void *key
)
397 assert(set
->key_hash_function
== NULL
||
398 hash
== set
->key_hash_function(key
));
399 return set_add(set
, hash
, key
);
403 _mesa_set_search_and_add(struct set
*set
, const void *key
, bool *replaced
)
405 assert(set
->key_hash_function
);
406 return _mesa_set_search_and_add_pre_hashed(set
,
407 set
->key_hash_function(key
),
412 _mesa_set_search_and_add_pre_hashed(struct set
*set
, uint32_t hash
,
413 const void *key
, bool *replaced
)
415 assert(set
->key_hash_function
== NULL
||
416 hash
== set
->key_hash_function(key
));
417 struct set_entry
*entry
= set_search_or_add(set
, hash
, key
, replaced
);
419 if (unlikely(!entry
))
422 /* This implements the replacement, same as _mesa_set_add(). The user will
423 * be notified if we're overwriting a found entry.
430 _mesa_set_search_or_add(struct set
*set
, const void *key
)
432 assert(set
->key_hash_function
);
433 return set_search_or_add(set
, set
->key_hash_function(key
), key
, NULL
);
437 _mesa_set_search_or_add_pre_hashed(struct set
*set
, uint32_t hash
,
440 assert(set
->key_hash_function
== NULL
||
441 hash
== set
->key_hash_function(key
));
442 return set_search_or_add(set
, hash
, key
, NULL
);
446 * This function deletes the given hash table entry.
448 * Note that deletion doesn't otherwise modify the table, so an iteration over
449 * the table deleting entries is safe.
452 _mesa_set_remove(struct set
*ht
, struct set_entry
*entry
)
457 entry
->key
= deleted_key
;
459 ht
->deleted_entries
++;
463 * Removes the entry with the corresponding key, if exists.
466 _mesa_set_remove_key(struct set
*set
, const void *key
)
468 _mesa_set_remove(set
, _mesa_set_search(set
, key
));
472 * This function is an iterator over the hash table.
474 * Pass in NULL for the first entry, as in the start of a for loop. Note that
475 * an iteration over the table is O(table_size) not O(entries).
478 _mesa_set_next_entry(const struct set
*ht
, struct set_entry
*entry
)
485 for (; entry
!= ht
->table
+ ht
->size
; entry
++) {
486 if (entry_is_present(entry
)) {
495 _mesa_set_random_entry(struct set
*ht
,
496 int (*predicate
)(struct set_entry
*entry
))
498 struct set_entry
*entry
;
499 uint32_t i
= rand() % ht
->size
;
501 if (ht
->entries
== 0)
504 for (entry
= ht
->table
+ i
; entry
!= ht
->table
+ ht
->size
; entry
++) {
505 if (entry_is_present(entry
) &&
506 (!predicate
|| predicate(entry
))) {
511 for (entry
= ht
->table
; entry
!= ht
->table
+ i
; entry
++) {
512 if (entry_is_present(entry
) &&
513 (!predicate
|| predicate(entry
))) {
522 * Helper to create a set with pointer keys.
525 _mesa_pointer_set_create(void *mem_ctx
)
527 return _mesa_set_create(mem_ctx
, _mesa_hash_pointer
,
528 _mesa_key_pointer_equal
);