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>
42 * From Knuth -- a good choice for hash/rehash values is p, p-2 where
43 * p and p-2 are both prime. These tables are sized to have an extra 10%
44 * free to avoid exponential performance degradation as the hash table fills
47 uint32_t deleted_key_value
;
48 const void *deleted_key
= &deleted_key_value
;
51 uint32_t max_entries
, size
, rehash
;
66 { 16384, 18043, 18041 },
67 { 32768, 36109, 36107 },
68 { 65536, 72091, 72089 },
69 { 131072, 144409, 144407 },
70 { 262144, 288361, 288359 },
71 { 524288, 576883, 576881 },
72 { 1048576, 1153459, 1153457 },
73 { 2097152, 2307163, 2307161 },
74 { 4194304, 4613893, 4613891 },
75 { 8388608, 9227641, 9227639 },
76 { 16777216, 18455029, 18455027 },
77 { 33554432, 36911011, 36911009 },
78 { 67108864, 73819861, 73819859 },
79 { 134217728, 147639589, 147639587 },
80 { 268435456, 295279081, 295279079 },
81 { 536870912, 590559793, 590559791 },
82 { 1073741824, 1181116273, 1181116271 },
83 { 2147483648ul, 2362232233ul, 2362232231ul }
87 entry_is_free(struct set_entry
*entry
)
89 return entry
->key
== NULL
;
93 entry_is_deleted(struct set_entry
*entry
)
95 return entry
->key
== deleted_key
;
99 entry_is_present(struct set_entry
*entry
)
101 return entry
->key
!= NULL
&& entry
->key
!= deleted_key
;
105 _mesa_set_create(void *mem_ctx
,
106 bool (*key_equals_function
)(const void *a
,
111 ht
= ralloc(mem_ctx
, struct set
);
116 ht
->size
= hash_sizes
[ht
->size_index
].size
;
117 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
118 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
119 ht
->key_equals_function
= key_equals_function
;
120 ht
->table
= rzalloc_array(ht
, struct set_entry
, ht
->size
);
122 ht
->deleted_entries
= 0;
124 if (ht
->table
== NULL
) {
133 * Frees the given set.
135 * If delete_function is passed, it gets called on each entry present before
139 _mesa_set_destroy(struct set
*ht
, void (*delete_function
)(struct set_entry
*entry
))
144 if (delete_function
) {
145 struct set_entry
*entry
;
147 set_foreach (ht
, entry
) {
148 delete_function(entry
);
151 ralloc_free(ht
->table
);
156 * Finds a set entry with the given key and hash of that key.
158 * Returns NULL if no entry is found.
161 _mesa_set_search(const struct set
*ht
, uint32_t hash
, const void *key
)
163 uint32_t hash_address
;
165 hash_address
= hash
% ht
->size
;
167 uint32_t double_hash
;
169 struct set_entry
*entry
= ht
->table
+ hash_address
;
171 if (entry_is_free(entry
)) {
173 } else if (entry_is_present(entry
) && entry
->hash
== hash
) {
174 if (ht
->key_equals_function(key
, entry
->key
)) {
179 double_hash
= 1 + hash
% ht
->rehash
;
181 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
182 } while (hash_address
!= hash
% ht
->size
);
188 set_rehash(struct set
*ht
, int new_size_index
)
191 struct set_entry
*table
, *entry
;
193 if (new_size_index
>= ARRAY_SIZE(hash_sizes
))
196 table
= rzalloc_array(ht
, struct set_entry
,
197 hash_sizes
[new_size_index
].size
);
204 ht
->size_index
= new_size_index
;
205 ht
->size
= hash_sizes
[ht
->size_index
].size
;
206 ht
->rehash
= hash_sizes
[ht
->size_index
].rehash
;
207 ht
->max_entries
= hash_sizes
[ht
->size_index
].max_entries
;
209 ht
->deleted_entries
= 0;
211 for (entry
= old_ht
.table
;
212 entry
!= old_ht
.table
+ old_ht
.size
;
214 if (entry_is_present(entry
)) {
215 _mesa_set_add(ht
, entry
->hash
, entry
->key
);
219 ralloc_free(old_ht
.table
);
223 * Inserts the key with the given hash into the table.
225 * Note that insertion may rearrange the table on a resize or rehash,
226 * so previously found hash_entries are no longer valid after this function.
229 _mesa_set_add(struct set
*ht
, uint32_t hash
, const void *key
)
231 uint32_t hash_address
;
233 if (ht
->entries
>= ht
->max_entries
) {
234 set_rehash(ht
, ht
->size_index
+ 1);
235 } else if (ht
->deleted_entries
+ ht
->entries
>= ht
->max_entries
) {
236 set_rehash(ht
, ht
->size_index
);
239 hash_address
= hash
% ht
->size
;
241 struct set_entry
*entry
= ht
->table
+ hash_address
;
242 uint32_t double_hash
;
244 if (!entry_is_present(entry
)) {
245 if (entry_is_deleted(entry
))
246 ht
->deleted_entries
--;
253 /* Implement replacement when another insert happens
254 * with a matching key. This is a relatively common
255 * feature of hash tables, with the alternative
256 * generally being "insert the new value as well, and
257 * return it first when the key is searched for".
259 * Note that the hash table doesn't have a delete callback.
260 * If freeing of old keys is required to avoid memory leaks,
261 * perform a search before inserting.
263 if (entry
->hash
== hash
&&
264 ht
->key_equals_function(key
, entry
->key
)) {
269 double_hash
= 1 + hash
% ht
->rehash
;
271 hash_address
= (hash_address
+ double_hash
) % ht
->size
;
272 } while (hash_address
!= hash
% ht
->size
);
274 /* We could hit here if a required resize failed. An unchecked-malloc
275 * application could ignore this result.
281 * This function deletes the given hash table entry.
283 * Note that deletion doesn't otherwise modify the table, so an iteration over
284 * the table deleting entries is safe.
287 _mesa_set_remove(struct set
*ht
, struct set_entry
*entry
)
292 entry
->key
= deleted_key
;
294 ht
->deleted_entries
++;
298 * This function is an iterator over the hash table.
300 * Pass in NULL for the first entry, as in the start of a for loop. Note that
301 * an iteration over the table is O(table_size) not O(entries).
304 _mesa_set_next_entry(const struct set
*ht
, struct set_entry
*entry
)
311 for (; entry
!= ht
->table
+ ht
->size
; entry
++) {
312 if (entry_is_present(entry
)) {
321 _mesa_set_random_entry(struct set
*ht
,
322 int (*predicate
)(struct set_entry
*entry
))
324 struct set_entry
*entry
;
325 uint32_t i
= rand() % ht
->size
;
327 if (ht
->entries
== 0)
330 for (entry
= ht
->table
+ i
; entry
!= ht
->table
+ ht
->size
; entry
++) {
331 if (entry_is_present(entry
) &&
332 (!predicate
|| predicate(entry
))) {
337 for (entry
= ht
->table
; entry
!= ht
->table
+ i
; entry
++) {
338 if (entry_is_present(entry
) &&
339 (!predicate
|| predicate(entry
))) {