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