1 /* Generalized hash and string hash (sash) classes.
2 * By Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: hash.h,v 1.6 2002/11/26 19:47:30 rich Exp $
26 /* Note, hash and sash are identical but for the fact that
27 * hash maps fixed sized keys to values (eg. int -> int or
28 * int -> struct) and sash maps nul-terminated ASCII strings
29 * to nul-terminated ASCII strings (ie. string -> string ONLY).
30 * shash maps nul-terminated ASCII strings to anything else.
34 typedef struct hash *hash;
37 typedef struct sash *sash;
40 typedef struct shash *shash;
42 /* Function: new_hash - allocate a new hash
45 * Allocate a new hash in @code{pool} mapping
46 * @code{key_type} to @code{value_type}. You can map both
47 * simple types like @code{int} and also aggregate types
48 * like structures and unions. However, beware of aggregate
49 * types that might contain 'holes' because of alignment --
50 * such types will probably not work as you expect, particularly
54 * a hash which maps strings to something, then calling
55 * @code{new_hash(pool, char *, char *)} (for example) will not do what
56 * you expect. You are better to use either a sash (string to string hash)
57 * or a shash (string to anything hash) instead (see
58 * @ref{new_sash(3)} and @ref{new_shash(3)}).
60 #define new_hash(pool,key_type,value_type) _hash_new ((pool), sizeof (key_type), sizeof (value_type))
61 extern hash _hash_new (pool, size_t key_size, size_t value_size);
63 /* Function: copy_hash - copy a hash
65 * Copy a hash into a new pool. This function copies the keys
66 * and values, but if keys and values are pointers, then it does
67 * not perform a 'deep' copy.
69 extern hash copy_hash (pool, hash);
71 /* Function: hash_get - look up in a hash
73 * Function: hash_get_ptr
74 * Function: _hash_get_ptr
75 * Function: hash_exists
77 * Get the @code{value} associated with key @code{key} and return true.
78 * If there is no @code{value} associated with @code{key}, this returns
79 * false and @code{value} is left unchanged.
81 * The @code{*_ptr} variants return a pointer rather than copying
82 * out the entire value object. The pointer is typically only
83 * valid for a short period of time. Particularly if you insert
84 * or remove elements from the hash, this pointer may become
87 * @code{hash_exists} simply tests whether or not @code{key} exists
88 * in the hash. If so, it returns true, otherwise false.
90 #define hash_get(h,key,value) _hash_get ((h), &(key), &(value))
91 extern int _hash_get (hash, const void *key, void *value);
92 #define hash_get_ptr(h,key,ptr) ((ptr) = ((typeof (ptr))_hash_get_ptr ((h), &(key))))
93 extern const void *_hash_get_ptr (hash, const void *key);
94 #define hash_exists(h,key) (_hash_get_ptr ((h), &(key)) ? 1 : 0)
96 /* Function: hash_insert - insert a (key, value) pair into a hash
97 * Function: _hash_insert
99 * Insert an element (@code{key}, @code{value}) into the hash.
100 * If @code{key} already
101 * exists in the hash, then the existing value is replaced by
103 * and the function returns true. If there was no previous @code{key}
104 * in the hash then this function returns false.
106 #define hash_insert(h,key,value) _hash_insert((h),&(key),&(value))
107 extern int _hash_insert (hash, const void *key, const void *value);
109 /* Function: hash_erase - erase a key from a hash
110 * Function: _hash_erase
112 * Erase @code{key} from the hash. If an element was erased,
113 * this returns true, else this returns false.
115 #define hash_erase(h,key) _hash_erase((h),&(key))
116 extern int _hash_erase (hash, const void *key);
118 /* Function: hash_keys - return a vector of the keys or values in a hash
119 * Function: hash_keys_in_pool
120 * Function: hash_values
121 * Function: hash_values_in_pool
123 * Return a vector containing all the keys or values of hash. The
124 * @code{*_in_pool} variants allow you to allocate the vector in
125 * another pool (the default is to allocate the vector in the same
128 extern vector hash_keys (hash);
129 extern vector hash_keys_in_pool (hash, pool);
130 extern vector hash_values (hash);
131 extern vector hash_values_in_pool (hash, pool);
133 /* Function: hash_size - return the number of (key, value) pairs in a hash
135 * Count the number of (key, value) pairs in the hash.
137 extern int hash_size (hash);
139 /* Function: hash_get_buckets_used - return the number of buckets in a hash
140 * Function: hash_get_buckets_allocated
142 * Return the number of hash buckets used and allocated. The number of
143 * buckets allocated is always a power of 2. See also
144 * @ref{hash_set_buckets_allocated} to change the number used
147 extern int hash_get_buckets_used (hash);
148 extern int hash_get_buckets_allocated (hash);
150 /* Function: hash_set_buckets_allocated - set the number of buckets
152 * Set the number of buckets allocated. You may ONLY do this when you
153 * have just created the hash and before you have inserted any elements.
154 * Otherwise the results are undefined (and probably bad). The number
155 * of buckets MUST be a power of 2.
157 extern void hash_set_buckets_allocated (hash, int);
159 /* Function: new_sash - allocate a new sash (string hash)
161 * Allocate a new sash in @code{pool} mapping
162 * strings to strings.
164 * Use a string hash in preference to a hash of
165 * @code{char *} -> @code{char *} which will probably not
166 * quite work as you expect.
168 extern sash new_sash (pool);
170 /* Function: copy_sash - copy a sash
172 * Copy a sash into a new pool. This function copies the keys
173 * and values, but if keys and values are pointers, then it does
174 * not perform a 'deep' copy.
176 extern sash copy_sash (pool, sash);
178 /* Function: sash_get - look up in a sash
179 * Function: _sash_get
180 * Function: sash_exists
182 * Get the @code{value} associated with key @code{key} and return true.
183 * If there is no @code{value} associated with @code{key}, this returns
184 * false and @code{value} is left unchanged.
186 * @code{sash_exists} simply tests whether or not @code{key} exists
187 * in the sash. If so, it returns true, otherwise false.
189 #define sash_get(sash,key,value) _sash_get((sash),(key),&(value))
190 extern int _sash_get (sash, const char *key, const char **ptr);
191 #define sash_exists(sash,key) _sash_get ((sash), (key), 0)
193 /* Function: sash_insert - insert a (key, value) pair into a sash
195 * Insert an element (@code{key}, @code{value}) into the sash.
196 * If @code{key} already
197 * exists in the sash, then the existing value is replaced by
199 * and the function returns true. If there was no previous @code{key}
200 * in the sash then this function returns false.
202 extern int sash_insert (sash, const char *key, const char *value);
204 /* Function: sash_erase - erase a key from a sash
206 * Erase @code{key} from the sash. If an element was erased,
207 * this returns true, else this returns false.
209 extern int sash_erase (sash, const char *key);
211 /* Function: sash_keys - return a vector of the keys or values in a sash
212 * Function: sash_keys_in_pool
213 * Function: sash_values
214 * Function: sash_values_in_pool
216 * Return a vector containing all the keys or values of sash. The
217 * @code{*_in_pool} variants allow you to allocate the vector in
218 * another pool (the default is to allocate the vector in the same
221 extern vector sash_keys (sash);
222 extern vector sash_keys_in_pool (sash, pool);
223 extern vector sash_values (sash);
224 extern vector sash_values_in_pool (sash, pool);
226 /* Function: sash_size - return the number of (key, value) pairs in a sash
228 * Count the number of (key, value) pairs in the sash.
230 extern int sash_size (sash);
232 /* Function: sash_get_buckets_used - return the number of buckets in a sash
233 * Function: sash_get_buckets_allocated
235 * Return the number of sash buckets used and allocated. The number of
236 * buckets allocated is always a power of 2. See also
237 * @ref{sash_set_buckets_allocated} to change the number used
240 extern int sash_get_buckets_used (sash);
241 extern int sash_get_buckets_allocated (sash);
243 /* Function: sash_set_buckets_allocated - set the number of buckets
245 * Set the number of buckets allocated. You may ONLY do this when you
246 * have just created the sash and before you have inserted any elements.
247 * Otherwise the results are undefined (and probably bad). The number
248 * of buckets MUST be a power of 2.
250 extern void sash_set_buckets_allocated (sash, int);
252 /* Function: new_shash - allocate a new shash (string -> something hash)
254 * Allocate a new shash in @code{pool} mapping
255 * strings to strings.
257 * Use a shash in preference to a hash of
258 * @code{char *} -> something which will probably not
259 * quite work as you expect.
261 #define new_shash(pool,value_type) _shash_new ((pool), sizeof (value_type))
262 extern shash _shash_new (pool, size_t value_size);
264 /* Function: copy_shash - copy a shash
266 * Copy a shash into a new pool. This function copies the keys
267 * and values, but if keys and values are pointers, then it does
268 * not perform a 'deep' copy.
270 extern shash copy_shash (pool, shash);
272 /* Function: shash_get - look up in a shash
273 * Function: _shash_get
274 * Function: shash_get_ptr
275 * Function: _shash_get_ptr
276 * Function: shash_exists
278 * Get the @code{value} associated with key @code{key} and return true.
279 * If there is no @code{value} associated with @code{key}, this returns
280 * false and @code{value} is left unchanged.
282 * The @code{*_ptr} variants return a pointer rather than copying
283 * out the entire value object. The pointer is typically only
284 * valid for a short period of time. Particularly if you insert
285 * or remove elements from the shash, this pointer may become
288 * @code{shash_exists} simply tests whether or not @code{key} exists
289 * in the shash. If so, it returns true, otherwise false.
291 #define shash_get(shash,key,value) _shash_get((shash),(key),&(value))
292 extern int _shash_get (shash, const char *key, void *value);
293 #define shash_get_ptr(h,key,ptr) ((ptr) = ((typeof (ptr))_shash_get_ptr ((h),(key))))
294 extern const void *_shash_get_ptr (shash, const char *key);
295 #define shash_exists(shash,key) (_shash_get_ptr ((shash), (key)) ? 1 : 0)
297 /* Function: shash_insert - insert a (key, value) pair into a shash
298 * Function: _shash_insert
300 * Insert an element (@code{key}, @code{value}) into the shash.
301 * If @code{key} already
302 * exists in the shash, then the existing value is replaced by
304 * and the function returns true. If there was no previous @code{key}
305 * in the shash then this function returns false.
307 #define shash_insert(h,key,value) _shash_insert((h),(key),&(value))
308 extern int _shash_insert (shash, const char *key, const void *value);
310 /* Function: shash_erase - erase a key from a shash
312 * Erase @code{key} from the shash. If an element was erased,
313 * this returns true, else this returns false.
315 extern int shash_erase (shash, const char *key);
317 /* Function: shash_keys - return a vector of the keys or values in a shash
318 * Function: shash_keys_in_pool
319 * Function: shash_values
320 * Function: shash_values_in_pool
322 * Return a vector containing all the keys or values of shash. The
323 * @code{*_in_pool} variants allow you to allocate the vector in
324 * another pool (the default is to allocate the vector in the same
325 * pool as the shash).
327 extern vector shash_keys (shash);
328 extern vector shash_keys_in_pool (shash, pool);
329 extern vector shash_values (shash);
330 extern vector shash_values_in_pool (shash, pool);
332 /* Function: shash_size - return the number of (key, value) pairs in a shash
334 * Count the number of (key, value) pairs in the shash.
336 extern int shash_size (shash);
338 /* Function: shash_get_buckets_used - return the number of buckets in a shash
339 * Function: shash_get_buckets_allocated
341 * Return the number of shash buckets used and allocated. The number of
342 * buckets allocated is always a power of 2. See also
343 * @ref{shash_set_buckets_allocated} to change the number used
346 extern int shash_get_buckets_used (shash);
347 extern int shash_get_buckets_allocated (shash);
349 /* Function: shash_set_buckets_allocated - set the number of buckets
351 * Set the number of buckets allocated. You may ONLY do this when you
352 * have just created the shash and before you have inserted any elements.
353 * Otherwise the results are undefined (and probably bad). The number
354 * of buckets MUST be a power of 2.
356 extern void shash_set_buckets_allocated (shash, int);