101e9b57fSespie /* An expandable hash tables datatype. 2*150b7e42Smiod Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. 301e9b57fSespie Contributed by Vladimir Makarov (vmakarov@cygnus.com). 401e9b57fSespie 501e9b57fSespie This program is free software; you can redistribute it and/or modify 601e9b57fSespie it under the terms of the GNU General Public License as published by 701e9b57fSespie the Free Software Foundation; either version 2 of the License, or 801e9b57fSespie (at your option) any later version. 901e9b57fSespie 1001e9b57fSespie This program is distributed in the hope that it will be useful, 1101e9b57fSespie but WITHOUT ANY WARRANTY; without even the implied warranty of 1201e9b57fSespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1301e9b57fSespie GNU General Public License for more details. 1401e9b57fSespie 1501e9b57fSespie You should have received a copy of the GNU General Public License 1601e9b57fSespie along with this program; if not, write to the Free Software 17*150b7e42Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 1801e9b57fSespie 1901e9b57fSespie /* This package implements basic hash table functionality. It is possible 2001e9b57fSespie to search for an entry, create an entry and destroy an entry. 2101e9b57fSespie 2201e9b57fSespie Elements in the table are generic pointers. 2301e9b57fSespie 2401e9b57fSespie The size of the table is not fixed; if the occupancy of the table 2501e9b57fSespie grows too high the hash table will be expanded. 2601e9b57fSespie 2701e9b57fSespie The abstract data implementation is based on generalized Algorithm D 2801e9b57fSespie from Knuth's book "The art of computer programming". Hash table is 2901e9b57fSespie expanded by creation of new hash table and transferring elements from 3001e9b57fSespie the old table to the new table. */ 3101e9b57fSespie 3201e9b57fSespie #ifndef __HASHTAB_H__ 3301e9b57fSespie #define __HASHTAB_H__ 3401e9b57fSespie 3501e9b57fSespie #ifdef __cplusplus 3601e9b57fSespie extern "C" { 3701e9b57fSespie #endif /* __cplusplus */ 3801e9b57fSespie 3937c53322Sespie #include "ansidecl.h" 4001e9b57fSespie 4137c53322Sespie #ifndef GTY 4237c53322Sespie #define GTY(X) 4337c53322Sespie #endif 4401e9b57fSespie 4537c53322Sespie /* The type for a hash code. */ 4637c53322Sespie typedef unsigned int hashval_t; 4737c53322Sespie 4837c53322Sespie /* Callback function pointer types. */ 4937c53322Sespie 5037c53322Sespie /* Calculate hash of a table entry. */ 5137c53322Sespie typedef hashval_t (*htab_hash) PARAMS ((const void *)); 5237c53322Sespie 5337c53322Sespie /* Compare a table entry with a possible entry. The entry already in 5437c53322Sespie the table always comes first, so the second element can be of a 5537c53322Sespie different type (but in this case htab_find and htab_find_slot 5637c53322Sespie cannot be used; instead the variants that accept a hash value 5737c53322Sespie must be used). */ 5837c53322Sespie typedef int (*htab_eq) PARAMS ((const void *, const void *)); 5937c53322Sespie 6037c53322Sespie /* Cleanup function called whenever a live element is removed from 6137c53322Sespie the hash table. */ 6237c53322Sespie typedef void (*htab_del) PARAMS ((void *)); 6337c53322Sespie 6437c53322Sespie /* Function called by htab_traverse for each live element. The first 6537c53322Sespie arg is the slot of the element (which can be passed to htab_clear_slot 6637c53322Sespie if desired), the second arg is the auxiliary pointer handed to 6737c53322Sespie htab_traverse. Return 1 to continue scan, 0 to stop. */ 6837c53322Sespie typedef int (*htab_trav) PARAMS ((void **, void *)); 6937c53322Sespie 7037c53322Sespie /* Memory-allocation function, with the same functionality as calloc(). 7137c53322Sespie Iff it returns NULL, the hash table implementation will pass an error 7237c53322Sespie code back to the user, so if your code doesn't handle errors, 7337c53322Sespie best if you use xcalloc instead. */ 7437c53322Sespie typedef PTR (*htab_alloc) PARAMS ((size_t, size_t)); 7537c53322Sespie 7637c53322Sespie /* We also need a free() routine. */ 7737c53322Sespie typedef void (*htab_free) PARAMS ((PTR)); 7801e9b57fSespie 79707f648cSespie /* Memory allocation and deallocation; variants which take an extra 80707f648cSespie argument. */ 81707f648cSespie typedef PTR (*htab_alloc_with_arg) PARAMS ((void *, size_t, size_t)); 82707f648cSespie typedef void (*htab_free_with_arg) PARAMS ((void *, void *)); 83707f648cSespie 84*150b7e42Smiod /* This macro defines reserved value for empty table entry. */ 85*150b7e42Smiod 86*150b7e42Smiod #define HTAB_EMPTY_ENTRY ((PTR) 0) 87*150b7e42Smiod 88*150b7e42Smiod /* This macro defines reserved value for table entry which contained 89*150b7e42Smiod a deleted element. */ 90*150b7e42Smiod 91*150b7e42Smiod #define HTAB_DELETED_ENTRY ((PTR) 1) 92*150b7e42Smiod 9301e9b57fSespie /* Hash tables are of the following type. The structure 9401e9b57fSespie (implementation) of this type is not needed for using the hash 9501e9b57fSespie tables. All work with hash table should be executed only through 96707f648cSespie functions mentioned below. The size of this structure is subject to 97707f648cSespie change. */ 9801e9b57fSespie 9937c53322Sespie struct htab GTY(()) 10001e9b57fSespie { 10137c53322Sespie /* Pointer to hash function. */ 10237c53322Sespie htab_hash hash_f; 10337c53322Sespie 10437c53322Sespie /* Pointer to comparison function. */ 10537c53322Sespie htab_eq eq_f; 10637c53322Sespie 10737c53322Sespie /* Pointer to cleanup function. */ 10837c53322Sespie htab_del del_f; 10937c53322Sespie 11037c53322Sespie /* Table itself. */ 11137c53322Sespie PTR * GTY ((use_param (""), length ("%h.size"))) entries; 11237c53322Sespie 113*150b7e42Smiod /* Current size (in entries) of the hash table. */ 11401e9b57fSespie size_t size; 11537c53322Sespie 116*150b7e42Smiod /* Current number of elements including also deleted elements. */ 11737c53322Sespie size_t n_elements; 11837c53322Sespie 119*150b7e42Smiod /* Current number of deleted elements in the table. */ 12037c53322Sespie size_t n_deleted; 12137c53322Sespie 12201e9b57fSespie /* The following member is used for debugging. Its value is number 12337c53322Sespie of all calls of `htab_find_slot' for the hash table. */ 12437c53322Sespie unsigned int searches; 12537c53322Sespie 12601e9b57fSespie /* The following member is used for debugging. Its value is number 12701e9b57fSespie of collisions fixed for time of work with the hash table. */ 12837c53322Sespie unsigned int collisions; 12901e9b57fSespie 13037c53322Sespie /* Pointers to allocate/free functions. */ 13137c53322Sespie htab_alloc alloc_f; 13237c53322Sespie htab_free free_f; 133707f648cSespie 134707f648cSespie /* Alternate allocate/free functions, which take an extra argument. */ 135707f648cSespie PTR GTY((skip (""))) alloc_arg; 136707f648cSespie htab_alloc_with_arg alloc_with_arg_f; 137707f648cSespie htab_free_with_arg free_with_arg_f; 138*150b7e42Smiod 139*150b7e42Smiod /* Current size (in entries) of the hash table, as an index into the 140*150b7e42Smiod table of primes. */ 141*150b7e42Smiod unsigned int size_prime_index; 14237c53322Sespie }; 14337c53322Sespie 14437c53322Sespie typedef struct htab *htab_t; 14537c53322Sespie 14637c53322Sespie /* An enum saying whether we insert into the hash table or not. */ 14737c53322Sespie enum insert_option {NO_INSERT, INSERT}; 14801e9b57fSespie 14901e9b57fSespie /* The prototypes of the package functions. */ 15001e9b57fSespie 151*150b7e42Smiod extern htab_t htab_create_alloc (size_t, htab_hash, 15237c53322Sespie htab_eq, htab_del, 153*150b7e42Smiod htab_alloc, htab_free); 15401e9b57fSespie 155*150b7e42Smiod extern htab_t htab_create_alloc_ex (size_t, htab_hash, 156707f648cSespie htab_eq, htab_del, 157*150b7e42Smiod void *, htab_alloc_with_arg, 158*150b7e42Smiod htab_free_with_arg); 159707f648cSespie 16037c53322Sespie /* Backward-compatibility functions. */ 161*150b7e42Smiod extern htab_t htab_create (size_t, htab_hash, htab_eq, htab_del); 162*150b7e42Smiod extern htab_t htab_try_create (size_t, htab_hash, htab_eq, htab_del); 16301e9b57fSespie 164*150b7e42Smiod extern void htab_set_functions_ex (htab_t, htab_hash, 165707f648cSespie htab_eq, htab_del, 166*150b7e42Smiod void *, htab_alloc_with_arg, 167*150b7e42Smiod htab_free_with_arg); 168707f648cSespie 169*150b7e42Smiod extern void htab_delete (htab_t); 170*150b7e42Smiod extern void htab_empty (htab_t); 17101e9b57fSespie 172*150b7e42Smiod extern void * htab_find (htab_t, const void *); 173*150b7e42Smiod extern void ** htab_find_slot (htab_t, const void *, enum insert_option); 174*150b7e42Smiod extern void * htab_find_with_hash (htab_t, const void *, hashval_t); 175*150b7e42Smiod extern void ** htab_find_slot_with_hash (htab_t, const void *, 176*150b7e42Smiod hashval_t, enum insert_option); 177*150b7e42Smiod extern void htab_clear_slot (htab_t, void **); 178*150b7e42Smiod extern void htab_remove_elt (htab_t, void *); 179*150b7e42Smiod extern void htab_remove_elt_with_hash (htab_t, void *, hashval_t); 18001e9b57fSespie 181*150b7e42Smiod extern void htab_traverse (htab_t, htab_trav, void *); 182*150b7e42Smiod extern void htab_traverse_noresize (htab_t, htab_trav, void *); 18301e9b57fSespie 184*150b7e42Smiod extern size_t htab_size (htab_t); 185*150b7e42Smiod extern size_t htab_elements (htab_t); 186*150b7e42Smiod extern double htab_collisions (htab_t); 18701e9b57fSespie 18837c53322Sespie /* A hash function for pointers. */ 18937c53322Sespie extern htab_hash htab_hash_pointer; 19001e9b57fSespie 19137c53322Sespie /* An equality function for pointers. */ 19237c53322Sespie extern htab_eq htab_eq_pointer; 19301e9b57fSespie 19437c53322Sespie /* A hash function for null-terminated strings. */ 195*150b7e42Smiod extern hashval_t htab_hash_string (const void *); 196*150b7e42Smiod 197*150b7e42Smiod /* An iterative hash function for arbitrary data. */ 198*150b7e42Smiod extern hashval_t iterative_hash (const void *, size_t, hashval_t); 199*150b7e42Smiod /* Shorthand for hashing something with an intrinsic size. */ 200*150b7e42Smiod #define iterative_hash_object(OB,INIT) iterative_hash (&OB, sizeof (OB), INIT) 20101e9b57fSespie 20201e9b57fSespie #ifdef __cplusplus 20301e9b57fSespie } 20401e9b57fSespie #endif /* __cplusplus */ 20501e9b57fSespie 20601e9b57fSespie #endif /* __HASHTAB_H */ 207