util: Helper to create sets and hashes with pointer keys
[mesa.git] / src / util / 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 #ifndef _HASH_TABLE_H
29 #define _HASH_TABLE_H
30
31 #include <stdlib.h>
32 #include <inttypes.h>
33 #include <stdbool.h>
34 #include "c99_compat.h"
35 #include "macros.h"
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 struct hash_entry {
42 uint32_t hash;
43 const void *key;
44 void *data;
45 };
46
47 struct hash_table {
48 struct hash_entry *table;
49 uint32_t (*key_hash_function)(const void *key);
50 bool (*key_equals_function)(const void *a, const void *b);
51 const void *deleted_key;
52 uint32_t size;
53 uint32_t rehash;
54 uint32_t max_entries;
55 uint32_t size_index;
56 uint32_t entries;
57 uint32_t deleted_entries;
58 };
59
60 struct hash_table *
61 _mesa_hash_table_create(void *mem_ctx,
62 uint32_t (*key_hash_function)(const void *key),
63 bool (*key_equals_function)(const void *a,
64 const void *b));
65
66 bool
67 _mesa_hash_table_init(struct hash_table *ht,
68 void *mem_ctx,
69 uint32_t (*key_hash_function)(const void *key),
70 bool (*key_equals_function)(const void *a,
71 const void *b));
72
73 struct hash_table *
74 _mesa_hash_table_clone(struct hash_table *src, void *dst_mem_ctx);
75 void _mesa_hash_table_destroy(struct hash_table *ht,
76 void (*delete_function)(struct hash_entry *entry));
77 void _mesa_hash_table_clear(struct hash_table *ht,
78 void (*delete_function)(struct hash_entry *entry));
79 void _mesa_hash_table_set_deleted_key(struct hash_table *ht,
80 const void *deleted_key);
81
82 static inline uint32_t _mesa_hash_table_num_entries(struct hash_table *ht)
83 {
84 return ht->entries;
85 }
86
87 struct hash_entry *
88 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data);
89 struct hash_entry *
90 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
91 const void *key, void *data);
92 struct hash_entry *
93 _mesa_hash_table_search(struct hash_table *ht, const void *key);
94 struct hash_entry *
95 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
96 const void *key);
97 void _mesa_hash_table_remove(struct hash_table *ht,
98 struct hash_entry *entry);
99 void _mesa_hash_table_remove_key(struct hash_table *ht,
100 const void *key);
101
102 struct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht,
103 struct hash_entry *entry);
104 struct hash_entry *
105 _mesa_hash_table_random_entry(struct hash_table *ht,
106 bool (*predicate)(struct hash_entry *entry));
107
108 uint32_t _mesa_hash_data(const void *data, size_t size);
109 uint32_t _mesa_hash_string(const void *key);
110 bool _mesa_key_string_equal(const void *a, const void *b);
111 bool _mesa_key_pointer_equal(const void *a, const void *b);
112
113 static inline uint32_t _mesa_key_hash_string(const void *key)
114 {
115 return _mesa_hash_string((const char *)key);
116 }
117
118 static inline uint32_t _mesa_hash_pointer(const void *pointer)
119 {
120 uintptr_t num = (uintptr_t) pointer;
121 return (uint32_t) ((num >> 2) ^ (num >> 6) ^ (num >> 10) ^ (num >> 14));
122 }
123
124 struct hash_table *
125 _mesa_pointer_hash_table_create(void *mem_ctx);
126
127 enum {
128 _mesa_fnv32_1a_offset_bias = 2166136261u,
129 };
130
131 static inline uint32_t
132 _mesa_fnv32_1a_accumulate_block(uint32_t hash, const void *data, size_t size)
133 {
134 const uint8_t *bytes = (const uint8_t *)data;
135
136 while (size-- != 0) {
137 hash ^= *bytes;
138 hash = hash * 0x01000193;
139 bytes++;
140 }
141
142 return hash;
143 }
144
145 #define _mesa_fnv32_1a_accumulate(hash, expr) \
146 _mesa_fnv32_1a_accumulate_block(hash, &(expr), sizeof(expr))
147
148 /**
149 * This foreach function is safe against deletion (which just replaces
150 * an entry's data with the deleted marker), but not against insertion
151 * (which may rehash the table, making entry a dangling pointer).
152 */
153 #define hash_table_foreach(ht, entry) \
154 for (struct hash_entry *entry = _mesa_hash_table_next_entry(ht, NULL); \
155 entry != NULL; \
156 entry = _mesa_hash_table_next_entry(ht, entry))
157
158 static inline void
159 hash_table_call_foreach(struct hash_table *ht,
160 void (*callback)(const void *key,
161 void *data,
162 void *closure),
163 void *closure)
164 {
165 hash_table_foreach(ht, entry)
166 callback(entry->key, entry->data, closure);
167 }
168
169 /**
170 * Hash table wrapper which supports 64-bit keys.
171 */
172 struct hash_table_u64 {
173 struct hash_table *table;
174 void *deleted_key_data;
175 };
176
177 struct hash_table_u64 *
178 _mesa_hash_table_u64_create(void *mem_ctx);
179
180 void
181 _mesa_hash_table_u64_destroy(struct hash_table_u64 *ht,
182 void (*delete_function)(struct hash_entry *entry));
183
184 void
185 _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
186 void *data);
187
188 void *
189 _mesa_hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key);
190
191 void
192 _mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key);
193
194 #ifdef __cplusplus
195 } /* extern C */
196 #endif
197
198 #endif /* _HASH_TABLE_H */