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