util: Helper to create sets and hashes with pointer keys
[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 #include <assert.h>
37 #include <string.h>
38
39 #include "hash_table.h"
40 #include "macros.h"
41 #include "ralloc.h"
42 #include "set.h"
43
44 /*
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
48 */
49
50 static const uint32_t deleted_key_value;
51 static const void *deleted_key = &deleted_key_value;
52
53 static const struct {
54 uint32_t max_entries, size, rehash;
55 } hash_sizes[] = {
56 { 2, 5, 3 },
57 { 4, 7, 5 },
58 { 8, 13, 11 },
59 { 16, 19, 17 },
60 { 32, 43, 41 },
61 { 64, 73, 71 },
62 { 128, 151, 149 },
63 { 256, 283, 281 },
64 { 512, 571, 569 },
65 { 1024, 1153, 1151 },
66 { 2048, 2269, 2267 },
67 { 4096, 4519, 4517 },
68 { 8192, 9013, 9011 },
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 }
87 };
88
89 static int
90 entry_is_free(struct set_entry *entry)
91 {
92 return entry->key == NULL;
93 }
94
95 static int
96 entry_is_deleted(struct set_entry *entry)
97 {
98 return entry->key == deleted_key;
99 }
100
101 static int
102 entry_is_present(struct set_entry *entry)
103 {
104 return entry->key != NULL && entry->key != deleted_key;
105 }
106
107 struct set *
108 _mesa_set_create(void *mem_ctx,
109 uint32_t (*key_hash_function)(const void *key),
110 bool (*key_equals_function)(const void *a,
111 const void *b))
112 {
113 struct set *ht;
114
115 ht = ralloc(mem_ctx, struct set);
116 if (ht == NULL)
117 return NULL;
118
119 ht->size_index = 0;
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);
126 ht->entries = 0;
127 ht->deleted_entries = 0;
128
129 if (ht->table == NULL) {
130 ralloc_free(ht);
131 return NULL;
132 }
133
134 return ht;
135 }
136
137 struct set *
138 _mesa_set_clone(struct set *set, void *dst_mem_ctx)
139 {
140 struct set *clone;
141
142 clone = ralloc(dst_mem_ctx, struct set);
143 if (clone == NULL)
144 return NULL;
145
146 memcpy(clone, set, sizeof(struct set));
147
148 clone->table = ralloc_array(clone, struct set_entry, clone->size);
149 if (clone->table == NULL) {
150 ralloc_free(clone);
151 return NULL;
152 }
153
154 memcpy(clone->table, set->table, clone->size * sizeof(struct set_entry));
155
156 return clone;
157 }
158
159 /**
160 * Frees the given set.
161 *
162 * If delete_function is passed, it gets called on each entry present before
163 * freeing.
164 */
165 void
166 _mesa_set_destroy(struct set *ht, void (*delete_function)(struct set_entry *entry))
167 {
168 if (!ht)
169 return;
170
171 if (delete_function) {
172 set_foreach (ht, entry) {
173 delete_function(entry);
174 }
175 }
176 ralloc_free(ht->table);
177 ralloc_free(ht);
178 }
179
180 /**
181 * Clears all values from the given set.
182 *
183 * If delete_function is passed, it gets called on each entry present before
184 * the set is cleared.
185 */
186 void
187 _mesa_set_clear(struct set *set, void (*delete_function)(struct set_entry *entry))
188 {
189 if (!set)
190 return;
191
192 set_foreach (set, entry) {
193 if (delete_function)
194 delete_function(entry);
195 entry->key = deleted_key;
196 }
197
198 set->entries = set->deleted_entries = 0;
199 }
200
201 /**
202 * Finds a set entry with the given key and hash of that key.
203 *
204 * Returns NULL if no entry is found.
205 */
206 static struct set_entry *
207 set_search(const struct set *ht, uint32_t hash, const void *key)
208 {
209 uint32_t hash_address;
210
211 hash_address = hash % ht->size;
212 do {
213 uint32_t double_hash;
214
215 struct set_entry *entry = ht->table + hash_address;
216
217 if (entry_is_free(entry)) {
218 return NULL;
219 } else if (entry_is_present(entry) && entry->hash == hash) {
220 if (ht->key_equals_function(key, entry->key)) {
221 return entry;
222 }
223 }
224
225 double_hash = 1 + hash % ht->rehash;
226
227 hash_address = (hash_address + double_hash) % ht->size;
228 } while (hash_address != hash % ht->size);
229
230 return NULL;
231 }
232
233 struct set_entry *
234 _mesa_set_search(const struct set *set, const void *key)
235 {
236 assert(set->key_hash_function);
237 return set_search(set, set->key_hash_function(key), key);
238 }
239
240 struct set_entry *
241 _mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
242 const void *key)
243 {
244 assert(set->key_hash_function == NULL ||
245 hash == set->key_hash_function(key));
246 return set_search(set, hash, key);
247 }
248
249 static struct set_entry *
250 set_add(struct set *ht, uint32_t hash, const void *key);
251
252 static void
253 set_rehash(struct set *ht, unsigned new_size_index)
254 {
255 struct set old_ht;
256 struct set_entry *table;
257
258 if (new_size_index >= ARRAY_SIZE(hash_sizes))
259 return;
260
261 table = rzalloc_array(ht, struct set_entry,
262 hash_sizes[new_size_index].size);
263 if (table == NULL)
264 return;
265
266 old_ht = *ht;
267
268 ht->table = table;
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;
273 ht->entries = 0;
274 ht->deleted_entries = 0;
275
276 set_foreach(&old_ht, entry) {
277 set_add(ht, entry->hash, entry->key);
278 }
279
280 ralloc_free(old_ht.table);
281 }
282
283 /**
284 * Inserts the key with the given hash into the table.
285 *
286 * Note that insertion may rearrange the table on a resize or rehash,
287 * so previously found hash_entries are no longer valid after this function.
288 */
289 static struct set_entry *
290 set_add(struct set *ht, uint32_t hash, const void *key)
291 {
292 uint32_t hash_address;
293 struct set_entry *available_entry = NULL;
294
295 if (ht->entries >= ht->max_entries) {
296 set_rehash(ht, ht->size_index + 1);
297 } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
298 set_rehash(ht, ht->size_index);
299 }
300
301 hash_address = hash % ht->size;
302 do {
303 struct set_entry *entry = ht->table + hash_address;
304 uint32_t double_hash;
305
306 if (!entry_is_present(entry)) {
307 /* Stash the first available entry we find */
308 if (available_entry == NULL)
309 available_entry = entry;
310 if (entry_is_free(entry))
311 break;
312 }
313
314 /* Implement replacement when another insert happens
315 * with a matching key. This is a relatively common
316 * feature of hash tables, with the alternative
317 * generally being "insert the new value as well, and
318 * return it first when the key is searched for".
319 *
320 * Note that the hash table doesn't have a delete callback.
321 * If freeing of old keys is required to avoid memory leaks,
322 * perform a search before inserting.
323 */
324 if (!entry_is_deleted(entry) &&
325 entry->hash == hash &&
326 ht->key_equals_function(key, entry->key)) {
327 entry->key = key;
328 return entry;
329 }
330
331 double_hash = 1 + hash % ht->rehash;
332
333 hash_address = (hash_address + double_hash) % ht->size;
334 } while (hash_address != hash % ht->size);
335
336 if (available_entry) {
337 if (entry_is_deleted(available_entry))
338 ht->deleted_entries--;
339 available_entry->hash = hash;
340 available_entry->key = key;
341 ht->entries++;
342 return available_entry;
343 }
344
345 /* We could hit here if a required resize failed. An unchecked-malloc
346 * application could ignore this result.
347 */
348 return NULL;
349 }
350
351 struct set_entry *
352 _mesa_set_add(struct set *set, const void *key)
353 {
354 assert(set->key_hash_function);
355 return set_add(set, set->key_hash_function(key), key);
356 }
357
358 struct set_entry *
359 _mesa_set_add_pre_hashed(struct set *set, uint32_t hash, const void *key)
360 {
361 assert(set->key_hash_function == NULL ||
362 hash == set->key_hash_function(key));
363 return set_add(set, hash, key);
364 }
365
366 /**
367 * This function deletes the given hash table entry.
368 *
369 * Note that deletion doesn't otherwise modify the table, so an iteration over
370 * the table deleting entries is safe.
371 */
372 void
373 _mesa_set_remove(struct set *ht, struct set_entry *entry)
374 {
375 if (!entry)
376 return;
377
378 entry->key = deleted_key;
379 ht->entries--;
380 ht->deleted_entries++;
381 }
382
383 /**
384 * Removes the entry with the corresponding key, if exists.
385 */
386 void
387 _mesa_set_remove_key(struct set *set, const void *key)
388 {
389 _mesa_set_remove(set, _mesa_set_search(set, key));
390 }
391
392 /**
393 * This function is an iterator over the hash table.
394 *
395 * Pass in NULL for the first entry, as in the start of a for loop. Note that
396 * an iteration over the table is O(table_size) not O(entries).
397 */
398 struct set_entry *
399 _mesa_set_next_entry(const struct set *ht, struct set_entry *entry)
400 {
401 if (entry == NULL)
402 entry = ht->table;
403 else
404 entry = entry + 1;
405
406 for (; entry != ht->table + ht->size; entry++) {
407 if (entry_is_present(entry)) {
408 return entry;
409 }
410 }
411
412 return NULL;
413 }
414
415 struct set_entry *
416 _mesa_set_random_entry(struct set *ht,
417 int (*predicate)(struct set_entry *entry))
418 {
419 struct set_entry *entry;
420 uint32_t i = rand() % ht->size;
421
422 if (ht->entries == 0)
423 return NULL;
424
425 for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
426 if (entry_is_present(entry) &&
427 (!predicate || predicate(entry))) {
428 return entry;
429 }
430 }
431
432 for (entry = ht->table; entry != ht->table + i; entry++) {
433 if (entry_is_present(entry) &&
434 (!predicate || predicate(entry))) {
435 return entry;
436 }
437 }
438
439 return NULL;
440 }
441
442 /**
443 * Helper to create a set with pointer keys.
444 */
445 struct set *
446 _mesa_pointer_set_create(void *mem_ctx)
447 {
448 return _mesa_set_create(mem_ctx, _mesa_hash_pointer,
449 _mesa_key_pointer_equal);
450 }