gallium/cso_hash: inline a bunch of functions
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_hash.h
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Hash table implementation.
31 *
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
39 * that)
40 *
41 * @author Zack Rusin <zackr@vmware.com>
42 */
43
44 #ifndef CSO_HASH_H
45 #define CSO_HASH_H
46
47 #include "pipe/p_compiler.h"
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53
54 struct cso_node {
55 struct cso_node *next;
56 unsigned key;
57 void *value;
58 };
59
60 struct cso_hash {
61 union {
62 struct cso_hash_data *d;
63 struct cso_node *e;
64 } data;
65 };
66
67 struct cso_hash_iter {
68 struct cso_hash *hash;
69 struct cso_node *node;
70 };
71
72 struct cso_hash_data {
73 struct cso_node *fakeNext;
74 struct cso_node **buckets;
75 int size;
76 int nodeSize;
77 short userNumBits;
78 short numBits;
79 int numBuckets;
80 };
81
82 struct cso_hash *cso_hash_create(void);
83 void cso_hash_delete(struct cso_hash *hash);
84
85
86 int cso_hash_size(struct cso_hash *hash);
87
88
89 /**
90 * Adds a data with the given key to the hash. If entry with the given
91 * key is already in the hash, this current entry is instered before it
92 * in the collision list.
93 * Function returns iterator pointing to the inserted item in the hash.
94 */
95 struct cso_hash_iter cso_hash_insert(struct cso_hash *hash, unsigned key,
96 void *data);
97 /**
98 * Removes the item pointed to by the current iterator from the hash.
99 * Note that the data itself is not erased and if it was a malloc'ed pointer
100 * it will have to be freed after calling this function by the callee.
101 * Function returns iterator pointing to the item after the removed one in
102 * the hash.
103 */
104 struct cso_hash_iter cso_hash_erase(struct cso_hash *hash, struct cso_hash_iter iter);
105
106 void *cso_hash_take(struct cso_hash *hash, unsigned key);
107
108
109
110 struct cso_hash_iter cso_hash_first_node(struct cso_hash *hash);
111
112 /**
113 * Returns true if a value with the given key exists in the hash
114 */
115 boolean cso_hash_contains(struct cso_hash *hash, unsigned key);
116
117
118 unsigned cso_hash_iter_key(struct cso_hash_iter iter);
119
120
121 struct cso_hash_iter cso_hash_iter_prev(struct cso_hash_iter iter);
122
123
124 /**
125 * Convenience routine to iterate over the collision list while doing a memory
126 * comparison to see which entry in the list is a direct copy of our template
127 * and returns that entry.
128 */
129 void *cso_hash_find_data_from_template( struct cso_hash *hash,
130 unsigned hash_key,
131 void *templ,
132 int size );
133
134 struct cso_node *cso_hash_data_next(struct cso_node *node);
135
136 static inline int
137 cso_hash_iter_is_null(struct cso_hash_iter iter)
138 {
139 if (!iter.node || iter.node == iter.hash->data.e)
140 return 1;
141 return 0;
142 }
143
144 static inline void *
145 cso_hash_iter_data(struct cso_hash_iter iter)
146 {
147 if (!iter.node || iter.hash->data.e == iter.node)
148 return 0;
149 return iter.node->value;
150 }
151
152 static inline struct cso_node **
153 cso_hash_find_node(struct cso_hash *hash, unsigned akey)
154 {
155 struct cso_node **node;
156
157 if (hash->data.d->numBuckets) {
158 node = (struct cso_node **)(&hash->data.d->buckets[akey % hash->data.d->numBuckets]);
159 assert(*node == hash->data.e || (*node)->next);
160 while (*node != hash->data.e && (*node)->key != akey)
161 node = &(*node)->next;
162 } else {
163 node = (struct cso_node **)((const struct cso_node * const *)(&hash->data.e));
164 }
165 return node;
166 }
167
168 /**
169 * Return an iterator pointing to the first entry in the collision list.
170 */
171 static inline struct cso_hash_iter
172 cso_hash_find(struct cso_hash *hash, unsigned key)
173 {
174 struct cso_node **nextNode = cso_hash_find_node(hash, key);
175 struct cso_hash_iter iter = {hash, *nextNode};
176 return iter;
177 }
178
179 static inline struct cso_hash_iter
180 cso_hash_iter_next(struct cso_hash_iter iter)
181 {
182 struct cso_hash_iter next = {iter.hash, cso_hash_data_next(iter.node)};
183 return next;
184 }
185
186 #ifdef __cplusplus
187 }
188 #endif
189
190 #endif