util: Move main/set to util/hash_set
[mesa.git] / src / util / set.c
1 /*
2 * Copyright © 2009-2012 Intel Corporation
3 * Copyright © 1988-2004 Keith Packard and Bart Massey.
4 *
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:
11 *
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
14 * Software.
15 *
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
22 * IN THE SOFTWARE.
23 *
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
28 * authors.
29 *
30 * Authors:
31 * Eric Anholt <eric@anholt.net>
32 * Keith Packard <keithp@keithp.com>
33 */
34
35 #include <stdlib.h>
36
37 #include "macros.h"
38 #include "ralloc.h"
39 #include "set.h"
40
41 /*
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
45 */
46
47 uint32_t deleted_key_value;
48 const void *deleted_key = &deleted_key_value;
49
50 static const struct {
51 uint32_t max_entries, size, rehash;
52 } hash_sizes[] = {
53 { 2, 5, 3 },
54 { 4, 7, 5 },
55 { 8, 13, 11 },
56 { 16, 19, 17 },
57 { 32, 43, 41 },
58 { 64, 73, 71 },
59 { 128, 151, 149 },
60 { 256, 283, 281 },
61 { 512, 571, 569 },
62 { 1024, 1153, 1151 },
63 { 2048, 2269, 2267 },
64 { 4096, 4519, 4517 },
65 { 8192, 9013, 9011 },
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 }
84 };
85
86 static int
87 entry_is_free(struct set_entry *entry)
88 {
89 return entry->key == NULL;
90 }
91
92 static int
93 entry_is_deleted(struct set_entry *entry)
94 {
95 return entry->key == deleted_key;
96 }
97
98 static int
99 entry_is_present(struct set_entry *entry)
100 {
101 return entry->key != NULL && entry->key != deleted_key;
102 }
103
104 struct set *
105 _mesa_set_create(void *mem_ctx,
106 bool (*key_equals_function)(const void *a,
107 const void *b))
108 {
109 struct set *ht;
110
111 ht = ralloc(mem_ctx, struct set);
112 if (ht == NULL)
113 return NULL;
114
115 ht->size_index = 0;
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);
121 ht->entries = 0;
122 ht->deleted_entries = 0;
123
124 if (ht->table == NULL) {
125 ralloc_free(ht);
126 return NULL;
127 }
128
129 return ht;
130 }
131
132 /**
133 * Frees the given set.
134 *
135 * If delete_function is passed, it gets called on each entry present before
136 * freeing.
137 */
138 void
139 _mesa_set_destroy(struct set *ht, void (*delete_function)(struct set_entry *entry))
140 {
141 if (!ht)
142 return;
143
144 if (delete_function) {
145 struct set_entry *entry;
146
147 set_foreach (ht, entry) {
148 delete_function(entry);
149 }
150 }
151 ralloc_free(ht->table);
152 ralloc_free(ht);
153 }
154
155 /**
156 * Finds a set entry with the given key and hash of that key.
157 *
158 * Returns NULL if no entry is found.
159 */
160 struct set_entry *
161 _mesa_set_search(const struct set *ht, uint32_t hash, const void *key)
162 {
163 uint32_t hash_address;
164
165 hash_address = hash % ht->size;
166 do {
167 uint32_t double_hash;
168
169 struct set_entry *entry = ht->table + hash_address;
170
171 if (entry_is_free(entry)) {
172 return NULL;
173 } else if (entry_is_present(entry) && entry->hash == hash) {
174 if (ht->key_equals_function(key, entry->key)) {
175 return entry;
176 }
177 }
178
179 double_hash = 1 + hash % ht->rehash;
180
181 hash_address = (hash_address + double_hash) % ht->size;
182 } while (hash_address != hash % ht->size);
183
184 return NULL;
185 }
186
187 static void
188 set_rehash(struct set *ht, int new_size_index)
189 {
190 struct set old_ht;
191 struct set_entry *table, *entry;
192
193 if (new_size_index >= ARRAY_SIZE(hash_sizes))
194 return;
195
196 table = rzalloc_array(ht, struct set_entry,
197 hash_sizes[new_size_index].size);
198 if (table == NULL)
199 return;
200
201 old_ht = *ht;
202
203 ht->table = table;
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;
208 ht->entries = 0;
209 ht->deleted_entries = 0;
210
211 for (entry = old_ht.table;
212 entry != old_ht.table + old_ht.size;
213 entry++) {
214 if (entry_is_present(entry)) {
215 _mesa_set_add(ht, entry->hash, entry->key);
216 }
217 }
218
219 ralloc_free(old_ht.table);
220 }
221
222 /**
223 * Inserts the key with the given hash into the table.
224 *
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.
227 */
228 struct set_entry *
229 _mesa_set_add(struct set *ht, uint32_t hash, const void *key)
230 {
231 uint32_t hash_address;
232
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);
237 }
238
239 hash_address = hash % ht->size;
240 do {
241 struct set_entry *entry = ht->table + hash_address;
242 uint32_t double_hash;
243
244 if (!entry_is_present(entry)) {
245 if (entry_is_deleted(entry))
246 ht->deleted_entries--;
247 entry->hash = hash;
248 entry->key = key;
249 ht->entries++;
250 return entry;
251 }
252
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".
258 *
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.
262 */
263 if (entry->hash == hash &&
264 ht->key_equals_function(key, entry->key)) {
265 entry->key = key;
266 return entry;
267 }
268
269 double_hash = 1 + hash % ht->rehash;
270
271 hash_address = (hash_address + double_hash) % ht->size;
272 } while (hash_address != hash % ht->size);
273
274 /* We could hit here if a required resize failed. An unchecked-malloc
275 * application could ignore this result.
276 */
277 return NULL;
278 }
279
280 /**
281 * This function deletes the given hash table entry.
282 *
283 * Note that deletion doesn't otherwise modify the table, so an iteration over
284 * the table deleting entries is safe.
285 */
286 void
287 _mesa_set_remove(struct set *ht, struct set_entry *entry)
288 {
289 if (!entry)
290 return;
291
292 entry->key = deleted_key;
293 ht->entries--;
294 ht->deleted_entries++;
295 }
296
297 /**
298 * This function is an iterator over the hash table.
299 *
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).
302 */
303 struct set_entry *
304 _mesa_set_next_entry(const struct set *ht, struct set_entry *entry)
305 {
306 if (entry == NULL)
307 entry = ht->table;
308 else
309 entry = entry + 1;
310
311 for (; entry != ht->table + ht->size; entry++) {
312 if (entry_is_present(entry)) {
313 return entry;
314 }
315 }
316
317 return NULL;
318 }
319
320 struct set_entry *
321 _mesa_set_random_entry(struct set *ht,
322 int (*predicate)(struct set_entry *entry))
323 {
324 struct set_entry *entry;
325 uint32_t i = rand() % ht->size;
326
327 if (ht->entries == 0)
328 return NULL;
329
330 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
331 if (entry_is_present(entry) &&
332 (!predicate || predicate(entry))) {
333 return entry;
334 }
335 }
336
337 for (entry = ht->table; entry != ht->table + i; entry++) {
338 if (entry_is_present(entry) &&
339 (!predicate || predicate(entry))) {
340 return entry;
341 }
342 }
343
344 return NULL;
345 }