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