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 * Zack Rusin <zackr@vmware.com>
37 #include "util_hash.h"
42 #define MAX(a, b) ((a > b) ? (a) : (b))
44 static const int MinNumBits = 4;
46 static const unsigned char prime_deltas[] = {
47 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 9, 25, 3,
48 1, 21, 3, 21, 7, 15, 9, 5, 3, 29, 15, 0, 0, 0, 0, 0
51 static int primeForNumBits(int numBits)
53 return (1 << numBits) + prime_deltas[numBits];
56 /* Returns the smallest integer n such that
57 primeForNumBits(n) >= hint.
59 static int countBits(int hint)
69 if (numBits >= (int)sizeof(prime_deltas)) {
70 numBits = sizeof(prime_deltas) - 1;
71 } else if (primeForNumBits(numBits) < hint) {
78 struct util_node *next;
83 struct util_hash_data {
84 struct util_node *fakeNext;
85 struct util_node **buckets;
95 struct util_hash_data *d;
100 static void *util_data_allocate_node(struct util_hash_data *hash)
102 return malloc(hash->nodeSize);
105 static void util_free_node(struct util_node *node)
110 static struct util_node *
111 util_hash_create_node(struct util_hash *hash,
112 unsigned akey, void *avalue,
113 struct util_node **anextNode)
115 struct util_node *node = util_data_allocate_node(hash->data.d);
121 node->value = avalue;
123 node->next = (struct util_node*)(*anextNode);
125 ++hash->data.d->size;
129 static void util_data_rehash(struct util_hash_data *hash, int hint)
132 hint = countBits(-hint);
133 if (hint < MinNumBits)
135 hash->userNumBits = (short)hint;
136 while (primeForNumBits(hint) < (hash->size >> 1))
138 } else if (hint < MinNumBits) {
142 if (hash->numBits != hint) {
143 struct util_node *e = (struct util_node *)(hash);
144 struct util_node **oldBuckets = hash->buckets;
145 int oldNumBuckets = hash->numBuckets;
148 hash->numBits = (short)hint;
149 hash->numBuckets = primeForNumBits(hint);
150 hash->buckets = malloc(sizeof(struct util_node*) * hash->numBuckets);
151 for (i = 0; i < hash->numBuckets; ++i)
152 hash->buckets[i] = e;
154 for (i = 0; i < oldNumBuckets; ++i) {
155 struct util_node *firstNode = oldBuckets[i];
156 while (firstNode != e) {
157 unsigned h = firstNode->key;
158 struct util_node *lastNode = firstNode;
159 struct util_node *afterLastNode;
160 struct util_node **beforeFirstNode;
162 while (lastNode->next != e && lastNode->next->key == h)
163 lastNode = lastNode->next;
165 afterLastNode = lastNode->next;
166 beforeFirstNode = &hash->buckets[h % hash->numBuckets];
167 while (*beforeFirstNode != e)
168 beforeFirstNode = &(*beforeFirstNode)->next;
169 lastNode->next = *beforeFirstNode;
170 *beforeFirstNode = firstNode;
171 firstNode = afterLastNode;
178 static void util_data_might_grow(struct util_hash_data *hash)
180 if (hash->size >= hash->numBuckets)
181 util_data_rehash(hash, hash->numBits + 1);
184 static void util_data_has_shrunk(struct util_hash_data *hash)
186 if (hash->size <= (hash->numBuckets >> 3) &&
187 hash->numBits > hash->userNumBits) {
188 int max = MAX(hash->numBits-2, hash->userNumBits);
189 util_data_rehash(hash, max);
193 static struct util_node *util_data_first_node(struct util_hash_data *hash)
195 struct util_node *e = (struct util_node *)(hash);
196 struct util_node **bucket = hash->buckets;
197 int n = hash->numBuckets;
206 static struct util_node **util_hash_find_node(struct util_hash *hash, unsigned akey)
208 struct util_node **node;
210 if (hash->data.d->numBuckets) {
211 node = (struct util_node **)(&hash->data.d->buckets[akey % hash->data.d->numBuckets]);
212 assert(*node == hash->data.e || (*node)->next);
213 while (*node != hash->data.e && (*node)->key != akey)
214 node = &(*node)->next;
216 node = (struct util_node **)((const struct util_node * const *)(&hash->data.e));
221 drm_private struct util_hash_iter
222 util_hash_insert(struct util_hash *hash, unsigned key, void *data)
224 util_data_might_grow(hash->data.d);
227 struct util_node **nextNode = util_hash_find_node(hash, key);
228 struct util_node *node = util_hash_create_node(hash, key, data, nextNode);
230 struct util_hash_iter null_iter = {hash, 0};
235 struct util_hash_iter iter = {hash, node};
241 drm_private struct util_hash *util_hash_create(void)
243 struct util_hash *hash = malloc(sizeof(struct util_hash));
247 hash->data.d = malloc(sizeof(struct util_hash_data));
253 hash->data.d->fakeNext = 0;
254 hash->data.d->buckets = 0;
255 hash->data.d->size = 0;
256 hash->data.d->nodeSize = sizeof(struct util_node);
257 hash->data.d->userNumBits = (short)MinNumBits;
258 hash->data.d->numBits = 0;
259 hash->data.d->numBuckets = 0;
264 drm_private void util_hash_delete(struct util_hash *hash)
266 struct util_node *e_for_x = (struct util_node *)(hash->data.d);
267 struct util_node **bucket = (struct util_node **)(hash->data.d->buckets);
268 int n = hash->data.d->numBuckets;
270 struct util_node *cur = *bucket++;
271 while (cur != e_for_x) {
272 struct util_node *next = cur->next;
277 free(hash->data.d->buckets);
282 drm_private struct util_hash_iter
283 util_hash_find(struct util_hash *hash, unsigned key)
285 struct util_node **nextNode = util_hash_find_node(hash, key);
286 struct util_hash_iter iter = {hash, *nextNode};
290 drm_private unsigned util_hash_iter_key(struct util_hash_iter iter)
292 if (!iter.node || iter.hash->data.e == iter.node)
294 return iter.node->key;
297 drm_private void *util_hash_iter_data(struct util_hash_iter iter)
299 if (!iter.node || iter.hash->data.e == iter.node)
301 return iter.node->value;
304 static struct util_node *util_hash_data_next(struct util_node *node)
307 struct util_node *next;
309 struct util_hash_data *d;
312 struct util_node **bucket;
317 /* iterating beyond the last element */
323 start = (node->key % a.d->numBuckets) + 1;
324 bucket = a.d->buckets + start;
325 n = a.d->numBuckets - start;
334 drm_private struct util_hash_iter
335 util_hash_iter_next(struct util_hash_iter iter)
337 struct util_hash_iter next = {iter.hash, util_hash_data_next(iter.node)};
341 drm_private int util_hash_iter_is_null(struct util_hash_iter iter)
343 if (!iter.node || iter.node == iter.hash->data.e)
348 drm_private void *util_hash_take(struct util_hash *hash, unsigned akey)
350 struct util_node **node = util_hash_find_node(hash, akey);
351 if (*node != hash->data.e) {
352 void *t = (*node)->value;
353 struct util_node *next = (*node)->next;
354 util_free_node(*node);
356 --hash->data.d->size;
357 util_data_has_shrunk(hash->data.d);
363 drm_private struct util_hash_iter util_hash_first_node(struct util_hash *hash)
365 struct util_hash_iter iter = {hash, util_data_first_node(hash->data.d)};
369 drm_private struct util_hash_iter
370 util_hash_erase(struct util_hash *hash, struct util_hash_iter iter)
372 struct util_hash_iter ret = iter;
373 struct util_node *node = iter.node;
374 struct util_node **node_ptr;
376 if (node == hash->data.e)
379 ret = util_hash_iter_next(ret);
380 node_ptr = (struct util_node**)(&hash->data.d->buckets[node->key % hash->data.d->numBuckets]);
381 while (*node_ptr != node)
382 node_ptr = &(*node_ptr)->next;
383 *node_ptr = node->next;
384 util_free_node(node);
385 --hash->data.d->size;