* g++.dg/template/using30.C: Move ...
[official-gcc.git] / gcc / hash-table.h
blob5485d06f5bd31ee308b76187592db43e2439a3db
1 /* A type-safe hash table template.
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 Contributed by Lawrence Crowl <crowl@google.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* This file implements a typed hash table.
23 The implementation borrows from libiberty's htab_t in hashtab.h.
26 INTRODUCTION TO TYPES
28 Users of the hash table generally need to be aware of three types.
30 1. The type being placed into the hash table. This type is called
31 the value type.
33 2. The type used to describe how to handle the value type within
34 the hash table. This descriptor type provides the hash table with
35 several things.
37 - A typedef named 'value_type' to the value type (from above).
39 - A static member function named 'hash' that takes a value_type
40 pointer and returns a hashval_t value.
42 - A typedef named 'compare_type' that is used to test when an value
43 is found. This type is the comparison type. Usually, it will be the
44 same as value_type. If it is not the same type, you must generally
45 explicitly compute hash values and pass them to the hash table.
47 - A static member function named 'equal' that takes a value_type
48 pointer and a compare_type pointer, and returns a bool.
50 - A static function named 'remove' that takes an value_type pointer
51 and frees the memory allocated by it. This function is used when
52 individual elements of the table need to be disposed of (e.g.,
53 when deleting a hash table, removing elements from the table, etc).
55 3. The type of the hash table itself. (More later.)
57 In very special circumstances, users may need to know about a fourth type.
59 4. The template type used to describe how hash table memory
60 is allocated. This type is called the allocator type. It is
61 parameterized on the value type. It provides four functions.
63 - A static member function named 'data_alloc'. This function
64 allocates the data elements in the table.
66 - A static member function named 'data_free'. This function
67 deallocates the data elements in the table.
69 Hash table are instantiated with two type arguments.
71 * The descriptor type, (2) above.
73 * The allocator type, (4) above. In general, you will not need to
74 provide your own allocator type. By default, hash tables will use
75 the class template xcallocator, which uses malloc/free for allocation.
78 DEFINING A DESCRIPTOR TYPE
80 The first task in using the hash table is to describe the element type.
81 We compose this into a few steps.
83 1. Decide on a removal policy for values stored in the table.
84 This header provides class templates for the two most common
85 policies.
87 * typed_free_remove implements the static 'remove' member function
88 by calling free().
90 * typed_noop_remove implements the static 'remove' member function
91 by doing nothing.
93 You can use these policies by simply deriving the descriptor type
94 from one of those class template, with the appropriate argument.
96 Otherwise, you need to write the static 'remove' member function
97 in the descriptor class.
99 2. Choose a hash function. Write the static 'hash' member function.
101 3. Choose an equality testing function. In most cases, its two
102 arguments will be value_type pointers. If not, the first argument must
103 be a value_type pointer, and the second argument a compare_type pointer.
106 AN EXAMPLE DESCRIPTOR TYPE
108 Suppose you want to put some_type into the hash table. You could define
109 the descriptor type as follows.
111 struct some_type_hasher : typed_noop_remove <some_type>
112 // Deriving from typed_noop_remove means that we get a 'remove' that does
113 // nothing. This choice is good for raw values.
115 typedef some_type value_type;
116 typedef some_type compare_type;
117 static inline hashval_t hash (const value_type *);
118 static inline bool equal (const value_type *, const compare_type *);
121 inline hashval_t
122 some_type_hasher::hash (const value_type *e)
123 { ... compute and return a hash value for E ... }
125 inline bool
126 some_type_hasher::equal (const value_type *p1, const compare_type *p2)
127 { ... compare P1 vs P2. Return true if they are the 'same' ... }
130 AN EXAMPLE HASH_TABLE DECLARATION
132 To instantiate a hash table for some_type:
134 hash_table <some_type_hasher> some_type_hash_table;
136 There is no need to mention some_type directly, as the hash table will
137 obtain it using some_type_hasher::value_type.
139 You can then used any of the functions in hash_table's public interface.
140 See hash_table for details. The interface is very similar to libiberty's
141 htab_t.
144 EASY DESCRIPTORS FOR POINTERS
146 The class template pointer_hash provides everything you need to hash
147 pointers (as opposed to what they point to). So, to instantiate a hash
148 table over pointers to whatever_type,
150 hash_table <pointer_hash <whatever_type>> whatever_type_hash_table;
153 HASH TABLE ITERATORS
155 The hash table provides standard C++ iterators. For example, consider a
156 hash table of some_info. We wish to consume each element of the table:
158 extern void consume (some_info *);
160 We define a convenience typedef and the hash table:
162 typedef hash_table <some_info_hasher> info_table_type;
163 info_table_type info_table;
165 Then we write the loop in typical C++ style:
167 for (info_table_type::iterator iter = info_table.begin ();
168 iter != info_table.end ();
169 ++iter)
170 if ((*iter).status == INFO_READY)
171 consume (&*iter);
173 Or with common sub-expression elimination:
175 for (info_table_type::iterator iter = info_table.begin ();
176 iter != info_table.end ();
177 ++iter)
179 some_info &elem = *iter;
180 if (elem.status == INFO_READY)
181 consume (&elem);
184 One can also use a more typical GCC style:
186 typedef some_info *some_info_p;
187 some_info *elem_ptr;
188 info_table_type::iterator iter;
189 FOR_EACH_HASH_TABLE_ELEMENT (info_table, elem_ptr, some_info_p, iter)
190 if (elem_ptr->status == INFO_READY)
191 consume (elem_ptr);
196 #ifndef TYPED_HASHTAB_H
197 #define TYPED_HASHTAB_H
199 #include "ggc.h"
200 #include "hashtab.h"
201 #include <new>
203 template<typename, typename, typename> class hash_map;
204 template<typename, typename> class hash_set;
206 /* The ordinary memory allocator. */
207 /* FIXME (crowl): This allocator may be extracted for wider sharing later. */
209 template <typename Type>
210 struct xcallocator
212 static Type *data_alloc (size_t count);
213 static void data_free (Type *memory);
217 /* Allocate memory for COUNT data blocks. */
219 template <typename Type>
220 inline Type *
221 xcallocator <Type>::data_alloc (size_t count)
223 return static_cast <Type *> (xcalloc (count, sizeof (Type)));
227 /* Free memory for data blocks. */
229 template <typename Type>
230 inline void
231 xcallocator <Type>::data_free (Type *memory)
233 return ::free (memory);
237 /* Helpful type for removing with free. */
239 template <typename Type>
240 struct typed_free_remove
242 static inline void remove (Type *p);
246 /* Remove with free. */
248 template <typename Type>
249 inline void
250 typed_free_remove <Type>::remove (Type *p)
252 free (p);
256 /* Helpful type for a no-op remove. */
258 template <typename Type>
259 struct typed_noop_remove
261 static inline void remove (Type *p);
265 /* Remove doing nothing. */
267 template <typename Type>
268 inline void
269 typed_noop_remove <Type>::remove (Type *p ATTRIBUTE_UNUSED)
274 /* Pointer hash with a no-op remove method. */
276 template <typename Type>
277 struct pointer_hash : typed_noop_remove <Type>
279 typedef Type *value_type;
280 typedef Type *compare_type;
281 typedef int store_values_directly;
283 static inline hashval_t hash (const value_type &);
285 static inline bool equal (const value_type &existing, const compare_type &candidate);
288 template <typename Type>
289 inline hashval_t
290 pointer_hash <Type>::hash (const value_type &candidate)
292 /* This is a really poor hash function, but it is what the current code uses,
293 so I am reusing it to avoid an additional axis in testing. */
294 return (hashval_t) ((intptr_t)candidate >> 3);
297 template <typename Type>
298 inline bool
299 pointer_hash <Type>::equal (const value_type &existing,
300 const compare_type &candidate)
302 return existing == candidate;
305 /* Hasher for entry in gc memory. */
307 template<typename T>
308 struct ggc_hasher
310 typedef T value_type;
311 typedef T compare_type;
312 typedef int store_values_directly;
314 static void remove (T) {}
316 static void
317 ggc_mx (T p)
319 extern void gt_ggc_mx (T &);
320 gt_ggc_mx (p);
323 static void
324 pch_nx (T &p)
326 extern void gt_pch_nx (T &);
327 gt_pch_nx (p);
330 static void
331 pch_nx (T &p, gt_pointer_operator op, void *cookie)
333 op (&p, cookie);
337 /* Hasher for cache entry in gc memory. */
339 template<typename T>
340 struct ggc_cache_hasher
342 typedef T value_type;
343 typedef T compare_type;
344 typedef int store_values_directly;
346 static void remove (T &) {}
348 /* Entries are weakly held because this is for caches. */
350 static void ggc_mx (T &) {}
352 static void
353 pch_nx (T &p)
355 extern void gt_pch_nx (T &);
356 gt_pch_nx (p);
359 static void
360 pch_nx (T &p, gt_pointer_operator op, void *cookie)
362 op (&p, cookie);
365 /* Clear out entries if they are about to be gc'd. */
367 static void
368 handle_cache_entry (T &e)
370 if (e != HTAB_EMPTY_ENTRY && e != HTAB_DELETED_ENTRY && !ggc_marked_p (e))
371 e = static_cast<T> (HTAB_DELETED_ENTRY);
376 /* Table of primes and their inversion information. */
378 struct prime_ent
380 hashval_t prime;
381 hashval_t inv;
382 hashval_t inv_m2; /* inverse of prime-2 */
383 hashval_t shift;
386 extern struct prime_ent const prime_tab[];
389 /* Functions for computing hash table indexes. */
391 extern unsigned int hash_table_higher_prime_index (unsigned long n);
392 extern hashval_t hash_table_mod1 (hashval_t hash, unsigned int index);
393 extern hashval_t hash_table_mod2 (hashval_t hash, unsigned int index);
395 /* The below is some template meta programming to decide if we should use the
396 hash table partial specialization that directly stores value_type instead of
397 pointers to value_type. If the Descriptor type defines the type
398 Descriptor::store_values_directly then values are stored directly otherwise
399 pointers to them are stored. */
400 template<typename T> struct notype { typedef void type; };
402 template<typename T, typename = void>
403 struct storage_tester
405 static const bool value = false;
408 template<typename T>
409 struct storage_tester<T, typename notype<typename
410 T::store_values_directly>::type>
412 static const bool value = true;
415 template<typename Traits>
416 struct has_is_deleted
418 template<typename U, bool (*)(U &)> struct helper {};
419 template<typename U> static char test (helper<U, U::is_deleted> *);
420 template<typename U> static int test (...);
421 static const bool value = sizeof (test<Traits> (0)) == sizeof (char);
424 template<typename Type, typename Traits, bool = has_is_deleted<Traits>::value>
425 struct is_deleted_helper
427 static inline bool
428 call (Type &v)
430 return Traits::is_deleted (v);
434 template<typename Type, typename Traits>
435 struct is_deleted_helper<Type *, Traits, false>
437 static inline bool
438 call (Type *v)
440 return v == HTAB_DELETED_ENTRY;
444 template<typename Traits>
445 struct has_is_empty
447 template<typename U, bool (*)(U &)> struct helper {};
448 template<typename U> static char test (helper<U, U::is_empty> *);
449 template<typename U> static int test (...);
450 static const bool value = sizeof (test<Traits> (0)) == sizeof (char);
453 template<typename Type, typename Traits, bool = has_is_deleted<Traits>::value>
454 struct is_empty_helper
456 static inline bool
457 call (Type &v)
459 return Traits::is_empty (v);
463 template<typename Type, typename Traits>
464 struct is_empty_helper<Type *, Traits, false>
466 static inline bool
467 call (Type *v)
469 return v == HTAB_EMPTY_ENTRY;
473 template<typename Traits>
474 struct has_mark_deleted
476 template<typename U, void (*)(U &)> struct helper {};
477 template<typename U> static char test (helper<U, U::mark_deleted> *);
478 template<typename U> static int test (...);
479 static const bool value = sizeof (test<Traits> (0)) == sizeof (char);
482 template<typename Type, typename Traits, bool = has_is_deleted<Traits>::value>
483 struct mark_deleted_helper
485 static inline void
486 call (Type &v)
488 Traits::mark_deleted (v);
492 template<typename Type, typename Traits>
493 struct mark_deleted_helper<Type *, Traits, false>
495 static inline void
496 call (Type *&v)
498 v = static_cast<Type *> (HTAB_DELETED_ENTRY);
502 template<typename Traits>
503 struct has_mark_empty
505 template<typename U, void (*)(U &)> struct helper {};
506 template<typename U> static char test (helper<U, U::mark_empty> *);
507 template<typename U> static int test (...);
508 static const bool value = sizeof (test<Traits> (0)) == sizeof (char);
511 template<typename Type, typename Traits, bool = has_is_deleted<Traits>::value>
512 struct mark_empty_helper
514 static inline void
515 call (Type &v)
517 Traits::mark_empty (v);
521 template<typename Type, typename Traits>
522 struct mark_empty_helper<Type *, Traits, false>
524 static inline void
525 call (Type *&v)
527 v = static_cast<Type *> (HTAB_EMPTY_ENTRY);
531 /* User-facing hash table type.
533 The table stores elements of type Descriptor::value_type, or pointers to
534 objects of type value_type if the descriptor does not define the type
535 store_values_directly.
537 It hashes values with the hash member function.
538 The table currently works with relatively weak hash functions.
539 Use typed_pointer_hash <Value> when hashing pointers instead of objects.
541 It compares elements with the equal member function.
542 Two elements with the same hash may not be equal.
543 Use typed_pointer_equal <Value> when hashing pointers instead of objects.
545 It removes elements with the remove member function.
546 This feature is useful for freeing memory.
547 Derive from typed_null_remove <Value> when not freeing objects.
548 Derive from typed_free_remove <Value> when doing a simple object free.
550 Specify the template Allocator to allocate and free memory.
551 The default is xcallocator.
553 Storage is an implementation detail and should not be used outside the
554 hash table code.
557 template <typename Descriptor,
558 template<typename Type> class Allocator= xcallocator,
559 bool Storage = storage_tester<Descriptor>::value>
560 class hash_table
564 template <typename Descriptor,
565 template<typename Type> class Allocator>
566 class hash_table<Descriptor, Allocator, false>
568 typedef typename Descriptor::value_type value_type;
569 typedef typename Descriptor::compare_type compare_type;
571 public:
572 hash_table (size_t);
573 ~hash_table ();
575 /* Current size (in entries) of the hash table. */
576 size_t size () const { return m_size; }
578 /* Return the current number of elements in this hash table. */
579 size_t elements () const { return m_n_elements - m_n_deleted; }
581 /* Return the current number of elements in this hash table. */
582 size_t elements_with_deleted () const { return m_n_elements; }
584 /* This function clears all entries in the given hash table. */
585 void empty ();
587 /* This function clears a specified SLOT in a hash table. It is
588 useful when you've already done the lookup and don't want to do it
589 again. */
591 void clear_slot (value_type **);
593 /* This function searches for a hash table entry equal to the given
594 COMPARABLE element starting with the given HASH value. It cannot
595 be used to insert or delete an element. */
596 value_type *find_with_hash (const compare_type *, hashval_t);
598 /* Like find_slot_with_hash, but compute the hash value from the element. */
599 value_type *find (const value_type *value)
601 return find_with_hash (value, Descriptor::hash (value));
604 value_type **find_slot (const value_type *value, insert_option insert)
606 return find_slot_with_hash (value, Descriptor::hash (value), insert);
609 /* This function searches for a hash table slot containing an entry
610 equal to the given COMPARABLE element and starting with the given
611 HASH. To delete an entry, call this with insert=NO_INSERT, then
612 call clear_slot on the slot returned (possibly after doing some
613 checks). To insert an entry, call this with insert=INSERT, then
614 write the value you want into the returned slot. When inserting an
615 entry, NULL may be returned if memory allocation fails. */
616 value_type **find_slot_with_hash (const compare_type *comparable,
617 hashval_t hash, enum insert_option insert);
619 /* This function deletes an element with the given COMPARABLE value
620 from hash table starting with the given HASH. If there is no
621 matching element in the hash table, this function does nothing. */
622 void remove_elt_with_hash (const compare_type *, hashval_t);
624 /* Like remove_elt_with_hash, but compute the hash value from the element. */
625 void remove_elt (const value_type *value)
627 remove_elt_with_hash (value, Descriptor::hash (value));
630 /* This function scans over the entire hash table calling CALLBACK for
631 each live entry. If CALLBACK returns false, the iteration stops.
632 ARGUMENT is passed as CALLBACK's second argument. */
633 template <typename Argument,
634 int (*Callback) (value_type **slot, Argument argument)>
635 void traverse_noresize (Argument argument);
637 /* Like traverse_noresize, but does resize the table when it is too empty
638 to improve effectivity of subsequent calls. */
639 template <typename Argument,
640 int (*Callback) (value_type **slot, Argument argument)>
641 void traverse (Argument argument);
643 class iterator
645 public:
646 iterator () : m_slot (NULL), m_limit (NULL) {}
648 iterator (value_type **slot, value_type **limit) :
649 m_slot (slot), m_limit (limit) {}
651 inline value_type *operator * () { return *m_slot; }
652 void slide ();
653 inline iterator &operator ++ ();
654 bool operator != (const iterator &other) const
656 return m_slot != other.m_slot || m_limit != other.m_limit;
659 private:
660 value_type **m_slot;
661 value_type **m_limit;
664 iterator begin () const
666 iterator iter (m_entries, m_entries + m_size);
667 iter.slide ();
668 return iter;
671 iterator end () const { return iterator (); }
673 double collisions () const
675 return m_searches ? static_cast <double> (m_collisions) / m_searches : 0;
678 private:
680 value_type **find_empty_slot_for_expand (hashval_t);
681 void expand ();
683 /* Table itself. */
684 typename Descriptor::value_type **m_entries;
686 size_t m_size;
688 /* Current number of elements including also deleted elements. */
689 size_t m_n_elements;
691 /* Current number of deleted elements in the table. */
692 size_t m_n_deleted;
694 /* The following member is used for debugging. Its value is number
695 of all calls of `htab_find_slot' for the hash table. */
696 unsigned int m_searches;
698 /* The following member is used for debugging. Its value is number
699 of collisions fixed for time of work with the hash table. */
700 unsigned int m_collisions;
702 /* Current size (in entries) of the hash table, as an index into the
703 table of primes. */
704 unsigned int m_size_prime_index;
707 template<typename Descriptor, template<typename Type> class Allocator>
708 hash_table<Descriptor, Allocator, false>::hash_table (size_t size) :
709 m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0)
711 unsigned int size_prime_index;
713 size_prime_index = hash_table_higher_prime_index (size);
714 size = prime_tab[size_prime_index].prime;
716 m_entries = Allocator <value_type*> ::data_alloc (size);
717 gcc_assert (m_entries != NULL);
718 m_size = size;
719 m_size_prime_index = size_prime_index;
722 template<typename Descriptor, template<typename Type> class Allocator>
723 hash_table<Descriptor, Allocator, false>::~hash_table ()
725 for (size_t i = m_size - 1; i < m_size; i--)
726 if (m_entries[i] != HTAB_EMPTY_ENTRY && m_entries[i] != HTAB_DELETED_ENTRY)
727 Descriptor::remove (m_entries[i]);
729 Allocator <value_type *> ::data_free (m_entries);
732 /* Similar to find_slot, but without several unwanted side effects:
733 - Does not call equal when it finds an existing entry.
734 - Does not change the count of elements/searches/collisions in the
735 hash table.
736 This function also assumes there are no deleted entries in the table.
737 HASH is the hash value for the element to be inserted. */
739 template<typename Descriptor, template<typename Type> class Allocator>
740 typename hash_table<Descriptor, Allocator, false>::value_type **
741 hash_table<Descriptor, Allocator, false>
742 ::find_empty_slot_for_expand (hashval_t hash)
744 hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
745 size_t size = m_size;
746 value_type **slot = m_entries + index;
747 hashval_t hash2;
749 if (*slot == HTAB_EMPTY_ENTRY)
750 return slot;
751 else if (*slot == HTAB_DELETED_ENTRY)
752 abort ();
754 hash2 = hash_table_mod2 (hash, m_size_prime_index);
755 for (;;)
757 index += hash2;
758 if (index >= size)
759 index -= size;
761 slot = m_entries + index;
762 if (*slot == HTAB_EMPTY_ENTRY)
763 return slot;
764 else if (*slot == HTAB_DELETED_ENTRY)
765 abort ();
769 /* The following function changes size of memory allocated for the
770 entries and repeatedly inserts the table elements. The occupancy
771 of the table after the call will be about 50%. Naturally the hash
772 table must already exist. Remember also that the place of the
773 table entries is changed. If memory allocation fails, this function
774 will abort. */
776 template<typename Descriptor, template<typename Type> class Allocator>
777 void
778 hash_table<Descriptor, Allocator, false>::expand ()
780 value_type **oentries = m_entries;
781 unsigned int oindex = m_size_prime_index;
782 size_t osize = size ();
783 value_type **olimit = oentries + osize;
784 size_t elts = elements ();
786 /* Resize only when table after removal of unused elements is either
787 too full or too empty. */
788 unsigned int nindex;
789 size_t nsize;
790 if (elts * 2 > osize || (elts * 8 < osize && osize > 32))
792 nindex = hash_table_higher_prime_index (elts * 2);
793 nsize = prime_tab[nindex].prime;
795 else
797 nindex = oindex;
798 nsize = osize;
801 value_type **nentries = Allocator <value_type *> ::data_alloc (nsize);
802 gcc_assert (nentries != NULL);
803 m_entries = nentries;
804 m_size = nsize;
805 m_size_prime_index = nindex;
806 m_n_elements -= m_n_deleted;
807 m_n_deleted = 0;
809 value_type **p = oentries;
812 value_type *x = *p;
814 if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
816 value_type **q = find_empty_slot_for_expand (Descriptor::hash (x));
818 *q = x;
821 p++;
823 while (p < olimit);
825 Allocator <value_type *> ::data_free (oentries);
828 template<typename Descriptor, template<typename Type> class Allocator>
829 void
830 hash_table<Descriptor, Allocator, false>::empty ()
832 size_t size = m_size;
833 value_type **entries = m_entries;
834 int i;
836 for (i = size - 1; i >= 0; i--)
837 if (entries[i] != HTAB_EMPTY_ENTRY && entries[i] != HTAB_DELETED_ENTRY)
838 Descriptor::remove (entries[i]);
840 /* Instead of clearing megabyte, downsize the table. */
841 if (size > 1024*1024 / sizeof (PTR))
843 int nindex = hash_table_higher_prime_index (1024 / sizeof (PTR));
844 int nsize = prime_tab[nindex].prime;
846 Allocator <value_type *> ::data_free (m_entries);
847 m_entries = Allocator <value_type *> ::data_alloc (nsize);
848 m_size = nsize;
849 m_size_prime_index = nindex;
851 else
852 memset (entries, 0, size * sizeof (value_type *));
853 m_n_deleted = 0;
854 m_n_elements = 0;
857 /* This function clears a specified SLOT in a hash table. It is
858 useful when you've already done the lookup and don't want to do it
859 again. */
861 template<typename Descriptor, template<typename Type> class Allocator>
862 void
863 hash_table<Descriptor, Allocator, false>::clear_slot (value_type **slot)
865 if (slot < m_entries || slot >= m_entries + size ()
866 || *slot == HTAB_EMPTY_ENTRY || *slot == HTAB_DELETED_ENTRY)
867 abort ();
869 Descriptor::remove (*slot);
871 *slot = static_cast <value_type *> (HTAB_DELETED_ENTRY);
872 m_n_deleted++;
875 /* This function searches for a hash table entry equal to the given
876 COMPARABLE element starting with the given HASH value. It cannot
877 be used to insert or delete an element. */
879 template<typename Descriptor, template<typename Type> class Allocator>
880 typename hash_table<Descriptor, Allocator, false>::value_type *
881 hash_table<Descriptor, Allocator, false>
882 ::find_with_hash (const compare_type *comparable, hashval_t hash)
884 m_searches++;
885 size_t size = m_size;
886 hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
888 value_type *entry = m_entries[index];
889 if (entry == HTAB_EMPTY_ENTRY
890 || (entry != HTAB_DELETED_ENTRY && Descriptor::equal (entry, comparable)))
891 return entry;
893 hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
894 for (;;)
896 m_collisions++;
897 index += hash2;
898 if (index >= size)
899 index -= size;
901 entry = m_entries[index];
902 if (entry == HTAB_EMPTY_ENTRY
903 || (entry != HTAB_DELETED_ENTRY
904 && Descriptor::equal (entry, comparable)))
905 return entry;
909 /* This function searches for a hash table slot containing an entry
910 equal to the given COMPARABLE element and starting with the given
911 HASH. To delete an entry, call this with insert=NO_INSERT, then
912 call clear_slot on the slot returned (possibly after doing some
913 checks). To insert an entry, call this with insert=INSERT, then
914 write the value you want into the returned slot. When inserting an
915 entry, NULL may be returned if memory allocation fails. */
917 template<typename Descriptor, template<typename Type> class Allocator>
918 typename hash_table<Descriptor, Allocator, false>::value_type **
919 hash_table<Descriptor, Allocator, false>
920 ::find_slot_with_hash (const compare_type *comparable, hashval_t hash,
921 enum insert_option insert)
923 if (insert == INSERT && m_size * 3 <= m_n_elements * 4)
924 expand ();
926 m_searches++;
928 value_type **first_deleted_slot = NULL;
929 hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
930 hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
931 value_type *entry = m_entries[index];
932 size_t size = m_size;
933 if (entry == HTAB_EMPTY_ENTRY)
934 goto empty_entry;
935 else if (entry == HTAB_DELETED_ENTRY)
936 first_deleted_slot = &m_entries[index];
937 else if (Descriptor::equal (entry, comparable))
938 return &m_entries[index];
940 for (;;)
942 m_collisions++;
943 index += hash2;
944 if (index >= size)
945 index -= size;
947 entry = m_entries[index];
948 if (entry == HTAB_EMPTY_ENTRY)
949 goto empty_entry;
950 else if (entry == HTAB_DELETED_ENTRY)
952 if (!first_deleted_slot)
953 first_deleted_slot = &m_entries[index];
955 else if (Descriptor::equal (entry, comparable))
956 return &m_entries[index];
959 empty_entry:
960 if (insert == NO_INSERT)
961 return NULL;
963 if (first_deleted_slot)
965 m_n_deleted--;
966 *first_deleted_slot = static_cast <value_type *> (HTAB_EMPTY_ENTRY);
967 return first_deleted_slot;
970 m_n_elements++;
971 return &m_entries[index];
974 /* This function deletes an element with the given COMPARABLE value
975 from hash table starting with the given HASH. If there is no
976 matching element in the hash table, this function does nothing. */
978 template<typename Descriptor, template<typename Type> class Allocator>
979 void
980 hash_table<Descriptor, Allocator, false>
981 ::remove_elt_with_hash (const compare_type *comparable, hashval_t hash)
983 value_type **slot = find_slot_with_hash (comparable, hash, NO_INSERT);
984 if (*slot == HTAB_EMPTY_ENTRY)
985 return;
987 Descriptor::remove (*slot);
989 *slot = static_cast <value_type *> (HTAB_DELETED_ENTRY);
990 m_n_deleted++;
993 /* This function scans over the entire hash table calling CALLBACK for
994 each live entry. If CALLBACK returns false, the iteration stops.
995 ARGUMENT is passed as CALLBACK's second argument. */
997 template<typename Descriptor, template<typename Type> class Allocator>
998 template<typename Argument,
999 int (*Callback) (typename hash_table<Descriptor, Allocator,
1000 false>::value_type **slot,
1001 Argument argument)>
1002 void
1003 hash_table<Descriptor, Allocator, false>::traverse_noresize (Argument argument)
1005 value_type **slot = m_entries;
1006 value_type **limit = slot + size ();
1010 value_type *x = *slot;
1012 if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
1013 if (! Callback (slot, argument))
1014 break;
1016 while (++slot < limit);
1019 /* Like traverse_noresize, but does resize the table when it is too empty
1020 to improve effectivity of subsequent calls. */
1022 template <typename Descriptor,
1023 template <typename Type> class Allocator>
1024 template <typename Argument,
1025 int (*Callback) (typename hash_table<Descriptor, Allocator,
1026 false>::value_type **slot,
1027 Argument argument)>
1028 void
1029 hash_table<Descriptor, Allocator, false>::traverse (Argument argument)
1031 size_t size = m_size;
1032 if (elements () * 8 < size && size > 32)
1033 expand ();
1035 traverse_noresize <Argument, Callback> (argument);
1038 /* Slide down the iterator slots until an active entry is found. */
1040 template<typename Descriptor, template<typename Type> class Allocator>
1041 void
1042 hash_table<Descriptor, Allocator, false>::iterator::slide ()
1044 for ( ; m_slot < m_limit; ++m_slot )
1046 value_type *x = *m_slot;
1047 if (x != HTAB_EMPTY_ENTRY && x != HTAB_DELETED_ENTRY)
1048 return;
1050 m_slot = NULL;
1051 m_limit = NULL;
1054 /* Bump the iterator. */
1056 template<typename Descriptor, template<typename Type> class Allocator>
1057 inline typename hash_table<Descriptor, Allocator, false>::iterator &
1058 hash_table<Descriptor, Allocator, false>::iterator::operator ++ ()
1060 ++m_slot;
1061 slide ();
1062 return *this;
1065 /* A partial specialization used when values should be stored directly. */
1067 template <typename Descriptor,
1068 template<typename Type> class Allocator>
1069 class hash_table<Descriptor, Allocator, true>
1071 typedef typename Descriptor::value_type value_type;
1072 typedef typename Descriptor::compare_type compare_type;
1074 public:
1075 explicit hash_table (size_t, bool ggc = false);
1076 ~hash_table ();
1078 /* Create a hash_table in gc memory. */
1080 static hash_table *
1081 create_ggc (size_t n)
1083 hash_table *table = ggc_alloc<hash_table> ();
1084 new (table) hash_table (n, true);
1085 return table;
1088 /* Current size (in entries) of the hash table. */
1089 size_t size () const { return m_size; }
1091 /* Return the current number of elements in this hash table. */
1092 size_t elements () const { return m_n_elements - m_n_deleted; }
1094 /* Return the current number of elements in this hash table. */
1095 size_t elements_with_deleted () const { return m_n_elements; }
1097 /* This function clears all entries in the given hash table. */
1098 void empty ();
1100 /* This function clears a specified SLOT in a hash table. It is
1101 useful when you've already done the lookup and don't want to do it
1102 again. */
1104 void clear_slot (value_type *);
1106 /* This function searches for a hash table entry equal to the given
1107 COMPARABLE element starting with the given HASH value. It cannot
1108 be used to insert or delete an element. */
1109 value_type &find_with_hash (const compare_type &, hashval_t);
1111 /* Like find_slot_with_hash, but compute the hash value from the element. */
1112 value_type &find (const value_type &value)
1114 return find_with_hash (value, Descriptor::hash (value));
1117 value_type *find_slot (const value_type &value, insert_option insert)
1119 return find_slot_with_hash (value, Descriptor::hash (value), insert);
1122 /* This function searches for a hash table slot containing an entry
1123 equal to the given COMPARABLE element and starting with the given
1124 HASH. To delete an entry, call this with insert=NO_INSERT, then
1125 call clear_slot on the slot returned (possibly after doing some
1126 checks). To insert an entry, call this with insert=INSERT, then
1127 write the value you want into the returned slot. When inserting an
1128 entry, NULL may be returned if memory allocation fails. */
1129 value_type *find_slot_with_hash (const compare_type &comparable,
1130 hashval_t hash, enum insert_option insert);
1132 /* This function deletes an element with the given COMPARABLE value
1133 from hash table starting with the given HASH. If there is no
1134 matching element in the hash table, this function does nothing. */
1135 void remove_elt_with_hash (const compare_type &, hashval_t);
1137 /* Like remove_elt_with_hash, but compute the hash value from the element. */
1138 void remove_elt (const value_type &value)
1140 remove_elt_with_hash (value, Descriptor::hash (value));
1143 /* This function scans over the entire hash table calling CALLBACK for
1144 each live entry. If CALLBACK returns false, the iteration stops.
1145 ARGUMENT is passed as CALLBACK's second argument. */
1146 template <typename Argument,
1147 int (*Callback) (value_type *slot, Argument argument)>
1148 void traverse_noresize (Argument argument);
1150 /* Like traverse_noresize, but does resize the table when it is too empty
1151 to improve effectivity of subsequent calls. */
1152 template <typename Argument,
1153 int (*Callback) (value_type *slot, Argument argument)>
1154 void traverse (Argument argument);
1156 class iterator
1158 public:
1159 iterator () : m_slot (NULL), m_limit (NULL) {}
1161 iterator (value_type *slot, value_type *limit) :
1162 m_slot (slot), m_limit (limit) {}
1164 inline value_type &operator * () { return *m_slot; }
1165 void slide ();
1166 inline iterator &operator ++ ();
1167 bool operator != (const iterator &other) const
1169 return m_slot != other.m_slot || m_limit != other.m_limit;
1172 private:
1173 value_type *m_slot;
1174 value_type *m_limit;
1177 iterator begin () const
1179 iterator iter (m_entries, m_entries + m_size);
1180 iter.slide ();
1181 return iter;
1184 iterator end () const { return iterator (); }
1186 double collisions () const
1188 return m_searches ? static_cast <double> (m_collisions) / m_searches : 0;
1191 private:
1192 template<typename T> friend void gt_ggc_mx (hash_table<T> *);
1193 template<typename T> friend void gt_pch_nx (hash_table<T> *);
1194 template<typename T> friend void
1195 hashtab_entry_note_pointers (void *, void *, gt_pointer_operator, void *);
1196 template<typename T, typename U, typename V> friend void
1197 gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *);
1198 template<typename T, typename U> friend void gt_pch_nx (hash_set<T, U> *,
1199 gt_pointer_operator,
1200 void *);
1201 template<typename T> friend void gt_pch_nx (hash_table<T> *,
1202 gt_pointer_operator, void *);
1204 value_type *alloc_entries (size_t n) const;
1205 value_type *find_empty_slot_for_expand (hashval_t);
1206 void expand ();
1207 static bool is_deleted (value_type &v)
1209 return is_deleted_helper<value_type, Descriptor>::call (v);
1211 static bool is_empty (value_type &v)
1213 return is_empty_helper<value_type, Descriptor>::call (v);
1216 static void mark_deleted (value_type &v)
1218 return mark_deleted_helper<value_type, Descriptor>::call (v);
1221 static void mark_empty (value_type &v)
1223 return mark_empty_helper<value_type, Descriptor>::call (v);
1226 /* Table itself. */
1227 typename Descriptor::value_type *m_entries;
1229 size_t m_size;
1231 /* Current number of elements including also deleted elements. */
1232 size_t m_n_elements;
1234 /* Current number of deleted elements in the table. */
1235 size_t m_n_deleted;
1237 /* The following member is used for debugging. Its value is number
1238 of all calls of `htab_find_slot' for the hash table. */
1239 unsigned int m_searches;
1241 /* The following member is used for debugging. Its value is number
1242 of collisions fixed for time of work with the hash table. */
1243 unsigned int m_collisions;
1245 /* Current size (in entries) of the hash table, as an index into the
1246 table of primes. */
1247 unsigned int m_size_prime_index;
1249 /* if m_entries is stored in ggc memory. */
1250 bool m_ggc;
1253 template<typename Descriptor, template<typename Type> class Allocator>
1254 hash_table<Descriptor, Allocator, true>::hash_table (size_t size, bool ggc) :
1255 m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0),
1256 m_ggc (ggc)
1258 unsigned int size_prime_index;
1260 size_prime_index = hash_table_higher_prime_index (size);
1261 size = prime_tab[size_prime_index].prime;
1263 m_entries = alloc_entries (size);
1264 m_size = size;
1265 m_size_prime_index = size_prime_index;
1268 template<typename Descriptor, template<typename Type> class Allocator>
1269 hash_table<Descriptor, Allocator, true>::~hash_table ()
1271 for (size_t i = m_size - 1; i < m_size; i--)
1272 if (!is_empty (m_entries[i]) && !is_deleted (m_entries[i]))
1273 Descriptor::remove (m_entries[i]);
1275 if (!m_ggc)
1276 Allocator <value_type> ::data_free (m_entries);
1277 else
1278 ggc_free (m_entries);
1281 /* This function returns an array of empty hash table elements. */
1283 template<typename Descriptor, template<typename Type> class Allocator>
1284 inline typename hash_table<Descriptor, Allocator, true>::value_type *
1285 hash_table<Descriptor, Allocator, true>::alloc_entries (size_t n) const
1287 value_type *nentries;
1289 if (!m_ggc)
1290 nentries = Allocator <value_type> ::data_alloc (n);
1291 else
1292 nentries = ::ggc_cleared_vec_alloc<value_type> (n);
1294 gcc_assert (nentries != NULL);
1295 for (size_t i = 0; i < n; i++)
1296 mark_empty (nentries[i]);
1298 return nentries;
1301 /* Similar to find_slot, but without several unwanted side effects:
1302 - Does not call equal when it finds an existing entry.
1303 - Does not change the count of elements/searches/collisions in the
1304 hash table.
1305 This function also assumes there are no deleted entries in the table.
1306 HASH is the hash value for the element to be inserted. */
1308 template<typename Descriptor, template<typename Type> class Allocator>
1309 typename hash_table<Descriptor, Allocator, true>::value_type *
1310 hash_table<Descriptor, Allocator, true>
1311 ::find_empty_slot_for_expand (hashval_t hash)
1313 hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
1314 size_t size = m_size;
1315 value_type *slot = m_entries + index;
1316 hashval_t hash2;
1318 if (is_empty (*slot))
1319 return slot;
1320 else if (is_deleted (*slot))
1321 abort ();
1323 hash2 = hash_table_mod2 (hash, m_size_prime_index);
1324 for (;;)
1326 index += hash2;
1327 if (index >= size)
1328 index -= size;
1330 slot = m_entries + index;
1331 if (is_empty (*slot))
1332 return slot;
1333 else if (is_deleted (*slot))
1334 abort ();
1338 /* The following function changes size of memory allocated for the
1339 entries and repeatedly inserts the table elements. The occupancy
1340 of the table after the call will be about 50%. Naturally the hash
1341 table must already exist. Remember also that the place of the
1342 table entries is changed. If memory allocation fails, this function
1343 will abort. */
1345 template<typename Descriptor, template<typename Type> class Allocator>
1346 void
1347 hash_table<Descriptor, Allocator, true>::expand ()
1349 value_type *oentries = m_entries;
1350 unsigned int oindex = m_size_prime_index;
1351 size_t osize = size ();
1352 value_type *olimit = oentries + osize;
1353 size_t elts = elements ();
1355 /* Resize only when table after removal of unused elements is either
1356 too full or too empty. */
1357 unsigned int nindex;
1358 size_t nsize;
1359 if (elts * 2 > osize || (elts * 8 < osize && osize > 32))
1361 nindex = hash_table_higher_prime_index (elts * 2);
1362 nsize = prime_tab[nindex].prime;
1364 else
1366 nindex = oindex;
1367 nsize = osize;
1370 value_type *nentries = alloc_entries (nsize);
1371 m_entries = nentries;
1372 m_size = nsize;
1373 m_size_prime_index = nindex;
1374 m_n_elements -= m_n_deleted;
1375 m_n_deleted = 0;
1377 value_type *p = oentries;
1380 value_type &x = *p;
1382 if (!is_empty (x) && !is_deleted (x))
1384 value_type *q = find_empty_slot_for_expand (Descriptor::hash (x));
1386 *q = x;
1389 p++;
1391 while (p < olimit);
1393 if (!m_ggc)
1394 Allocator <value_type> ::data_free (oentries);
1395 else
1396 ggc_free (oentries);
1399 template<typename Descriptor, template<typename Type> class Allocator>
1400 void
1401 hash_table<Descriptor, Allocator, true>::empty ()
1403 size_t size = m_size;
1404 value_type *entries = m_entries;
1405 int i;
1407 for (i = size - 1; i >= 0; i--)
1408 if (!is_empty (entries[i]) && !is_deleted (entries[i]))
1409 Descriptor::remove (entries[i]);
1411 /* Instead of clearing megabyte, downsize the table. */
1412 if (size > 1024*1024 / sizeof (PTR))
1414 int nindex = hash_table_higher_prime_index (1024 / sizeof (PTR));
1415 int nsize = prime_tab[nindex].prime;
1417 if (!m_ggc)
1418 Allocator <value_type> ::data_free (m_entries);
1419 else
1420 ggc_free (m_entries);
1422 m_entries = alloc_entries (nsize);
1423 m_size = nsize;
1424 m_size_prime_index = nindex;
1426 else
1427 memset (entries, 0, size * sizeof (value_type));
1428 m_n_deleted = 0;
1429 m_n_elements = 0;
1432 /* This function clears a specified SLOT in a hash table. It is
1433 useful when you've already done the lookup and don't want to do it
1434 again. */
1436 template<typename Descriptor, template<typename Type> class Allocator>
1437 void
1438 hash_table<Descriptor, Allocator, true>::clear_slot (value_type *slot)
1440 if (slot < m_entries || slot >= m_entries + size ()
1441 || is_empty (*slot) || is_deleted (*slot))
1442 abort ();
1444 Descriptor::remove (*slot);
1446 mark_deleted (*slot);
1447 m_n_deleted++;
1450 /* This function searches for a hash table entry equal to the given
1451 COMPARABLE element starting with the given HASH value. It cannot
1452 be used to insert or delete an element. */
1454 template<typename Descriptor, template<typename Type> class Allocator>
1455 typename hash_table<Descriptor, Allocator, true>::value_type &
1456 hash_table<Descriptor, Allocator, true>
1457 ::find_with_hash (const compare_type &comparable, hashval_t hash)
1459 m_searches++;
1460 size_t size = m_size;
1461 hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
1463 value_type *entry = &m_entries[index];
1464 if (is_empty (*entry)
1465 || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
1466 return *entry;
1468 hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
1469 for (;;)
1471 m_collisions++;
1472 index += hash2;
1473 if (index >= size)
1474 index -= size;
1476 entry = &m_entries[index];
1477 if (is_empty (*entry)
1478 || (!is_deleted (*entry) && Descriptor::equal (*entry, comparable)))
1479 return *entry;
1483 /* This function searches for a hash table slot containing an entry
1484 equal to the given COMPARABLE element and starting with the given
1485 HASH. To delete an entry, call this with insert=NO_INSERT, then
1486 call clear_slot on the slot returned (possibly after doing some
1487 checks). To insert an entry, call this with insert=INSERT, then
1488 write the value you want into the returned slot. When inserting an
1489 entry, NULL may be returned if memory allocation fails. */
1491 template<typename Descriptor, template<typename Type> class Allocator>
1492 typename hash_table<Descriptor, Allocator, true>::value_type *
1493 hash_table<Descriptor, Allocator, true>
1494 ::find_slot_with_hash (const compare_type &comparable, hashval_t hash,
1495 enum insert_option insert)
1497 if (insert == INSERT && m_size * 3 <= m_n_elements * 4)
1498 expand ();
1500 m_searches++;
1502 value_type *first_deleted_slot = NULL;
1503 hashval_t index = hash_table_mod1 (hash, m_size_prime_index);
1504 hashval_t hash2 = hash_table_mod2 (hash, m_size_prime_index);
1505 value_type *entry = &m_entries[index];
1506 size_t size = m_size;
1507 if (is_empty (*entry))
1508 goto empty_entry;
1509 else if (is_deleted (*entry))
1510 first_deleted_slot = &m_entries[index];
1511 else if (Descriptor::equal (*entry, comparable))
1512 return &m_entries[index];
1514 for (;;)
1516 m_collisions++;
1517 index += hash2;
1518 if (index >= size)
1519 index -= size;
1521 entry = &m_entries[index];
1522 if (is_empty (*entry))
1523 goto empty_entry;
1524 else if (is_deleted (*entry))
1526 if (!first_deleted_slot)
1527 first_deleted_slot = &m_entries[index];
1529 else if (Descriptor::equal (*entry, comparable))
1530 return &m_entries[index];
1533 empty_entry:
1534 if (insert == NO_INSERT)
1535 return NULL;
1537 if (first_deleted_slot)
1539 m_n_deleted--;
1540 mark_empty (*first_deleted_slot);
1541 return first_deleted_slot;
1544 m_n_elements++;
1545 return &m_entries[index];
1548 /* This function deletes an element with the given COMPARABLE value
1549 from hash table starting with the given HASH. If there is no
1550 matching element in the hash table, this function does nothing. */
1552 template<typename Descriptor, template<typename Type> class Allocator>
1553 void
1554 hash_table<Descriptor, Allocator, true>
1555 ::remove_elt_with_hash (const compare_type &comparable, hashval_t hash)
1557 value_type *slot = find_slot_with_hash (comparable, hash, NO_INSERT);
1558 if (is_empty (*slot))
1559 return;
1561 Descriptor::remove (*slot);
1563 mark_deleted (*slot);
1564 m_n_deleted++;
1567 /* This function scans over the entire hash table calling CALLBACK for
1568 each live entry. If CALLBACK returns false, the iteration stops.
1569 ARGUMENT is passed as CALLBACK's second argument. */
1571 template<typename Descriptor,
1572 template<typename Type> class Allocator>
1573 template<typename Argument,
1574 int (*Callback) (typename hash_table<Descriptor, Allocator,
1575 true>::value_type *slot,
1576 Argument argument)>
1577 void
1578 hash_table<Descriptor, Allocator, true>::traverse_noresize (Argument argument)
1580 value_type *slot = m_entries;
1581 value_type *limit = slot + size ();
1585 value_type &x = *slot;
1587 if (!is_empty (x) && !is_deleted (x))
1588 if (! Callback (slot, argument))
1589 break;
1591 while (++slot < limit);
1594 /* Like traverse_noresize, but does resize the table when it is too empty
1595 to improve effectivity of subsequent calls. */
1597 template <typename Descriptor,
1598 template <typename Type> class Allocator>
1599 template <typename Argument,
1600 int (*Callback) (typename hash_table<Descriptor, Allocator,
1601 true>::value_type *slot,
1602 Argument argument)>
1603 void
1604 hash_table<Descriptor, Allocator, true>::traverse (Argument argument)
1606 size_t size = m_size;
1607 if (elements () * 8 < size && size > 32)
1608 expand ();
1610 traverse_noresize <Argument, Callback> (argument);
1613 /* Slide down the iterator slots until an active entry is found. */
1615 template<typename Descriptor, template<typename Type> class Allocator>
1616 void
1617 hash_table<Descriptor, Allocator, true>::iterator::slide ()
1619 for ( ; m_slot < m_limit; ++m_slot )
1621 value_type &x = *m_slot;
1622 if (!is_empty (x) && !is_deleted (x))
1623 return;
1625 m_slot = NULL;
1626 m_limit = NULL;
1629 /* Bump the iterator. */
1631 template<typename Descriptor, template<typename Type> class Allocator>
1632 inline typename hash_table<Descriptor, Allocator, true>::iterator &
1633 hash_table<Descriptor, Allocator, true>::iterator::operator ++ ()
1635 ++m_slot;
1636 slide ();
1637 return *this;
1641 /* Iterate through the elements of hash_table HTAB,
1642 using hash_table <....>::iterator ITER,
1643 storing each element in RESULT, which is of type TYPE. */
1645 #define FOR_EACH_HASH_TABLE_ELEMENT(HTAB, RESULT, TYPE, ITER) \
1646 for ((ITER) = (HTAB).begin (); \
1647 (ITER) != (HTAB).end () ? (RESULT = *(ITER) , true) : false; \
1648 ++(ITER))
1650 /* ggc walking routines. */
1652 template<typename E>
1653 static inline void
1654 gt_ggc_mx (hash_table<E> *h)
1656 typedef hash_table<E> table;
1658 if (!ggc_test_and_set_mark (h->m_entries))
1659 return;
1661 for (size_t i = 0; i < h->m_size; i++)
1663 if (table::is_empty (h->m_entries[i])
1664 || table::is_deleted (h->m_entries[i]))
1665 continue;
1667 E::ggc_mx (h->m_entries[i]);
1671 template<typename D>
1672 static inline void
1673 hashtab_entry_note_pointers (void *obj, void *h, gt_pointer_operator op,
1674 void *cookie)
1676 hash_table<D> *map = static_cast<hash_table<D> *> (h);
1677 gcc_checking_assert (map->m_entries == obj);
1678 for (size_t i = 0; i < map->m_size; i++)
1680 typedef hash_table<D> table;
1681 if (table::is_empty (map->m_entries[i])
1682 || table::is_deleted (map->m_entries[i]))
1683 continue;
1685 D::pch_nx (map->m_entries[i], op, cookie);
1689 template<typename D>
1690 static void
1691 gt_pch_nx (hash_table<D> *h)
1693 bool success
1694 = gt_pch_note_object (h->m_entries, h, hashtab_entry_note_pointers<D>);
1695 gcc_checking_assert (success);
1696 for (size_t i = 0; i < h->m_size; i++)
1698 if (hash_table<D>::is_empty (h->m_entries[i])
1699 || hash_table<D>::is_deleted (h->m_entries[i]))
1700 continue;
1702 D::pch_nx (h->m_entries[i]);
1706 template<typename D>
1707 static inline void
1708 gt_pch_nx (hash_table<D> *h, gt_pointer_operator op, void *cookie)
1710 op (&h->m_entries, cookie);
1713 template<typename H>
1714 inline void
1715 gt_cleare_cache (hash_table<H> *h)
1717 if (!h)
1718 return;
1720 for (typename hash_table<H>::iterator iter = h->begin (); iter != h->end ();
1721 ++iter)
1722 H::handle_cache_entry (*iter);
1725 #endif /* TYPED_HASHTAB_H */