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
)
82 /* --- @hash_bin@ --- *
84 * Arguments: @hash_table *t@ = pointer to hashtable
85 * @uint32 hash@ = hash value to look up
87 * Returns: Pointer to the bin's list head.
89 * Use: Given a hash value return the address of the appropriate list
90 * head. It is safe to remove the current entry from the table.
93 hash_base
**hash_bin(hash_table
*t
, uint32 hash
)
95 return (HASH_BIN(t
, hash
));
98 /* --- @hash_extend@ --- *
100 * Arguments: @hash_table *t@ = pointer to hashtable to extend
102 * Returns: Nonzero if extension was successful.
104 * Use: Extends a hashtable. The number of bins is doubled and the
105 * entries are redistributed.
108 int hash_extend(hash_table
*t
)
111 uint32 m
= t
->mask
+ 1;
114 /* --- Allocate a new hash bin vector --- */
116 if ((v
= A_REALLOC(t
->a
, t
->v
,
117 2 * m
* sizeof(hash_base
*),
118 m
* sizeof(hash_base
*))) == 0) {
122 t
->mask
= (m
* 2) - 1;
124 /* --- Wander through the table rehashing things --- */
126 for (i
= 0; i
< m
; i
++) {
127 hash_base
**p
= v
+ i
;
128 hash_base
**q
= p
+ m
;
131 if (!((*p
)->hash
& m
))
146 /* --- @hash_remove@ --- *
148 * Arguments: @hash_table *t@ = pointer to hashtable
149 * @hash_base *p@ = pointer to item to remove
153 * Use: Removes an item from a hashtable. The item itself is not
154 * freed, although it is removed from the data structure and is
158 void hash_remove(hash_table
*t
, hash_base
*p
)
160 hash_base
**b
= HASH_BIN(t
, p
->hash
);
171 /* --- @hash_mkiter@ --- *
173 * Arguments: @hash_iter *i@ = pointer to iterator to create
174 * @hash_table *t@ = pointer to hashtable to iterate
178 * Use: Initializes a hashtable iterator.
181 void hash_mkiter(hash_iter
*i
, hash_table
*t
) { HASH_MKITER(i
, t
); }
183 /* --- @hash_next@ --- *
185 * Arguments: @hash_iter *i@ = pointer to iterator
187 * Returns: Pointer to the next hashtable entry found, or null.
189 * Use: Returns the next entry from the hashtable.
192 hash_base
*hash_next(hash_iter
*i
)
199 /*----- That's all, folks -------------------------------------------------*/