1 /**************************************************************************
3 * Copyright 2007 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 * 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 <zack@tungstengraphics.com>
57 struct cso_hash_iter
{
58 struct cso_hash
*hash
;
59 struct cso_node
*node
;
63 struct cso_hash
*cso_hash_create(void);
64 void cso_hash_delete(struct cso_hash
*hash
);
67 int cso_hash_size(struct cso_hash
*hash
);
71 * Adds a data with the given key to the hash. If entry with the given
72 * key is already in the hash, this current entry is instered before it
73 * in the collision list.
74 * Function returns iterator pointing to the inserted item in the hash.
76 struct cso_hash_iter
cso_hash_insert(struct cso_hash
*hash
, unsigned key
,
79 * Removes the item pointed to by the current iterator from the hash.
80 * Note that the data itself is not erased and if it was a malloc'ed pointer
81 * it will have to be freed after calling this function by the callee.
82 * Function returns iterator pointing to the item after the removed one in
85 struct cso_hash_iter
cso_hash_erase(struct cso_hash
*hash
, struct cso_hash_iter iter
);
87 void *cso_hash_take(struct cso_hash
*hash
, unsigned key
);
91 struct cso_hash_iter
cso_hash_first_node(struct cso_hash
*hash
);
94 * Return an iterator pointing to the first entry in the collision list.
96 struct cso_hash_iter
cso_hash_find(struct cso_hash
*hash
, unsigned key
);
99 int cso_hash_iter_is_null(struct cso_hash_iter iter
);
100 unsigned cso_hash_iter_key(struct cso_hash_iter iter
);
101 void *cso_hash_iter_data(struct cso_hash_iter iter
);
104 struct cso_hash_iter
cso_hash_iter_next(struct cso_hash_iter iter
);
105 struct cso_hash_iter
cso_hash_iter_prev(struct cso_hash_iter iter
);
109 * Convenience routine to iterate over the collision list while doing a memory
110 * comparison to see which entry in the list is a direct copy of our template
111 * and returns that entry.
113 void *cso_hash_find_data_from_template( struct cso_hash
*hash
,