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