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,
28 /*----- Header files ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @hash_create@ --- *
40 * Arguments: @hash_table *t@ = pointer to hashtable to initialize
41 * @size_t n@ = number of bins to allocate (zero initially)
45 * Use: Initializes a hashtable with a given number of bins. The
46 * bins are initially empty. The number of bins must be a power
47 * of two. This is not checked.
50 void hash_create(hash_table *t, size_t n)
55 X_NEWV(t->v, t->a, n);
57 for (v = t->v; n; v++, n--)
61 /* --- @hash_destroy@ --- *
63 * Arguments: @hash_table *t@ = pointer to hashtable to destroy
67 * Use: Frees a hashtable. The items are not freed: they are the
68 * responsibility of the implementation.
71 void hash_destroy(hash_table *t) { x_free(t->a, t->v); }
73 /* --- @hash_bin@ --- *
75 * Arguments: @hash_table *t@ = pointer to hashtable
76 * @uint32 hash@ = hash value to look up
78 * Returns: Pointer to the bin's list head.
80 * Use: Given a hash value return the address of the appropriate list
81 * head. It is safe to remove the current entry from the table.
84 hash_base **hash_bin(hash_table *t, uint32 hash)
85 { return (HASH_BIN(t, hash)); }
87 /* --- @hash_extend@ --- *
89 * Arguments: @hash_table *t@ = pointer to hashtable to extend
91 * Returns: Nonzero if extension was successful.
93 * Use: Extends a hashtable. The number of bins is doubled and the
94 * entries are redistributed.
97 int hash_extend(hash_table *t)
100 uint32 m = t->mask + 1;
103 /* --- Allocate a new hash bin vector --- */
105 if ((v = A_REALLOC(t->a, t->v,
106 2 * m * sizeof(hash_base *),
107 m * sizeof(hash_base *))) == 0) {
111 t->mask = (m * 2) - 1;
113 /* --- Wander through the table rehashing things --- */
115 for (i = 0; i < m; i++) {
116 hash_base **p = v + i;
117 hash_base **q = p + m;
120 if (!((*p)->hash & m))
135 /* --- @hash_remove@ --- *
137 * Arguments: @hash_table *t@ = pointer to hashtable
138 * @hash_base *p@ = pointer to item to remove
142 * Use: Removes an item from a hashtable. The item itself is not
143 * freed, although it is removed from the data structure and is
147 void hash_remove(hash_table *t, hash_base *p)
149 hash_base **b = HASH_BIN(t, p->hash);
160 /* --- @hash_mkiter@ --- *
162 * Arguments: @hash_iter *i@ = pointer to iterator to create
163 * @hash_table *t@ = pointer to hashtable to iterate
167 * Use: Initializes a hashtable iterator.
170 void hash_mkiter(hash_iter *i, hash_table *t) { HASH_MKITER(i, 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 hash_base *hash_next(hash_iter *i)
188 /*----- That's all, folks -------------------------------------------------*/