| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: hash.c,v 1.3 2000/07/16 12:29:16 mdw Exp $ |
| 4 | * |
| 5 | * General hashtable infrastructure |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of the mLib utilities library. |
| 13 | * |
| 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. |
| 18 | * |
| 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. |
| 23 | * |
| 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, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: hash.c,v $ |
| 33 | * Revision 1.3 2000/07/16 12:29:16 mdw |
| 34 | * Change to arena `realloc' interface, to fix a design bug. |
| 35 | * |
| 36 | * Revision 1.2 2000/06/17 10:37:39 mdw |
| 37 | * Add support for arena management. |
| 38 | * |
| 39 | * Revision 1.1 1999/08/02 14:45:48 mdw |
| 40 | * Break low-level hashtable code out from sym. |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | /*----- Header files ------------------------------------------------------*/ |
| 45 | |
| 46 | #include <stdio.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <string.h> |
| 49 | |
| 50 | #include "alloc.h" |
| 51 | #include "arena.h" |
| 52 | #include "bits.h" |
| 53 | #include "exc.h" |
| 54 | #include "hash.h" |
| 55 | |
| 56 | /*----- Main code ---------------------------------------------------------*/ |
| 57 | |
| 58 | /* --- @hash_create@ --- * |
| 59 | * |
| 60 | * Arguments: @hash_table *t@ = pointer to hashtable to initialize |
| 61 | * @size_t n@ = number of bins to allocate (zero initially) |
| 62 | * |
| 63 | * Returns: --- |
| 64 | * |
| 65 | * Use: Initializes a hashtable with a given number of bins. The |
| 66 | * bins are initially empty. The number of bins must be a power |
| 67 | * of two. This is not checked. |
| 68 | */ |
| 69 | |
| 70 | void hash_create(hash_table *t, size_t n) |
| 71 | { |
| 72 | hash_base **v; |
| 73 | |
| 74 | t->a = arena_global; |
| 75 | t->v = x_alloc(t->a, n * sizeof(hash_base *)); |
| 76 | t->mask = n - 1; |
| 77 | for (v = t->v; n; v++, n--) |
| 78 | *v = 0; |
| 79 | } |
| 80 | |
| 81 | /* --- @hash_destroy@ --- * |
| 82 | * |
| 83 | * Arguments: @hash_table *t@ = pointer to hashtable to destroy |
| 84 | * |
| 85 | * Returns: --- |
| 86 | * |
| 87 | * Use: Frees a hashtable. The items are not freed: they are the |
| 88 | * responsibility of the implementation. |
| 89 | */ |
| 90 | |
| 91 | void hash_destroy(hash_table *t) |
| 92 | { |
| 93 | x_free(t->a, t->v); |
| 94 | } |
| 95 | |
| 96 | /* --- @hash_bin@ --- * |
| 97 | * |
| 98 | * Arguments: @hash_table *t@ = pointer to hashtable |
| 99 | * @uint32 hash@ = hash value to look up |
| 100 | * |
| 101 | * Returns: Pointer to the bin's list head. |
| 102 | * |
| 103 | * Use: Given a hash value return the address of the appropriate list |
| 104 | * head. It is safe to remove the current entry from the table. |
| 105 | */ |
| 106 | |
| 107 | hash_base **hash_bin(hash_table *t, uint32 hash) |
| 108 | { |
| 109 | return (HASH_BIN(t, hash)); |
| 110 | } |
| 111 | |
| 112 | /* --- @hash_extend@ --- * |
| 113 | * |
| 114 | * Arguments: @hash_table *t@ = pointer to hashtable to extend |
| 115 | * |
| 116 | * Returns: Nonzero if extension was successful. |
| 117 | * |
| 118 | * Use: Extends a hashtable. The number of bins is doubled and the |
| 119 | * entries are redistributed. |
| 120 | */ |
| 121 | |
| 122 | int hash_extend(hash_table *t) |
| 123 | { |
| 124 | hash_base **v; |
| 125 | uint32 m = t->mask + 1; |
| 126 | size_t i; |
| 127 | |
| 128 | /* --- Allocate a new hash bin vector --- */ |
| 129 | |
| 130 | if ((v = A_REALLOC(t->a, t->v, |
| 131 | 2 * m * sizeof(hash_base *), |
| 132 | m * sizeof(hash_base *))) == 0) { |
| 133 | return (0); |
| 134 | } |
| 135 | t->v = v; |
| 136 | t->mask = (m * 2) - 1; |
| 137 | |
| 138 | /* --- Wander through the table rehashing things --- */ |
| 139 | |
| 140 | for (i = 0; i < m; i++) { |
| 141 | hash_base **p = v + i; |
| 142 | hash_base **q = p + m; |
| 143 | |
| 144 | while (*p) { |
| 145 | if (!((*p)->hash & m)) |
| 146 | p = &(*p)->next; |
| 147 | else { |
| 148 | *q = *p; |
| 149 | q = &(*q)->next; |
| 150 | *p = (*p)->next; |
| 151 | } |
| 152 | } |
| 153 | *p = 0; |
| 154 | *q = 0; |
| 155 | } |
| 156 | |
| 157 | return (1); |
| 158 | } |
| 159 | |
| 160 | /* --- @hash_remove@ --- * |
| 161 | * |
| 162 | * Arguments: @hash_table *t@ = pointer to hashtable |
| 163 | * @hash_base *p@ = pointer to item to remove |
| 164 | * |
| 165 | * Returns: --- |
| 166 | * |
| 167 | * Use: Removes an item from a hashtable. The item itself is not |
| 168 | * freed, although it is removed from the data structure and is |
| 169 | * safe to free. |
| 170 | */ |
| 171 | |
| 172 | void hash_remove(hash_table *t, hash_base *p) |
| 173 | { |
| 174 | hash_base **b = HASH_BIN(t, p->hash); |
| 175 | while (*b) { |
| 176 | if (*b == p) { |
| 177 | *b = p->next; |
| 178 | return; |
| 179 | } |
| 180 | b = &(*b)->next; |
| 181 | } |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | /* --- @hash_mkiter@ --- * |
| 186 | * |
| 187 | * Arguments: @hash_iter *i@ = pointer to iterator to create |
| 188 | * @hash_table *t@ = pointer to hashtable to iterate |
| 189 | * |
| 190 | * Returns: --- |
| 191 | * |
| 192 | * Use: Initializes a hashtable iterator. |
| 193 | */ |
| 194 | |
| 195 | void hash_mkiter(hash_iter *i, hash_table *t) { HASH_MKITER(i, t); } |
| 196 | |
| 197 | /* --- @hash_next@ --- * |
| 198 | * |
| 199 | * Arguments: @hash_iter *i@ = pointer to iterator |
| 200 | * |
| 201 | * Returns: Pointer to the next hashtable entry found, or null. |
| 202 | * |
| 203 | * Use: Returns the next entry from the hashtable. |
| 204 | */ |
| 205 | |
| 206 | hash_base *hash_next(hash_iter *i) |
| 207 | { |
| 208 | hash_base *b; |
| 209 | HASH_NEXT(i, b); |
| 210 | return (b); |
| 211 | } |
| 212 | |
| 213 | /*----- That's all, folks -------------------------------------------------*/ |