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>
43 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
44 * p and p-2 are both prime. These tables are sized to have an extra 10%
45 * free to avoid exponential performance degradation as the hash table fills
48 static const uint32_t deleted_key_value
;
49 static const void *deleted_key
= &deleted_key_value
;
52 uint32_t max_entries
, size
, rehash
;
67 { 16384, 18043, 18041 },
68 { 32768, 36109, 36107 },
69 { 65536, 72091, 72089 },
70 { 131072, 144409, 144407 },
71 { 262144, 288361, 288359 },
72 { 524288, 576883, 576881 },
73 { 1048576, 1153459, 1153457 },
74 { 2097152, 2307163, 2307161 },
75 { 4194304, 4613893, 4613891 },
76 { 8388608, 9227641, 9227639 },
77 { 16777216, 18455029, 18455027 },
78 { 33554432, 36911011, 36911009 },
79 { 67108864, 73819861, 73819859 },
80 { 134217728, 147639589, 147639587 },
81 { 268435456, 295279081, 295279079 },
82 { 536870912, 590559793, 590559791 },
83 { 1073741824, 1181116273, 1181116271 },
84 { 2147483648ul, 2362232233ul, 2362232231ul }
88 entry_is_free(struct set_entry
*entry
)
90 return entry
->key
== NULL
;
94 entry_is_deleted(struct set_entry
*entry
)
96 return entry
->key
== deleted_key
;
100 entry_is_present(struct set_entry
*entry
)
102 return entry
->key
!= NULL
&& entry
->key
!= deleted_key
;
106 _mesa_set_create(void *mem_ctx
,
107 uint32_t (*key_hash_function
)(const void *key
),
108 bool (*key_equals_function
)(const void *a
,
113 ht
= ralloc(mem_ctx
, struct set
);
118 ht
->size
= hash_sizes
[ht
->size_index
].size
;
119 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
120 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
121 ht
->key_hash_function
= key_hash_function
;
122 ht
->key_equals_function
= key_equals_function
;
123 ht
->table
= rzalloc_array(ht
, struct set_entry
, ht
->size
);
125 ht
->deleted_entries
= 0;
127 if (ht
->table
== NULL
) {
136 * Frees the given set.
138 * If delete_function is passed, it gets called on each entry present before
142 _mesa_set_destroy(struct set
*ht
, void (*delete_function
)(struct set_entry
*entry
))
147 if (delete_function
) {
148 struct set_entry
*entry
;
150 set_foreach (ht
, entry
) {
151 delete_function(entry
);
154 ralloc_free(ht
->table
);
159 * Finds a set entry with the given key and hash of that key.
161 * Returns NULL if no entry is found.
163 static struct set_entry
*
164 set_search(const struct set
*ht
, uint32_t hash
, const void *key
)
166 uint32_t hash_address
;
168 hash_address
= hash
% ht
->size
;
170 uint32_t double_hash
;
172 struct set_entry
*entry
= ht
->table
+ hash_address
;
174 if (entry_is_free(entry
)) {
176 } else if (entry_is_present(entry
) && entry
->hash
== hash
) {
177 if (ht
->key_equals_function(key
, entry
->key
)) {
182 double_hash
= 1 + hash
% ht
->rehash
;
184 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
185 } while (hash_address
!= hash
% ht
->size
);
191 _mesa_set_search(const struct set
*set
, const void *key
)
193 assert(set
->key_hash_function
);
194 return set_search(set
, set
->key_hash_function(key
), key
);
198 _mesa_set_search_pre_hashed(const struct set
*set
, uint32_t hash
,
201 assert(set
->key_hash_function
== NULL
||
202 hash
== set
->key_hash_function(key
));
203 return set_search(set
, hash
, key
);
206 static struct set_entry
*
207 set_add(struct set
*ht
, uint32_t hash
, const void *key
);
210 set_rehash(struct set
*ht
, unsigned new_size_index
)
213 struct set_entry
*table
, *entry
;
215 if (new_size_index
>= ARRAY_SIZE(hash_sizes
))
218 table
= rzalloc_array(ht
, struct set_entry
,
219 hash_sizes
[new_size_index
].size
);
226 ht
->size_index
= new_size_index
;
227 ht
->size
= hash_sizes
[ht
->size_index
].size
;
228 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
229 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
231 ht
->deleted_entries
= 0;
233 set_foreach(&old_ht
, entry
) {
234 set_add(ht
, entry
->hash
, entry
->key
);
237 ralloc_free(old_ht
.table
);
241 * Inserts the key with the given hash into the table.
243 * Note that insertion may rearrange the table on a resize or rehash,
244 * so previously found hash_entries are no longer valid after this function.
246 static struct set_entry
*
247 set_add(struct set
*ht
, uint32_t hash
, const void *key
)
249 uint32_t hash_address
;
250 struct set_entry
*available_entry
= NULL
;
252 if (ht
->entries
>= ht
->max_entries
) {
253 set_rehash(ht
, ht
->size_index
+ 1);
254 } else if (ht
->deleted_entries
+ ht
->entries
>= ht
->max_entries
) {
255 set_rehash(ht
, ht
->size_index
);
258 hash_address
= hash
% ht
->size
;
260 struct set_entry
*entry
= ht
->table
+ hash_address
;
261 uint32_t double_hash
;
263 if (!entry_is_present(entry
)) {
264 /* Stash the first available entry we find */
265 if (available_entry
== NULL
)
266 available_entry
= entry
;
267 if (entry_is_free(entry
))
271 /* Implement replacement when another insert happens
272 * with a matching key. This is a relatively common
273 * feature of hash tables, with the alternative
274 * generally being "insert the new value as well, and
275 * return it first when the key is searched for".
277 * Note that the hash table doesn't have a delete callback.
278 * If freeing of old keys is required to avoid memory leaks,
279 * perform a search before inserting.
281 if (!entry_is_deleted(entry
) &&
282 entry
->hash
== hash
&&
283 ht
->key_equals_function(key
, entry
->key
)) {
288 double_hash
= 1 + hash
% ht
->rehash
;
290 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
291 } while (hash_address
!= hash
% ht
->size
);
293 if (available_entry
) {
294 if (entry_is_deleted(available_entry
))
295 ht
->deleted_entries
--;
296 available_entry
->hash
= hash
;
297 available_entry
->key
= key
;
299 return available_entry
;
302 /* We could hit here if a required resize failed. An unchecked-malloc
303 * application could ignore this result.
309 _mesa_set_add(struct set
*set
, const void *key
)
311 assert(set
->key_hash_function
);
312 return set_add(set
, set
->key_hash_function(key
), key
);
316 _mesa_set_add_pre_hashed(struct set
*set
, uint32_t hash
, const void *key
)
318 assert(set
->key_hash_function
== NULL
||
319 hash
== set
->key_hash_function(key
));
320 return set_add(set
, hash
, key
);
324 * This function deletes the given hash table entry.
326 * Note that deletion doesn't otherwise modify the table, so an iteration over
327 * the table deleting entries is safe.
330 _mesa_set_remove(struct set
*ht
, struct set_entry
*entry
)
335 entry
->key
= deleted_key
;
337 ht
->deleted_entries
++;
341 * This function is an iterator over the hash table.
343 * Pass in NULL for the first entry, as in the start of a for loop. Note that
344 * an iteration over the table is O(table_size) not O(entries).
347 _mesa_set_next_entry(const struct set
*ht
, struct set_entry
*entry
)
354 for (; entry
!= ht
->table
+ ht
->size
; entry
++) {
355 if (entry_is_present(entry
)) {
364 _mesa_set_random_entry(struct set
*ht
,
365 int (*predicate
)(struct set_entry
*entry
))
367 struct set_entry
*entry
;
368 uint32_t i
= rand() % ht
->size
;
370 if (ht
->entries
== 0)
373 for (entry
= ht
->table
+ i
; entry
!= ht
->table
+ ht
->size
; entry
++) {
374 if (entry_is_present(entry
) &&
375 (!predicate
|| predicate(entry
))) {
380 for (entry
= ht
->table
; entry
!= ht
->table
+ i
; entry
++) {
381 if (entry_is_present(entry
) &&
382 (!predicate
|| predicate(entry
))) {