mesa: Import a copy of the open-addressing hash table code I wrote.
[mesa.git] / src / mesa / main / hash_table.h
1 /*
2 * Copyright © 2009,2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include <inttypes.h>
29 #include <stdbool.h>
30
31 #ifndef _HASH_TABLE_H
32 #define _HASH_TABLE_H
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 struct hash_entry {
39 uint32_t hash;
40 const void *key;
41 void *data;
42 };
43
44 struct hash_table {
45 void *mem_ctx;
46 struct hash_entry *table;
47 bool (*key_equals_function)(const void *a, const void *b);
48 const void *deleted_key;
49 uint32_t size;
50 uint32_t rehash;
51 uint32_t max_entries;
52 uint32_t size_index;
53 uint32_t entries;
54 uint32_t deleted_entries;
55 };
56
57 struct hash_table *
58 _mesa_hash_table_create(void *mem_ctx,
59 bool (*key_equals_function)(const void *a,
60 const void *b));
61 void _mesa_hash_table_destroy(struct hash_table *ht,
62 void (*delete_function)(struct hash_entry *entry));
63 void _mesa_hash_table_set_deleted_key(struct hash_table *ht,
64 const void *deleted_key);
65
66 struct hash_entry *
67 _mesa_hash_table_insert(struct hash_table *ht, uint32_t hash,
68 const void *key, void *data);
69 struct hash_entry *
70 _mesa_hash_table_search(struct hash_table *ht, uint32_t hash,
71 const void *key);
72 void _mesa_hash_table_remove(struct hash_table *ht,
73 struct hash_entry *entry);
74
75 struct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht,
76 struct hash_entry *entry);
77 struct hash_entry *
78 _mesa_hash_table_random_entry(struct hash_table *ht,
79 bool (*predicate)(struct hash_entry *entry));
80
81 uint32_t _mesa_hash_data(const void *data, size_t size);
82 uint32_t _mesa_hash_string(const char *key);
83 bool _mesa_key_string_equal(const void *a, const void *b);
84 bool _mesa_key_pointer_equal(const void *a, const void *b);
85
86 static inline uint32_t _mesa_hash_pointer(const void *pointer)
87 {
88 return _mesa_hash_data(&pointer, sizeof(pointer));
89 }
90
91 /**
92 * This foreach function is safe against deletion (which just replaces
93 * an entry's data with the deleted marker), but not against insertion
94 * (which may rehash the table, making entry a dangling pointer).
95 */
96 #define hash_table_foreach(ht, entry) \
97 for (entry = _mesa_hash_table_next_entry(ht, NULL); \
98 entry != NULL; \
99 entry = _mesa_hash_table_next_entry(ht, entry))
100
101 #ifdef __cplusplus
102 } /* extern C */
103 #endif
104
105 #endif /* _HASH_TABLE_H */