1 /* An expandable hash tables datatype.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov (vmakarov@cygnus.com).
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* This package implements basic hash table functionality. It is possible
22 to search for an entry, create an entry and destroy an entry.
24 Elements in the table are generic pointers.
26 The size of the table is not fixed; if the occupancy of the table
27 grows too high the hash table will be expanded.
29 The abstract data implementation is based on generalized Algorithm D
30 from Knuth's book "The art of computer programming". Hash table is
31 expanded by creation of new hash table and transferring elements from
32 the old table to the new table. */
38 #include <sys/types.h>
46 #include "libiberty.h"
49 /* This macro defines reserved value for empty table entry. */
51 #define EMPTY_ENTRY ((void *) 0)
53 /* This macro defines reserved value for table entry which contained
56 #define DELETED_ENTRY ((void *) 1)
58 static unsigned long higher_prime_number PARAMS ((unsigned long));
60 /* The following function returns the nearest prime number which is
61 greater than a given source number, N. */
64 higher_prime_number (n)
69 /* Ensure we have a larger number and then force to odd. */
73 /* All odd numbers < 9 are prime. */
77 /* Otherwise find the next prime using a sieve. */
81 for (i = 3; i * i <= n; i += 2)
91 /* This function creates table with length slightly longer than given
92 source length. Created hash table is initiated as empty (all the
93 hash table entries are EMPTY_ENTRY). The function returns the
94 created hash table. */
97 htab_create (size, hash_f, eq_f, del_f)
105 size = higher_prime_number (size);
106 result = (htab_t) xcalloc (1, sizeof (struct htab));
107 result->entries = (void **) xcalloc (size, sizeof (void *));
109 result->hash_f = hash_f;
111 result->del_f = del_f;
115 /* This function frees all memory allocated for given hash table.
116 Naturally the hash table must already exist. */
125 for (i = htab->size - 1; i >= 0; i--)
126 if (htab->entries[i] != EMPTY_ENTRY
127 && htab->entries[i] != DELETED_ENTRY)
128 (*htab->del_f) (htab->entries[i]);
130 free (htab->entries);
134 /* This function clears all entries in the given hash table. */
143 for (i = htab->size - 1; i >= 0; i--)
144 if (htab->entries[i] != EMPTY_ENTRY
145 && htab->entries[i] != DELETED_ENTRY)
146 (*htab->del_f) (htab->entries[i]);
148 memset (htab->entries, 0, htab->size * sizeof (void *));
151 /* Similar to htab_find_slot, but without several unwanted side effects:
152 - Does not call htab->eq_f when it finds an existing entry.
153 - Does not change the count of elements/searches/collisions in the
155 This function also assumes there are no deleted entries in the table.
156 HASH is the hash value for the element to be inserted. */
159 find_empty_slot_for_expand (htab, hash)
163 size_t size = htab->size;
164 hashval_t hash2 = 1 + hash % (size - 2);
165 unsigned int index = hash % size;
169 void **slot = htab->entries + index;
171 if (*slot == EMPTY_ENTRY)
173 else if (*slot == DELETED_ENTRY)
182 /* The following function changes size of memory allocated for the
183 entries and repeatedly inserts the table elements. The occupancy
184 of the table after the call will be about 50%. Naturally the hash
185 table must already exist. Remember also that the place of the
186 table entries is changed. */
196 oentries = htab->entries;
197 olimit = oentries + htab->size;
199 htab->size = higher_prime_number (htab->size * 2);
200 htab->entries = xcalloc (htab->size, sizeof (void **));
202 htab->n_elements -= htab->n_deleted;
210 if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
212 void **q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x));
224 /* This function searches for a hash table entry equal to the given
225 element. It cannot be used to insert or delete an element. */
228 htab_find_with_hash (htab, element, hash)
242 entry = htab->entries[index];
243 if (entry == EMPTY_ENTRY
244 || (entry != DELETED_ENTRY && (*htab->eq_f) (entry, element)))
247 hash2 = 1 + hash % (size - 2);
256 entry = htab->entries[index];
257 if (entry == EMPTY_ENTRY
258 || (entry != DELETED_ENTRY && (*htab->eq_f) (entry, element)))
263 /* Like htab_find_slot_with_hash, but compute the hash value from the
267 htab_find (htab, element)
271 return htab_find_with_hash (htab, element, (*htab->hash_f) (element));
274 /* This function searches for a hash table slot containing an entry
275 equal to the given element. To delete an entry, call this with
276 INSERT = 0, then call htab_clear_slot on the slot returned (possibly
277 after doing some checks). To insert an entry, call this with
278 INSERT = 1, then write the value you want into the returned slot. */
281 htab_find_slot_with_hash (htab, element, hash, insert)
285 enum insert_option insert;
287 void **first_deleted_slot;
292 if (insert == INSERT && htab->size * 3 <= htab->n_elements * 4)
296 hash2 = 1 + hash % (size - 2);
300 first_deleted_slot = NULL;
304 void *entry = htab->entries[index];
305 if (entry == EMPTY_ENTRY)
307 if (insert == NO_INSERT)
312 if (first_deleted_slot)
314 *first_deleted_slot = EMPTY_ENTRY;
315 return first_deleted_slot;
318 return &htab->entries[index];
321 if (entry == DELETED_ENTRY)
323 if (!first_deleted_slot)
324 first_deleted_slot = &htab->entries[index];
326 else if ((*htab->eq_f) (entry, element))
327 return &htab->entries[index];
336 /* Like htab_find_slot_with_hash, but compute the hash value from the
340 htab_find_slot (htab, element, insert)
343 enum insert_option insert;
345 return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element),
349 /* This function deletes an element with the given value from hash
350 table. If there is no matching element in the hash table, this
351 function does nothing. */
354 htab_remove_elt (htab, element)
360 slot = htab_find_slot (htab, element, NO_INSERT);
361 if (*slot == EMPTY_ENTRY)
365 (*htab->del_f) (*slot);
367 *slot = DELETED_ENTRY;
371 /* This function clears a specified slot in a hash table. It is
372 useful when you've already done the lookup and don't want to do it
376 htab_clear_slot (htab, slot)
380 if (slot < htab->entries || slot >= htab->entries + htab->size
381 || *slot == EMPTY_ENTRY || *slot == DELETED_ENTRY)
385 (*htab->del_f) (*slot);
387 *slot = DELETED_ENTRY;
391 /* This function scans over the entire hash table calling
392 CALLBACK for each live entry. If CALLBACK returns false,
393 the iteration stops. INFO is passed as CALLBACK's second
397 htab_traverse (htab, callback, info)
402 void **slot = htab->entries;
403 void **limit = slot + htab->size;
409 if (x != EMPTY_ENTRY && x != DELETED_ENTRY)
410 if (!(*callback) (slot, info))
413 while (++slot < limit);
416 /* Return the current size of given hash table. */
425 /* Return the current number of elements in given hash table. */
431 return htab->n_elements - htab->n_deleted;
434 /* Return the fraction of fixed collisions during all work with given
438 htab_collisions (htab)
441 if (htab->searches == 0)
444 return (double) htab->collisions / (double) htab->searches;