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