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>
44 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
45 * p and p-2 are both prime. These tables are sized to have an extra 10%
46 * free to avoid exponential performance degradation as the hash table fills
49 static const uint32_t deleted_key_value
;
50 static const void *deleted_key
= &deleted_key_value
;
53 uint32_t max_entries
, size
, rehash
;
68 { 16384, 18043, 18041 },
69 { 32768, 36109, 36107 },
70 { 65536, 72091, 72089 },
71 { 131072, 144409, 144407 },
72 { 262144, 288361, 288359 },
73 { 524288, 576883, 576881 },
74 { 1048576, 1153459, 1153457 },
75 { 2097152, 2307163, 2307161 },
76 { 4194304, 4613893, 4613891 },
77 { 8388608, 9227641, 9227639 },
78 { 16777216, 18455029, 18455027 },
79 { 33554432, 36911011, 36911009 },
80 { 67108864, 73819861, 73819859 },
81 { 134217728, 147639589, 147639587 },
82 { 268435456, 295279081, 295279079 },
83 { 536870912, 590559793, 590559791 },
84 { 1073741824, 1181116273, 1181116271 },
85 { 2147483648ul, 2362232233ul, 2362232231ul }
89 entry_is_free(struct set_entry
*entry
)
91 return entry
->key
== NULL
;
95 entry_is_deleted(struct set_entry
*entry
)
97 return entry
->key
== deleted_key
;
101 entry_is_present(struct set_entry
*entry
)
103 return entry
->key
!= NULL
&& entry
->key
!= deleted_key
;
107 _mesa_set_create(void *mem_ctx
,
108 uint32_t (*key_hash_function
)(const void *key
),
109 bool (*key_equals_function
)(const void *a
,
114 ht
= ralloc(mem_ctx
, struct set
);
119 ht
->size
= hash_sizes
[ht
->size_index
].size
;
120 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
121 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
122 ht
->key_hash_function
= key_hash_function
;
123 ht
->key_equals_function
= key_equals_function
;
124 ht
->table
= rzalloc_array(ht
, struct set_entry
, ht
->size
);
126 ht
->deleted_entries
= 0;
128 if (ht
->table
== NULL
) {
137 _mesa_set_clone(struct set
*set
, void *dst_mem_ctx
)
141 clone
= ralloc(dst_mem_ctx
, struct set
);
145 memcpy(clone
, set
, sizeof(struct set
));
147 clone
->table
= ralloc_array(clone
, struct set_entry
, clone
->size
);
148 if (clone
->table
== NULL
) {
153 memcpy(clone
->table
, set
->table
, clone
->size
* sizeof(struct set_entry
));
159 * Frees the given set.
161 * If delete_function is passed, it gets called on each entry present before
165 _mesa_set_destroy(struct set
*ht
, void (*delete_function
)(struct set_entry
*entry
))
170 if (delete_function
) {
171 struct set_entry
*entry
;
173 set_foreach (ht
, entry
) {
174 delete_function(entry
);
177 ralloc_free(ht
->table
);
182 * Clears all values from the given set.
184 * If delete_function is passed, it gets called on each entry present before
185 * the set is cleared.
188 _mesa_set_clear(struct set
*set
, void (*delete_function
)(struct set_entry
*entry
))
190 struct set_entry
*entry
;
195 set_foreach (set
, entry
) {
197 delete_function(entry
);
198 entry
->key
= deleted_key
;
201 set
->entries
= set
->deleted_entries
= 0;
205 * Finds a set entry with the given key and hash of that key.
207 * Returns NULL if no entry is found.
209 static struct set_entry
*
210 set_search(const struct set
*ht
, uint32_t hash
, const void *key
)
212 uint32_t hash_address
;
214 hash_address
= hash
% ht
->size
;
216 uint32_t double_hash
;
218 struct set_entry
*entry
= ht
->table
+ hash_address
;
220 if (entry_is_free(entry
)) {
222 } else if (entry_is_present(entry
) && entry
->hash
== hash
) {
223 if (ht
->key_equals_function(key
, entry
->key
)) {
228 double_hash
= 1 + hash
% ht
->rehash
;
230 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
231 } while (hash_address
!= hash
% ht
->size
);
237 _mesa_set_search(const struct set
*set
, const void *key
)
239 assert(set
->key_hash_function
);
240 return set_search(set
, set
->key_hash_function(key
), key
);
244 _mesa_set_search_pre_hashed(const struct set
*set
, uint32_t hash
,
247 assert(set
->key_hash_function
== NULL
||
248 hash
== set
->key_hash_function(key
));
249 return set_search(set
, hash
, key
);
252 static struct set_entry
*
253 set_add(struct set
*ht
, uint32_t hash
, const void *key
);
256 set_rehash(struct set
*ht
, unsigned new_size_index
)
259 struct set_entry
*table
, *entry
;
261 if (new_size_index
>= ARRAY_SIZE(hash_sizes
))
264 table
= rzalloc_array(ht
, struct set_entry
,
265 hash_sizes
[new_size_index
].size
);
272 ht
->size_index
= new_size_index
;
273 ht
->size
= hash_sizes
[ht
->size_index
].size
;
274 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
275 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
277 ht
->deleted_entries
= 0;
279 set_foreach(&old_ht
, entry
) {
280 set_add(ht
, entry
->hash
, entry
->key
);
283 ralloc_free(old_ht
.table
);
287 * Inserts the key with the given hash into the table.
289 * Note that insertion may rearrange the table on a resize or rehash,
290 * so previously found hash_entries are no longer valid after this function.
292 static struct set_entry
*
293 set_add(struct set
*ht
, uint32_t hash
, const void *key
)
295 uint32_t hash_address
;
296 struct set_entry
*available_entry
= NULL
;
298 if (ht
->entries
>= ht
->max_entries
) {
299 set_rehash(ht
, ht
->size_index
+ 1);
300 } else if (ht
->deleted_entries
+ ht
->entries
>= ht
->max_entries
) {
301 set_rehash(ht
, ht
->size_index
);
304 hash_address
= hash
% ht
->size
;
306 struct set_entry
*entry
= ht
->table
+ hash_address
;
307 uint32_t double_hash
;
309 if (!entry_is_present(entry
)) {
310 /* Stash the first available entry we find */
311 if (available_entry
== NULL
)
312 available_entry
= entry
;
313 if (entry_is_free(entry
))
317 /* Implement replacement when another insert happens
318 * with a matching key. This is a relatively common
319 * feature of hash tables, with the alternative
320 * generally being "insert the new value as well, and
321 * return it first when the key is searched for".
323 * Note that the hash table doesn't have a delete callback.
324 * If freeing of old keys is required to avoid memory leaks,
325 * perform a search before inserting.
327 if (!entry_is_deleted(entry
) &&
328 entry
->hash
== hash
&&
329 ht
->key_equals_function(key
, entry
->key
)) {
334 double_hash
= 1 + hash
% ht
->rehash
;
336 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
337 } while (hash_address
!= hash
% ht
->size
);
339 if (available_entry
) {
340 if (entry_is_deleted(available_entry
))
341 ht
->deleted_entries
--;
342 available_entry
->hash
= hash
;
343 available_entry
->key
= key
;
345 return available_entry
;
348 /* We could hit here if a required resize failed. An unchecked-malloc
349 * application could ignore this result.
355 _mesa_set_add(struct set
*set
, const void *key
)
357 assert(set
->key_hash_function
);
358 return set_add(set
, set
->key_hash_function(key
), key
);
362 _mesa_set_add_pre_hashed(struct set
*set
, uint32_t hash
, const void *key
)
364 assert(set
->key_hash_function
== NULL
||
365 hash
== set
->key_hash_function(key
));
366 return set_add(set
, hash
, key
);
370 * This function deletes the given hash table entry.
372 * Note that deletion doesn't otherwise modify the table, so an iteration over
373 * the table deleting entries is safe.
376 _mesa_set_remove(struct set
*ht
, struct set_entry
*entry
)
381 entry
->key
= deleted_key
;
383 ht
->deleted_entries
++;
387 * Removes the entry with the corresponding key, if exists.
390 _mesa_set_remove_key(struct set
*set
, const void *key
)
392 _mesa_set_remove(set
, _mesa_set_search(set
, key
));
396 * This function is an iterator over the hash table.
398 * Pass in NULL for the first entry, as in the start of a for loop. Note that
399 * an iteration over the table is O(table_size) not O(entries).
402 _mesa_set_next_entry(const struct set
*ht
, struct set_entry
*entry
)
409 for (; entry
!= ht
->table
+ ht
->size
; entry
++) {
410 if (entry_is_present(entry
)) {
419 _mesa_set_random_entry(struct set
*ht
,
420 int (*predicate
)(struct set_entry
*entry
))
422 struct set_entry
*entry
;
423 uint32_t i
= rand() % ht
->size
;
425 if (ht
->entries
== 0)
428 for (entry
= ht
->table
+ i
; entry
!= ht
->table
+ ht
->size
; entry
++) {
429 if (entry_is_present(entry
) &&
430 (!predicate
|| predicate(entry
))) {
435 for (entry
= ht
->table
; entry
!= ht
->table
+ i
; entry
++) {
436 if (entry_is_present(entry
) &&
437 (!predicate
|| predicate(entry
))) {