3 * General hashtable infrastructure
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /*----- Notes -------------------------------------------------------------*
37 * This isn't a complete implementation of anything. It's a collection of
38 * useful types and functions which will help when building hashtables of
39 * things. The general-purpose hashtable is provided by the @sym@ functions.
42 /*----- Header files ------------------------------------------------------*/
54 /*----- Data structures ---------------------------------------------------*/
56 /* --- Hashtable basics --- *
58 * This contains the bare minimum to actually get anything useful done. In
59 * particular it doesn't maintain any weighting information: when to extend
60 * the table is left up to the particular implementation.
63 typedef struct hash_table {
64 uint32 mask; /* Bit mask of hash bits */
65 struct hash_base **v; /* Vector of hash bins */
66 arena *a; /* Allocation arena */
69 /* --- A hashtable entry --- *
71 * This is the bare minimum of what needs to be remembered in each hashtable
72 * entry. Comparison of elements is left to the implementation, so I don't
73 * need to know anything about how to represent hash keys here.
76 typedef struct hash_base {
77 struct hash_base *next; /* Next entry in hash bin */
78 uint32 hash; /* Hash value for this entry */
81 /* --- A hashtable iterator --- */
83 typedef struct hash_iter {
84 hash_table *t; /* Hashtable being iterated */
85 hash_base *p; /* Address of next item to return */
86 size_t i; /* Index of next hash bin to use */
89 /*----- Functions provided ------------------------------------------------*/
91 /* --- @hash_create@ --- *
93 * Arguments: @hash_table *t@ = pointer to hashtable to initialize
94 * @size_t n@ = number of bins to allocate (zero initially)
98 * Use: Initializes a hashtable with a given number of bins. The
99 * bins are initially empty. The number of bins must be a power
100 * of two. This is not checked.
103 extern void hash_create(hash_table */*t*/, size_t /*n*/);
105 /* --- @hash_destroy@ --- *
107 * Arguments: @hash_table *t@ = pointer to hashtable to destroy
111 * Use: Frees a hashtable. The items are not freed: they are the
112 * responsibility of the implementation.
115 extern void hash_destroy(hash_table */*t*/);
117 /* --- @hash_bin@ --- *
119 * Arguments: @hash_table *t@ = pointer to hashtable
120 * @uint32 hash@ = hash value to look up
122 * Returns: Pointer to the bin's list head.
124 * Use: Given a hash value return the address of the appropriate list
125 * head. It is safe to remove the current entry from the table.
128 #define HASH_BIN(t, hash) ((t)->v + ((hash) & (t)->mask))
130 extern hash_base **hash_bin(hash_table */*t*/, uint32 /*hash*/);
132 /* --- @hash_extend@ --- *
134 * Arguments: @hash_table *t@ = pointer to hashtable to extend
136 * Returns: Nonzero if extension was successful.
138 * Use: Extends a hashtable. The number of bins is doubled and the
139 * entries are redistributed.
142 extern int hash_extend(hash_table */*t*/);
144 /* --- @hash_remove@ --- *
146 * Arguments: @hash_table *t@ = pointer to hashtable
147 * @hash_base *p@ = pointer to item to remove
151 * Use: Removes an item from a hashtable. The item itself is not
152 * freed, although it is removed from the data structure and is
156 extern void hash_remove(hash_table */*t*/, hash_base */*p*/);
158 /* --- @hash_mkiter@ --- *
160 * Arguments: @hash_iter *i@ = pointer to iterator to create
161 * @hash_table *t@ = pointer to hashtable to iterate
165 * Use: Initializes a hashtable iterator.
168 #define HASH_MKITER(i_, t_) ((i_)->t = (t_), (i_)->p = 0, (i_)->i = 0)
170 extern void hash_mkiter(hash_iter */*i*/, hash_table */*t*/);
172 /* --- @hash_next@ --- *
174 * Arguments: @hash_iter *i@ = pointer to iterator
176 * Returns: Pointer to the next hashtable entry found, or null.
178 * Use: Returns the next entry from the hashtable.
181 #define HASH_NEXT(i_, b_) do { \
182 hash_iter *_i = (i_); \
184 hash_table *_t = _i->t; \
185 uint32 _m = _t->mask; \
192 } else if (_i->i > _m) { \
196 _i->p = _t->v[_i->i++]; \
201 extern hash_base *hash_next(hash_iter */*i*/);
203 /*----- That's all, folks -------------------------------------------------*/