1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 * Hash table implementation.
32 * This file provides a hash implementation that is capable of dealing
33 * with collisions. It stores colliding entries in linked list. All
34 * functions operating on the hash return an iterator. The iterator
35 * itself points to the collision list. If there wasn't any collision
36 * the list will have just one entry, otherwise client code should
37 * iterate over the entries to find the exact entry among ones that
38 * had the same key (e.g. memcmp could be used on the data to check
41 * @author Zack Rusin <zackr@vmware.com>
47 #include "pipe/p_compiler.h"
55 struct cso_node
*next
;
62 struct cso_hash_data
*d
;
67 struct cso_hash_iter
{
68 struct cso_hash
*hash
;
69 struct cso_node
*node
;
72 struct cso_hash_data
{
73 struct cso_node
*fakeNext
;
74 struct cso_node
**buckets
;
81 bool cso_hash_init(struct cso_hash
*hash
);
82 void cso_hash_deinit(struct cso_hash
*hash
);
85 int cso_hash_size(struct cso_hash
*hash
);
89 * Adds a data with the given key to the hash. If entry with the given
90 * key is already in the hash, this current entry is instered before it
91 * in the collision list.
92 * Function returns iterator pointing to the inserted item in the hash.
94 struct cso_hash_iter
cso_hash_insert(struct cso_hash
*hash
, unsigned key
,
97 * Removes the item pointed to by the current iterator from the hash.
98 * Note that the data itself is not erased and if it was a malloc'ed pointer
99 * it will have to be freed after calling this function by the callee.
100 * Function returns iterator pointing to the item after the removed one in
103 struct cso_hash_iter
cso_hash_erase(struct cso_hash
*hash
, struct cso_hash_iter iter
);
105 void *cso_hash_take(struct cso_hash
*hash
, unsigned key
);
109 struct cso_hash_iter
cso_hash_first_node(struct cso_hash
*hash
);
112 * Returns true if a value with the given key exists in the hash
114 boolean
cso_hash_contains(struct cso_hash
*hash
, unsigned key
);
117 unsigned cso_hash_iter_key(struct cso_hash_iter iter
);
120 struct cso_hash_iter
cso_hash_iter_prev(struct cso_hash_iter iter
);
124 * Convenience routine to iterate over the collision list while doing a memory
125 * comparison to see which entry in the list is a direct copy of our template
126 * and returns that entry.
128 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,
133 struct cso_node
*cso_hash_data_next(struct cso_node
*node
);
136 cso_hash_iter_is_null(struct cso_hash_iter iter
)
138 if (!iter
.node
|| iter
.node
== iter
.hash
->data
.e
)
144 cso_hash_iter_data(struct cso_hash_iter iter
)
146 if (!iter
.node
|| iter
.hash
->data
.e
== iter
.node
)
148 return iter
.node
->value
;
151 static inline struct cso_node
**
152 cso_hash_find_node(struct cso_hash
*hash
, unsigned akey
)
154 struct cso_node
**node
;
156 if (hash
->data
.d
->numBuckets
) {
157 node
= (struct cso_node
**)(&hash
->data
.d
->buckets
[akey
% hash
->data
.d
->numBuckets
]);
158 assert(*node
== hash
->data
.e
|| (*node
)->next
);
159 while (*node
!= hash
->data
.e
&& (*node
)->key
!= akey
)
160 node
= &(*node
)->next
;
162 node
= (struct cso_node
**)((const struct cso_node
* const *)(&hash
->data
.e
));
168 * Return an iterator pointing to the first entry in the collision list.
170 static inline struct cso_hash_iter
171 cso_hash_find(struct cso_hash
*hash
, unsigned key
)
173 struct cso_node
**nextNode
= cso_hash_find_node(hash
, key
);
174 struct cso_hash_iter iter
= {hash
, *nextNode
};
178 static inline struct cso_hash_iter
179 cso_hash_iter_next(struct cso_hash_iter iter
)
181 struct cso_hash_iter next
= {iter
.hash
, cso_hash_data_next(iter
.node
)};