1*38fd1498Szrj /* A type-safe hash table template.
2*38fd1498Szrj Copyright (C) 2012-2018 Free Software Foundation, Inc.
3*38fd1498Szrj Contributed by Lawrence Crowl <crowl@google.com>
4*38fd1498Szrj
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3. If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>. */
20*38fd1498Szrj
21*38fd1498Szrj
22*38fd1498Szrj /* This file implements a typed hash table.
23*38fd1498Szrj The implementation borrows from libiberty's htab_t in hashtab.h.
24*38fd1498Szrj
25*38fd1498Szrj
26*38fd1498Szrj INTRODUCTION TO TYPES
27*38fd1498Szrj
28*38fd1498Szrj Users of the hash table generally need to be aware of three types.
29*38fd1498Szrj
30*38fd1498Szrj 1. The type being placed into the hash table. This type is called
31*38fd1498Szrj the value type.
32*38fd1498Szrj
33*38fd1498Szrj 2. The type used to describe how to handle the value type within
34*38fd1498Szrj the hash table. This descriptor type provides the hash table with
35*38fd1498Szrj several things.
36*38fd1498Szrj
37*38fd1498Szrj - A typedef named 'value_type' to the value type (from above).
38*38fd1498Szrj
39*38fd1498Szrj - A static member function named 'hash' that takes a value_type
40*38fd1498Szrj (or 'const value_type &') and returns a hashval_t value.
41*38fd1498Szrj
42*38fd1498Szrj - A typedef named 'compare_type' that is used to test when a value
43*38fd1498Szrj is found. This type is the comparison type. Usually, it will be the
44*38fd1498Szrj same as value_type. If it is not the same type, you must generally
45*38fd1498Szrj explicitly compute hash values and pass them to the hash table.
46*38fd1498Szrj
47*38fd1498Szrj - A static member function named 'equal' that takes a value_type
48*38fd1498Szrj and a compare_type, and returns a bool. Both arguments can be
49*38fd1498Szrj const references.
50*38fd1498Szrj
51*38fd1498Szrj - A static function named 'remove' that takes an value_type pointer
52*38fd1498Szrj and frees the memory allocated by it. This function is used when
53*38fd1498Szrj individual elements of the table need to be disposed of (e.g.,
54*38fd1498Szrj when deleting a hash table, removing elements from the table, etc).
55*38fd1498Szrj
56*38fd1498Szrj - An optional static function named 'keep_cache_entry'. This
57*38fd1498Szrj function is provided only for garbage-collected elements that
58*38fd1498Szrj are not marked by the normal gc mark pass. It describes what
59*38fd1498Szrj what should happen to the element at the end of the gc mark phase.
60*38fd1498Szrj The return value should be:
61*38fd1498Szrj - 0 if the element should be deleted
62*38fd1498Szrj - 1 if the element should be kept and needs to be marked
63*38fd1498Szrj - -1 if the element should be kept and is already marked.
64*38fd1498Szrj Returning -1 rather than 1 is purely an optimization.
65*38fd1498Szrj
66*38fd1498Szrj 3. The type of the hash table itself. (More later.)
67*38fd1498Szrj
68*38fd1498Szrj In very special circumstances, users may need to know about a fourth type.
69*38fd1498Szrj
70*38fd1498Szrj 4. The template type used to describe how hash table memory
71*38fd1498Szrj is allocated. This type is called the allocator type. It is
72*38fd1498Szrj parameterized on the value type. It provides two functions:
73*38fd1498Szrj
74*38fd1498Szrj - A static member function named 'data_alloc'. This function
75*38fd1498Szrj allocates the data elements in the table.
76*38fd1498Szrj
77*38fd1498Szrj - A static member function named 'data_free'. This function
78*38fd1498Szrj deallocates the data elements in the table.
79*38fd1498Szrj
80*38fd1498Szrj Hash table are instantiated with two type arguments.
81*38fd1498Szrj
82*38fd1498Szrj * The descriptor type, (2) above.
83*38fd1498Szrj
84*38fd1498Szrj * The allocator type, (4) above. In general, you will not need to
85*38fd1498Szrj provide your own allocator type. By default, hash tables will use
86*38fd1498Szrj the class template xcallocator, which uses malloc/free for allocation.
87*38fd1498Szrj
88*38fd1498Szrj
89*38fd1498Szrj DEFINING A DESCRIPTOR TYPE
90*38fd1498Szrj
91*38fd1498Szrj The first task in using the hash table is to describe the element type.
92*38fd1498Szrj We compose this into a few steps.
93*38fd1498Szrj
94*38fd1498Szrj 1. Decide on a removal policy for values stored in the table.
95*38fd1498Szrj hash-traits.h provides class templates for the four most common
96*38fd1498Szrj policies:
97*38fd1498Szrj
98*38fd1498Szrj * typed_free_remove implements the static 'remove' member function
99*38fd1498Szrj by calling free().
100*38fd1498Szrj
101*38fd1498Szrj * typed_noop_remove implements the static 'remove' member function
102*38fd1498Szrj by doing nothing.
103*38fd1498Szrj
104*38fd1498Szrj * ggc_remove implements the static 'remove' member by doing nothing,
105*38fd1498Szrj but instead provides routines for gc marking and for PCH streaming.
106*38fd1498Szrj Use this for garbage-collected data that needs to be preserved across
107*38fd1498Szrj collections.
108*38fd1498Szrj
109*38fd1498Szrj * ggc_cache_remove is like ggc_remove, except that it does not
110*38fd1498Szrj mark the entries during the normal gc mark phase. Instead it
111*38fd1498Szrj uses 'keep_cache_entry' (described above) to keep elements that
112*38fd1498Szrj were not collected and delete those that were. Use this for
113*38fd1498Szrj garbage-collected caches that should not in themselves stop
114*38fd1498Szrj the data from being collected.
115*38fd1498Szrj
116*38fd1498Szrj You can use these policies by simply deriving the descriptor type
117*38fd1498Szrj from one of those class template, with the appropriate argument.
118*38fd1498Szrj
119*38fd1498Szrj Otherwise, you need to write the static 'remove' member function
120*38fd1498Szrj in the descriptor class.
121*38fd1498Szrj
122*38fd1498Szrj 2. Choose a hash function. Write the static 'hash' member function.
123*38fd1498Szrj
124*38fd1498Szrj 3. Decide whether the lookup function should take as input an object
125*38fd1498Szrj of type value_type or something more restricted. Define compare_type
126*38fd1498Szrj accordingly.
127*38fd1498Szrj
128*38fd1498Szrj 4. Choose an equality testing function 'equal' that compares a value_type
129*38fd1498Szrj and a compare_type.
130*38fd1498Szrj
131*38fd1498Szrj If your elements are pointers, it is usually easiest to start with one
132*38fd1498Szrj of the generic pointer descriptors described below and override the bits
133*38fd1498Szrj you need to change.
134*38fd1498Szrj
135*38fd1498Szrj AN EXAMPLE DESCRIPTOR TYPE
136*38fd1498Szrj
137*38fd1498Szrj Suppose you want to put some_type into the hash table. You could define
138*38fd1498Szrj the descriptor type as follows.
139*38fd1498Szrj
140*38fd1498Szrj struct some_type_hasher : nofree_ptr_hash <some_type>
141*38fd1498Szrj // Deriving from nofree_ptr_hash means that we get a 'remove' that does
142*38fd1498Szrj // nothing. This choice is good for raw values.
143*38fd1498Szrj {
144*38fd1498Szrj static inline hashval_t hash (const value_type *);
145*38fd1498Szrj static inline bool equal (const value_type *, const compare_type *);
146*38fd1498Szrj };
147*38fd1498Szrj
148*38fd1498Szrj inline hashval_t
149*38fd1498Szrj some_type_hasher::hash (const value_type *e)
150*38fd1498Szrj { ... compute and return a hash value for E ... }
151*38fd1498Szrj
152*38fd1498Szrj inline bool
153*38fd1498Szrj some_type_hasher::equal (const value_type *p1, const compare_type *p2)
154*38fd1498Szrj { ... compare P1 vs P2. Return true if they are the 'same' ... }
155*38fd1498Szrj
156*38fd1498Szrj
157*38fd1498Szrj AN EXAMPLE HASH_TABLE DECLARATION
158*38fd1498Szrj
159*38fd1498Szrj To instantiate a hash table for some_type:
160*38fd1498Szrj
161*38fd1498Szrj hash_table <some_type_hasher> some_type_hash_table;
162*38fd1498Szrj
163*38fd1498Szrj There is no need to mention some_type directly, as the hash table will
164*38fd1498Szrj obtain it using some_type_hasher::value_type.
165*38fd1498Szrj
166*38fd1498Szrj You can then use any of the functions in hash_table's public interface.
167*38fd1498Szrj See hash_table for details. The interface is very similar to libiberty's
168*38fd1498Szrj htab_t.
169*38fd1498Szrj
170*38fd1498Szrj
171*38fd1498Szrj EASY DESCRIPTORS FOR POINTERS
172*38fd1498Szrj
173*38fd1498Szrj There are four descriptors for pointer elements, one for each of
174*38fd1498Szrj the removal policies above:
175*38fd1498Szrj
176*38fd1498Szrj * nofree_ptr_hash (based on typed_noop_remove)
177*38fd1498Szrj * free_ptr_hash (based on typed_free_remove)
178*38fd1498Szrj * ggc_ptr_hash (based on ggc_remove)
179*38fd1498Szrj * ggc_cache_ptr_hash (based on ggc_cache_remove)
180*38fd1498Szrj
181*38fd1498Szrj These descriptors hash and compare elements by their pointer value,
182*38fd1498Szrj rather than what they point to. So, to instantiate a hash table over
183*38fd1498Szrj pointers to whatever_type, without freeing the whatever_types, use:
184*38fd1498Szrj
185*38fd1498Szrj hash_table <nofree_ptr_hash <whatever_type> > whatever_type_hash_table;
186*38fd1498Szrj
187*38fd1498Szrj
188*38fd1498Szrj HASH TABLE ITERATORS
189*38fd1498Szrj
190*38fd1498Szrj The hash table provides standard C++ iterators. For example, consider a
191*38fd1498Szrj hash table of some_info. We wish to consume each element of the table:
192*38fd1498Szrj
193*38fd1498Szrj extern void consume (some_info *);
194*38fd1498Szrj
195*38fd1498Szrj We define a convenience typedef and the hash table:
196*38fd1498Szrj
197*38fd1498Szrj typedef hash_table <some_info_hasher> info_table_type;
198*38fd1498Szrj info_table_type info_table;
199*38fd1498Szrj
200*38fd1498Szrj Then we write the loop in typical C++ style:
201*38fd1498Szrj
202*38fd1498Szrj for (info_table_type::iterator iter = info_table.begin ();
203*38fd1498Szrj iter != info_table.end ();
204*38fd1498Szrj ++iter)
205*38fd1498Szrj if ((*iter).status == INFO_READY)
206*38fd1498Szrj consume (&*iter);
207*38fd1498Szrj
208*38fd1498Szrj Or with common sub-expression elimination:
209*38fd1498Szrj
210*38fd1498Szrj for (info_table_type::iterator iter = info_table.begin ();
211*38fd1498Szrj iter != info_table.end ();
212*38fd1498Szrj ++iter)
213*38fd1498Szrj {
214*38fd1498Szrj some_info &elem = *iter;
215*38fd1498Szrj if (elem.status == INFO_READY)
216*38fd1498Szrj consume (&elem);
217*38fd1498Szrj }
218*38fd1498Szrj
219*38fd1498Szrj One can also use a more typical GCC style:
220*38fd1498Szrj
221*38fd1498Szrj typedef some_info *some_info_p;
222*38fd1498Szrj some_info *elem_ptr;
223*38fd1498Szrj info_table_type::iterator iter;
224*38fd1498Szrj FOR_EACH_HASH_TABLE_ELEMENT (info_table, elem_ptr, some_info_p, iter)
225*38fd1498Szrj if (elem_ptr->status == INFO_READY)
226*38fd1498Szrj consume (elem_ptr);
227*38fd1498Szrj
228*38fd1498Szrj */
229*38fd1498Szrj
230*38fd1498Szrj
231*38fd1498Szrj #ifndef TYPED_HASHTAB_H
232*38fd1498Szrj #define TYPED_HASHTAB_H
233*38fd1498Szrj
234*38fd1498Szrj #include "statistics.h"
235*38fd1498Szrj #include "ggc.h"
236*38fd1498Szrj #include "vec.h"
237*38fd1498Szrj #include "hashtab.h"
238*38fd1498Szrj #include "inchash.h"
239*38fd1498Szrj #include "mem-stats-traits.h"
240*38fd1498Szrj #include "hash-traits.h"
241*38fd1498Szrj #include "hash-map-traits.h"
242*38fd1498Szrj
243*38fd1498Szrj template<typename, typename, typename> class hash_map;
244*38fd1498Szrj template<typename, typename> class hash_set;
245*38fd1498Szrj
246*38fd1498Szrj /* The ordinary memory allocator. */
247*38fd1498Szrj /* FIXME (crowl): This allocator may be extracted for wider sharing later. */
248*38fd1498Szrj
249*38fd1498Szrj template <typename Type>
250*38fd1498Szrj struct xcallocator
251*38fd1498Szrj {
252*38fd1498Szrj static Type *data_alloc (size_t count);
253*38fd1498Szrj static void data_free (Type *memory);
254*38fd1498Szrj };
255*38fd1498Szrj
256*38fd1498Szrj
257*38fd1498Szrj /* Allocate memory for COUNT data blocks. */
258*38fd1498Szrj
259*38fd1498Szrj template <typename Type>
260*38fd1498Szrj inline Type *
data_alloc(size_t count)261*38fd1498Szrj xcallocator <Type>::data_alloc (size_t count)
262*38fd1498Szrj {
263*38fd1498Szrj return static_cast <Type *> (xcalloc (count, sizeof (Type)));
264*38fd1498Szrj }
265*38fd1498Szrj
266*38fd1498Szrj
267*38fd1498Szrj /* Free memory for data blocks. */
268*38fd1498Szrj
269*38fd1498Szrj template <typename Type>
270*38fd1498Szrj inline void
data_free(Type * memory)271*38fd1498Szrj xcallocator <Type>::data_free (Type *memory)
272*38fd1498Szrj {
273*38fd1498Szrj return ::free (memory);
274*38fd1498Szrj }
275*38fd1498Szrj
276*38fd1498Szrj
277*38fd1498Szrj /* Table of primes and their inversion information. */
278*38fd1498Szrj
279*38fd1498Szrj struct prime_ent
280*38fd1498Szrj {
281*38fd1498Szrj hashval_t prime;
282*38fd1498Szrj hashval_t inv;
283*38fd1498Szrj hashval_t inv_m2; /* inverse of prime-2 */
284*38fd1498Szrj hashval_t shift;
285*38fd1498Szrj };
286*38fd1498Szrj
287*38fd1498Szrj extern struct prime_ent const prime_tab[];
288*38fd1498Szrj
289*38fd1498Szrj
290*38fd1498Szrj /* Functions for computing hash table indexes. */
291*38fd1498Szrj
292*38fd1498Szrj extern unsigned int hash_table_higher_prime_index (unsigned long n)
293*38fd1498Szrj ATTRIBUTE_PURE;
294*38fd1498Szrj
295*38fd1498Szrj /* Return X % Y using multiplicative inverse values INV and SHIFT.
296*38fd1498Szrj
297*38fd1498Szrj The multiplicative inverses computed above are for 32-bit types,
298*38fd1498Szrj and requires that we be able to compute a highpart multiply.
299*38fd1498Szrj
300*38fd1498Szrj FIX: I am not at all convinced that
301*38fd1498Szrj 3 loads, 2 multiplications, 3 shifts, and 3 additions
302*38fd1498Szrj will be faster than
303*38fd1498Szrj 1 load and 1 modulus
304*38fd1498Szrj on modern systems running a compiler. */
305*38fd1498Szrj
306*38fd1498Szrj inline hashval_t
mul_mod(hashval_t x,hashval_t y,hashval_t inv,int shift)307*38fd1498Szrj mul_mod (hashval_t x, hashval_t y, hashval_t inv, int shift)
308*38fd1498Szrj {
309*38fd1498Szrj hashval_t t1, t2, t3, t4, q, r;
310*38fd1498Szrj
311*38fd1498Szrj t1 = ((uint64_t)x * inv) >> 32;
312*38fd1498Szrj t2 = x - t1;
313*38fd1498Szrj t3 = t2 >> 1;
314*38fd1498Szrj t4 = t1 + t3;
315*38fd1498Szrj q = t4 >> shift;
316*38fd1498Szrj r = x - (q * y);
317*38fd1498Szrj
318*38fd1498Szrj return r;
319*38fd1498Szrj }
320*38fd1498Szrj
321*38fd1498Szrj /* Compute the primary table index for HASH given current prime index. */
322*38fd1498Szrj
323*38fd1498Szrj inline hashval_t
hash_table_mod1(hashval_t hash,unsigned int index)324*38fd1498Szrj hash_table_mod1 (hashval_t hash, unsigned int index)
325*38fd1498Szrj {
326*38fd1498Szrj const struct prime_ent *p = &prime_tab[index];
327*38fd1498Szrj gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32);
328*38fd1498Szrj return mul_mod (hash, p->prime, p->inv, p->shift);
329*38fd1498Szrj }
330*38fd1498Szrj
331*38fd1498Szrj /* Compute the secondary table index for HASH given current prime index. */
332*38fd1498Szrj
333*38fd1498Szrj inline hashval_t
hash_table_mod2(hashval_t hash,unsigned int index)334*38fd1498Szrj hash_table_mod2 (hashval_t hash, unsigned int index)
335*38fd1498Szrj {
336*38fd1498Szrj const struct prime_ent *p = &prime_tab[index];
337*38fd1498Szrj gcc_checking_assert (sizeof (hashval_t) * CHAR_BIT <= 32);
338*38fd1498Szrj return 1 + mul_mod (hash, p->prime - 2, p->inv_m2, p->shift);
339*38fd1498Szrj }
340*38fd1498Szrj
341*38fd1498Szrj class mem_usage;
342*38fd1498Szrj
343*38fd1498Szrj /* User-facing hash table type.
344*38fd1498Szrj
345*38fd1498Szrj The table stores elements of type Descriptor::value_type and uses
346*38fd1498Szrj the static descriptor functions described at the top of the file
347*38fd1498Szrj to hash, compare and remove elements.
348*38fd1498Szrj
349*38fd1498Szrj Specify the template Allocator to allocate and free memory.
350*38fd1498Szrj The default is xcallocator.
351*38fd1498Szrj
352*38fd1498Szrj Storage is an implementation detail and should not be used outside the
353*38fd1498Szrj hash table code.
354*38fd1498Szrj
355*38fd1498Szrj */
356*38fd1498Szrj template <typename Descriptor,
357*38fd1498Szrj template<typename Type> class Allocator = xcallocator>
358*38fd1498Szrj class hash_table
359*38fd1498Szrj {
360*38fd1498Szrj typedef typename Descriptor::value_type value_type;
361*38fd1498Szrj typedef typename Descriptor::compare_type compare_type;
362*38fd1498Szrj
363*38fd1498Szrj public:
364*38fd1498Szrj explicit hash_table (size_t, bool ggc = false,
365*38fd1498Szrj bool gather_mem_stats = GATHER_STATISTICS,
366*38fd1498Szrj mem_alloc_origin origin = HASH_TABLE_ORIGIN
367*38fd1498Szrj CXX_MEM_STAT_INFO);
368*38fd1498Szrj explicit hash_table (const hash_table &, bool ggc = false,
369*38fd1498Szrj bool gather_mem_stats = GATHER_STATISTICS,
370*38fd1498Szrj mem_alloc_origin origin = HASH_TABLE_ORIGIN
371*38fd1498Szrj CXX_MEM_STAT_INFO);
372*38fd1498Szrj ~hash_table ();
373*38fd1498Szrj
374*38fd1498Szrj /* Create a hash_table in gc memory. */
375*38fd1498Szrj static hash_table *
create_ggc(size_t n CXX_MEM_STAT_INFO)376*38fd1498Szrj create_ggc (size_t n CXX_MEM_STAT_INFO)
377*38fd1498Szrj {
378*38fd1498Szrj hash_table *table = ggc_alloc<hash_table> ();
379*38fd1498Szrj new (table) hash_table (n, true, GATHER_STATISTICS,
380*38fd1498Szrj HASH_TABLE_ORIGIN PASS_MEM_STAT);
381*38fd1498Szrj return table;
382*38fd1498Szrj }
383*38fd1498Szrj
384*38fd1498Szrj /* Current size (in entries) of the hash table. */
size()385*38fd1498Szrj size_t size () const { return m_size; }
386*38fd1498Szrj
387*38fd1498Szrj /* Return the current number of elements in this hash table. */
elements()388*38fd1498Szrj size_t elements () const { return m_n_elements - m_n_deleted; }
389*38fd1498Szrj
390*38fd1498Szrj /* Return the current number of elements in this hash table. */
elements_with_deleted()391*38fd1498Szrj size_t elements_with_deleted () const { return m_n_elements; }
392*38fd1498Szrj
393*38fd1498Szrj /* This function clears all entries in this hash table. */
empty()394*38fd1498Szrj void empty () { if (elements ()) empty_slow (); }
395*38fd1498Szrj
396*38fd1498Szrj /* This function clears a specified SLOT in a hash table. It is
397*38fd1498Szrj useful when you've already done the lookup and don't want to do it
398*38fd1498Szrj again. */
399*38fd1498Szrj void clear_slot (value_type *);
400*38fd1498Szrj
401*38fd1498Szrj /* This function searches for a hash table entry equal to the given
402*38fd1498Szrj COMPARABLE element starting with the given HASH value. It cannot
403*38fd1498Szrj be used to insert or delete an element. */
404*38fd1498Szrj value_type &find_with_hash (const compare_type &, hashval_t);
405*38fd1498Szrj
406*38fd1498Szrj /* Like find_slot_with_hash, but compute the hash value from the element. */
find(const value_type & value)407*38fd1498Szrj value_type &find (const value_type &value)
408*38fd1498Szrj {
409*38fd1498Szrj return find_with_hash (value, Descriptor::hash (value));
410*38fd1498Szrj }
411*38fd1498Szrj
find_slot(const value_type & value,insert_option insert)412*38fd1498Szrj value_type *find_slot (const value_type &value, insert_option insert)
413*38fd1498Szrj {
414*38fd1498Szrj return find_slot_with_hash (value, Descriptor::hash (value), insert);
415*38fd1498Szrj }
416*38fd1498Szrj
417*38fd1498Szrj /* This function searches for a hash table slot containing an entry
418*38fd1498Szrj equal to the given COMPARABLE element and starting with the given
419*38fd1498Szrj HASH. To delete an entry, call this with insert=NO_INSERT, then
420*38fd1498Szrj call clear_slot on the slot returned (possibly after doing some
421*38fd1498Szrj checks). To insert an entry, call this with insert=INSERT, then
422*38fd1498Szrj write the value you want into the returned slot. When inserting an
423*38fd1498Szrj entry, NULL may be returned if memory allocation fails. */
424*38fd1498Szrj value_type *find_slot_with_hash (const compare_type &comparable,
425*38fd1498Szrj hashval_t hash, enum insert_option insert);
426*38fd1498Szrj
427*38fd1498Szrj /* This function deletes an element with the given COMPARABLE value
428*38fd1498Szrj from hash table starting with the given HASH. If there is no
429*38fd1498Szrj matching element in the hash table, this function does nothing. */
430*38fd1498Szrj void remove_elt_with_hash (const compare_type &, hashval_t);
431*38fd1498Szrj
432*38fd1498Szrj /* Like remove_elt_with_hash, but compute the hash value from the
433*38fd1498Szrj element. */
remove_elt(const value_type & value)434*38fd1498Szrj void remove_elt (const value_type &value)
435*38fd1498Szrj {
436*38fd1498Szrj remove_elt_with_hash (value, Descriptor::hash (value));
437*38fd1498Szrj }
438*38fd1498Szrj
439*38fd1498Szrj /* This function scans over the entire hash table calling CALLBACK for
440*38fd1498Szrj each live entry. If CALLBACK returns false, the iteration stops.
441*38fd1498Szrj ARGUMENT is passed as CALLBACK's second argument. */
442*38fd1498Szrj template <typename Argument,
443*38fd1498Szrj int (*Callback) (value_type *slot, Argument argument)>
444*38fd1498Szrj void traverse_noresize (Argument argument);
445*38fd1498Szrj
446*38fd1498Szrj /* Like traverse_noresize, but does resize the table when it is too empty
447*38fd1498Szrj to improve effectivity of subsequent calls. */
448*38fd1498Szrj template <typename Argument,
449*38fd1498Szrj int (*Callback) (value_type *slot, Argument argument)>
450*38fd1498Szrj void traverse (Argument argument);
451*38fd1498Szrj
452*38fd1498Szrj class iterator
453*38fd1498Szrj {
454*38fd1498Szrj public:
iterator()455*38fd1498Szrj iterator () : m_slot (NULL), m_limit (NULL) {}
456*38fd1498Szrj
iterator(value_type * slot,value_type * limit)457*38fd1498Szrj iterator (value_type *slot, value_type *limit) :
458*38fd1498Szrj m_slot (slot), m_limit (limit) {}
459*38fd1498Szrj
460*38fd1498Szrj inline value_type &operator * () { return *m_slot; }
461*38fd1498Szrj void slide ();
462*38fd1498Szrj inline iterator &operator ++ ();
463*38fd1498Szrj bool operator != (const iterator &other) const
464*38fd1498Szrj {
465*38fd1498Szrj return m_slot != other.m_slot || m_limit != other.m_limit;
466*38fd1498Szrj }
467*38fd1498Szrj
468*38fd1498Szrj private:
469*38fd1498Szrj value_type *m_slot;
470*38fd1498Szrj value_type *m_limit;
471*38fd1498Szrj };
472*38fd1498Szrj
begin()473*38fd1498Szrj iterator begin () const
474*38fd1498Szrj {
475*38fd1498Szrj iterator iter (m_entries, m_entries + m_size);
476*38fd1498Szrj iter.slide ();
477*38fd1498Szrj return iter;
478*38fd1498Szrj }
479*38fd1498Szrj
end()480*38fd1498Szrj iterator end () const { return iterator (); }
481*38fd1498Szrj
collisions()482*38fd1498Szrj double collisions () const
483*38fd1498Szrj {
484*38fd1498Szrj return m_searches ? static_cast <double> (m_collisions) / m_searches : 0;
485*38fd1498Szrj }
486*38fd1498Szrj
487*38fd1498Szrj private:
488*38fd1498Szrj template<typename T> friend void gt_ggc_mx (hash_table<T> *);
489*38fd1498Szrj template<typename T> friend void gt_pch_nx (hash_table<T> *);
490*38fd1498Szrj template<typename T> friend void
491*38fd1498Szrj hashtab_entry_note_pointers (void *, void *, gt_pointer_operator, void *);
492*38fd1498Szrj template<typename T, typename U, typename V> friend void
493*38fd1498Szrj gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *);
494*38fd1498Szrj template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *,
495*38fd1498Szrj gt_pointer_operator,
496*38fd1498Szrj void *);
497*38fd1498Szrj template<typename T> friend void gt_pch_nx (hash_table<T> *,
498*38fd1498Szrj gt_pointer_operator, void *);
499*38fd1498Szrj
500*38fd1498Szrj template<typename T> friend void gt_cleare_cache (hash_table<T> *);
501*38fd1498Szrj
502*38fd1498Szrj void empty_slow ();
503*38fd1498Szrj
504*38fd1498Szrj value_type *alloc_entries (size_t n CXX_MEM_STAT_INFO) const;
505*38fd1498Szrj value_type *find_empty_slot_for_expand (hashval_t);
506*38fd1498Szrj bool too_empty_p (unsigned int);
507*38fd1498Szrj void expand ();
is_deleted(value_type & v)508*38fd1498Szrj static bool is_deleted (value_type &v)
509*38fd1498Szrj {
510*38fd1498Szrj return Descriptor::is_deleted (v);
511*38fd1498Szrj }
512*38fd1498Szrj
is_empty(value_type & v)513*38fd1498Szrj static bool is_empty (value_type &v)
514*38fd1498Szrj {
515*38fd1498Szrj return Descriptor::is_empty (v);
516*38fd1498Szrj }
517*38fd1498Szrj
mark_deleted(value_type & v)518*38fd1498Szrj static void mark_deleted (value_type &v)
519*38fd1498Szrj {
520*38fd1498Szrj Descriptor::mark_deleted (v);
521*38fd1498Szrj }
522*38fd1498Szrj
mark_empty(value_type & v)523*38fd1498Szrj static void mark_empty (value_type &v)
524*38fd1498Szrj {
525*38fd1498Szrj Descriptor::mark_empty (v);
526*38fd1498Szrj }
527*38fd1498Szrj
528*38fd1498Szrj /* Table itself. */
529*38fd1498Szrj typename Descriptor::value_type *m_entries;
530*38fd1498Szrj
531*38fd1498Szrj size_t m_size;
532*38fd1498Szrj
533*38fd1498Szrj /* Current number of elements including also deleted elements. */
534*38fd1498Szrj size_t m_n_elements;
535*38fd1498Szrj
536*38fd1498Szrj /* Current number of deleted elements in the table. */
537*38fd1498Szrj size_t m_n_deleted;
538*38fd1498Szrj
539*38fd1498Szrj /* The following member is used for debugging. Its value is number
540*38fd1498Szrj of all calls of `htab_find_slot' for the hash table. */
541*38fd1498Szrj unsigned int m_searches;
542*38fd1498Szrj
543*38fd1498Szrj /* The following member is used for debugging. Its value is number
544*38fd1498Szrj of collisions fixed for time of work with the hash table. */
545*38fd1498Szrj unsigned int m_collisions;
546*38fd1498Szrj
547*38fd1498Szrj /* Current size (in entries) of the hash table, as an index into the
548*38fd1498Szrj table of primes. */
549*38fd1498Szrj unsigned int m_size_prime_index;
550*38fd1498Szrj
551*38fd1498Szrj /* if m_entries is stored in ggc memory. */
552*38fd1498Szrj bool m_ggc;
553*38fd1498Szrj
554*38fd1498Szrj /* If we should gather memory statistics for the table. */
555*38fd1498Szrj bool m_gather_mem_stats;
556*38fd1498Szrj };
557*38fd1498Szrj
558*38fd1498Szrj /* As mem-stats.h heavily utilizes hash maps (hash tables), we have to include
559*38fd1498Szrj mem-stats.h after hash_table declaration. */
560*38fd1498Szrj
561*38fd1498Szrj #include "mem-stats.h"
562*38fd1498Szrj #include "hash-map.h"
563*38fd1498Szrj
564*38fd1498Szrj extern mem_alloc_description<mem_usage> hash_table_usage;
565*38fd1498Szrj
566*38fd1498Szrj /* Support function for statistics. */
567*38fd1498Szrj extern void dump_hash_table_loc_statistics (void);
568*38fd1498Szrj
569*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
hash_table(size_t size,bool ggc,bool gather_mem_stats,mem_alloc_origin origin MEM_STAT_DECL)570*38fd1498Szrj hash_table<Descriptor, Allocator>::hash_table (size_t size, bool ggc, bool
571*38fd1498Szrj gather_mem_stats,
572*38fd1498Szrj mem_alloc_origin origin
573*38fd1498Szrj MEM_STAT_DECL) :
574*38fd1498Szrj m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0),
575*38fd1498Szrj m_ggc (ggc), m_gather_mem_stats (gather_mem_stats)
576*38fd1498Szrj {
577*38fd1498Szrj unsigned int size_prime_index;
578*38fd1498Szrj
579*38fd1498Szrj size_prime_index = hash_table_higher_prime_index (size);
580*38fd1498Szrj size = prime_tab[size_prime_index].prime;
581*38fd1498Szrj
582*38fd1498Szrj if (m_gather_mem_stats)
583*38fd1498Szrj hash_table_usage.register_descriptor (this, origin, ggc
584*38fd1498Szrj FINAL_PASS_MEM_STAT);
585*38fd1498Szrj
586*38fd1498Szrj m_entries = alloc_entries (size PASS_MEM_STAT);
587*38fd1498Szrj m_size = size;
588*38fd1498Szrj m_size_prime_index = size_prime_index;
589*38fd1498Szrj }
590*38fd1498Szrj
591*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
hash_table(const hash_table & h,bool ggc,bool gather_mem_stats,mem_alloc_origin origin MEM_STAT_DECL)592*38fd1498Szrj hash_table<Descriptor, Allocator>::hash_table (const hash_table &h, bool ggc,
593*38fd1498Szrj bool gather_mem_stats,
594*38fd1498Szrj mem_alloc_origin origin
595*38fd1498Szrj MEM_STAT_DECL) :
596*38fd1498Szrj m_n_elements (h.m_n_elements), m_n_deleted (h.m_n_deleted),
597*38fd1498Szrj m_searches (0), m_collisions (0), m_ggc (ggc),
598*38fd1498Szrj m_gather_mem_stats (gather_mem_stats)
599*38fd1498Szrj {
600*38fd1498Szrj size_t size = h.m_size;
601*38fd1498Szrj
602*38fd1498Szrj if (m_gather_mem_stats)
603*38fd1498Szrj hash_table_usage.register_descriptor (this, origin, ggc
604*38fd1498Szrj FINAL_PASS_MEM_STAT);
605*38fd1498Szrj
606*38fd1498Szrj value_type *nentries = alloc_entries (size PASS_MEM_STAT);
607*38fd1498Szrj for (size_t i = 0; i < size; ++i)
608*38fd1498Szrj {
609*38fd1498Szrj value_type &entry = h.m_entries[i];
610*38fd1498Szrj if (is_deleted (entry))
611*38fd1498Szrj mark_deleted (nentries[i]);
612*38fd1498Szrj else if (!is_empty (entry))
613*38fd1498Szrj nentries[i] = entry;
614*38fd1498Szrj }
615*38fd1498Szrj m_entries = nentries;
616*38fd1498Szrj m_size = size;
617*38fd1498Szrj m_size_prime_index = h.m_size_prime_index;
618*38fd1498Szrj }
619*38fd1498Szrj
620*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
~hash_table()621*38fd1498Szrj hash_table<Descriptor, Allocator>::~hash_table ()
622*38fd1498Szrj {
623*38fd1498Szrj for (size_t i = m_size - 1; i < m_size; i--)
624*38fd1498Szrj if (!is_empty (m_entries[i]) && !is_deleted (m_entries[i]))
625*38fd1498Szrj Descriptor::remove (m_entries[i]);
626*38fd1498Szrj
627*38fd1498Szrj if (!m_ggc)
628*38fd1498Szrj Allocator <value_type> ::data_free (m_entries);
629*38fd1498Szrj else
630*38fd1498Szrj ggc_free (m_entries);
631*38fd1498Szrj
632*38fd1498Szrj if (m_gather_mem_stats)
633*38fd1498Szrj hash_table_usage.release_instance_overhead (this,
634*38fd1498Szrj sizeof (value_type) * m_size,
635*38fd1498Szrj true);
636*38fd1498Szrj }
637*38fd1498Szrj
638*38fd1498Szrj /* This function returns an array of empty hash table elements. */
639*38fd1498Szrj
640*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
641*38fd1498Szrj inline typename hash_table<Descriptor, Allocator>::value_type *
alloc_entries(size_t n MEM_STAT_DECL)642*38fd1498Szrj hash_table<Descriptor, Allocator>::alloc_entries (size_t n MEM_STAT_DECL) const
643*38fd1498Szrj {
644*38fd1498Szrj value_type *nentries;
645*38fd1498Szrj
646*38fd1498Szrj if (m_gather_mem_stats)
647*38fd1498Szrj hash_table_usage.register_instance_overhead (sizeof (value_type) * n, this);
648*38fd1498Szrj
649*38fd1498Szrj if (!m_ggc)
650*38fd1498Szrj nentries = Allocator <value_type> ::data_alloc (n);
651*38fd1498Szrj else
652*38fd1498Szrj nentries = ::ggc_cleared_vec_alloc<value_type> (n PASS_MEM_STAT);
653*38fd1498Szrj
654*38fd1498Szrj gcc_assert (nentries != NULL);
655*38fd1498Szrj for (size_t i = 0; i < n; i++)
656*38fd1498Szrj mark_empty (nentries[i]);
657*38fd1498Szrj
658*38fd1498Szrj return nentries;
659*38fd1498Szrj }
660*38fd1498Szrj
661*38fd1498Szrj /* Similar to find_slot, but without several unwanted side effects:
662*38fd1498Szrj - Does not call equal when it finds an existing entry.
663*38fd1498Szrj - Does not change the count of elements/searches/collisions in the
664*38fd1498Szrj hash table.
665*38fd1498Szrj This function also assumes there are no deleted entries in the table.
666*38fd1498Szrj HASH is the hash value for the element to be inserted. */
667*38fd1498Szrj
668*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
669*38fd1498Szrj typename hash_table<Descriptor, Allocator>::value_type *
find_empty_slot_for_expand(hashval_t hash)670*38fd1498Szrj hash_table<Descriptor, Allocator>::find_empty_slot_for_expand (hashval_t hash)
671*38fd1498Szrj {
672*38fd1498Szrj hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
673*38fd1498Szrj size_t size = m_size;
674*38fd1498Szrj value_type *slot = m_entries + index;
675*38fd1498Szrj hashval_t hash2;
676*38fd1498Szrj
677*38fd1498Szrj if (is_empty (*slot))
678*38fd1498Szrj return slot;
679*38fd1498Szrj gcc_checking_assert (!is_deleted (*slot));
680*38fd1498Szrj
681*38fd1498Szrj hash2 = hash_table_mod2 (hash, m_size_prime_index);
682*38fd1498Szrj for (;;)
683*38fd1498Szrj {
684*38fd1498Szrj index += hash2;
685*38fd1498Szrj if (index >= size)
686*38fd1498Szrj index -= size;
687*38fd1498Szrj
688*38fd1498Szrj slot = m_entries + index;
689*38fd1498Szrj if (is_empty (*slot))
690*38fd1498Szrj return slot;
691*38fd1498Szrj gcc_checking_assert (!is_deleted (*slot));
692*38fd1498Szrj }
693*38fd1498Szrj }
694*38fd1498Szrj
695*38fd1498Szrj /* Return true if the current table is excessively big for ELTS elements. */
696*38fd1498Szrj
697*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
698*38fd1498Szrj inline bool
too_empty_p(unsigned int elts)699*38fd1498Szrj hash_table<Descriptor, Allocator>::too_empty_p (unsigned int elts)
700*38fd1498Szrj {
701*38fd1498Szrj return elts * 8 < m_size && m_size > 32;
702*38fd1498Szrj }
703*38fd1498Szrj
704*38fd1498Szrj /* The following function changes size of memory allocated for the
705*38fd1498Szrj entries and repeatedly inserts the table elements. The occupancy
706*38fd1498Szrj of the table after the call will be about 50%. Naturally the hash
707*38fd1498Szrj table must already exist. Remember also that the place of the
708*38fd1498Szrj table entries is changed. If memory allocation fails, this function
709*38fd1498Szrj will abort. */
710*38fd1498Szrj
711*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
712*38fd1498Szrj void
expand()713*38fd1498Szrj hash_table<Descriptor, Allocator>::expand ()
714*38fd1498Szrj {
715*38fd1498Szrj value_type *oentries = m_entries;
716*38fd1498Szrj unsigned int oindex = m_size_prime_index;
717*38fd1498Szrj size_t osize = size ();
718*38fd1498Szrj value_type *olimit = oentries + osize;
719*38fd1498Szrj size_t elts = elements ();
720*38fd1498Szrj
721*38fd1498Szrj /* Resize only when table after removal of unused elements is either
722*38fd1498Szrj too full or too empty. */
723*38fd1498Szrj unsigned int nindex;
724*38fd1498Szrj size_t nsize;
725*38fd1498Szrj if (elts * 2 > osize || too_empty_p (elts))
726*38fd1498Szrj {
727*38fd1498Szrj nindex = hash_table_higher_prime_index (elts * 2);
728*38fd1498Szrj nsize = prime_tab[nindex].prime;
729*38fd1498Szrj }
730*38fd1498Szrj else
731*38fd1498Szrj {
732*38fd1498Szrj nindex = oindex;
733*38fd1498Szrj nsize = osize;
734*38fd1498Szrj }
735*38fd1498Szrj
736*38fd1498Szrj value_type *nentries = alloc_entries (nsize);
737*38fd1498Szrj
738*38fd1498Szrj if (m_gather_mem_stats)
739*38fd1498Szrj hash_table_usage.release_instance_overhead (this, sizeof (value_type)
740*38fd1498Szrj * osize);
741*38fd1498Szrj
742*38fd1498Szrj m_entries = nentries;
743*38fd1498Szrj m_size = nsize;
744*38fd1498Szrj m_size_prime_index = nindex;
745*38fd1498Szrj m_n_elements -= m_n_deleted;
746*38fd1498Szrj m_n_deleted = 0;
747*38fd1498Szrj
748*38fd1498Szrj value_type *p = oentries;
749*38fd1498Szrj do
750*38fd1498Szrj {
751*38fd1498Szrj value_type &x = *p;
752*38fd1498Szrj
753*38fd1498Szrj if (!is_empty (x) && !is_deleted (x))
754*38fd1498Szrj {
755*38fd1498Szrj value_type *q = find_empty_slot_for_expand (Descriptor::hash (x));
756*38fd1498Szrj
757*38fd1498Szrj *q = x;
758*38fd1498Szrj }
759*38fd1498Szrj
760*38fd1498Szrj p++;
761*38fd1498Szrj }
762*38fd1498Szrj while (p < olimit);
763*38fd1498Szrj
764*38fd1498Szrj if (!m_ggc)
765*38fd1498Szrj Allocator <value_type> ::data_free (oentries);
766*38fd1498Szrj else
767*38fd1498Szrj ggc_free (oentries);
768*38fd1498Szrj }
769*38fd1498Szrj
770*38fd1498Szrj /* Implements empty() in cases where it isn't a no-op. */
771*38fd1498Szrj
772*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
773*38fd1498Szrj void
empty_slow()774*38fd1498Szrj hash_table<Descriptor, Allocator>::empty_slow ()
775*38fd1498Szrj {
776*38fd1498Szrj size_t size = m_size;
777*38fd1498Szrj size_t nsize = size;
778*38fd1498Szrj value_type *entries = m_entries;
779*38fd1498Szrj int i;
780*38fd1498Szrj
781*38fd1498Szrj for (i = size - 1; i >= 0; i--)
782*38fd1498Szrj if (!is_empty (entries[i]) && !is_deleted (entries[i]))
783*38fd1498Szrj Descriptor::remove (entries[i]);
784*38fd1498Szrj
785*38fd1498Szrj /* Instead of clearing megabyte, downsize the table. */
786*38fd1498Szrj if (size > 1024*1024 / sizeof (value_type))
787*38fd1498Szrj nsize = 1024 / sizeof (value_type);
788*38fd1498Szrj else if (too_empty_p (m_n_elements))
789*38fd1498Szrj nsize = m_n_elements * 2;
790*38fd1498Szrj
791*38fd1498Szrj if (nsize != size)
792*38fd1498Szrj {
793*38fd1498Szrj int nindex = hash_table_higher_prime_index (nsize);
794*38fd1498Szrj int nsize = prime_tab[nindex].prime;
795*38fd1498Szrj
796*38fd1498Szrj if (!m_ggc)
797*38fd1498Szrj Allocator <value_type> ::data_free (m_entries);
798*38fd1498Szrj else
799*38fd1498Szrj ggc_free (m_entries);
800*38fd1498Szrj
801*38fd1498Szrj m_entries = alloc_entries (nsize);
802*38fd1498Szrj m_size = nsize;
803*38fd1498Szrj m_size_prime_index = nindex;
804*38fd1498Szrj }
805*38fd1498Szrj else
806*38fd1498Szrj {
807*38fd1498Szrj #ifndef BROKEN_VALUE_INITIALIZATION
808*38fd1498Szrj for ( ; size; ++entries, --size)
809*38fd1498Szrj *entries = value_type ();
810*38fd1498Szrj #else
811*38fd1498Szrj memset (entries, 0, size * sizeof (value_type));
812*38fd1498Szrj #endif
813*38fd1498Szrj }
814*38fd1498Szrj m_n_deleted = 0;
815*38fd1498Szrj m_n_elements = 0;
816*38fd1498Szrj }
817*38fd1498Szrj
818*38fd1498Szrj /* This function clears a specified SLOT in a hash table. It is
819*38fd1498Szrj useful when you've already done the lookup and don't want to do it
820*38fd1498Szrj again. */
821*38fd1498Szrj
822*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
823*38fd1498Szrj void
clear_slot(value_type * slot)824*38fd1498Szrj hash_table<Descriptor, Allocator>::clear_slot (value_type *slot)
825*38fd1498Szrj {
826*38fd1498Szrj gcc_checking_assert (!(slot < m_entries || slot >= m_entries + size ()
827*38fd1498Szrj || is_empty (*slot) || is_deleted (*slot)));
828*38fd1498Szrj
829*38fd1498Szrj Descriptor::remove (*slot);
830*38fd1498Szrj
831*38fd1498Szrj mark_deleted (*slot);
832*38fd1498Szrj m_n_deleted++;
833*38fd1498Szrj }
834*38fd1498Szrj
835*38fd1498Szrj /* This function searches for a hash table entry equal to the given
836*38fd1498Szrj COMPARABLE element starting with the given HASH value. It cannot
837*38fd1498Szrj be used to insert or delete an element. */
838*38fd1498Szrj
839*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
840*38fd1498Szrj typename hash_table<Descriptor, Allocator>::value_type &
841*38fd1498Szrj hash_table<Descriptor, Allocator>
find_with_hash(const compare_type & comparable,hashval_t hash)842*38fd1498Szrj ::find_with_hash (const compare_type &comparable, hashval_t hash)
843*38fd1498Szrj {
844*38fd1498Szrj m_searches++;
845*38fd1498Szrj size_t size = m_size;
846*38fd1498Szrj hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
847*38fd1498Szrj
848*38fd1498Szrj value_type *entry = &m_entries[index];
849*38fd1498Szrj if (is_empty (*entry)
850*38fd1498Szrj || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
851*38fd1498Szrj return *entry;
852*38fd1498Szrj
853*38fd1498Szrj hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
854*38fd1498Szrj for (;;)
855*38fd1498Szrj {
856*38fd1498Szrj m_collisions++;
857*38fd1498Szrj index += hash2;
858*38fd1498Szrj if (index >= size)
859*38fd1498Szrj index -= size;
860*38fd1498Szrj
861*38fd1498Szrj entry = &m_entries[index];
862*38fd1498Szrj if (is_empty (*entry)
863*38fd1498Szrj || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
864*38fd1498Szrj return *entry;
865*38fd1498Szrj }
866*38fd1498Szrj }
867*38fd1498Szrj
868*38fd1498Szrj /* This function searches for a hash table slot containing an entry
869*38fd1498Szrj equal to the given COMPARABLE element and starting with the given
870*38fd1498Szrj HASH. To delete an entry, call this with insert=NO_INSERT, then
871*38fd1498Szrj call clear_slot on the slot returned (possibly after doing some
872*38fd1498Szrj checks). To insert an entry, call this with insert=INSERT, then
873*38fd1498Szrj write the value you want into the returned slot. When inserting an
874*38fd1498Szrj entry, NULL may be returned if memory allocation fails. */
875*38fd1498Szrj
876*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
877*38fd1498Szrj typename hash_table<Descriptor, Allocator>::value_type *
878*38fd1498Szrj hash_table<Descriptor, Allocator>
find_slot_with_hash(const compare_type & comparable,hashval_t hash,enum insert_option insert)879*38fd1498Szrj ::find_slot_with_hash (const compare_type &comparable, hashval_t hash,
880*38fd1498Szrj enum insert_option insert)
881*38fd1498Szrj {
882*38fd1498Szrj if (insert == INSERT && m_size * 3 <= m_n_elements * 4)
883*38fd1498Szrj expand ();
884*38fd1498Szrj
885*38fd1498Szrj m_searches++;
886*38fd1498Szrj
887*38fd1498Szrj value_type *first_deleted_slot = NULL;
888*38fd1498Szrj hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
889*38fd1498Szrj hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
890*38fd1498Szrj value_type *entry = &m_entries[index];
891*38fd1498Szrj size_t size = m_size;
892*38fd1498Szrj if (is_empty (*entry))
893*38fd1498Szrj goto empty_entry;
894*38fd1498Szrj else if (is_deleted (*entry))
895*38fd1498Szrj first_deleted_slot = &m_entries[index];
896*38fd1498Szrj else if (Descriptor::equal (*entry, comparable))
897*38fd1498Szrj return &m_entries[index];
898*38fd1498Szrj
899*38fd1498Szrj for (;;)
900*38fd1498Szrj {
901*38fd1498Szrj m_collisions++;
902*38fd1498Szrj index += hash2;
903*38fd1498Szrj if (index >= size)
904*38fd1498Szrj index -= size;
905*38fd1498Szrj
906*38fd1498Szrj entry = &m_entries[index];
907*38fd1498Szrj if (is_empty (*entry))
908*38fd1498Szrj goto empty_entry;
909*38fd1498Szrj else if (is_deleted (*entry))
910*38fd1498Szrj {
911*38fd1498Szrj if (!first_deleted_slot)
912*38fd1498Szrj first_deleted_slot = &m_entries[index];
913*38fd1498Szrj }
914*38fd1498Szrj else if (Descriptor::equal (*entry, comparable))
915*38fd1498Szrj return &m_entries[index];
916*38fd1498Szrj }
917*38fd1498Szrj
918*38fd1498Szrj empty_entry:
919*38fd1498Szrj if (insert == NO_INSERT)
920*38fd1498Szrj return NULL;
921*38fd1498Szrj
922*38fd1498Szrj if (first_deleted_slot)
923*38fd1498Szrj {
924*38fd1498Szrj m_n_deleted--;
925*38fd1498Szrj mark_empty (*first_deleted_slot);
926*38fd1498Szrj return first_deleted_slot;
927*38fd1498Szrj }
928*38fd1498Szrj
929*38fd1498Szrj m_n_elements++;
930*38fd1498Szrj return &m_entries[index];
931*38fd1498Szrj }
932*38fd1498Szrj
933*38fd1498Szrj /* This function deletes an element with the given COMPARABLE value
934*38fd1498Szrj from hash table starting with the given HASH. If there is no
935*38fd1498Szrj matching element in the hash table, this function does nothing. */
936*38fd1498Szrj
937*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
938*38fd1498Szrj void
939*38fd1498Szrj hash_table<Descriptor, Allocator>
remove_elt_with_hash(const compare_type & comparable,hashval_t hash)940*38fd1498Szrj ::remove_elt_with_hash (const compare_type &comparable, hashval_t hash)
941*38fd1498Szrj {
942*38fd1498Szrj value_type *slot = find_slot_with_hash (comparable, hash, NO_INSERT);
943*38fd1498Szrj if (is_empty (*slot))
944*38fd1498Szrj return;
945*38fd1498Szrj
946*38fd1498Szrj Descriptor::remove (*slot);
947*38fd1498Szrj
948*38fd1498Szrj mark_deleted (*slot);
949*38fd1498Szrj m_n_deleted++;
950*38fd1498Szrj }
951*38fd1498Szrj
952*38fd1498Szrj /* This function scans over the entire hash table calling CALLBACK for
953*38fd1498Szrj each live entry. If CALLBACK returns false, the iteration stops.
954*38fd1498Szrj ARGUMENT is passed as CALLBACK's second argument. */
955*38fd1498Szrj
956*38fd1498Szrj template<typename Descriptor,
957*38fd1498Szrj template<typename Type> class Allocator>
958*38fd1498Szrj template<typename Argument,
959*38fd1498Szrj int (*Callback)
960*38fd1498Szrj (typename hash_table<Descriptor, Allocator>::value_type *slot,
961*38fd1498Szrj Argument argument)>
962*38fd1498Szrj void
traverse_noresize(Argument argument)963*38fd1498Szrj hash_table<Descriptor, Allocator>::traverse_noresize (Argument argument)
964*38fd1498Szrj {
965*38fd1498Szrj value_type *slot = m_entries;
966*38fd1498Szrj value_type *limit = slot + size ();
967*38fd1498Szrj
968*38fd1498Szrj do
969*38fd1498Szrj {
970*38fd1498Szrj value_type &x = *slot;
971*38fd1498Szrj
972*38fd1498Szrj if (!is_empty (x) && !is_deleted (x))
973*38fd1498Szrj if (! Callback (slot, argument))
974*38fd1498Szrj break;
975*38fd1498Szrj }
976*38fd1498Szrj while (++slot < limit);
977*38fd1498Szrj }
978*38fd1498Szrj
979*38fd1498Szrj /* Like traverse_noresize, but does resize the table when it is too empty
980*38fd1498Szrj to improve effectivity of subsequent calls. */
981*38fd1498Szrj
982*38fd1498Szrj template <typename Descriptor,
983*38fd1498Szrj template <typename Type> class Allocator>
984*38fd1498Szrj template <typename Argument,
985*38fd1498Szrj int (*Callback)
986*38fd1498Szrj (typename hash_table<Descriptor, Allocator>::value_type *slot,
987*38fd1498Szrj Argument argument)>
988*38fd1498Szrj void
traverse(Argument argument)989*38fd1498Szrj hash_table<Descriptor, Allocator>::traverse (Argument argument)
990*38fd1498Szrj {
991*38fd1498Szrj if (too_empty_p (elements ()))
992*38fd1498Szrj expand ();
993*38fd1498Szrj
994*38fd1498Szrj traverse_noresize <Argument, Callback> (argument);
995*38fd1498Szrj }
996*38fd1498Szrj
997*38fd1498Szrj /* Slide down the iterator slots until an active entry is found. */
998*38fd1498Szrj
999*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
1000*38fd1498Szrj void
slide()1001*38fd1498Szrj hash_table<Descriptor, Allocator>::iterator::slide ()
1002*38fd1498Szrj {
1003*38fd1498Szrj for ( ; m_slot < m_limit; ++m_slot )
1004*38fd1498Szrj {
1005*38fd1498Szrj value_type &x = *m_slot;
1006*38fd1498Szrj if (!is_empty (x) && !is_deleted (x))
1007*38fd1498Szrj return;
1008*38fd1498Szrj }
1009*38fd1498Szrj m_slot = NULL;
1010*38fd1498Szrj m_limit = NULL;
1011*38fd1498Szrj }
1012*38fd1498Szrj
1013*38fd1498Szrj /* Bump the iterator. */
1014*38fd1498Szrj
1015*38fd1498Szrj template<typename Descriptor, template<typename Type> class Allocator>
1016*38fd1498Szrj inline typename hash_table<Descriptor, Allocator>::iterator &
1017*38fd1498Szrj hash_table<Descriptor, Allocator>::iterator::operator ++ ()
1018*38fd1498Szrj {
1019*38fd1498Szrj ++m_slot;
1020*38fd1498Szrj slide ();
1021*38fd1498Szrj return *this;
1022*38fd1498Szrj }
1023*38fd1498Szrj
1024*38fd1498Szrj
1025*38fd1498Szrj /* Iterate through the elements of hash_table HTAB,
1026*38fd1498Szrj using hash_table <....>::iterator ITER,
1027*38fd1498Szrj storing each element in RESULT, which is of type TYPE. */
1028*38fd1498Szrj
1029*38fd1498Szrj #define FOR_EACH_HASH_TABLE_ELEMENT(HTAB, RESULT, TYPE, ITER) \
1030*38fd1498Szrj for ((ITER) = (HTAB).begin (); \
1031*38fd1498Szrj (ITER) != (HTAB).end () ? (RESULT = *(ITER) , true) : false; \
1032*38fd1498Szrj ++(ITER))
1033*38fd1498Szrj
1034*38fd1498Szrj /* ggc walking routines. */
1035*38fd1498Szrj
1036*38fd1498Szrj template<typename E>
1037*38fd1498Szrj static inline void
gt_ggc_mx(hash_table<E> * h)1038*38fd1498Szrj gt_ggc_mx (hash_table<E> *h)
1039*38fd1498Szrj {
1040*38fd1498Szrj typedef hash_table<E> table;
1041*38fd1498Szrj
1042*38fd1498Szrj if (!ggc_test_and_set_mark (h->m_entries))
1043*38fd1498Szrj return;
1044*38fd1498Szrj
1045*38fd1498Szrj for (size_t i = 0; i < h->m_size; i++)
1046*38fd1498Szrj {
1047*38fd1498Szrj if (table::is_empty (h->m_entries[i])
1048*38fd1498Szrj || table::is_deleted (h->m_entries[i]))
1049*38fd1498Szrj continue;
1050*38fd1498Szrj
1051*38fd1498Szrj /* Use ggc_maxbe_mx so we don't mark right away for cache tables; we'll
1052*38fd1498Szrj mark in gt_cleare_cache if appropriate. */
1053*38fd1498Szrj E::ggc_maybe_mx (h->m_entries[i]);
1054*38fd1498Szrj }
1055*38fd1498Szrj }
1056*38fd1498Szrj
1057*38fd1498Szrj template<typename D>
1058*38fd1498Szrj static inline void
hashtab_entry_note_pointers(void * obj,void * h,gt_pointer_operator op,void * cookie)1059*38fd1498Szrj hashtab_entry_note_pointers (void *obj, void *h, gt_pointer_operator op,
1060*38fd1498Szrj void *cookie)
1061*38fd1498Szrj {
1062*38fd1498Szrj hash_table<D> *map = static_cast<hash_table<D> *> (h);
1063*38fd1498Szrj gcc_checking_assert (map->m_entries == obj);
1064*38fd1498Szrj for (size_t i = 0; i < map->m_size; i++)
1065*38fd1498Szrj {
1066*38fd1498Szrj typedef hash_table<D> table;
1067*38fd1498Szrj if (table::is_empty (map->m_entries[i])
1068*38fd1498Szrj || table::is_deleted (map->m_entries[i]))
1069*38fd1498Szrj continue;
1070*38fd1498Szrj
1071*38fd1498Szrj D::pch_nx (map->m_entries[i], op, cookie);
1072*38fd1498Szrj }
1073*38fd1498Szrj }
1074*38fd1498Szrj
1075*38fd1498Szrj template<typename D>
1076*38fd1498Szrj static void
gt_pch_nx(hash_table<D> * h)1077*38fd1498Szrj gt_pch_nx (hash_table<D> *h)
1078*38fd1498Szrj {
1079*38fd1498Szrj bool success
1080*38fd1498Szrj = gt_pch_note_object (h->m_entries, h, hashtab_entry_note_pointers<D>);
1081*38fd1498Szrj gcc_checking_assert (success);
1082*38fd1498Szrj for (size_t i = 0; i < h->m_size; i++)
1083*38fd1498Szrj {
1084*38fd1498Szrj if (hash_table<D>::is_empty (h->m_entries[i])
1085*38fd1498Szrj || hash_table<D>::is_deleted (h->m_entries[i]))
1086*38fd1498Szrj continue;
1087*38fd1498Szrj
1088*38fd1498Szrj D::pch_nx (h->m_entries[i]);
1089*38fd1498Szrj }
1090*38fd1498Szrj }
1091*38fd1498Szrj
1092*38fd1498Szrj template<typename D>
1093*38fd1498Szrj static inline void
gt_pch_nx(hash_table<D> * h,gt_pointer_operator op,void * cookie)1094*38fd1498Szrj gt_pch_nx (hash_table<D> *h, gt_pointer_operator op, void *cookie)
1095*38fd1498Szrj {
1096*38fd1498Szrj op (&h->m_entries, cookie);
1097*38fd1498Szrj }
1098*38fd1498Szrj
1099*38fd1498Szrj template<typename H>
1100*38fd1498Szrj inline void
gt_cleare_cache(hash_table<H> * h)1101*38fd1498Szrj gt_cleare_cache (hash_table<H> *h)
1102*38fd1498Szrj {
1103*38fd1498Szrj typedef hash_table<H> table;
1104*38fd1498Szrj if (!h)
1105*38fd1498Szrj return;
1106*38fd1498Szrj
1107*38fd1498Szrj for (typename table::iterator iter = h->begin (); iter != h->end (); ++iter)
1108*38fd1498Szrj if (!table::is_empty (*iter) && !table::is_deleted (*iter))
1109*38fd1498Szrj {
1110*38fd1498Szrj int res = H::keep_cache_entry (*iter);
1111*38fd1498Szrj if (res == 0)
1112*38fd1498Szrj h->clear_slot (&*iter);
1113*38fd1498Szrj else if (res != -1)
1114*38fd1498Szrj H::ggc_mx (*iter);
1115*38fd1498Szrj }
1116*38fd1498Szrj }
1117*38fd1498Szrj
1118*38fd1498Szrj #endif /* TYPED_HASHTAB_H */
1119