set.c revision 01e04c3f
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
49static const uint32_t deleted_key_value;
50static const void *deleted_key = &deleted_key_value;
51
52static 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
88static int
89entry_is_free(struct set_entry *entry)
90{
91   return entry->key == NULL;
92}
93
94static int
95entry_is_deleted(struct set_entry *entry)
96{
97   return entry->key == deleted_key;
98}
99
100static int
101entry_is_present(struct set_entry *entry)
102{
103   return entry->key != NULL && entry->key != deleted_key;
104}
105
106struct 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
136struct 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 */
164void
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      set_foreach (ht, entry) {
172         delete_function(entry);
173      }
174   }
175   ralloc_free(ht->table);
176   ralloc_free(ht);
177}
178
179/**
180 * Clears all values from the given set.
181 *
182 * If delete_function is passed, it gets called on each entry present before
183 * the set is cleared.
184 */
185void
186_mesa_set_clear(struct set *set, void (*delete_function)(struct set_entry *entry))
187{
188   if (!set)
189      return;
190
191   set_foreach (set, entry) {
192      if (delete_function)
193         delete_function(entry);
194      entry->key = deleted_key;
195   }
196
197   set->entries = set->deleted_entries = 0;
198}
199
200/**
201 * Finds a set entry with the given key and hash of that key.
202 *
203 * Returns NULL if no entry is found.
204 */
205static struct set_entry *
206set_search(const struct set *ht, uint32_t hash, const void *key)
207{
208   uint32_t hash_address;
209
210   hash_address = hash % ht->size;
211   do {
212      uint32_t double_hash;
213
214      struct set_entry *entry = ht->table + hash_address;
215
216      if (entry_is_free(entry)) {
217         return NULL;
218      } else if (entry_is_present(entry) && entry->hash == hash) {
219         if (ht->key_equals_function(key, entry->key)) {
220            return entry;
221         }
222      }
223
224      double_hash = 1 + hash % ht->rehash;
225
226      hash_address = (hash_address + double_hash) % ht->size;
227   } while (hash_address != hash % ht->size);
228
229   return NULL;
230}
231
232struct set_entry *
233_mesa_set_search(const struct set *set, const void *key)
234{
235   assert(set->key_hash_function);
236   return set_search(set, set->key_hash_function(key), key);
237}
238
239struct set_entry *
240_mesa_set_search_pre_hashed(const struct set *set, uint32_t hash,
241                            const void *key)
242{
243   assert(set->key_hash_function == NULL ||
244          hash == set->key_hash_function(key));
245   return set_search(set, hash, key);
246}
247
248static struct set_entry *
249set_add(struct set *ht, uint32_t hash, const void *key);
250
251static void
252set_rehash(struct set *ht, unsigned new_size_index)
253{
254   struct set old_ht;
255   struct set_entry *table;
256
257   if (new_size_index >= ARRAY_SIZE(hash_sizes))
258      return;
259
260   table = rzalloc_array(ht, struct set_entry,
261                         hash_sizes[new_size_index].size);
262   if (table == NULL)
263      return;
264
265   old_ht = *ht;
266
267   ht->table = table;
268   ht->size_index = new_size_index;
269   ht->size = hash_sizes[ht->size_index].size;
270   ht->rehash = hash_sizes[ht->size_index].rehash;
271   ht->max_entries = hash_sizes[ht->size_index].max_entries;
272   ht->entries = 0;
273   ht->deleted_entries = 0;
274
275   set_foreach(&old_ht, entry) {
276      set_add(ht, entry->hash, entry->key);
277   }
278
279   ralloc_free(old_ht.table);
280}
281
282/**
283 * Inserts the key with the given hash into the table.
284 *
285 * Note that insertion may rearrange the table on a resize or rehash,
286 * so previously found hash_entries are no longer valid after this function.
287 */
288static struct set_entry *
289set_add(struct set *ht, uint32_t hash, const void *key)
290{
291   uint32_t hash_address;
292   struct set_entry *available_entry = NULL;
293
294   if (ht->entries >= ht->max_entries) {
295      set_rehash(ht, ht->size_index + 1);
296   } else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
297      set_rehash(ht, ht->size_index);
298   }
299
300   hash_address = hash % ht->size;
301   do {
302      struct set_entry *entry = ht->table + hash_address;
303      uint32_t double_hash;
304
305      if (!entry_is_present(entry)) {
306         /* Stash the first available entry we find */
307         if (available_entry == NULL)
308            available_entry = entry;
309         if (entry_is_free(entry))
310            break;
311      }
312
313      /* Implement replacement when another insert happens
314       * with a matching key.  This is a relatively common
315       * feature of hash tables, with the alternative
316       * generally being "insert the new value as well, and
317       * return it first when the key is searched for".
318       *
319       * Note that the hash table doesn't have a delete callback.
320       * If freeing of old keys is required to avoid memory leaks,
321       * perform a search before inserting.
322       */
323      if (!entry_is_deleted(entry) &&
324          entry->hash == hash &&
325          ht->key_equals_function(key, entry->key)) {
326         entry->key = key;
327         return entry;
328      }
329
330      double_hash = 1 + hash % ht->rehash;
331
332      hash_address = (hash_address + double_hash) % ht->size;
333   } while (hash_address != hash % ht->size);
334
335   if (available_entry) {
336      if (entry_is_deleted(available_entry))
337         ht->deleted_entries--;
338      available_entry->hash = hash;
339      available_entry->key = key;
340      ht->entries++;
341      return available_entry;
342   }
343
344   /* We could hit here if a required resize failed. An unchecked-malloc
345    * application could ignore this result.
346    */
347   return NULL;
348}
349
350struct set_entry *
351_mesa_set_add(struct set *set, const void *key)
352{
353   assert(set->key_hash_function);
354   return set_add(set, set->key_hash_function(key), key);
355}
356
357struct set_entry *
358_mesa_set_add_pre_hashed(struct set *set, uint32_t hash, const void *key)
359{
360   assert(set->key_hash_function == NULL ||
361          hash == set->key_hash_function(key));
362   return set_add(set, hash, key);
363}
364
365/**
366 * This function deletes the given hash table entry.
367 *
368 * Note that deletion doesn't otherwise modify the table, so an iteration over
369 * the table deleting entries is safe.
370 */
371void
372_mesa_set_remove(struct set *ht, struct set_entry *entry)
373{
374   if (!entry)
375      return;
376
377   entry->key = deleted_key;
378   ht->entries--;
379   ht->deleted_entries++;
380}
381
382/**
383 * Removes the entry with the corresponding key, if exists.
384 */
385void
386_mesa_set_remove_key(struct set *set, const void *key)
387{
388   _mesa_set_remove(set, _mesa_set_search(set, key));
389}
390
391/**
392 * This function is an iterator over the hash table.
393 *
394 * Pass in NULL for the first entry, as in the start of a for loop.  Note that
395 * an iteration over the table is O(table_size) not O(entries).
396 */
397struct set_entry *
398_mesa_set_next_entry(const struct set *ht, struct set_entry *entry)
399{
400   if (entry == NULL)
401      entry = ht->table;
402   else
403      entry = entry + 1;
404
405   for (; entry != ht->table + ht->size; entry++) {
406      if (entry_is_present(entry)) {
407         return entry;
408      }
409   }
410
411   return NULL;
412}
413
414struct set_entry *
415_mesa_set_random_entry(struct set *ht,
416                       int (*predicate)(struct set_entry *entry))
417{
418   struct set_entry *entry;
419   uint32_t i = rand() % ht->size;
420
421   if (ht->entries == 0)
422      return NULL;
423
424   for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
425      if (entry_is_present(entry) &&
426          (!predicate || predicate(entry))) {
427         return entry;
428      }
429   }
430
431   for (entry = ht->table; entry != ht->table + i; entry++) {
432      if (entry_is_present(entry) &&
433          (!predicate || predicate(entry))) {
434         return entry;
435      }
436   }
437
438   return NULL;
439}
440