1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * General purpose hash table implementation.
32 * Just uses the cso_hash for now, but it might be better switch to a linear
33 * probing hash table implementation at some point -- as it is said they have
34 * better lookup and cache performance and it appears to be possible to write
35 * a lock-free implementation of such hash tables .
37 * @author José Fonseca <jrfonseca@tungstengraphics.com>
41 #include "pipe/p_compiler.h"
42 #include "pipe/p_debug.h"
44 #include "cso_cache/cso_hash.h"
46 #include "util/u_memory.h"
47 #include "util/u_hash_table.h"
55 unsigned (*hash
)(void *key
);
57 /** Compare two keys */
58 int (*compare
)(void *key1
, void *key2
);
60 /* TODO: key, value destructors? */
64 struct hash_table_item
71 static INLINE
struct hash_table_item
*
72 hash_table_item(struct cso_hash_iter iter
)
74 return (struct hash_table_item
*)cso_hash_iter_data(iter
);
79 hash_table_create(unsigned (*hash
)(void *key
),
80 int (*compare
)(void *key1
, void *key2
))
82 struct hash_table
*ht
;
84 ht
= MALLOC_STRUCT(hash_table
);
88 ht
->cso
= cso_hash_create();
95 ht
->compare
= compare
;
101 static INLINE
struct cso_hash_iter
102 hash_table_find_iter(struct hash_table
*ht
,
106 struct cso_hash_iter iter
;
107 struct hash_table_item
*item
;
109 iter
= cso_hash_find(ht
->cso
, key_hash
);
110 while (!cso_hash_iter_is_null(iter
)) {
111 item
= (struct hash_table_item
*)cso_hash_iter_data(iter
);
112 if (!ht
->compare(item
->key
, key
))
114 iter
= cso_hash_iter_next(iter
);
121 static INLINE
struct hash_table_item
*
122 hash_table_find_item(struct hash_table
*ht
,
126 struct cso_hash_iter iter
;
127 struct hash_table_item
*item
;
129 iter
= cso_hash_find(ht
->cso
, key_hash
);
130 while (!cso_hash_iter_is_null(iter
)) {
131 item
= (struct hash_table_item
*)cso_hash_iter_data(iter
);
132 if (!ht
->compare(item
->key
, key
))
134 iter
= cso_hash_iter_next(iter
);
142 hash_table_set(struct hash_table
*ht
,
147 struct hash_table_item
*item
;
148 struct cso_hash_iter iter
;
152 key_hash
= ht
->hash(key
);
154 item
= hash_table_find_item(ht
, key
, key_hash
);
156 /* TODO: key/value destruction? */
161 item
= MALLOC_STRUCT(hash_table_item
);
163 return PIPE_ERROR_OUT_OF_MEMORY
;
168 iter
= cso_hash_insert(ht
->cso
, key_hash
, item
);
169 if(cso_hash_iter_is_null(iter
)) {
171 return PIPE_ERROR_OUT_OF_MEMORY
;
179 hash_table_get(struct hash_table
*ht
,
183 struct hash_table_item
*item
;
187 key_hash
= ht
->hash(key
);
189 item
= hash_table_find_item(ht
, key
, key_hash
);
198 hash_table_remove(struct hash_table
*ht
,
202 struct cso_hash_iter iter
;
203 struct hash_table_item
*item
;
207 key_hash
= ht
->hash(key
);
209 iter
= hash_table_find_iter(ht
, key
, key_hash
);
210 if(cso_hash_iter_is_null(iter
))
213 item
= hash_table_item(iter
);
217 cso_hash_erase(ht
->cso
, iter
);
222 hash_table_clear(struct hash_table
*ht
)
224 struct cso_hash_iter iter
;
225 struct hash_table_item
*item
;
229 iter
= cso_hash_first_node(ht
->cso
);
230 while (!cso_hash_iter_is_null(iter
)) {
231 item
= (struct hash_table_item
*)cso_hash_take(ht
->cso
, cso_hash_iter_key(iter
));
233 iter
= cso_hash_first_node(ht
->cso
);
239 hash_table_foreach(struct hash_table
*ht
,
240 enum pipe_error (*callback
)(void *key
, void *value
, void *data
),
243 struct cso_hash_iter iter
;
244 struct hash_table_item
*item
;
245 enum pipe_error result
;
249 iter
= cso_hash_first_node(ht
->cso
);
250 while (!cso_hash_iter_is_null(iter
)) {
251 item
= (struct hash_table_item
*)cso_hash_iter_data(iter
);
252 result
= callback(item
->key
, item
->value
, data
);
253 if(result
!= PIPE_OK
)
255 iter
= cso_hash_iter_next(iter
);
263 hash_table_destroy(struct hash_table
*ht
)
265 struct cso_hash_iter iter
;
266 struct hash_table_item
*item
;
270 iter
= cso_hash_first_node(ht
->cso
);
271 while (!cso_hash_iter_is_null(iter
)) {
272 item
= (struct hash_table_item
*)cso_hash_iter_data(iter
);
274 iter
= cso_hash_iter_next(iter
);
277 cso_hash_delete(ht
->cso
);