17 \h'-\w'\\$1\ 'u'\\$1\ \c
22 .TH hash 3 "2 August 1999" "Straylight/Edgeware" "mLib utilities library"
24 hash \- low-level hashtable implementation
39 .B "#include <mLib/hash.h>"
49 .B " hash_base *next;"
53 .B "typedef struct { ...\& } hash_iter;"
55 .BI "void hash_create(hash_table *" t ", size_t " n );
56 .BI "void hash_destroy(hash_table *" t );
57 .BI "hash_base **hash_bin(hash_table *" t ", uint32 " hash );
58 .BI "int hash_extend(hash_table *" t );
59 .BI "void hash_remove(hash_table *" t ", hash_base *" b );
60 .BI "void hash_mkiter(hash_iter *" i ", hash_table *" t );
61 .BI "hash_base *hash_next(hash_iter *" i );
63 .BI "hash_base **HASH_BIN(hash_table *" t ", uint32 " hash );
64 .BI "void HASH_MKITER(hash_iter *" i ", hash_table *" t );
65 .BI "void HASH_NEXT(hash_iter *" i ", " b );
70 functions provide the basis for an extensible hashtable implementation.
71 The implementation is not complete. Many decisions have been left to
74 how keys should be represented, hashed and compared;
76 how objects contained within the table should be allocated; and
78 when the hashtable should be extended.
80 A complete hashtable implementation will need to take the above
81 decisions. If you just want a prepackaged solution, see
84 .SH "IMPLEMENTATION DETAILS"
85 Each item in the hashtable is assigned a 32-bit integer
87 a number computed somehow from the item's data such that two items which
88 are considered equal will yield the same hash. Ideally, different items
89 will yield different hashes. It is important for this implementation
90 that all bits of the hash are similarly good.
92 In order to look up an item, the high bits of the hash are masked off
93 and the remainder used as an index into a vector of
95 Each bin contains a list of items with identical low-order bits of their
98 A table expansion involves doubling the size of the bin vector. Each
99 bin list is then split into two, items being placed into a new bin
100 depending on the setting of the next lowest hash bit. By expanding the
101 hashtable as needed, lookups remain constant-time.
103 A low-level hashtable is represented by a
105 structure. It contains two members:
108 The current bitmask to be applied to hashes. This is one less than the
109 current number of bins in the hashtable, and is applied to hash values
110 in order to decide which bin an item should be in.
113 The bin vector. It is an array of pointers to hashtable items.
115 A hashtable item consists of a
117 structure. A full hashtable implementation will need to extend this
118 structure by adding keying information and other data; the
120 only contains the bare minimum of information needed to maintain the
121 hashtable at a low level. It contains the following members:
124 Pointer to the next item in the bin list. The final item has a null
126 pointer. The entry in the bin vector is null if the bin list is empty.
127 It is up to the high-level implementation to insert items into the list.
130 The hash for this item. This must be the full 32-bit hash for the
131 current item. It is used during hashtable expansion to determine which
132 bin an item should be moved to.
133 .SH "FUNCTIONALITY PROVIDED"
134 This section describes the functions and macros provided for building
135 hashtables. Code examples are given throughout. They assume the
136 following definitions:
138 /* --- A table of items --- */
140 typedef struct item_table {
145 /* --- An item --- */
147 typedef struct item {
153 The implementation presented here is simple but relatively bad. The
156 presents a more realistic example, but is rather more complex.
157 .SS "Initialization and finalization"
158 An empty hashtable is initialized by calling
160 with the address of a
162 structure to be filled in and the initial number of hash bins to create.
164 For example, an item table might be initialized like this:
166 void item_createtab(item_table *t)
168 hash_create(&t->t, ITEM_INITSZ);
169 t->load = ITEM_INITLOAD;
172 A hashtable can be destroyed by calling
174 with the address of the
176 structure. This does not attempt to deallocate the individual items;
177 that must be done beforehand.
179 The usual way to deallocate the individual hashtable items is using the
180 iteration constructs described below.
182 void item_destroytab(item_table *t)
186 for (hash_mkiter(&i, &t->t); (b = hash_next(&i)) != 0; ) {
187 item *ii = (item *)b;
196 .SS "Searching, adding and removing"
197 Items must be searched for and added by hand.
201 returns the address of the bin list haed for a particular hashtable and
202 hash value. The function
204 works the same way and provides the same result, but since the macro is
205 very simple its use is preferred. However, it will evaluate its
206 arguments multiple times.
208 Once the bin list has been found, it's fairly easy to search for an
209 exact match. A simple search might look something like this:
211 item *lookup(item_table *t, const char *k)
213 uint32 h = hash(k); /* Hash @k@ somehow */
214 hash_base **bin = HASH_BIN(&t->t, h);
216 for (b = *bin; b; b = b->next) {
218 if (h == i->b.hash && strcmp(k, i->k) == 0)
224 Insertion is also relatively trivial given the bin list head. Insertion
225 may make the hashtable too large, so it might be necessary to extend
226 it. Extension is performed by
228 which is passed only the address of the hashtable. It returns nonzero
229 if extension was successful.
231 item *add(item_table *t, const char *k, /* ... */)
237 /* --- See if the item is already there --- */
239 if ((i = = lookup(t, k)) != 0)
242 /* --- Make a new hashtable item --- */
248 /* --- Link it into the bin list --- */
250 h = i->b.hash = hash(k);
251 bin = HASH_BIN(&t->t, h);
255 /* --- Maybe extend the hashtable --- */
259 else if (hash_extend(&t->t))
260 t->load = recalc_load(t);
269 implementation is rather more sophisticated in its approach but the idea
270 is the same. In particular,
272 provides a single interface for lookup and insertion which prevents the
273 unnecessary rehashing performed by the above code.
275 Removal of items is more straightforward. The function
277 will unlink a given item from its bin list, after which point it is safe
280 Iteration allows code to be performed on all the items in a hashtable.
281 This is done using an
283 object, represented by a
285 structure. An iterator is initialized by calling
289 yields a different item from the hashtable until there are none left, a
290 condition signified by a null return value.
296 do the same jobs as the above functions. However,
298 has a slightly bizarre argument passing convention: its second argument
301 which is updated to contain the address of the next item.
303 The finalization code above contained an example of iteration.
308 Mark Wooding, <mdw@distorted.org.uk>