00001 /* An expandable hash tables datatype. 00002 Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc. 00003 Contributed by Vladimir Makarov (vmakarov@cygnus.com). 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 00018 00019 /* This package implements basic hash table functionality. It is possible 00020 to search for an entry, create an entry and destroy an entry. 00021 00022 Elements in the table are generic pointers. 00023 00024 The size of the table is not fixed; if the occupancy of the table 00025 grows too high the hash table will be expanded. 00026 00027 The abstract data implementation is based on generalized Algorithm D 00028 from Knuth's book "The art of computer programming". Hash table is 00029 expanded by creation of new hash table and transferring elements from 00030 the old table to the new table. */ 00031 00032 #ifndef __HASHTAB_H__ 00033 #define __HASHTAB_H__ 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif /* __cplusplus */ 00038 00039 #include "ansidecl.h" 00040 00041 #ifndef GTY 00042 #define GTY(X) 00043 #endif 00044 00045 /* The type for a hash code. */ 00046 typedef unsigned int hashval_t; 00047 00048 /* Callback function pointer types. */ 00049 00050 /* Calculate hash of a table entry. */ 00051 typedef hashval_t (*htab_hash) (const void *); 00052 00053 /* Compare a table entry with a possible entry. The entry already in 00054 the table always comes first, so the second element can be of a 00055 different type (but in this case htab_find and htab_find_slot 00056 cannot be used; instead the variants that accept a hash value 00057 must be used). */ 00058 typedef int (*htab_eq) (const void *, const void *); 00059 00060 /* Cleanup function called whenever a live element is removed from 00061 the hash table. */ 00062 typedef void (*htab_del) (void *); 00063 00064 /* Function called by htab_traverse for each live element. The first 00065 arg is the slot of the element (which can be passed to htab_clear_slot 00066 if desired), the second arg is the auxiliary pointer handed to 00067 htab_traverse. Return 1 to continue scan, 0 to stop. */ 00068 typedef int (*htab_trav) (void **, void *); 00069 00070 /* Memory-allocation function, with the same functionality as calloc(). 00071 Iff it returns NULL, the hash table implementation will pass an error 00072 code back to the user, so if your code doesn't handle errors, 00073 best if you use xcalloc instead. */ 00074 typedef void *(*htab_alloc) (size_t, size_t); 00075 00076 /* We also need a free() routine. */ 00077 typedef void (*htab_free) (void *); 00078 00079 /* Memory allocation and deallocation; variants which take an extra 00080 argument. */ 00081 typedef void *(*htab_alloc_with_arg) (void *, size_t, size_t); 00082 typedef void (*htab_free_with_arg) (void *, void *); 00083 00084 /* This macro defines reserved value for empty table entry. */ 00085 00086 #define HTAB_EMPTY_ENTRY ((PTR) 0) 00087 00088 /* This macro defines reserved value for table entry which contained 00089 a deleted element. */ 00090 00091 #define HTAB_DELETED_ENTRY ((PTR) 1) 00092 00093 /* Hash tables are of the following type. The structure 00094 (implementation) of this type is not needed for using the hash 00095 tables. All work with hash table should be executed only through 00096 functions mentioned below. The size of this structure is subject to 00097 change. */ 00098 00099 struct htab GTY(()) 00100 { 00101 /* Pointer to hash function. */ 00102 htab_hash hash_f; 00103 00104 /* Pointer to comparison function. */ 00105 htab_eq eq_f; 00106 00107 /* Pointer to cleanup function. */ 00108 htab_del del_f; 00109 00110 /* Table itself. */ 00111 void ** GTY ((use_param, length ("%h.size"))) entries; 00112 00113 /* Current size (in entries) of the hash table. */ 00114 size_t size; 00115 00116 /* Current number of elements including also deleted elements. */ 00117 size_t n_elements; 00118 00119 /* Current number of deleted elements in the table. */ 00120 size_t n_deleted; 00121 00122 /* The following member is used for debugging. Its value is number 00123 of all calls of `htab_find_slot' for the hash table. */ 00124 unsigned int searches; 00125 00126 /* The following member is used for debugging. Its value is number 00127 of collisions fixed for time of work with the hash table. */ 00128 unsigned int collisions; 00129 00130 /* Pointers to allocate/free functions. */ 00131 htab_alloc alloc_f; 00132 htab_free free_f; 00133 00134 /* Alternate allocate/free functions, which take an extra argument. */ 00135 void * GTY((skip)) alloc_arg; 00136 htab_alloc_with_arg alloc_with_arg_f; 00137 htab_free_with_arg free_with_arg_f; 00138 00139 /* Current size (in entries) of the hash table, as an index into the 00140 table of primes. */ 00141 unsigned int size_prime_index; 00142 }; 00143 00144 typedef struct htab *htab_t; 00145 00146 /* An enum saying whether we insert into the hash table or not. */ 00147 enum insert_option {NO_INSERT, INSERT}; 00148 00149 /* The prototypes of the package functions. */ 00150 00151 extern htab_t htab_create_alloc (size_t, htab_hash, 00152 htab_eq, htab_del, 00153 htab_alloc, htab_free); 00154 00155 extern htab_t htab_create_alloc_ex (size_t, htab_hash, 00156 htab_eq, htab_del, 00157 void *, htab_alloc_with_arg, 00158 htab_free_with_arg); 00159 00160 /* Backward-compatibility functions. */ 00161 extern htab_t htab_create (size_t, htab_hash, htab_eq, htab_del); 00162 extern htab_t htab_try_create (size_t, htab_hash, htab_eq, htab_del); 00163 00164 extern void htab_set_functions_ex (htab_t, htab_hash, 00165 htab_eq, htab_del, 00166 void *, htab_alloc_with_arg, 00167 htab_free_with_arg); 00168 00169 extern void htab_delete (htab_t); 00170 extern void htab_empty (htab_t); 00171 00172 extern void * htab_find (htab_t, const void *); 00173 extern void ** htab_find_slot (htab_t, const void *, enum insert_option); 00174 extern void * htab_find_with_hash (htab_t, const void *, hashval_t); 00175 extern void ** htab_find_slot_with_hash (htab_t, const void *, 00176 hashval_t, enum insert_option); 00177 extern void htab_clear_slot (htab_t, void **); 00178 extern void htab_remove_elt (htab_t, void *); 00179 extern void htab_remove_elt_with_hash (htab_t, void *, hashval_t); 00180 00181 extern void htab_traverse (htab_t, htab_trav, void *); 00182 extern void htab_traverse_noresize (htab_t, htab_trav, void *); 00183 00184 extern size_t htab_size (htab_t); 00185 extern size_t htab_elements (htab_t); 00186 extern double htab_collisions (htab_t); 00187 00188 /* A hash function for pointers. */ 00189 extern htab_hash htab_hash_pointer; 00190 00191 /* An equality function for pointers. */ 00192 extern htab_eq htab_eq_pointer; 00193 00194 /* A hash function for null-terminated strings. */ 00195 extern hashval_t htab_hash_string (const void *); 00196 00197 /* An iterative hash function for arbitrary data. */ 00198 extern hashval_t iterative_hash (const void *, size_t, hashval_t); 00199 /* Shorthand for hashing something with an intrinsic size. */ 00200 #define iterative_hash_object(OB,INIT) iterative_hash (&OB, sizeof (OB), INIT) 00201 00202 #ifdef __cplusplus 00203 } 00204 #endif /* __cplusplus */ 00205 00206 #endif /* __HASHTAB_H */
1.5.6