2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * An afs_lhash is a linear hash table. It is intended for use where
12 * the number of elements in the hash table is not known in advance.
13 * It grows as required in order to keep the average hash chain length
14 * within certain bounds, which keeps average lookup times small.
15 * Growth is efficient and does not require rehashing of all the
16 * elements in the table at once.
18 * The caller is responsible for doing any required locking.
20 * For more information on the algorithm, see
23 * Per-Åke Larson (Per-Ake Larson)
24 * Communications of the ACM
25 * Vol. 31, No. 4 (April 1988), Pages 446-457
34 * The user is responsible for generating the key values corresponding
35 * to the data in the hash table. The key values should be distributed
36 * over some interval [0,M], where M is sufficiently large, say
37 * M > 2**20 (1048576).
40 typedef struct afs_lhash afs_lhash;
42 struct afs_lhash_stat {
43 size_t min_chain_length;
44 size_t max_chain_length;
48 size_t search_calls; /* cumulative afs_lhash_search() call count */
49 size_t search_tests; /* cumulative afs_lhash_search() comparison count */
50 size_t remove_calls; /* cumulative afs_lhash_remove() call count */
51 size_t remove_tests; /* cumulative afs_lhash_remove() comparison count */
55 * afs_lhash_create() allocates a new hash table.
57 * equal() -- compares table elements for equality
59 * allocate() -- allocates memory
61 * deallocate() -- frees memory acquired via allocate()
63 * afs_lhash_create() returns a pointer to the new hash table, or 0 on
69 ( int (*equal)(const void *a, const void *b)
70 /* returns true if the elements pointed to by
71 a and b are the same, false otherwise */
72 , void *(*allocate)(size_t n)
73 , void (*deallocate)(void *p, size_t n)
77 * afs_lhash_destroy() destroys the given hash table.
79 * Note that the caller is responsible for freeing the table elements if
89 * afs_lhash_iter() calls the given function for each element of the
92 * The function called with the key and data pointer for each element of
93 * the hash table. It is also given the hash table index value, in case
94 * the function wants to compute how many elements are in each bucket.
100 , void(*f)(size_t index, unsigned key, void *data)
104 * afs_lhash_search() searches the given hash table for the given key
107 * The element may be incomplete; it is compared to elements in the hash
108 * table using the hash table's equal() function.
110 * If the element is found, it is moved to the front of its hash chain
111 * list to try to make future lookups faster.
113 * afs_lhash_search() returns a pointer to the desired data element if
114 * found, 0 otherwise.
125 * afs_lhash_rosearch() searches the given hash table for the given key
128 * The element may be incomplete; it is compared to elements in the hash
129 * table using the hash table's equal() function.
131 * The hash table is not modified.
133 * afs_lhash_rosearch() returns a pointer to the desired data element if
134 * found, 0 otherwise.
139 ( const afs_lhash *lh
145 * afs_lhash_remove() removes an item matching the given key and data
146 * from the hash table.
148 * afs_lhash_remove() returns a pointer to the item removed on success,
160 * afs_lhash_enter() enters the given data element into the given hash
161 * table using the given key.
163 * It is not an error to enter the same [key, data] twice, so the
164 * caller should check for duplicates if that is important.
166 * afs_lhash_enter() returns 0 on success, nonzero otherwise.
177 * afs_lhash_stat() writes certain statistics about the given hash table
178 * into the given afs_lhash_stat structure.
180 * afs_lhash_stat() returns 0 on success, nonzero otherwise.
186 , struct afs_lhash_stat *sb
189 #endif /* AFS_LHASH_H */