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 ------------------------------------------------------*/
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @hash_create@ --- *
44 * Arguments: @hash_table *t@ = pointer to hashtable to initialize
45 * @size_t n@ = number of bins to allocate (zero initially)
49 * Use: Initializes a hashtable with a given number of bins. The
50 * bins are initially empty. The number of bins must be a power
51 * of two. This is not checked.
54 void hash_create(hash_table
*t
, size_t n
)
59 t
->v
= x_alloc(t
->a
, n
* sizeof(hash_base
*));
61 for (v
= t
->v
; n
; v
++, n
--)
65 /* --- @hash_destroy@ --- *
67 * Arguments: @hash_table *t@ = pointer to hashtable to destroy
71 * Use: Frees a hashtable. The items are not freed: they are the
72 * responsibility of the implementation.
75 void hash_destroy(hash_table
*t
) { x_free(t
->a
, t
->v
); }
77 /* --- @hash_bin@ --- *
79 * Arguments: @hash_table *t@ = pointer to hashtable
80 * @uint32 hash@ = hash value to look up
82 * Returns: Pointer to the bin's list head.
84 * Use: Given a hash value return the address of the appropriate list
85 * head. It is safe to remove the current entry from the table.
88 hash_base
**hash_bin(hash_table
*t
, uint32 hash
)
89 { return (HASH_BIN(t
, hash
)); }
91 /* --- @hash_extend@ --- *
93 * Arguments: @hash_table *t@ = pointer to hashtable to extend
95 * Returns: Nonzero if extension was successful.
97 * Use: Extends a hashtable. The number of bins is doubled and the
98 * entries are redistributed.
101 int hash_extend(hash_table
*t
)
104 uint32 m
= t
->mask
+ 1;
107 /* --- Allocate a new hash bin vector --- */
109 if ((v
= A_REALLOC(t
->a
, t
->v
,
110 2 * m
* sizeof(hash_base
*),
111 m
* sizeof(hash_base
*))) == 0) {
115 t
->mask
= (m
* 2) - 1;
117 /* --- Wander through the table rehashing things --- */
119 for (i
= 0; i
< m
; i
++) {
120 hash_base
**p
= v
+ i
;
121 hash_base
**q
= p
+ m
;
124 if (!((*p
)->hash
& m
))
139 /* --- @hash_remove@ --- *
141 * Arguments: @hash_table *t@ = pointer to hashtable
142 * @hash_base *p@ = pointer to item to remove
146 * Use: Removes an item from a hashtable. The item itself is not
147 * freed, although it is removed from the data structure and is
151 void hash_remove(hash_table
*t
, hash_base
*p
)
153 hash_base
**b
= HASH_BIN(t
, p
->hash
);
164 /* --- @hash_mkiter@ --- *
166 * Arguments: @hash_iter *i@ = pointer to iterator to create
167 * @hash_table *t@ = pointer to hashtable to iterate
171 * Use: Initializes a hashtable iterator.
174 void hash_mkiter(hash_iter
*i
, hash_table
*t
) { HASH_MKITER(i
, t
); }
176 /* --- @hash_next@ --- *
178 * Arguments: @hash_iter *i@ = pointer to iterator
180 * Returns: Pointer to the next hashtable entry found, or null.
182 * Use: Returns the next entry from the hashtable.
185 hash_base
*hash_next(hash_iter
*i
)
192 /*----- That's all, folks -------------------------------------------------*/