util: Move fetch_rgba to a separate function table.
[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 uint64_t size_magic;
55 uint64_t rehash_magic;
56 uint32_t max_entries;
57 uint32_t size_index;
58 uint32_t entries;
59 uint32_t deleted_entries;
60 };
61
62 struct hash_table *
63 _mesa_hash_table_create(void *mem_ctx,
64 uint32_t (*key_hash_function)(const void *key),
65 bool (*key_equals_function)(const void *a,
66 const void *b));
67
68 bool
69 _mesa_hash_table_init(struct hash_table *ht,
70 void *mem_ctx,
71 uint32_t (*key_hash_function)(const void *key),
72 bool (*key_equals_function)(const void *a,
73 const void *b));
74
75 struct hash_table *
76 _mesa_hash_table_clone(struct hash_table *src, void *dst_mem_ctx);
77 void _mesa_hash_table_destroy(struct hash_table *ht,
78 void (*delete_function)(struct hash_entry *entry));
79 void _mesa_hash_table_clear(struct hash_table *ht,
80 void (*delete_function)(struct hash_entry *entry));
81 void _mesa_hash_table_set_deleted_key(struct hash_table *ht,
82 const void *deleted_key);
83
84 static inline uint32_t _mesa_hash_table_num_entries(struct hash_table *ht)
85 {
86 return ht->entries;
87 }
88
89 struct hash_entry *
90 _mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data);
91 struct hash_entry *
92 _mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
93 const void *key, void *data);
94 struct hash_entry *
95 _mesa_hash_table_search(struct hash_table *ht, const void *key);
96 struct hash_entry *
97 _mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
98 const void *key);
99 void _mesa_hash_table_remove(struct hash_table *ht,
100 struct hash_entry *entry);
101 void _mesa_hash_table_remove_key(struct hash_table *ht,
102 const void *key);
103
104 struct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht,
105 struct hash_entry *entry);
106 struct hash_entry *
107 _mesa_hash_table_random_entry(struct hash_table *ht,
108 bool (*predicate)(struct hash_entry *entry));
109
110 uint32_t _mesa_hash_data(const void *data, size_t size);
111
112 uint32_t _mesa_hash_int(const void *key);
113 uint32_t _mesa_hash_uint(const void *key);
114 uint32_t _mesa_hash_u32(const void *key);
115 uint32_t _mesa_hash_string(const void *key);
116 uint32_t _mesa_hash_pointer(const void *pointer);
117
118 bool _mesa_key_int_equal(const void *a, const void *b);
119 bool _mesa_key_uint_equal(const void *a, const void *b);
120 bool _mesa_key_u32_equal(const void *a, const void *b);
121 bool _mesa_key_string_equal(const void *a, const void *b);
122 bool _mesa_key_pointer_equal(const void *a, const void *b);
123
124 struct hash_table *
125 _mesa_pointer_hash_table_create(void *mem_ctx);
126
127 /**
128 * This foreach function is safe against deletion (which just replaces
129 * an entry's data with the deleted marker), but not against insertion
130 * (which may rehash the table, making entry a dangling pointer).
131 */
132 #define hash_table_foreach(ht, entry) \
133 for (struct hash_entry *entry = _mesa_hash_table_next_entry(ht, NULL); \
134 entry != NULL; \
135 entry = _mesa_hash_table_next_entry(ht, entry))
136
137 static inline void
138 hash_table_call_foreach(struct hash_table *ht,
139 void (*callback)(const void *key,
140 void *data,
141 void *closure),
142 void *closure)
143 {
144 hash_table_foreach(ht, entry)
145 callback(entry->key, entry->data, closure);
146 }
147
148 /**
149 * Hash table wrapper which supports 64-bit keys.
150 */
151 struct hash_table_u64 {
152 struct hash_table *table;
153 void *freed_key_data;
154 void *deleted_key_data;
155 };
156
157 struct hash_table_u64 *
158 _mesa_hash_table_u64_create(void *mem_ctx);
159
160 void
161 _mesa_hash_table_u64_destroy(struct hash_table_u64 *ht,
162 void (*delete_function)(struct hash_entry *entry));
163
164 void
165 _mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key,
166 void *data);
167
168 void *
169 _mesa_hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key);
170
171 void
172 _mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key);
173
174 void
175 _mesa_hash_table_u64_clear(struct hash_table_u64 *ht,
176 void (*delete_function)(struct hash_entry *entry));
177
178 #ifdef __cplusplus
179 } /* extern C */
180 #endif
181
182 #endif /* _HASH_TABLE_H */