00001 /* An expandable hash tables datatype. 00002 Copyright (C) 1999, 2000 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 /* The type for a hash code. */ 00040 typedef unsigned int hashval_t; 00041 00042 /* Callback function pointer types. */ 00043 00044 /* Calculate hash of a table entry. */ 00045 typedef hashval_t (*htab_hash) (const void *); 00046 00047 /* Compare a table entry with a possible entry. The entry already in 00048 the table always comes first, so the second element can be of a 00049 different type (but in this case htab_find and htab_find_slot 00050 cannot be used; instead the variants that accept a hash value 00051 must be used). */ 00052 typedef int (*htab_eq) (const void *, const void *); 00053 00054 /* Cleanup function called whenever a live element is removed from 00055 the hash table. */ 00056 typedef void (*htab_del) (void *); 00057 00058 /* Function called by htab_traverse for each live element. The first 00059 arg is the slot of the element (which can be passed to htab_clear_slot 00060 if desired), the second arg is the auxiliary pointer handed to 00061 htab_traverse. Return 1 to continue scan, 0 to stop. */ 00062 typedef int (*htab_trav) (void **, void *); 00063 00064 /* Hash tables are of the following type. The structure 00065 (implementation) of this type is not needed for using the hash 00066 tables. All work with hash table should be executed only through 00067 functions mentioned below. */ 00068 00069 struct htab 00070 { 00071 /* Pointer to hash function. */ 00072 htab_hash hash_f; 00073 00074 /* Pointer to comparison function. */ 00075 htab_eq eq_f; 00076 00077 /* Pointer to cleanup function. */ 00078 htab_del del_f; 00079 00080 /* Table itself. */ 00081 void **entries; 00082 00083 /* Current size (in entries) of the hash table */ 00084 size_t size; 00085 00086 /* Current number of elements including also deleted elements */ 00087 size_t n_elements; 00088 00089 /* Current number of deleted elements in the table */ 00090 size_t n_deleted; 00091 00092 /* The following member is used for debugging. Its value is number 00093 of all calls of `htab_find_slot' for the hash table. */ 00094 unsigned int searches; 00095 00096 /* The following member is used for debugging. Its value is number 00097 of collisions fixed for time of work with the hash table. */ 00098 unsigned int collisions; 00099 00100 /* This is non-zero if we are allowed to return NULL for function calls 00101 that allocate memory. */ 00102 int return_allocation_failure; 00103 }; 00104 00105 typedef struct htab *htab_t; 00106 00107 /* An enum saying whether we insert into the hash table or not. */ 00108 enum insert_option {NO_INSERT, INSERT}; 00109 00110 /* The prototypes of the package functions. */ 00111 00112 /* This function is like htab_create, but may return NULL if memory 00113 allocation fails, and also signals that htab_find_slot_with_hash and 00114 htab_find_slot are allowed to return NULL when inserting. */ 00115 extern htab_t htab_try_create (size_t, htab_hash, htab_eq, htab_del); 00116 extern void htab_delete (htab_t); 00117 extern void htab_empty (htab_t); 00118 00119 extern void *htab_find (htab_t, const void *); 00120 extern void **htab_find_slot (htab_t, const void *, enum insert_option); 00121 extern void *htab_find_with_hash (htab_t, const void *, hashval_t); 00122 extern void **htab_find_slot_with_hash (htab_t, const void *, hashval_t, 00123 enum insert_option); 00124 extern void htab_clear_slot (htab_t, void **); 00125 extern void htab_remove_elt (htab_t, void *); 00126 00127 extern void htab_traverse (htab_t, htab_trav, void *); 00128 00129 extern size_t htab_size (htab_t); 00130 extern size_t htab_elements (htab_t); 00131 extern double htab_collisions (htab_t); 00132 00133 /* A hash function for pointers. */ 00134 extern htab_hash htab_hash_pointer; 00135 00136 /* An equality function for pointers. */ 00137 extern htab_eq htab_eq_pointer; 00138 00139 #ifdef __cplusplus 00140 } 00141 #endif /* __cplusplus */ 00142 00143 #endif /* __HASHTAB_H */