e7ab067a3bd66f7fddc201d390a10bedcb48c397
[mesa.git] / src / mesa / program / hash_table.h
1 /*
2 * Copyright © 2008 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file hash_table.h
26 * \brief Implementation of a generic, opaque hash table data type.
27 *
28 * \author Ian Romanick <ian.d.romanick@intel.com>
29 */
30
31 #ifndef HASH_TABLE_H
32 #define HASH_TABLE_H
33
34 struct hash_table;
35
36 typedef unsigned (*hash_func_t)(const void *key);
37 typedef int (*hash_compare_func_t)(const void *key1, const void *key2);
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44 * Hash table constructor
45 *
46 * Creates a hash table with the specified number of buckets. The supplied
47 * \c hash and \c compare routines are used when adding elements to the table
48 * and when searching for elements in the table.
49 *
50 * \param num_buckets Number of buckets (bins) in the hash table.
51 * \param hash Function used to compute hash value of input keys.
52 * \param compare Function used to compare keys.
53 */
54 extern struct hash_table *hash_table_ctor(unsigned num_buckets,
55 hash_func_t hash, hash_compare_func_t compare);
56
57
58 /**
59 * Release all memory associated with a hash table
60 *
61 * \warning
62 * This function cannot release memory occupied either by keys or data.
63 */
64 extern void hash_table_dtor(struct hash_table *ht);
65
66
67 /**
68 * Flush all entries from a hash table
69 *
70 * \param ht Table to be cleared of its entries.
71 */
72 extern void hash_table_clear(struct hash_table *ht);
73
74
75 /**
76 * Search a hash table for a specific element
77 *
78 * \param ht Table to be searched
79 * \param key Key of the desired element
80 *
81 * \return
82 * The \c data value supplied to \c hash_table_insert when the element with
83 * the matching key was added. If no matching key exists in the table,
84 * \c NULL is returned.
85 */
86 extern void *hash_table_find(struct hash_table *ht, const void *key);
87
88
89 /**
90 * Add an element to a hash table
91 *
92 * \warning
93 * If \c key is already in the hash table, it will be added again. Future
94 * calls to \c hash_table_find and \c hash_table_remove will return or remove,
95 * repsectively, the most recently added instance of \c key.
96 *
97 * \sa hash_table_replace
98 */
99 extern void hash_table_insert(struct hash_table *ht, void *data,
100 const void *key);
101
102 /**
103 * Add an element to a hash table with replacement
104 *
105 * \warning
106 * If \c key is already in the hash table, \c data will \b replace the most
107 * recently inserted \c data (see the warning in \c hash_table_insert) for
108 * that key.
109 *
110 * \sa hash_table_insert
111 */
112 extern void hash_table_replace(struct hash_table *ht, void *data,
113 const void *key);
114
115 /**
116 * Remove a specific element from a hash table.
117 */
118 extern void hash_table_remove(struct hash_table *ht, const void *key);
119
120 /**
121 * Compute hash value of a string
122 *
123 * Computes the hash value of a string using the DJB2 algorithm developed by
124 * Professor Daniel J. Bernstein. It was published on comp.lang.c once upon
125 * a time. I was unable to find the original posting in the archives.
126 *
127 * \param key Pointer to a NUL terminated string to be hashed.
128 *
129 * \sa hash_table_string_compare
130 */
131 extern unsigned hash_table_string_hash(const void *key);
132
133
134 /**
135 * Compare two strings used as keys
136 *
137 * This is just a macro wrapper around \c strcmp.
138 *
139 * \sa hash_table_string_hash
140 */
141 #define hash_table_string_compare ((hash_compare_func_t) strcmp)
142
143
144 /**
145 * Compute hash value of a pointer
146 *
147 * \param key Pointer to be used as a hash key
148 *
149 * \note
150 * The memory pointed to by \c key is \b never accessed. The value of \c key
151 * itself is used as the hash key
152 *
153 * \sa hash_table_pointer_compare
154 */
155 unsigned
156 hash_table_pointer_hash(const void *key);
157
158
159 /**
160 * Compare two pointers used as keys
161 *
162 * \sa hash_table_pointer_hash
163 */
164 int
165 hash_table_pointer_compare(const void *key1, const void *key2);
166
167 void
168 hash_table_call_foreach(struct hash_table *ht,
169 void (*callback)(const void *key,
170 void *data,
171 void *closure),
172 void *closure);
173
174 #ifdef __cplusplus
175 }
176 #endif
177 #endif /* HASH_TABLE_H */