1*38fd1498Szrj /* An expandable hash tables datatype. 2*38fd1498Szrj Copyright (C) 1999-2018 Free Software Foundation, Inc. 3*38fd1498Szrj Contributed by Vladimir Makarov (vmakarov@cygnus.com). 4*38fd1498Szrj 5*38fd1498Szrj This program is free software; you can redistribute it and/or modify 6*38fd1498Szrj it under the terms of the GNU General Public License as published by 7*38fd1498Szrj the Free Software Foundation; either version 2 of the License, or 8*38fd1498Szrj (at your option) any later version. 9*38fd1498Szrj 10*38fd1498Szrj This program is distributed in the hope that it will be useful, 11*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of 12*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*38fd1498Szrj GNU General Public License for more details. 14*38fd1498Szrj 15*38fd1498Szrj You should have received a copy of the GNU General Public License 16*38fd1498Szrj along with this program; if not, write to the Free Software 17*38fd1498Szrj Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 18*38fd1498Szrj 19*38fd1498Szrj /* This package implements basic hash table functionality. It is possible 20*38fd1498Szrj to search for an entry, create an entry and destroy an entry. 21*38fd1498Szrj 22*38fd1498Szrj Elements in the table are generic pointers. 23*38fd1498Szrj 24*38fd1498Szrj The size of the table is not fixed; if the occupancy of the table 25*38fd1498Szrj grows too high the hash table will be expanded. 26*38fd1498Szrj 27*38fd1498Szrj The abstract data implementation is based on generalized Algorithm D 28*38fd1498Szrj from Knuth's book "The art of computer programming". Hash table is 29*38fd1498Szrj expanded by creation of new hash table and transferring elements from 30*38fd1498Szrj the old table to the new table. */ 31*38fd1498Szrj 32*38fd1498Szrj #ifndef __HASHTAB_H__ 33*38fd1498Szrj #define __HASHTAB_H__ 34*38fd1498Szrj 35*38fd1498Szrj #ifdef __cplusplus 36*38fd1498Szrj extern "C" { 37*38fd1498Szrj #endif /* __cplusplus */ 38*38fd1498Szrj 39*38fd1498Szrj #include "ansidecl.h" 40*38fd1498Szrj 41*38fd1498Szrj /* The type for a hash code. */ 42*38fd1498Szrj typedef unsigned int hashval_t; 43*38fd1498Szrj 44*38fd1498Szrj /* Callback function pointer types. */ 45*38fd1498Szrj 46*38fd1498Szrj /* Calculate hash of a table entry. */ 47*38fd1498Szrj typedef hashval_t (*htab_hash) (const void *); 48*38fd1498Szrj 49*38fd1498Szrj /* Compare a table entry with a possible entry. The entry already in 50*38fd1498Szrj the table always comes first, so the second element can be of a 51*38fd1498Szrj different type (but in this case htab_find and htab_find_slot 52*38fd1498Szrj cannot be used; instead the variants that accept a hash value 53*38fd1498Szrj must be used). */ 54*38fd1498Szrj typedef int (*htab_eq) (const void *, const void *); 55*38fd1498Szrj 56*38fd1498Szrj /* Cleanup function called whenever a live element is removed from 57*38fd1498Szrj the hash table. */ 58*38fd1498Szrj typedef void (*htab_del) (void *); 59*38fd1498Szrj 60*38fd1498Szrj /* Function called by htab_traverse for each live element. The first 61*38fd1498Szrj arg is the slot of the element (which can be passed to htab_clear_slot 62*38fd1498Szrj if desired), the second arg is the auxiliary pointer handed to 63*38fd1498Szrj htab_traverse. Return 1 to continue scan, 0 to stop. */ 64*38fd1498Szrj typedef int (*htab_trav) (void **, void *); 65*38fd1498Szrj 66*38fd1498Szrj /* Memory-allocation function, with the same functionality as calloc(). 67*38fd1498Szrj Iff it returns NULL, the hash table implementation will pass an error 68*38fd1498Szrj code back to the user, so if your code doesn't handle errors, 69*38fd1498Szrj best if you use xcalloc instead. */ 70*38fd1498Szrj typedef void *(*htab_alloc) (size_t, size_t); 71*38fd1498Szrj 72*38fd1498Szrj /* We also need a free() routine. */ 73*38fd1498Szrj typedef void (*htab_free) (void *); 74*38fd1498Szrj 75*38fd1498Szrj /* Memory allocation and deallocation; variants which take an extra 76*38fd1498Szrj argument. */ 77*38fd1498Szrj typedef void *(*htab_alloc_with_arg) (void *, size_t, size_t); 78*38fd1498Szrj typedef void (*htab_free_with_arg) (void *, void *); 79*38fd1498Szrj 80*38fd1498Szrj /* This macro defines reserved value for empty table entry. */ 81*38fd1498Szrj 82*38fd1498Szrj #define HTAB_EMPTY_ENTRY ((PTR) 0) 83*38fd1498Szrj 84*38fd1498Szrj /* This macro defines reserved value for table entry which contained 85*38fd1498Szrj a deleted element. */ 86*38fd1498Szrj 87*38fd1498Szrj #define HTAB_DELETED_ENTRY ((PTR) 1) 88*38fd1498Szrj 89*38fd1498Szrj /* Hash tables are of the following type. The structure 90*38fd1498Szrj (implementation) of this type is not needed for using the hash 91*38fd1498Szrj tables. All work with hash table should be executed only through 92*38fd1498Szrj functions mentioned below. The size of this structure is subject to 93*38fd1498Szrj change. */ 94*38fd1498Szrj 95*38fd1498Szrj struct htab { 96*38fd1498Szrj /* Pointer to hash function. */ 97*38fd1498Szrj htab_hash hash_f; 98*38fd1498Szrj 99*38fd1498Szrj /* Pointer to comparison function. */ 100*38fd1498Szrj htab_eq eq_f; 101*38fd1498Szrj 102*38fd1498Szrj /* Pointer to cleanup function. */ 103*38fd1498Szrj htab_del del_f; 104*38fd1498Szrj 105*38fd1498Szrj /* Table itself. */ 106*38fd1498Szrj void **entries; 107*38fd1498Szrj 108*38fd1498Szrj /* Current size (in entries) of the hash table. */ 109*38fd1498Szrj size_t size; 110*38fd1498Szrj 111*38fd1498Szrj /* Current number of elements including also deleted elements. */ 112*38fd1498Szrj size_t n_elements; 113*38fd1498Szrj 114*38fd1498Szrj /* Current number of deleted elements in the table. */ 115*38fd1498Szrj size_t n_deleted; 116*38fd1498Szrj 117*38fd1498Szrj /* The following member is used for debugging. Its value is number 118*38fd1498Szrj of all calls of `htab_find_slot' for the hash table. */ 119*38fd1498Szrj unsigned int searches; 120*38fd1498Szrj 121*38fd1498Szrj /* The following member is used for debugging. Its value is number 122*38fd1498Szrj of collisions fixed for time of work with the hash table. */ 123*38fd1498Szrj unsigned int collisions; 124*38fd1498Szrj 125*38fd1498Szrj /* Pointers to allocate/free functions. */ 126*38fd1498Szrj htab_alloc alloc_f; 127*38fd1498Szrj htab_free free_f; 128*38fd1498Szrj 129*38fd1498Szrj /* Alternate allocate/free functions, which take an extra argument. */ 130*38fd1498Szrj void *alloc_arg; 131*38fd1498Szrj htab_alloc_with_arg alloc_with_arg_f; 132*38fd1498Szrj htab_free_with_arg free_with_arg_f; 133*38fd1498Szrj 134*38fd1498Szrj /* Current size (in entries) of the hash table, as an index into the 135*38fd1498Szrj table of primes. */ 136*38fd1498Szrj unsigned int size_prime_index; 137*38fd1498Szrj }; 138*38fd1498Szrj 139*38fd1498Szrj typedef struct htab *htab_t; 140*38fd1498Szrj 141*38fd1498Szrj /* An enum saying whether we insert into the hash table or not. */ 142*38fd1498Szrj enum insert_option {NO_INSERT, INSERT}; 143*38fd1498Szrj 144*38fd1498Szrj /* The prototypes of the package functions. */ 145*38fd1498Szrj 146*38fd1498Szrj extern htab_t htab_create_alloc (size_t, htab_hash, 147*38fd1498Szrj htab_eq, htab_del, 148*38fd1498Szrj htab_alloc, htab_free); 149*38fd1498Szrj 150*38fd1498Szrj extern htab_t htab_create_alloc_ex (size_t, htab_hash, 151*38fd1498Szrj htab_eq, htab_del, 152*38fd1498Szrj void *, htab_alloc_with_arg, 153*38fd1498Szrj htab_free_with_arg); 154*38fd1498Szrj 155*38fd1498Szrj extern htab_t htab_create_typed_alloc (size_t, htab_hash, htab_eq, htab_del, 156*38fd1498Szrj htab_alloc, htab_alloc, htab_free); 157*38fd1498Szrj 158*38fd1498Szrj /* Backward-compatibility functions. */ 159*38fd1498Szrj extern htab_t htab_create (size_t, htab_hash, htab_eq, htab_del); 160*38fd1498Szrj extern htab_t htab_try_create (size_t, htab_hash, htab_eq, htab_del); 161*38fd1498Szrj 162*38fd1498Szrj extern void htab_set_functions_ex (htab_t, htab_hash, 163*38fd1498Szrj htab_eq, htab_del, 164*38fd1498Szrj void *, htab_alloc_with_arg, 165*38fd1498Szrj htab_free_with_arg); 166*38fd1498Szrj 167*38fd1498Szrj extern void htab_delete (htab_t); 168*38fd1498Szrj extern void htab_empty (htab_t); 169*38fd1498Szrj 170*38fd1498Szrj extern void * htab_find (htab_t, const void *); 171*38fd1498Szrj extern void ** htab_find_slot (htab_t, const void *, enum insert_option); 172*38fd1498Szrj extern void * htab_find_with_hash (htab_t, const void *, hashval_t); 173*38fd1498Szrj extern void ** htab_find_slot_with_hash (htab_t, const void *, 174*38fd1498Szrj hashval_t, enum insert_option); 175*38fd1498Szrj extern void htab_clear_slot (htab_t, void **); 176*38fd1498Szrj extern void htab_remove_elt (htab_t, void *); 177*38fd1498Szrj extern void htab_remove_elt_with_hash (htab_t, void *, hashval_t); 178*38fd1498Szrj 179*38fd1498Szrj extern void htab_traverse (htab_t, htab_trav, void *); 180*38fd1498Szrj extern void htab_traverse_noresize (htab_t, htab_trav, void *); 181*38fd1498Szrj 182*38fd1498Szrj extern size_t htab_size (htab_t); 183*38fd1498Szrj extern size_t htab_elements (htab_t); 184*38fd1498Szrj extern double htab_collisions (htab_t); 185*38fd1498Szrj 186*38fd1498Szrj /* A hash function for pointers. */ 187*38fd1498Szrj extern htab_hash htab_hash_pointer; 188*38fd1498Szrj 189*38fd1498Szrj /* An equality function for pointers. */ 190*38fd1498Szrj extern htab_eq htab_eq_pointer; 191*38fd1498Szrj 192*38fd1498Szrj /* A hash function for null-terminated strings. */ 193*38fd1498Szrj extern hashval_t htab_hash_string (const void *); 194*38fd1498Szrj 195*38fd1498Szrj /* An iterative hash function for arbitrary data. */ 196*38fd1498Szrj extern hashval_t iterative_hash (const void *, size_t, hashval_t); 197*38fd1498Szrj /* Shorthand for hashing something with an intrinsic size. */ 198*38fd1498Szrj #define iterative_hash_object(OB,INIT) iterative_hash (&OB, sizeof (OB), INIT) 199*38fd1498Szrj 200*38fd1498Szrj #ifdef __cplusplus 201*38fd1498Szrj } 202*38fd1498Szrj #endif /* __cplusplus */ 203*38fd1498Szrj 204*38fd1498Szrj #endif /* __HASHTAB_H */ 205