3 * $Id: hash.c,v 1.4 2004/04/08 01:36:11 mdw Exp $
5 * General hashtable infrastructure
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @hash_create@ --- *
46 * Arguments: @hash_table *t@ = pointer to hashtable to initialize
47 * @size_t n@ = number of bins to allocate (zero initially)
51 * Use: Initializes a hashtable with a given number of bins. The
52 * bins are initially empty. The number of bins must be a power
53 * of two. This is not checked.
56 void hash_create(hash_table *t, size_t n)
61 t->v = x_alloc(t->a, n * sizeof(hash_base *));
63 for (v = t->v; n; v++, n--)
67 /* --- @hash_destroy@ --- *
69 * Arguments: @hash_table *t@ = pointer to hashtable to destroy
73 * Use: Frees a hashtable. The items are not freed: they are the
74 * responsibility of the implementation.
77 void hash_destroy(hash_table *t) { x_free(t->a, t->v); }
79 /* --- @hash_bin@ --- *
81 * Arguments: @hash_table *t@ = pointer to hashtable
82 * @uint32 hash@ = hash value to look up
84 * Returns: Pointer to the bin's list head.
86 * Use: Given a hash value return the address of the appropriate list
87 * head. It is safe to remove the current entry from the table.
90 hash_base **hash_bin(hash_table *t, uint32 hash)
91 { return (HASH_BIN(t, hash)); }
93 /* --- @hash_extend@ --- *
95 * Arguments: @hash_table *t@ = pointer to hashtable to extend
97 * Returns: Nonzero if extension was successful.
99 * Use: Extends a hashtable. The number of bins is doubled and the
100 * entries are redistributed.
103 int hash_extend(hash_table *t)
106 uint32 m = t->mask + 1;
109 /* --- Allocate a new hash bin vector --- */
111 if ((v = A_REALLOC(t->a, t->v,
112 2 * m * sizeof(hash_base *),
113 m * sizeof(hash_base *))) == 0) {
117 t->mask = (m * 2) - 1;
119 /* --- Wander through the table rehashing things --- */
121 for (i = 0; i < m; i++) {
122 hash_base **p = v + i;
123 hash_base **q = p + m;
126 if (!((*p)->hash & m))
141 /* --- @hash_remove@ --- *
143 * Arguments: @hash_table *t@ = pointer to hashtable
144 * @hash_base *p@ = pointer to item to remove
148 * Use: Removes an item from a hashtable. The item itself is not
149 * freed, although it is removed from the data structure and is
153 void hash_remove(hash_table *t, hash_base *p)
155 hash_base **b = HASH_BIN(t, p->hash);
166 /* --- @hash_mkiter@ --- *
168 * Arguments: @hash_iter *i@ = pointer to iterator to create
169 * @hash_table *t@ = pointer to hashtable to iterate
173 * Use: Initializes a hashtable iterator.
176 void hash_mkiter(hash_iter *i, hash_table *t) { HASH_MKITER(i, t); }
178 /* --- @hash_next@ --- *
180 * Arguments: @hash_iter *i@ = pointer to iterator
182 * Returns: Pointer to the next hashtable entry found, or null.
184 * Use: Returns the next entry from the hashtable.
187 hash_base *hash_next(hash_iter *i)
194 /*----- That's all, folks -------------------------------------------------*/