198b9484cSchristos /* An expandable hash tables datatype. 2*5173eb0aSchristos Copyright (C) 1999-2024 Free Software Foundation, Inc. 398b9484cSchristos Contributed by Vladimir Makarov (vmakarov@cygnus.com). 498b9484cSchristos 598b9484cSchristos This file is part of the libiberty library. 698b9484cSchristos Libiberty is free software; you can redistribute it and/or 798b9484cSchristos modify it under the terms of the GNU Library General Public 898b9484cSchristos License as published by the Free Software Foundation; either 998b9484cSchristos version 2 of the License, or (at your option) any later version. 1098b9484cSchristos 1198b9484cSchristos Libiberty is distributed in the hope that it will be useful, 1298b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1398b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1498b9484cSchristos Library General Public License for more details. 1598b9484cSchristos 1698b9484cSchristos You should have received a copy of the GNU Library General Public 1798b9484cSchristos License along with libiberty; see the file COPYING.LIB. If 1898b9484cSchristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 1998b9484cSchristos Boston, MA 02110-1301, USA. */ 2098b9484cSchristos 2198b9484cSchristos /* This package implements basic hash table functionality. It is possible 2298b9484cSchristos to search for an entry, create an entry and destroy an entry. 2398b9484cSchristos 2498b9484cSchristos Elements in the table are generic pointers. 2598b9484cSchristos 2698b9484cSchristos The size of the table is not fixed; if the occupancy of the table 2798b9484cSchristos grows too high the hash table will be expanded. 2898b9484cSchristos 2998b9484cSchristos The abstract data implementation is based on generalized Algorithm D 3098b9484cSchristos from Knuth's book "The art of computer programming". Hash table is 3198b9484cSchristos expanded by creation of new hash table and transferring elements from 3298b9484cSchristos the old table to the new table. */ 3398b9484cSchristos 3498b9484cSchristos #ifdef HAVE_CONFIG_H 3598b9484cSchristos #include "config.h" 3698b9484cSchristos #endif 3798b9484cSchristos 3898b9484cSchristos #include <sys/types.h> 3998b9484cSchristos 4098b9484cSchristos #ifdef HAVE_STDLIB_H 4198b9484cSchristos #include <stdlib.h> 4298b9484cSchristos #endif 4398b9484cSchristos #ifdef HAVE_STRING_H 4498b9484cSchristos #include <string.h> 4598b9484cSchristos #endif 4698b9484cSchristos #ifdef HAVE_MALLOC_H 4798b9484cSchristos #include <malloc.h> 4898b9484cSchristos #endif 4998b9484cSchristos #ifdef HAVE_LIMITS_H 5098b9484cSchristos #include <limits.h> 5198b9484cSchristos #endif 5298b9484cSchristos #ifdef HAVE_INTTYPES_H 5398b9484cSchristos #include <inttypes.h> 5498b9484cSchristos #endif 5598b9484cSchristos #ifdef HAVE_STDINT_H 5698b9484cSchristos #include <stdint.h> 5798b9484cSchristos #endif 5898b9484cSchristos 5998b9484cSchristos #include <stdio.h> 6098b9484cSchristos 6198b9484cSchristos #include "libiberty.h" 6298b9484cSchristos #include "ansidecl.h" 6398b9484cSchristos #include "hashtab.h" 6498b9484cSchristos 6598b9484cSchristos #ifndef CHAR_BIT 6698b9484cSchristos #define CHAR_BIT 8 6798b9484cSchristos #endif 6898b9484cSchristos 6998b9484cSchristos static unsigned int higher_prime_index (unsigned long); 7098b9484cSchristos static hashval_t htab_mod_1 (hashval_t, hashval_t, hashval_t, int); 7198b9484cSchristos static hashval_t htab_mod (hashval_t, htab_t); 7298b9484cSchristos static hashval_t htab_mod_m2 (hashval_t, htab_t); 7398b9484cSchristos static hashval_t hash_pointer (const void *); 7498b9484cSchristos static int eq_pointer (const void *, const void *); 7598b9484cSchristos static int htab_expand (htab_t); 764b169a6bSchristos static void **find_empty_slot_for_expand (htab_t, hashval_t); 7798b9484cSchristos 7898b9484cSchristos /* At some point, we could make these be NULL, and modify the 7998b9484cSchristos hash-table routines to handle NULL specially; that would avoid 8098b9484cSchristos function-call overhead for the common case of hashing pointers. */ 8198b9484cSchristos htab_hash htab_hash_pointer = hash_pointer; 8298b9484cSchristos htab_eq htab_eq_pointer = eq_pointer; 8398b9484cSchristos 8498b9484cSchristos /* Table of primes and multiplicative inverses. 8598b9484cSchristos 8698b9484cSchristos Note that these are not minimally reduced inverses. Unlike when generating 8798b9484cSchristos code to divide by a constant, we want to be able to use the same algorithm 8898b9484cSchristos all the time. All of these inverses (are implied to) have bit 32 set. 8998b9484cSchristos 9098b9484cSchristos For the record, here's the function that computed the table; it's a 9198b9484cSchristos vastly simplified version of the function of the same name from gcc. */ 9298b9484cSchristos 9398b9484cSchristos #if 0 9498b9484cSchristos unsigned int 9598b9484cSchristos ceil_log2 (unsigned int x) 9698b9484cSchristos { 9798b9484cSchristos int i; 9898b9484cSchristos for (i = 31; i >= 0 ; --i) 9998b9484cSchristos if (x > (1u << i)) 10098b9484cSchristos return i+1; 10198b9484cSchristos abort (); 10298b9484cSchristos } 10398b9484cSchristos 10498b9484cSchristos unsigned int 10598b9484cSchristos choose_multiplier (unsigned int d, unsigned int *mlp, unsigned char *shiftp) 10698b9484cSchristos { 10798b9484cSchristos unsigned long long mhigh; 10898b9484cSchristos double nx; 10998b9484cSchristos int lgup, post_shift; 11098b9484cSchristos int pow, pow2; 11198b9484cSchristos int n = 32, precision = 32; 11298b9484cSchristos 11398b9484cSchristos lgup = ceil_log2 (d); 11498b9484cSchristos pow = n + lgup; 11598b9484cSchristos pow2 = n + lgup - precision; 11698b9484cSchristos 11798b9484cSchristos nx = ldexp (1.0, pow) + ldexp (1.0, pow2); 11898b9484cSchristos mhigh = nx / d; 11998b9484cSchristos 12098b9484cSchristos *shiftp = lgup - 1; 12198b9484cSchristos *mlp = mhigh; 12298b9484cSchristos return mhigh >> 32; 12398b9484cSchristos } 12498b9484cSchristos #endif 12598b9484cSchristos 12698b9484cSchristos struct prime_ent 12798b9484cSchristos { 12898b9484cSchristos hashval_t prime; 12998b9484cSchristos hashval_t inv; 13098b9484cSchristos hashval_t inv_m2; /* inverse of prime-2 */ 13198b9484cSchristos hashval_t shift; 13298b9484cSchristos }; 13398b9484cSchristos 13498b9484cSchristos static struct prime_ent const prime_tab[] = { 13598b9484cSchristos { 7, 0x24924925, 0x9999999b, 2 }, 13698b9484cSchristos { 13, 0x3b13b13c, 0x745d1747, 3 }, 13798b9484cSchristos { 31, 0x08421085, 0x1a7b9612, 4 }, 13898b9484cSchristos { 61, 0x0c9714fc, 0x15b1e5f8, 5 }, 13998b9484cSchristos { 127, 0x02040811, 0x0624dd30, 6 }, 14098b9484cSchristos { 251, 0x05197f7e, 0x073260a5, 7 }, 14198b9484cSchristos { 509, 0x01824366, 0x02864fc8, 8 }, 14298b9484cSchristos { 1021, 0x00c0906d, 0x014191f7, 9 }, 14398b9484cSchristos { 2039, 0x0121456f, 0x0161e69e, 10 }, 14498b9484cSchristos { 4093, 0x00300902, 0x00501908, 11 }, 14598b9484cSchristos { 8191, 0x00080041, 0x00180241, 12 }, 14698b9484cSchristos { 16381, 0x000c0091, 0x00140191, 13 }, 14798b9484cSchristos { 32749, 0x002605a5, 0x002a06e6, 14 }, 14898b9484cSchristos { 65521, 0x000f00e2, 0x00110122, 15 }, 14998b9484cSchristos { 131071, 0x00008001, 0x00018003, 16 }, 15098b9484cSchristos { 262139, 0x00014002, 0x0001c004, 17 }, 15198b9484cSchristos { 524287, 0x00002001, 0x00006001, 18 }, 15298b9484cSchristos { 1048573, 0x00003001, 0x00005001, 19 }, 15398b9484cSchristos { 2097143, 0x00004801, 0x00005801, 20 }, 15498b9484cSchristos { 4194301, 0x00000c01, 0x00001401, 21 }, 15598b9484cSchristos { 8388593, 0x00001e01, 0x00002201, 22 }, 15698b9484cSchristos { 16777213, 0x00000301, 0x00000501, 23 }, 15798b9484cSchristos { 33554393, 0x00001381, 0x00001481, 24 }, 15898b9484cSchristos { 67108859, 0x00000141, 0x000001c1, 25 }, 15998b9484cSchristos { 134217689, 0x000004e1, 0x00000521, 26 }, 16098b9484cSchristos { 268435399, 0x00000391, 0x000003b1, 27 }, 16198b9484cSchristos { 536870909, 0x00000019, 0x00000029, 28 }, 16298b9484cSchristos { 1073741789, 0x0000008d, 0x00000095, 29 }, 16398b9484cSchristos { 2147483647, 0x00000003, 0x00000007, 30 }, 16498b9484cSchristos /* Avoid "decimal constant so large it is unsigned" for 4294967291. */ 16598b9484cSchristos { 0xfffffffb, 0x00000006, 0x00000008, 31 } 16698b9484cSchristos }; 16798b9484cSchristos 16898b9484cSchristos /* The following function returns an index into the above table of the 16998b9484cSchristos nearest prime number which is greater than N, and near a power of two. */ 17098b9484cSchristos 17198b9484cSchristos static unsigned int 17298b9484cSchristos higher_prime_index (unsigned long n) 17398b9484cSchristos { 17498b9484cSchristos unsigned int low = 0; 17598b9484cSchristos unsigned int high = sizeof(prime_tab) / sizeof(prime_tab[0]); 17698b9484cSchristos 17798b9484cSchristos while (low != high) 17898b9484cSchristos { 17998b9484cSchristos unsigned int mid = low + (high - low) / 2; 18098b9484cSchristos if (n > prime_tab[mid].prime) 18198b9484cSchristos low = mid + 1; 18298b9484cSchristos else 18398b9484cSchristos high = mid; 18498b9484cSchristos } 18598b9484cSchristos 18698b9484cSchristos /* If we've run out of primes, abort. */ 18798b9484cSchristos if (n > prime_tab[low].prime) 18898b9484cSchristos { 18998b9484cSchristos fprintf (stderr, "Cannot find prime bigger than %lu\n", n); 19098b9484cSchristos abort (); 19198b9484cSchristos } 19298b9484cSchristos 19398b9484cSchristos return low; 19498b9484cSchristos } 19598b9484cSchristos 19698b9484cSchristos /* Returns non-zero if P1 and P2 are equal. */ 19798b9484cSchristos 19898b9484cSchristos static int 1994b169a6bSchristos eq_pointer (const void *p1, const void *p2) 20098b9484cSchristos { 20198b9484cSchristos return p1 == p2; 20298b9484cSchristos } 20398b9484cSchristos 20498b9484cSchristos 20598b9484cSchristos /* The parens around the function names in the next two definitions 20698b9484cSchristos are essential in order to prevent macro expansions of the name. 20798b9484cSchristos The bodies, however, are expanded as expected, so they are not 20898b9484cSchristos recursive definitions. */ 20998b9484cSchristos 21098b9484cSchristos /* Return the current size of given hash table. */ 21198b9484cSchristos 21298b9484cSchristos #define htab_size(htab) ((htab)->size) 21398b9484cSchristos 21498b9484cSchristos size_t 21598b9484cSchristos (htab_size) (htab_t htab) 21698b9484cSchristos { 21798b9484cSchristos return htab_size (htab); 21898b9484cSchristos } 21998b9484cSchristos 22098b9484cSchristos /* Return the current number of elements in given hash table. */ 22198b9484cSchristos 22298b9484cSchristos #define htab_elements(htab) ((htab)->n_elements - (htab)->n_deleted) 22398b9484cSchristos 22498b9484cSchristos size_t 22598b9484cSchristos (htab_elements) (htab_t htab) 22698b9484cSchristos { 22798b9484cSchristos return htab_elements (htab); 22898b9484cSchristos } 22998b9484cSchristos 23098b9484cSchristos /* Return X % Y. */ 23198b9484cSchristos 23298b9484cSchristos static inline hashval_t 23398b9484cSchristos htab_mod_1 (hashval_t x, hashval_t y, hashval_t inv, int shift) 23498b9484cSchristos { 23598b9484cSchristos /* The multiplicative inverses computed above are for 32-bit types, and 23698b9484cSchristos requires that we be able to compute a highpart multiply. */ 23798b9484cSchristos #ifdef UNSIGNED_64BIT_TYPE 23898b9484cSchristos __extension__ typedef UNSIGNED_64BIT_TYPE ull; 23998b9484cSchristos if (sizeof (hashval_t) * CHAR_BIT <= 32) 24098b9484cSchristos { 24198b9484cSchristos hashval_t t1, t2, t3, t4, q, r; 24298b9484cSchristos 24398b9484cSchristos t1 = ((ull)x * inv) >> 32; 24498b9484cSchristos t2 = x - t1; 24598b9484cSchristos t3 = t2 >> 1; 24698b9484cSchristos t4 = t1 + t3; 24798b9484cSchristos q = t4 >> shift; 24898b9484cSchristos r = x - (q * y); 24998b9484cSchristos 25098b9484cSchristos return r; 25198b9484cSchristos } 25298b9484cSchristos #endif 25398b9484cSchristos 25498b9484cSchristos /* Otherwise just use the native division routines. */ 25598b9484cSchristos return x % y; 25698b9484cSchristos } 25798b9484cSchristos 25898b9484cSchristos /* Compute the primary hash for HASH given HTAB's current size. */ 25998b9484cSchristos 26098b9484cSchristos static inline hashval_t 26198b9484cSchristos htab_mod (hashval_t hash, htab_t htab) 26298b9484cSchristos { 26398b9484cSchristos const struct prime_ent *p = &prime_tab[htab->size_prime_index]; 26498b9484cSchristos return htab_mod_1 (hash, p->prime, p->inv, p->shift); 26598b9484cSchristos } 26698b9484cSchristos 26798b9484cSchristos /* Compute the secondary hash for HASH given HTAB's current size. */ 26898b9484cSchristos 26998b9484cSchristos static inline hashval_t 27098b9484cSchristos htab_mod_m2 (hashval_t hash, htab_t htab) 27198b9484cSchristos { 27298b9484cSchristos const struct prime_ent *p = &prime_tab[htab->size_prime_index]; 27398b9484cSchristos return 1 + htab_mod_1 (hash, p->prime - 2, p->inv_m2, p->shift); 27498b9484cSchristos } 27598b9484cSchristos 27698b9484cSchristos /* This function creates table with length slightly longer than given 27798b9484cSchristos source length. Created hash table is initiated as empty (all the 27898b9484cSchristos hash table entries are HTAB_EMPTY_ENTRY). The function returns the 27998b9484cSchristos created hash table, or NULL if memory allocation fails. */ 28098b9484cSchristos 28198b9484cSchristos htab_t 28298b9484cSchristos htab_create_alloc (size_t size, htab_hash hash_f, htab_eq eq_f, 28398b9484cSchristos htab_del del_f, htab_alloc alloc_f, htab_free free_f) 28498b9484cSchristos { 28598b9484cSchristos return htab_create_typed_alloc (size, hash_f, eq_f, del_f, alloc_f, alloc_f, 28698b9484cSchristos free_f); 28798b9484cSchristos } 28898b9484cSchristos 28998b9484cSchristos /* As above, but uses the variants of ALLOC_F and FREE_F which accept 29098b9484cSchristos an extra argument. */ 29198b9484cSchristos 29298b9484cSchristos htab_t 29398b9484cSchristos htab_create_alloc_ex (size_t size, htab_hash hash_f, htab_eq eq_f, 29498b9484cSchristos htab_del del_f, void *alloc_arg, 29598b9484cSchristos htab_alloc_with_arg alloc_f, 29698b9484cSchristos htab_free_with_arg free_f) 29798b9484cSchristos { 29898b9484cSchristos htab_t result; 29998b9484cSchristos unsigned int size_prime_index; 30098b9484cSchristos 30198b9484cSchristos size_prime_index = higher_prime_index (size); 30298b9484cSchristos size = prime_tab[size_prime_index].prime; 30398b9484cSchristos 30498b9484cSchristos result = (htab_t) (*alloc_f) (alloc_arg, 1, sizeof (struct htab)); 30598b9484cSchristos if (result == NULL) 30698b9484cSchristos return NULL; 3074b169a6bSchristos result->entries = (void **) (*alloc_f) (alloc_arg, size, sizeof (void *)); 30898b9484cSchristos if (result->entries == NULL) 30998b9484cSchristos { 31098b9484cSchristos if (free_f != NULL) 31198b9484cSchristos (*free_f) (alloc_arg, result); 31298b9484cSchristos return NULL; 31398b9484cSchristos } 31498b9484cSchristos result->size = size; 31598b9484cSchristos result->size_prime_index = size_prime_index; 31698b9484cSchristos result->hash_f = hash_f; 31798b9484cSchristos result->eq_f = eq_f; 31898b9484cSchristos result->del_f = del_f; 31998b9484cSchristos result->alloc_arg = alloc_arg; 32098b9484cSchristos result->alloc_with_arg_f = alloc_f; 32198b9484cSchristos result->free_with_arg_f = free_f; 32298b9484cSchristos return result; 32398b9484cSchristos } 32498b9484cSchristos 32598b9484cSchristos /* 32698b9484cSchristos 32798b9484cSchristos @deftypefn Supplemental htab_t htab_create_typed_alloc (size_t @var{size}, @ 32898b9484cSchristos htab_hash @var{hash_f}, htab_eq @var{eq_f}, htab_del @var{del_f}, @ 32998b9484cSchristos htab_alloc @var{alloc_tab_f}, htab_alloc @var{alloc_f}, @ 33098b9484cSchristos htab_free @var{free_f}) 33198b9484cSchristos 33298b9484cSchristos This function creates a hash table that uses two different allocators 33398b9484cSchristos @var{alloc_tab_f} and @var{alloc_f} to use for allocating the table itself 33498b9484cSchristos and its entries respectively. This is useful when variables of different 33598b9484cSchristos types need to be allocated with different allocators. 33698b9484cSchristos 33798b9484cSchristos The created hash table is slightly larger than @var{size} and it is 33898b9484cSchristos initially empty (all the hash table entries are @code{HTAB_EMPTY_ENTRY}). 33998b9484cSchristos The function returns the created hash table, or @code{NULL} if memory 34098b9484cSchristos allocation fails. 34198b9484cSchristos 34298b9484cSchristos @end deftypefn 34398b9484cSchristos 34498b9484cSchristos */ 34598b9484cSchristos 34698b9484cSchristos htab_t 34798b9484cSchristos htab_create_typed_alloc (size_t size, htab_hash hash_f, htab_eq eq_f, 34898b9484cSchristos htab_del del_f, htab_alloc alloc_tab_f, 34998b9484cSchristos htab_alloc alloc_f, htab_free free_f) 35098b9484cSchristos { 35198b9484cSchristos htab_t result; 35298b9484cSchristos unsigned int size_prime_index; 35398b9484cSchristos 35498b9484cSchristos size_prime_index = higher_prime_index (size); 35598b9484cSchristos size = prime_tab[size_prime_index].prime; 35698b9484cSchristos 35798b9484cSchristos result = (htab_t) (*alloc_tab_f) (1, sizeof (struct htab)); 35898b9484cSchristos if (result == NULL) 35998b9484cSchristos return NULL; 3604b169a6bSchristos result->entries = (void **) (*alloc_f) (size, sizeof (void *)); 36198b9484cSchristos if (result->entries == NULL) 36298b9484cSchristos { 36398b9484cSchristos if (free_f != NULL) 36498b9484cSchristos (*free_f) (result); 36598b9484cSchristos return NULL; 36698b9484cSchristos } 36798b9484cSchristos result->size = size; 36898b9484cSchristos result->size_prime_index = size_prime_index; 36998b9484cSchristos result->hash_f = hash_f; 37098b9484cSchristos result->eq_f = eq_f; 37198b9484cSchristos result->del_f = del_f; 37298b9484cSchristos result->alloc_f = alloc_f; 37398b9484cSchristos result->free_f = free_f; 37498b9484cSchristos return result; 37598b9484cSchristos } 37698b9484cSchristos 37798b9484cSchristos 37898b9484cSchristos /* Update the function pointers and allocation parameter in the htab_t. */ 37998b9484cSchristos 38098b9484cSchristos void 38198b9484cSchristos htab_set_functions_ex (htab_t htab, htab_hash hash_f, htab_eq eq_f, 3824b169a6bSchristos htab_del del_f, void *alloc_arg, 38398b9484cSchristos htab_alloc_with_arg alloc_f, htab_free_with_arg free_f) 38498b9484cSchristos { 38598b9484cSchristos htab->hash_f = hash_f; 38698b9484cSchristos htab->eq_f = eq_f; 38798b9484cSchristos htab->del_f = del_f; 38898b9484cSchristos htab->alloc_arg = alloc_arg; 38998b9484cSchristos htab->alloc_with_arg_f = alloc_f; 39098b9484cSchristos htab->free_with_arg_f = free_f; 39198b9484cSchristos } 39298b9484cSchristos 39398b9484cSchristos /* These functions exist solely for backward compatibility. */ 39498b9484cSchristos 39598b9484cSchristos #undef htab_create 39698b9484cSchristos htab_t 39798b9484cSchristos htab_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f) 39898b9484cSchristos { 39998b9484cSchristos return htab_create_alloc (size, hash_f, eq_f, del_f, xcalloc, free); 40098b9484cSchristos } 40198b9484cSchristos 40298b9484cSchristos htab_t 40398b9484cSchristos htab_try_create (size_t size, htab_hash hash_f, htab_eq eq_f, htab_del del_f) 40498b9484cSchristos { 40598b9484cSchristos return htab_create_alloc (size, hash_f, eq_f, del_f, calloc, free); 40698b9484cSchristos } 40798b9484cSchristos 40898b9484cSchristos /* This function frees all memory allocated for given hash table. 40998b9484cSchristos Naturally the hash table must already exist. */ 41098b9484cSchristos 41198b9484cSchristos void 41298b9484cSchristos htab_delete (htab_t htab) 41398b9484cSchristos { 41498b9484cSchristos size_t size = htab_size (htab); 4154b169a6bSchristos void **entries = htab->entries; 41698b9484cSchristos int i; 41798b9484cSchristos 41898b9484cSchristos if (htab->del_f) 41998b9484cSchristos for (i = size - 1; i >= 0; i--) 42098b9484cSchristos if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY) 42198b9484cSchristos (*htab->del_f) (entries[i]); 42298b9484cSchristos 42398b9484cSchristos if (htab->free_f != NULL) 42498b9484cSchristos { 42598b9484cSchristos (*htab->free_f) (entries); 42698b9484cSchristos (*htab->free_f) (htab); 42798b9484cSchristos } 42898b9484cSchristos else if (htab->free_with_arg_f != NULL) 42998b9484cSchristos { 43098b9484cSchristos (*htab->free_with_arg_f) (htab->alloc_arg, entries); 43198b9484cSchristos (*htab->free_with_arg_f) (htab->alloc_arg, htab); 43298b9484cSchristos } 43398b9484cSchristos } 43498b9484cSchristos 43598b9484cSchristos /* This function clears all entries in the given hash table. */ 43698b9484cSchristos 43798b9484cSchristos void 43898b9484cSchristos htab_empty (htab_t htab) 43998b9484cSchristos { 44098b9484cSchristos size_t size = htab_size (htab); 4414b169a6bSchristos void **entries = htab->entries; 44298b9484cSchristos int i; 44398b9484cSchristos 44498b9484cSchristos if (htab->del_f) 44598b9484cSchristos for (i = size - 1; i >= 0; i--) 44698b9484cSchristos if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY) 44798b9484cSchristos (*htab->del_f) (entries[i]); 44898b9484cSchristos 44998b9484cSchristos /* Instead of clearing megabyte, downsize the table. */ 4504b169a6bSchristos if (size > 1024*1024 / sizeof (void *)) 45198b9484cSchristos { 4524b169a6bSchristos int nindex = higher_prime_index (1024 / sizeof (void *)); 45398b9484cSchristos int nsize = prime_tab[nindex].prime; 45498b9484cSchristos 45598b9484cSchristos if (htab->free_f != NULL) 45698b9484cSchristos (*htab->free_f) (htab->entries); 45798b9484cSchristos else if (htab->free_with_arg_f != NULL) 45898b9484cSchristos (*htab->free_with_arg_f) (htab->alloc_arg, htab->entries); 45998b9484cSchristos if (htab->alloc_with_arg_f != NULL) 4604b169a6bSchristos htab->entries = (void **) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize, 4614b169a6bSchristos sizeof (void *)); 46298b9484cSchristos else 4634b169a6bSchristos htab->entries = (void **) (*htab->alloc_f) (nsize, sizeof (void *)); 46498b9484cSchristos htab->size = nsize; 46598b9484cSchristos htab->size_prime_index = nindex; 46698b9484cSchristos } 46798b9484cSchristos else 4684b169a6bSchristos memset (entries, 0, size * sizeof (void *)); 46998b9484cSchristos htab->n_deleted = 0; 47098b9484cSchristos htab->n_elements = 0; 47198b9484cSchristos } 47298b9484cSchristos 47398b9484cSchristos /* Similar to htab_find_slot, but without several unwanted side effects: 47498b9484cSchristos - Does not call htab->eq_f when it finds an existing entry. 47598b9484cSchristos - Does not change the count of elements/searches/collisions in the 47698b9484cSchristos hash table. 47798b9484cSchristos This function also assumes there are no deleted entries in the table. 47898b9484cSchristos HASH is the hash value for the element to be inserted. */ 47998b9484cSchristos 4804b169a6bSchristos static void ** 48198b9484cSchristos find_empty_slot_for_expand (htab_t htab, hashval_t hash) 48298b9484cSchristos { 48398b9484cSchristos hashval_t index = htab_mod (hash, htab); 48498b9484cSchristos size_t size = htab_size (htab); 4854b169a6bSchristos void **slot = htab->entries + index; 48698b9484cSchristos hashval_t hash2; 48798b9484cSchristos 48898b9484cSchristos if (*slot == HTAB_EMPTY_ENTRY) 48998b9484cSchristos return slot; 49098b9484cSchristos else if (*slot == HTAB_DELETED_ENTRY) 49198b9484cSchristos abort (); 49298b9484cSchristos 49398b9484cSchristos hash2 = htab_mod_m2 (hash, htab); 49498b9484cSchristos for (;;) 49598b9484cSchristos { 49698b9484cSchristos index += hash2; 49798b9484cSchristos if (index >= size) 49898b9484cSchristos index -= size; 49998b9484cSchristos 50098b9484cSchristos slot = htab->entries + index; 50198b9484cSchristos if (*slot == HTAB_EMPTY_ENTRY) 50298b9484cSchristos return slot; 50398b9484cSchristos else if (*slot == HTAB_DELETED_ENTRY) 50498b9484cSchristos abort (); 50598b9484cSchristos } 50698b9484cSchristos } 50798b9484cSchristos 50898b9484cSchristos /* The following function changes size of memory allocated for the 50998b9484cSchristos entries and repeatedly inserts the table elements. The occupancy 51098b9484cSchristos of the table after the call will be about 50%. Naturally the hash 51198b9484cSchristos table must already exist. Remember also that the place of the 51298b9484cSchristos table entries is changed. If memory allocation failures are allowed, 51398b9484cSchristos this function will return zero, indicating that the table could not be 51498b9484cSchristos expanded. If all goes well, it will return a non-zero value. */ 51598b9484cSchristos 51698b9484cSchristos static int 51798b9484cSchristos htab_expand (htab_t htab) 51898b9484cSchristos { 5194b169a6bSchristos void **oentries; 5204b169a6bSchristos void **olimit; 5214b169a6bSchristos void **p; 5224b169a6bSchristos void **nentries; 52398b9484cSchristos size_t nsize, osize, elts; 52498b9484cSchristos unsigned int oindex, nindex; 52598b9484cSchristos 52698b9484cSchristos oentries = htab->entries; 52798b9484cSchristos oindex = htab->size_prime_index; 52898b9484cSchristos osize = htab->size; 52998b9484cSchristos olimit = oentries + osize; 53098b9484cSchristos elts = htab_elements (htab); 53198b9484cSchristos 53298b9484cSchristos /* Resize only when table after removal of unused elements is either 53398b9484cSchristos too full or too empty. */ 53498b9484cSchristos if (elts * 2 > osize || (elts * 8 < osize && osize > 32)) 53598b9484cSchristos { 53698b9484cSchristos nindex = higher_prime_index (elts * 2); 53798b9484cSchristos nsize = prime_tab[nindex].prime; 53898b9484cSchristos } 53998b9484cSchristos else 54098b9484cSchristos { 54198b9484cSchristos nindex = oindex; 54298b9484cSchristos nsize = osize; 54398b9484cSchristos } 54498b9484cSchristos 54598b9484cSchristos if (htab->alloc_with_arg_f != NULL) 5464b169a6bSchristos nentries = (void **) (*htab->alloc_with_arg_f) (htab->alloc_arg, nsize, 5474b169a6bSchristos sizeof (void *)); 54898b9484cSchristos else 5494b169a6bSchristos nentries = (void **) (*htab->alloc_f) (nsize, sizeof (void *)); 55098b9484cSchristos if (nentries == NULL) 55198b9484cSchristos return 0; 55298b9484cSchristos htab->entries = nentries; 55398b9484cSchristos htab->size = nsize; 55498b9484cSchristos htab->size_prime_index = nindex; 55598b9484cSchristos htab->n_elements -= htab->n_deleted; 55698b9484cSchristos htab->n_deleted = 0; 55798b9484cSchristos 55898b9484cSchristos p = oentries; 55998b9484cSchristos do 56098b9484cSchristos { 5614b169a6bSchristos void *x = *p; 56298b9484cSchristos 56398b9484cSchristos if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY) 56498b9484cSchristos { 5654b169a6bSchristos void **q = find_empty_slot_for_expand (htab, (*htab->hash_f) (x)); 56698b9484cSchristos 56798b9484cSchristos *q = x; 56898b9484cSchristos } 56998b9484cSchristos 57098b9484cSchristos p++; 57198b9484cSchristos } 57298b9484cSchristos while (p < olimit); 57398b9484cSchristos 57498b9484cSchristos if (htab->free_f != NULL) 57598b9484cSchristos (*htab->free_f) (oentries); 57698b9484cSchristos else if (htab->free_with_arg_f != NULL) 57798b9484cSchristos (*htab->free_with_arg_f) (htab->alloc_arg, oentries); 57898b9484cSchristos return 1; 57998b9484cSchristos } 58098b9484cSchristos 58198b9484cSchristos /* This function searches for a hash table entry equal to the given 58298b9484cSchristos element. It cannot be used to insert or delete an element. */ 58398b9484cSchristos 5844b169a6bSchristos void * 5854b169a6bSchristos htab_find_with_hash (htab_t htab, const void *element, hashval_t hash) 58698b9484cSchristos { 58798b9484cSchristos hashval_t index, hash2; 58898b9484cSchristos size_t size; 5894b169a6bSchristos void *entry; 59098b9484cSchristos 59198b9484cSchristos htab->searches++; 59298b9484cSchristos size = htab_size (htab); 59398b9484cSchristos index = htab_mod (hash, htab); 59498b9484cSchristos 59598b9484cSchristos entry = htab->entries[index]; 59698b9484cSchristos if (entry == HTAB_EMPTY_ENTRY 59798b9484cSchristos || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element))) 59898b9484cSchristos return entry; 59998b9484cSchristos 60098b9484cSchristos hash2 = htab_mod_m2 (hash, htab); 60198b9484cSchristos for (;;) 60298b9484cSchristos { 60398b9484cSchristos htab->collisions++; 60498b9484cSchristos index += hash2; 60598b9484cSchristos if (index >= size) 60698b9484cSchristos index -= size; 60798b9484cSchristos 60898b9484cSchristos entry = htab->entries[index]; 60998b9484cSchristos if (entry == HTAB_EMPTY_ENTRY 61098b9484cSchristos || (entry != HTAB_DELETED_ENTRY && (*htab->eq_f) (entry, element))) 61198b9484cSchristos return entry; 61298b9484cSchristos } 61398b9484cSchristos } 61498b9484cSchristos 61598b9484cSchristos /* Like htab_find_slot_with_hash, but compute the hash value from the 61698b9484cSchristos element. */ 61798b9484cSchristos 6184b169a6bSchristos void * 6194b169a6bSchristos htab_find (htab_t htab, const void *element) 62098b9484cSchristos { 62198b9484cSchristos return htab_find_with_hash (htab, element, (*htab->hash_f) (element)); 62298b9484cSchristos } 62398b9484cSchristos 62498b9484cSchristos /* This function searches for a hash table slot containing an entry 62598b9484cSchristos equal to the given element. To delete an entry, call this with 62698b9484cSchristos insert=NO_INSERT, then call htab_clear_slot on the slot returned 62798b9484cSchristos (possibly after doing some checks). To insert an entry, call this 62898b9484cSchristos with insert=INSERT, then write the value you want into the returned 62998b9484cSchristos slot. When inserting an entry, NULL may be returned if memory 63098b9484cSchristos allocation fails. */ 63198b9484cSchristos 6324b169a6bSchristos void ** 6334b169a6bSchristos htab_find_slot_with_hash (htab_t htab, const void *element, 63498b9484cSchristos hashval_t hash, enum insert_option insert) 63598b9484cSchristos { 6364b169a6bSchristos void **first_deleted_slot; 63798b9484cSchristos hashval_t index, hash2; 63898b9484cSchristos size_t size; 6394b169a6bSchristos void *entry; 64098b9484cSchristos 64198b9484cSchristos size = htab_size (htab); 64298b9484cSchristos if (insert == INSERT && size * 3 <= htab->n_elements * 4) 64398b9484cSchristos { 64498b9484cSchristos if (htab_expand (htab) == 0) 64598b9484cSchristos return NULL; 64698b9484cSchristos size = htab_size (htab); 64798b9484cSchristos } 64898b9484cSchristos 64998b9484cSchristos index = htab_mod (hash, htab); 65098b9484cSchristos 65198b9484cSchristos htab->searches++; 65298b9484cSchristos first_deleted_slot = NULL; 65398b9484cSchristos 65498b9484cSchristos entry = htab->entries[index]; 65598b9484cSchristos if (entry == HTAB_EMPTY_ENTRY) 65698b9484cSchristos goto empty_entry; 65798b9484cSchristos else if (entry == HTAB_DELETED_ENTRY) 65898b9484cSchristos first_deleted_slot = &htab->entries[index]; 65998b9484cSchristos else if ((*htab->eq_f) (entry, element)) 66098b9484cSchristos return &htab->entries[index]; 66198b9484cSchristos 66298b9484cSchristos hash2 = htab_mod_m2 (hash, htab); 66398b9484cSchristos for (;;) 66498b9484cSchristos { 66598b9484cSchristos htab->collisions++; 66698b9484cSchristos index += hash2; 66798b9484cSchristos if (index >= size) 66898b9484cSchristos index -= size; 66998b9484cSchristos 67098b9484cSchristos entry = htab->entries[index]; 67198b9484cSchristos if (entry == HTAB_EMPTY_ENTRY) 67298b9484cSchristos goto empty_entry; 67398b9484cSchristos else if (entry == HTAB_DELETED_ENTRY) 67498b9484cSchristos { 67598b9484cSchristos if (!first_deleted_slot) 67698b9484cSchristos first_deleted_slot = &htab->entries[index]; 67798b9484cSchristos } 67898b9484cSchristos else if ((*htab->eq_f) (entry, element)) 67998b9484cSchristos return &htab->entries[index]; 68098b9484cSchristos } 68198b9484cSchristos 68298b9484cSchristos empty_entry: 68398b9484cSchristos if (insert == NO_INSERT) 68498b9484cSchristos return NULL; 68598b9484cSchristos 68698b9484cSchristos if (first_deleted_slot) 68798b9484cSchristos { 68898b9484cSchristos htab->n_deleted--; 68998b9484cSchristos *first_deleted_slot = HTAB_EMPTY_ENTRY; 69098b9484cSchristos return first_deleted_slot; 69198b9484cSchristos } 69298b9484cSchristos 69398b9484cSchristos htab->n_elements++; 69498b9484cSchristos return &htab->entries[index]; 69598b9484cSchristos } 69698b9484cSchristos 69798b9484cSchristos /* Like htab_find_slot_with_hash, but compute the hash value from the 69898b9484cSchristos element. */ 69998b9484cSchristos 7004b169a6bSchristos void ** 7014b169a6bSchristos htab_find_slot (htab_t htab, const void *element, enum insert_option insert) 70298b9484cSchristos { 70398b9484cSchristos return htab_find_slot_with_hash (htab, element, (*htab->hash_f) (element), 70498b9484cSchristos insert); 70598b9484cSchristos } 70698b9484cSchristos 70798b9484cSchristos /* This function deletes an element with the given value from hash 70898b9484cSchristos table (the hash is computed from the element). If there is no matching 70998b9484cSchristos element in the hash table, this function does nothing. */ 71098b9484cSchristos 71198b9484cSchristos void 7124b169a6bSchristos htab_remove_elt (htab_t htab, const void *element) 71398b9484cSchristos { 71498b9484cSchristos htab_remove_elt_with_hash (htab, element, (*htab->hash_f) (element)); 71598b9484cSchristos } 71698b9484cSchristos 71798b9484cSchristos 71898b9484cSchristos /* This function deletes an element with the given value from hash 71998b9484cSchristos table. If there is no matching element in the hash table, this 72098b9484cSchristos function does nothing. */ 72198b9484cSchristos 72298b9484cSchristos void 7234b169a6bSchristos htab_remove_elt_with_hash (htab_t htab, const void *element, hashval_t hash) 72498b9484cSchristos { 7254b169a6bSchristos void **slot; 72698b9484cSchristos 72798b9484cSchristos slot = htab_find_slot_with_hash (htab, element, hash, NO_INSERT); 7288dffb485Schristos if (slot == NULL) 72998b9484cSchristos return; 73098b9484cSchristos 73198b9484cSchristos if (htab->del_f) 73298b9484cSchristos (*htab->del_f) (*slot); 73398b9484cSchristos 73498b9484cSchristos *slot = HTAB_DELETED_ENTRY; 73598b9484cSchristos htab->n_deleted++; 73698b9484cSchristos } 73798b9484cSchristos 73898b9484cSchristos /* This function clears a specified slot in a hash table. It is 73998b9484cSchristos useful when you've already done the lookup and don't want to do it 74098b9484cSchristos again. */ 74198b9484cSchristos 74298b9484cSchristos void 7434b169a6bSchristos htab_clear_slot (htab_t htab, void **slot) 74498b9484cSchristos { 74598b9484cSchristos if (slot < htab->entries || slot >= htab->entries + htab_size (htab) 74698b9484cSchristos || *slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY) 74798b9484cSchristos abort (); 74898b9484cSchristos 74998b9484cSchristos if (htab->del_f) 75098b9484cSchristos (*htab->del_f) (*slot); 75198b9484cSchristos 75298b9484cSchristos *slot = HTAB_DELETED_ENTRY; 75398b9484cSchristos htab->n_deleted++; 75498b9484cSchristos } 75598b9484cSchristos 75698b9484cSchristos /* This function scans over the entire hash table calling 75798b9484cSchristos CALLBACK for each live entry. If CALLBACK returns false, 75898b9484cSchristos the iteration stops. INFO is passed as CALLBACK's second 75998b9484cSchristos argument. */ 76098b9484cSchristos 76198b9484cSchristos void 7624b169a6bSchristos htab_traverse_noresize (htab_t htab, htab_trav callback, void *info) 76398b9484cSchristos { 7644b169a6bSchristos void **slot; 7654b169a6bSchristos void **limit; 76698b9484cSchristos 76798b9484cSchristos slot = htab->entries; 76898b9484cSchristos limit = slot + htab_size (htab); 76998b9484cSchristos 77098b9484cSchristos do 77198b9484cSchristos { 7724b169a6bSchristos void *x = *slot; 77398b9484cSchristos 77498b9484cSchristos if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY) 77598b9484cSchristos if (!(*callback) (slot, info)) 77698b9484cSchristos break; 77798b9484cSchristos } 77898b9484cSchristos while (++slot < limit); 77998b9484cSchristos } 78098b9484cSchristos 78198b9484cSchristos /* Like htab_traverse_noresize, but does resize the table when it is 78298b9484cSchristos too empty to improve effectivity of subsequent calls. */ 78398b9484cSchristos 78498b9484cSchristos void 7854b169a6bSchristos htab_traverse (htab_t htab, htab_trav callback, void *info) 78698b9484cSchristos { 78798b9484cSchristos size_t size = htab_size (htab); 78898b9484cSchristos if (htab_elements (htab) * 8 < size && size > 32) 78998b9484cSchristos htab_expand (htab); 79098b9484cSchristos 79198b9484cSchristos htab_traverse_noresize (htab, callback, info); 79298b9484cSchristos } 79398b9484cSchristos 79498b9484cSchristos /* Return the fraction of fixed collisions during all work with given 79598b9484cSchristos hash table. */ 79698b9484cSchristos 79798b9484cSchristos double 79898b9484cSchristos htab_collisions (htab_t htab) 79998b9484cSchristos { 80098b9484cSchristos if (htab->searches == 0) 80198b9484cSchristos return 0.0; 80298b9484cSchristos 80398b9484cSchristos return (double) htab->collisions / (double) htab->searches; 80498b9484cSchristos } 80598b9484cSchristos 80698b9484cSchristos /* Hash P as a null-terminated string. 80798b9484cSchristos 80898b9484cSchristos Copied from gcc/hashtable.c. Zack had the following to say with respect 80998b9484cSchristos to applicability, though note that unlike hashtable.c, this hash table 81098b9484cSchristos implementation re-hashes rather than chain buckets. 81198b9484cSchristos 81298b9484cSchristos http://gcc.gnu.org/ml/gcc-patches/2001-08/msg01021.html 81398b9484cSchristos From: Zack Weinberg <zackw@panix.com> 81498b9484cSchristos Date: Fri, 17 Aug 2001 02:15:56 -0400 81598b9484cSchristos 81698b9484cSchristos I got it by extracting all the identifiers from all the source code 81798b9484cSchristos I had lying around in mid-1999, and testing many recurrences of 81898b9484cSchristos the form "H_n = H_{n-1} * K + c_n * L + M" where K, L, M were either 81998b9484cSchristos prime numbers or the appropriate identity. This was the best one. 82098b9484cSchristos I don't remember exactly what constituted "best", except I was 82198b9484cSchristos looking at bucket-length distributions mostly. 82298b9484cSchristos 82398b9484cSchristos So it should be very good at hashing identifiers, but might not be 82498b9484cSchristos as good at arbitrary strings. 82598b9484cSchristos 82698b9484cSchristos I'll add that it thoroughly trounces the hash functions recommended 82798b9484cSchristos for this use at http://burtleburtle.net/bob/hash/index.html, both 82898b9484cSchristos on speed and bucket distribution. I haven't tried it against the 82998b9484cSchristos function they just started using for Perl's hashes. */ 83098b9484cSchristos 83198b9484cSchristos hashval_t 8324b169a6bSchristos htab_hash_string (const void *p) 83398b9484cSchristos { 83498b9484cSchristos const unsigned char *str = (const unsigned char *) p; 83598b9484cSchristos hashval_t r = 0; 83698b9484cSchristos unsigned char c; 83798b9484cSchristos 83898b9484cSchristos while ((c = *str++) != 0) 83998b9484cSchristos r = r * 67 + c - 113; 84098b9484cSchristos 84198b9484cSchristos return r; 84298b9484cSchristos } 84398b9484cSchristos 8444b169a6bSchristos /* An equality function for null-terminated strings. */ 8454b169a6bSchristos int 8464b169a6bSchristos htab_eq_string (const void *a, const void *b) 8474b169a6bSchristos { 8484b169a6bSchristos return strcmp ((const char *) a, (const char *) b) == 0; 8494b169a6bSchristos } 8504b169a6bSchristos 85198b9484cSchristos /* DERIVED FROM: 85298b9484cSchristos -------------------------------------------------------------------- 85398b9484cSchristos lookup2.c, by Bob Jenkins, December 1996, Public Domain. 85498b9484cSchristos hash(), hash2(), hash3, and mix() are externally useful functions. 85598b9484cSchristos Routines to test the hash are included if SELF_TEST is defined. 85698b9484cSchristos You can use this free for any purpose. It has no warranty. 85798b9484cSchristos -------------------------------------------------------------------- 85898b9484cSchristos */ 85998b9484cSchristos 86098b9484cSchristos /* 86198b9484cSchristos -------------------------------------------------------------------- 86298b9484cSchristos mix -- mix 3 32-bit values reversibly. 86398b9484cSchristos For every delta with one or two bit set, and the deltas of all three 86498b9484cSchristos high bits or all three low bits, whether the original value of a,b,c 86598b9484cSchristos is almost all zero or is uniformly distributed, 86698b9484cSchristos * If mix() is run forward or backward, at least 32 bits in a,b,c 86798b9484cSchristos have at least 1/4 probability of changing. 86898b9484cSchristos * If mix() is run forward, every bit of c will change between 1/3 and 86998b9484cSchristos 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.) 87098b9484cSchristos mix() was built out of 36 single-cycle latency instructions in a 87198b9484cSchristos structure that could supported 2x parallelism, like so: 87298b9484cSchristos a -= b; 87398b9484cSchristos a -= c; x = (c>>13); 87498b9484cSchristos b -= c; a ^= x; 87598b9484cSchristos b -= a; x = (a<<8); 87698b9484cSchristos c -= a; b ^= x; 87798b9484cSchristos c -= b; x = (b>>13); 87898b9484cSchristos ... 87998b9484cSchristos Unfortunately, superscalar Pentiums and Sparcs can't take advantage 88098b9484cSchristos of that parallelism. They've also turned some of those single-cycle 88198b9484cSchristos latency instructions into multi-cycle latency instructions. Still, 88298b9484cSchristos this is the fastest good hash I could find. There were about 2^^68 88398b9484cSchristos to choose from. I only looked at a billion or so. 88498b9484cSchristos -------------------------------------------------------------------- 88598b9484cSchristos */ 88698b9484cSchristos /* same, but slower, works on systems that might have 8 byte hashval_t's */ 88798b9484cSchristos #define mix(a,b,c) \ 88898b9484cSchristos { \ 88998b9484cSchristos a -= b; a -= c; a ^= (c>>13); \ 89098b9484cSchristos b -= c; b -= a; b ^= (a<< 8); \ 89198b9484cSchristos c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \ 89298b9484cSchristos a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \ 89398b9484cSchristos b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \ 89498b9484cSchristos c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \ 89598b9484cSchristos a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \ 89698b9484cSchristos b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \ 89798b9484cSchristos c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \ 89898b9484cSchristos } 89998b9484cSchristos 90098b9484cSchristos /* 90198b9484cSchristos -------------------------------------------------------------------- 90298b9484cSchristos hash() -- hash a variable-length key into a 32-bit value 90398b9484cSchristos k : the key (the unaligned variable-length array of bytes) 90498b9484cSchristos len : the length of the key, counting by bytes 90598b9484cSchristos level : can be any 4-byte value 90698b9484cSchristos Returns a 32-bit value. Every bit of the key affects every bit of 90798b9484cSchristos the return value. Every 1-bit and 2-bit delta achieves avalanche. 90898b9484cSchristos About 36+6len instructions. 90998b9484cSchristos 91098b9484cSchristos The best hash table sizes are powers of 2. There is no need to do 91198b9484cSchristos mod a prime (mod is sooo slow!). If you need less than 32 bits, 91298b9484cSchristos use a bitmask. For example, if you need only 10 bits, do 91398b9484cSchristos h = (h & hashmask(10)); 91498b9484cSchristos In which case, the hash table should have hashsize(10) elements. 91598b9484cSchristos 91698b9484cSchristos If you are hashing n strings (ub1 **)k, do it like this: 91798b9484cSchristos for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h); 91898b9484cSchristos 91998b9484cSchristos By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this 92098b9484cSchristos code any way you wish, private, educational, or commercial. It's free. 92198b9484cSchristos 92298b9484cSchristos See http://burtleburtle.net/bob/hash/evahash.html 92398b9484cSchristos Use for hash table lookup, or anything where one collision in 2^32 is 92498b9484cSchristos acceptable. Do NOT use for cryptographic purposes. 92598b9484cSchristos -------------------------------------------------------------------- 92698b9484cSchristos */ 92798b9484cSchristos 92898b9484cSchristos hashval_t 9294b169a6bSchristos iterative_hash (const void *k_in /* the key */, 93098b9484cSchristos register size_t length /* the length of the key */, 93198b9484cSchristos register hashval_t initval /* the previous hash, or 93298b9484cSchristos an arbitrary value */) 93398b9484cSchristos { 93498b9484cSchristos register const unsigned char *k = (const unsigned char *)k_in; 93598b9484cSchristos register hashval_t a,b,c,len; 93698b9484cSchristos 93798b9484cSchristos /* Set up the internal state */ 93898b9484cSchristos len = length; 93998b9484cSchristos a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ 94098b9484cSchristos c = initval; /* the previous hash value */ 94198b9484cSchristos 94298b9484cSchristos /*---------------------------------------- handle most of the key */ 94398b9484cSchristos #ifndef WORDS_BIGENDIAN 94498b9484cSchristos /* On a little-endian machine, if the data is 4-byte aligned we can hash 94598b9484cSchristos by word for better speed. This gives nondeterministic results on 94698b9484cSchristos big-endian machines. */ 94798b9484cSchristos if (sizeof (hashval_t) == 4 && (((size_t)k)&3) == 0) 94898b9484cSchristos while (len >= 12) /* aligned */ 94998b9484cSchristos { 95098b9484cSchristos a += *(hashval_t *)(k+0); 95198b9484cSchristos b += *(hashval_t *)(k+4); 95298b9484cSchristos c += *(hashval_t *)(k+8); 95398b9484cSchristos mix(a,b,c); 95498b9484cSchristos k += 12; len -= 12; 95598b9484cSchristos } 95698b9484cSchristos else /* unaligned */ 95798b9484cSchristos #endif 95898b9484cSchristos while (len >= 12) 95998b9484cSchristos { 96098b9484cSchristos a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24)); 96198b9484cSchristos b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24)); 96298b9484cSchristos c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24)); 96398b9484cSchristos mix(a,b,c); 96498b9484cSchristos k += 12; len -= 12; 96598b9484cSchristos } 96698b9484cSchristos 96798b9484cSchristos /*------------------------------------- handle the last 11 bytes */ 96898b9484cSchristos c += length; 96998b9484cSchristos switch(len) /* all the case statements fall through */ 97098b9484cSchristos { 971796c32c9Schristos case 11: c+=((hashval_t)k[10]<<24); /* fall through */ 972796c32c9Schristos case 10: c+=((hashval_t)k[9]<<16); /* fall through */ 973796c32c9Schristos case 9 : c+=((hashval_t)k[8]<<8); /* fall through */ 97498b9484cSchristos /* the first byte of c is reserved for the length */ 975796c32c9Schristos case 8 : b+=((hashval_t)k[7]<<24); /* fall through */ 976796c32c9Schristos case 7 : b+=((hashval_t)k[6]<<16); /* fall through */ 977796c32c9Schristos case 6 : b+=((hashval_t)k[5]<<8); /* fall through */ 978796c32c9Schristos case 5 : b+=k[4]; /* fall through */ 979796c32c9Schristos case 4 : a+=((hashval_t)k[3]<<24); /* fall through */ 980796c32c9Schristos case 3 : a+=((hashval_t)k[2]<<16); /* fall through */ 981796c32c9Schristos case 2 : a+=((hashval_t)k[1]<<8); /* fall through */ 98298b9484cSchristos case 1 : a+=k[0]; 98398b9484cSchristos /* case 0: nothing left to add */ 98498b9484cSchristos } 98598b9484cSchristos mix(a,b,c); 98698b9484cSchristos /*-------------------------------------------- report the result */ 98798b9484cSchristos return c; 98898b9484cSchristos } 98903467a24Schristos 99003467a24Schristos /* Returns a hash code for pointer P. Simplified version of evahash */ 99103467a24Schristos 99203467a24Schristos static hashval_t 9934b169a6bSchristos hash_pointer (const void *p) 99403467a24Schristos { 99503467a24Schristos intptr_t v = (intptr_t) p; 99603467a24Schristos unsigned a, b, c; 99703467a24Schristos 99803467a24Schristos a = b = 0x9e3779b9; 99903467a24Schristos a += v >> (sizeof (intptr_t) * CHAR_BIT / 2); 100003467a24Schristos b += v & (((intptr_t) 1 << (sizeof (intptr_t) * CHAR_BIT / 2)) - 1); 100103467a24Schristos c = 0x42135234; 100203467a24Schristos mix (a, b, c); 100303467a24Schristos return c; 100403467a24Schristos } 1005