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