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
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
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.
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
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
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
87 * typed_free_remove implements the static 'remove' member function
90 * typed_noop_remove implements the static 'remove' member function
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 *);
122 some_type_hasher::hash (const value_type *e)
123 { ... compute and return a hash value for E ... }
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
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;
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 ();
170 if ((*iter).status == INFO_READY)
173 Or with common sub-expression elimination:
175 for (info_table_type::iterator iter = info_table.begin ();
176 iter != info_table.end ();
179 some_info &elem = *iter;
180 if (elem.status == INFO_READY)
184 One can also use a more typical GCC style:
186 typedef some_info *some_info_p;
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)
196 #ifndef TYPED_HASHTAB_H
197 #define TYPED_HASHTAB_H
202 /* The ordinary memory allocator. */
203 /* FIXME (crowl): This allocator may be extracted for wider sharing later. */
205 template <typename Type
>
208 static Type
*data_alloc (size_t count
);
209 static void data_free (Type
*memory
);
213 /* Allocate memory for COUNT data blocks. */
215 template <typename Type
>
217 xcallocator
<Type
>::data_alloc (size_t count
)
219 return static_cast <Type
*> (xcalloc (count
, sizeof (Type
)));
223 /* Free memory for data blocks. */
225 template <typename Type
>
227 xcallocator
<Type
>::data_free (Type
*memory
)
229 return ::free (memory
);
233 /* Helpful type for removing with free. */
235 template <typename Type
>
236 struct typed_free_remove
238 static inline void remove (Type
*p
);
242 /* Remove with free. */
244 template <typename Type
>
246 typed_free_remove
<Type
>::remove (Type
*p
)
252 /* Helpful type for a no-op remove. */
254 template <typename Type
>
255 struct typed_noop_remove
257 static inline void remove (Type
*p
);
261 /* Remove doing nothing. */
263 template <typename Type
>
265 typed_noop_remove
<Type
>::remove (Type
*p ATTRIBUTE_UNUSED
)
270 /* Pointer hash with a no-op remove method. */
272 template <typename Type
>
273 struct pointer_hash
: typed_noop_remove
<Type
>
275 typedef Type
*value_type
;
276 typedef Type
*compare_type
;
277 typedef int store_values_directly
;
279 static inline hashval_t
hash (const value_type
&);
281 static inline bool equal (const value_type
&existing
, const compare_type
&candidate
);
284 template <typename Type
>
286 pointer_hash
<Type
>::hash (const value_type
&candidate
)
288 /* This is a really poor hash function, but it is what the current code uses,
289 so I am reusing it to avoid an additional axis in testing. */
290 return (hashval_t
) ((intptr_t)candidate
>> 3);
293 template <typename Type
>
295 pointer_hash
<Type
>::equal (const value_type
&existing
,
296 const compare_type
&candidate
)
298 return existing
== candidate
;
302 /* Table of primes and their inversion information. */
308 hashval_t inv_m2
; /* inverse of prime-2 */
312 extern struct prime_ent
const prime_tab
[];
315 /* Functions for computing hash table indexes. */
317 extern unsigned int hash_table_higher_prime_index (unsigned long n
);
318 extern hashval_t
hash_table_mod1 (hashval_t hash
, unsigned int index
);
319 extern hashval_t
hash_table_mod2 (hashval_t hash
, unsigned int index
);
321 /* The below is some template meta programming to decide if we should use the
322 hash table partial specialization that directly stores value_type instead of
323 pointers to value_type. If the Descriptor type defines the type
324 Descriptor::store_values_directly then values are stored directly otherwise
325 pointers to them are stored. */
326 template<typename T
> struct notype
{ typedef void type
; };
328 template<typename T
, typename
= void>
329 struct storage_tester
331 static const bool value
= false;
335 struct storage_tester
<T
, typename notype
<typename
336 T::store_values_directly
>::type
>
338 static const bool value
= true;
341 template<typename Traits
>
342 struct has_is_deleted
344 template<typename U
, bool (*)(U
&)> struct helper
{};
345 template<typename U
> static char test (helper
<U
, U::is_deleted
> *);
346 template<typename U
> static int test (...);
347 static const bool value
= sizeof (test
<Traits
> (0)) == sizeof (char);
350 template<typename Type
, typename Traits
, bool = has_is_deleted
<Traits
>::value
>
351 struct is_deleted_helper
356 return Traits::is_deleted (v
);
360 template<typename Type
, typename Traits
>
361 struct is_deleted_helper
<Type
*, Traits
, false>
366 return v
== HTAB_DELETED_ENTRY
;
370 template<typename Traits
>
373 template<typename U
, bool (*)(U
&)> struct helper
{};
374 template<typename U
> static char test (helper
<U
, U::is_empty
> *);
375 template<typename U
> static int test (...);
376 static const bool value
= sizeof (test
<Traits
> (0)) == sizeof (char);
379 template<typename Type
, typename Traits
, bool = has_is_deleted
<Traits
>::value
>
380 struct is_empty_helper
385 return Traits::is_empty (v
);
389 template<typename Type
, typename Traits
>
390 struct is_empty_helper
<Type
*, Traits
, false>
395 return v
== HTAB_EMPTY_ENTRY
;
399 template<typename Traits
>
400 struct has_mark_deleted
402 template<typename U
, void (*)(U
&)> struct helper
{};
403 template<typename U
> static char test (helper
<U
, U::mark_deleted
> *);
404 template<typename U
> static int test (...);
405 static const bool value
= sizeof (test
<Traits
> (0)) == sizeof (char);
408 template<typename Type
, typename Traits
, bool = has_is_deleted
<Traits
>::value
>
409 struct mark_deleted_helper
414 Traits::mark_deleted (v
);
418 template<typename Type
, typename Traits
>
419 struct mark_deleted_helper
<Type
*, Traits
, false>
424 v
= static_cast<Type
*> (HTAB_DELETED_ENTRY
);
428 template<typename Traits
>
429 struct has_mark_empty
431 template<typename U
, void (*)(U
&)> struct helper
{};
432 template<typename U
> static char test (helper
<U
, U::mark_empty
> *);
433 template<typename U
> static int test (...);
434 static const bool value
= sizeof (test
<Traits
> (0)) == sizeof (char);
437 template<typename Type
, typename Traits
, bool = has_is_deleted
<Traits
>::value
>
438 struct mark_empty_helper
443 Traits::mark_empty (v
);
447 template<typename Type
, typename Traits
>
448 struct mark_empty_helper
<Type
*, Traits
, false>
453 v
= static_cast<Type
*> (HTAB_EMPTY_ENTRY
);
457 /* User-facing hash table type.
459 The table stores elements of type Descriptor::value_type, or pointers to
460 objects of type value_type if the descriptor does not define the type
461 store_values_directly.
463 It hashes values with the hash member function.
464 The table currently works with relatively weak hash functions.
465 Use typed_pointer_hash <Value> when hashing pointers instead of objects.
467 It compares elements with the equal member function.
468 Two elements with the same hash may not be equal.
469 Use typed_pointer_equal <Value> when hashing pointers instead of objects.
471 It removes elements with the remove member function.
472 This feature is useful for freeing memory.
473 Derive from typed_null_remove <Value> when not freeing objects.
474 Derive from typed_free_remove <Value> when doing a simple object free.
476 Specify the template Allocator to allocate and free memory.
477 The default is xcallocator.
479 Storage is an implementation detail and should not be used outside the
483 template <typename Descriptor
,
484 template<typename Type
> class Allocator
= xcallocator
,
485 bool Storage
= storage_tester
<Descriptor
>::value
>
490 template <typename Descriptor
,
491 template<typename Type
> class Allocator
>
492 class hash_table
<Descriptor
, Allocator
, false>
494 typedef typename
Descriptor::value_type value_type
;
495 typedef typename
Descriptor::compare_type compare_type
;
501 /* Current size (in entries) of the hash table. */
502 size_t size () const { return m_size
; }
504 /* Return the current number of elements in this hash table. */
505 size_t elements () const { return m_n_elements
- m_n_deleted
; }
507 /* Return the current number of elements in this hash table. */
508 size_t elements_with_deleted () const { return m_n_elements
; }
510 /* This function clears all entries in the given hash table. */
513 /* This function clears a specified SLOT in a hash table. It is
514 useful when you've already done the lookup and don't want to do it
517 void clear_slot (value_type
**);
519 /* This function searches for a hash table entry equal to the given
520 COMPARABLE element starting with the given HASH value. It cannot
521 be used to insert or delete an element. */
522 value_type
*find_with_hash (const compare_type
*, hashval_t
);
524 /* Like find_slot_with_hash, but compute the hash value from the element. */
525 value_type
*find (const value_type
*value
)
527 return find_with_hash (value
, Descriptor::hash (value
));
530 value_type
**find_slot (const value_type
*value
, insert_option insert
)
532 return find_slot_with_hash (value
, Descriptor::hash (value
), insert
);
535 /* This function searches for a hash table slot containing an entry
536 equal to the given COMPARABLE element and starting with the given
537 HASH. To delete an entry, call this with insert=NO_INSERT, then
538 call clear_slot on the slot returned (possibly after doing some
539 checks). To insert an entry, call this with insert=INSERT, then
540 write the value you want into the returned slot. When inserting an
541 entry, NULL may be returned if memory allocation fails. */
542 value_type
**find_slot_with_hash (const compare_type
*comparable
,
543 hashval_t hash
, enum insert_option insert
);
545 /* This function deletes an element with the given COMPARABLE value
546 from hash table starting with the given HASH. If there is no
547 matching element in the hash table, this function does nothing. */
548 void remove_elt_with_hash (const compare_type
*, hashval_t
);
550 /* Like remove_elt_with_hash, but compute the hash value from the element. */
551 void remove_elt (const value_type
*value
)
553 remove_elt_with_hash (value
, Descriptor::hash (value
));
556 /* This function scans over the entire hash table calling CALLBACK for
557 each live entry. If CALLBACK returns false, the iteration stops.
558 ARGUMENT is passed as CALLBACK's second argument. */
559 template <typename Argument
,
560 int (*Callback
) (value_type
**slot
, Argument argument
)>
561 void traverse_noresize (Argument argument
);
563 /* Like traverse_noresize, but does resize the table when it is too empty
564 to improve effectivity of subsequent calls. */
565 template <typename Argument
,
566 int (*Callback
) (value_type
**slot
, Argument argument
)>
567 void traverse (Argument argument
);
572 iterator () : m_slot (NULL
), m_limit (NULL
) {}
574 iterator (value_type
**slot
, value_type
**limit
) :
575 m_slot (slot
), m_limit (limit
) {}
577 inline value_type
*operator * () { return *m_slot
; }
579 inline iterator
&operator ++ ();
580 bool operator != (const iterator
&other
) const
582 return m_slot
!= other
.m_slot
|| m_limit
!= other
.m_limit
;
587 value_type
**m_limit
;
590 iterator
begin () const
592 iterator
iter (m_entries
, m_entries
+ m_size
);
597 iterator
end () const { return iterator (); }
599 double collisions () const
601 return m_searches
? static_cast <double> (m_collisions
) / m_searches
: 0;
606 value_type
**find_empty_slot_for_expand (hashval_t
);
610 typename
Descriptor::value_type
**m_entries
;
614 /* Current number of elements including also deleted elements. */
617 /* Current number of deleted elements in the table. */
620 /* The following member is used for debugging. Its value is number
621 of all calls of `htab_find_slot' for the hash table. */
622 unsigned int m_searches
;
624 /* The following member is used for debugging. Its value is number
625 of collisions fixed for time of work with the hash table. */
626 unsigned int m_collisions
;
628 /* Current size (in entries) of the hash table, as an index into the
630 unsigned int m_size_prime_index
;
633 template<typename Descriptor
, template<typename Type
> class Allocator
>
634 hash_table
<Descriptor
, Allocator
, false>::hash_table (size_t size
) :
635 m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0)
637 unsigned int size_prime_index
;
639 size_prime_index
= hash_table_higher_prime_index (size
);
640 size
= prime_tab
[size_prime_index
].prime
;
642 m_entries
= Allocator
<value_type
*> ::data_alloc (size
);
643 gcc_assert (m_entries
!= NULL
);
645 m_size_prime_index
= size_prime_index
;
648 template<typename Descriptor
, template<typename Type
> class Allocator
>
649 hash_table
<Descriptor
, Allocator
, false>::~hash_table ()
651 for (size_t i
= m_size
- 1; i
< m_size
; i
--)
652 if (m_entries
[i
] != HTAB_EMPTY_ENTRY
&& m_entries
[i
] != HTAB_DELETED_ENTRY
)
653 Descriptor::remove (m_entries
[i
]);
655 Allocator
<value_type
*> ::data_free (m_entries
);
658 /* Similar to find_slot, but without several unwanted side effects:
659 - Does not call equal when it finds an existing entry.
660 - Does not change the count of elements/searches/collisions in the
662 This function also assumes there are no deleted entries in the table.
663 HASH is the hash value for the element to be inserted. */
665 template<typename Descriptor
, template<typename Type
> class Allocator
>
666 typename hash_table
<Descriptor
, Allocator
, false>::value_type
**
667 hash_table
<Descriptor
, Allocator
, false>
668 ::find_empty_slot_for_expand (hashval_t hash
)
670 hashval_t index
= hash_table_mod1 (hash
, m_size_prime_index
);
671 size_t size
= m_size
;
672 value_type
**slot
= m_entries
+ index
;
675 if (*slot
== HTAB_EMPTY_ENTRY
)
677 else if (*slot
== HTAB_DELETED_ENTRY
)
680 hash2
= hash_table_mod2 (hash
, m_size_prime_index
);
687 slot
= m_entries
+ index
;
688 if (*slot
== HTAB_EMPTY_ENTRY
)
690 else if (*slot
== HTAB_DELETED_ENTRY
)
695 /* The following function changes size of memory allocated for the
696 entries and repeatedly inserts the table elements. The occupancy
697 of the table after the call will be about 50%. Naturally the hash
698 table must already exist. Remember also that the place of the
699 table entries is changed. If memory allocation fails, this function
702 template<typename Descriptor
, template<typename Type
> class Allocator
>
704 hash_table
<Descriptor
, Allocator
, false>::expand ()
706 value_type
**oentries
= m_entries
;
707 unsigned int oindex
= m_size_prime_index
;
708 size_t osize
= size ();
709 value_type
**olimit
= oentries
+ osize
;
710 size_t elts
= elements ();
712 /* Resize only when table after removal of unused elements is either
713 too full or too empty. */
716 if (elts
* 2 > osize
|| (elts
* 8 < osize
&& osize
> 32))
718 nindex
= hash_table_higher_prime_index (elts
* 2);
719 nsize
= prime_tab
[nindex
].prime
;
727 value_type
**nentries
= Allocator
<value_type
*> ::data_alloc (nsize
);
728 gcc_assert (nentries
!= NULL
);
729 m_entries
= nentries
;
731 m_size_prime_index
= nindex
;
732 m_n_elements
-= m_n_deleted
;
735 value_type
**p
= oentries
;
740 if (x
!= HTAB_EMPTY_ENTRY
&& x
!= HTAB_DELETED_ENTRY
)
742 value_type
**q
= find_empty_slot_for_expand (Descriptor::hash (x
));
751 Allocator
<value_type
*> ::data_free (oentries
);
754 template<typename Descriptor
, template<typename Type
> class Allocator
>
756 hash_table
<Descriptor
, Allocator
, false>::empty ()
758 size_t size
= m_size
;
759 value_type
**entries
= m_entries
;
762 for (i
= size
- 1; i
>= 0; i
--)
763 if (entries
[i
] != HTAB_EMPTY_ENTRY
&& entries
[i
] != HTAB_DELETED_ENTRY
)
764 Descriptor::remove (entries
[i
]);
766 /* Instead of clearing megabyte, downsize the table. */
767 if (size
> 1024*1024 / sizeof (PTR
))
769 int nindex
= hash_table_higher_prime_index (1024 / sizeof (PTR
));
770 int nsize
= prime_tab
[nindex
].prime
;
772 Allocator
<value_type
*> ::data_free (m_entries
);
773 m_entries
= Allocator
<value_type
*> ::data_alloc (nsize
);
775 m_size_prime_index
= nindex
;
778 memset (entries
, 0, size
* sizeof (value_type
*));
783 /* This function clears a specified SLOT in a hash table. It is
784 useful when you've already done the lookup and don't want to do it
787 template<typename Descriptor
, template<typename Type
> class Allocator
>
789 hash_table
<Descriptor
, Allocator
, false>::clear_slot (value_type
**slot
)
791 if (slot
< m_entries
|| slot
>= m_entries
+ size ()
792 || *slot
== HTAB_EMPTY_ENTRY
|| *slot
== HTAB_DELETED_ENTRY
)
795 Descriptor::remove (*slot
);
797 *slot
= static_cast <value_type
*> (HTAB_DELETED_ENTRY
);
801 /* This function searches for a hash table entry equal to the given
802 COMPARABLE element starting with the given HASH value. It cannot
803 be used to insert or delete an element. */
805 template<typename Descriptor
, template<typename Type
> class Allocator
>
806 typename hash_table
<Descriptor
, Allocator
, false>::value_type
*
807 hash_table
<Descriptor
, Allocator
, false>
808 ::find_with_hash (const compare_type
*comparable
, hashval_t hash
)
811 size_t size
= m_size
;
812 hashval_t index
= hash_table_mod1 (hash
, m_size_prime_index
);
814 value_type
*entry
= m_entries
[index
];
815 if (entry
== HTAB_EMPTY_ENTRY
816 || (entry
!= HTAB_DELETED_ENTRY
&& Descriptor::equal (entry
, comparable
)))
819 hashval_t hash2
= hash_table_mod2 (hash
, m_size_prime_index
);
827 entry
= m_entries
[index
];
828 if (entry
== HTAB_EMPTY_ENTRY
829 || (entry
!= HTAB_DELETED_ENTRY
830 && Descriptor::equal (entry
, comparable
)))
835 /* This function searches for a hash table slot containing an entry
836 equal to the given COMPARABLE element and starting with the given
837 HASH. To delete an entry, call this with insert=NO_INSERT, then
838 call clear_slot on the slot returned (possibly after doing some
839 checks). To insert an entry, call this with insert=INSERT, then
840 write the value you want into the returned slot. When inserting an
841 entry, NULL may be returned if memory allocation fails. */
843 template<typename Descriptor
, template<typename Type
> class Allocator
>
844 typename hash_table
<Descriptor
, Allocator
, false>::value_type
**
845 hash_table
<Descriptor
, Allocator
, false>
846 ::find_slot_with_hash (const compare_type
*comparable
, hashval_t hash
,
847 enum insert_option insert
)
849 if (insert
== INSERT
&& m_size
* 3 <= m_n_elements
* 4)
854 value_type
**first_deleted_slot
= NULL
;
855 hashval_t index
= hash_table_mod1 (hash
, m_size_prime_index
);
856 hashval_t hash2
= hash_table_mod2 (hash
, m_size_prime_index
);
857 value_type
*entry
= m_entries
[index
];
858 size_t size
= m_size
;
859 if (entry
== HTAB_EMPTY_ENTRY
)
861 else if (entry
== HTAB_DELETED_ENTRY
)
862 first_deleted_slot
= &m_entries
[index
];
863 else if (Descriptor::equal (entry
, comparable
))
864 return &m_entries
[index
];
873 entry
= m_entries
[index
];
874 if (entry
== HTAB_EMPTY_ENTRY
)
876 else if (entry
== HTAB_DELETED_ENTRY
)
878 if (!first_deleted_slot
)
879 first_deleted_slot
= &m_entries
[index
];
881 else if (Descriptor::equal (entry
, comparable
))
882 return &m_entries
[index
];
886 if (insert
== NO_INSERT
)
889 if (first_deleted_slot
)
892 *first_deleted_slot
= static_cast <value_type
*> (HTAB_EMPTY_ENTRY
);
893 return first_deleted_slot
;
897 return &m_entries
[index
];
900 /* This function deletes an element with the given COMPARABLE value
901 from hash table starting with the given HASH. If there is no
902 matching element in the hash table, this function does nothing. */
904 template<typename Descriptor
, template<typename Type
> class Allocator
>
906 hash_table
<Descriptor
, Allocator
, false>
907 ::remove_elt_with_hash (const compare_type
*comparable
, hashval_t hash
)
909 value_type
**slot
= find_slot_with_hash (comparable
, hash
, NO_INSERT
);
910 if (*slot
== HTAB_EMPTY_ENTRY
)
913 Descriptor::remove (*slot
);
915 *slot
= static_cast <value_type
*> (HTAB_DELETED_ENTRY
);
919 /* This function scans over the entire hash table calling CALLBACK for
920 each live entry. If CALLBACK returns false, the iteration stops.
921 ARGUMENT is passed as CALLBACK's second argument. */
923 template<typename Descriptor
, template<typename Type
> class Allocator
>
924 template<typename Argument
,
925 int (*Callback
) (typename hash_table
<Descriptor
, Allocator
,
926 false>::value_type
**slot
,
929 hash_table
<Descriptor
, Allocator
, false>::traverse_noresize (Argument argument
)
931 value_type
**slot
= m_entries
;
932 value_type
**limit
= slot
+ size ();
936 value_type
*x
= *slot
;
938 if (x
!= HTAB_EMPTY_ENTRY
&& x
!= HTAB_DELETED_ENTRY
)
939 if (! Callback (slot
, argument
))
942 while (++slot
< limit
);
945 /* Like traverse_noresize, but does resize the table when it is too empty
946 to improve effectivity of subsequent calls. */
948 template <typename Descriptor
,
949 template <typename Type
> class Allocator
>
950 template <typename Argument
,
951 int (*Callback
) (typename hash_table
<Descriptor
, Allocator
,
952 false>::value_type
**slot
,
955 hash_table
<Descriptor
, Allocator
, false>::traverse (Argument argument
)
957 size_t size
= m_size
;
958 if (elements () * 8 < size
&& size
> 32)
961 traverse_noresize
<Argument
, Callback
> (argument
);
964 /* Slide down the iterator slots until an active entry is found. */
966 template<typename Descriptor
, template<typename Type
> class Allocator
>
968 hash_table
<Descriptor
, Allocator
, false>::iterator::slide ()
970 for ( ; m_slot
< m_limit
; ++m_slot
)
972 value_type
*x
= *m_slot
;
973 if (x
!= HTAB_EMPTY_ENTRY
&& x
!= HTAB_DELETED_ENTRY
)
980 /* Bump the iterator. */
982 template<typename Descriptor
, template<typename Type
> class Allocator
>
983 inline typename hash_table
<Descriptor
, Allocator
, false>::iterator
&
984 hash_table
<Descriptor
, Allocator
, false>::iterator::operator ++ ()
991 /* A partial specialization used when values should be stored directly. */
993 template <typename Descriptor
,
994 template<typename Type
> class Allocator
>
995 class hash_table
<Descriptor
, Allocator
, true>
997 typedef typename
Descriptor::value_type value_type
;
998 typedef typename
Descriptor::compare_type compare_type
;
1001 hash_table (size_t);
1004 /* Current size (in entries) of the hash table. */
1005 size_t size () const { return m_size
; }
1007 /* Return the current number of elements in this hash table. */
1008 size_t elements () const { return m_n_elements
- m_n_deleted
; }
1010 /* Return the current number of elements in this hash table. */
1011 size_t elements_with_deleted () const { return m_n_elements
; }
1013 /* This function clears all entries in the given hash table. */
1016 /* This function clears a specified SLOT in a hash table. It is
1017 useful when you've already done the lookup and don't want to do it
1020 void clear_slot (value_type
*);
1022 /* This function searches for a hash table entry equal to the given
1023 COMPARABLE element starting with the given HASH value. It cannot
1024 be used to insert or delete an element. */
1025 value_type
&find_with_hash (const compare_type
&, hashval_t
);
1027 /* Like find_slot_with_hash, but compute the hash value from the element. */
1028 value_type
&find (const value_type
&value
)
1030 return find_with_hash (value
, Descriptor::hash (value
));
1033 value_type
*find_slot (const value_type
&value
, insert_option insert
)
1035 return find_slot_with_hash (value
, Descriptor::hash (value
), insert
);
1038 /* This function searches for a hash table slot containing an entry
1039 equal to the given COMPARABLE element and starting with the given
1040 HASH. To delete an entry, call this with insert=NO_INSERT, then
1041 call clear_slot on the slot returned (possibly after doing some
1042 checks). To insert an entry, call this with insert=INSERT, then
1043 write the value you want into the returned slot. When inserting an
1044 entry, NULL may be returned if memory allocation fails. */
1045 value_type
*find_slot_with_hash (const compare_type
&comparable
,
1046 hashval_t hash
, enum insert_option insert
);
1048 /* This function deletes an element with the given COMPARABLE value
1049 from hash table starting with the given HASH. If there is no
1050 matching element in the hash table, this function does nothing. */
1051 void remove_elt_with_hash (const compare_type
&, hashval_t
);
1053 /* Like remove_elt_with_hash, but compute the hash value from the element. */
1054 void remove_elt (const value_type
&value
)
1056 remove_elt_with_hash (value
, Descriptor::hash (value
));
1059 /* This function scans over the entire hash table calling CALLBACK for
1060 each live entry. If CALLBACK returns false, the iteration stops.
1061 ARGUMENT is passed as CALLBACK's second argument. */
1062 template <typename Argument
,
1063 int (*Callback
) (value_type
*slot
, Argument argument
)>
1064 void traverse_noresize (Argument argument
);
1066 /* Like traverse_noresize, but does resize the table when it is too empty
1067 to improve effectivity of subsequent calls. */
1068 template <typename Argument
,
1069 int (*Callback
) (value_type
*slot
, Argument argument
)>
1070 void traverse (Argument argument
);
1075 iterator () : m_slot (NULL
), m_limit (NULL
) {}
1077 iterator (value_type
*slot
, value_type
*limit
) :
1078 m_slot (slot
), m_limit (limit
) {}
1080 inline value_type
&operator * () { return *m_slot
; }
1082 inline iterator
&operator ++ ();
1083 bool operator != (const iterator
&other
) const
1085 return m_slot
!= other
.m_slot
|| m_limit
!= other
.m_limit
;
1090 value_type
*m_limit
;
1093 iterator
begin () const
1095 iterator
iter (m_entries
, m_entries
+ m_size
);
1100 iterator
end () const { return iterator (); }
1102 double collisions () const
1104 return m_searches
? static_cast <double> (m_collisions
) / m_searches
: 0;
1109 value_type
*find_empty_slot_for_expand (hashval_t
);
1111 static bool is_deleted (value_type
&v
)
1113 return is_deleted_helper
<value_type
, Descriptor
>::call (v
);
1115 static bool is_empty (value_type
&v
)
1117 return is_empty_helper
<value_type
, Descriptor
>::call (v
);
1120 static void mark_deleted (value_type
&v
)
1122 return mark_deleted_helper
<value_type
, Descriptor
>::call (v
);
1125 static void mark_empty (value_type
&v
)
1127 return mark_empty_helper
<value_type
, Descriptor
>::call (v
);
1131 typename
Descriptor::value_type
*m_entries
;
1135 /* Current number of elements including also deleted elements. */
1136 size_t m_n_elements
;
1138 /* Current number of deleted elements in the table. */
1141 /* The following member is used for debugging. Its value is number
1142 of all calls of `htab_find_slot' for the hash table. */
1143 unsigned int m_searches
;
1145 /* The following member is used for debugging. Its value is number
1146 of collisions fixed for time of work with the hash table. */
1147 unsigned int m_collisions
;
1149 /* Current size (in entries) of the hash table, as an index into the
1151 unsigned int m_size_prime_index
;
1154 template<typename Descriptor
, template<typename Type
> class Allocator
>
1155 hash_table
<Descriptor
, Allocator
, true>::hash_table (size_t size
) :
1156 m_n_elements (0), m_n_deleted (0), m_searches (0), m_collisions (0)
1158 unsigned int size_prime_index
;
1160 size_prime_index
= hash_table_higher_prime_index (size
);
1161 size
= prime_tab
[size_prime_index
].prime
;
1163 m_entries
= Allocator
<value_type
> ::data_alloc (size
);
1164 gcc_assert (m_entries
!= NULL
);
1166 m_size_prime_index
= size_prime_index
;
1169 template<typename Descriptor
, template<typename Type
> class Allocator
>
1170 hash_table
<Descriptor
, Allocator
, true>::~hash_table ()
1172 for (size_t i
= m_size
- 1; i
< m_size
; i
--)
1173 if (!is_empty (m_entries
[i
]) && !is_deleted (m_entries
[i
]))
1174 Descriptor::remove (m_entries
[i
]);
1176 Allocator
<value_type
> ::data_free (m_entries
);
1179 /* Similar to find_slot, but without several unwanted side effects:
1180 - Does not call equal when it finds an existing entry.
1181 - Does not change the count of elements/searches/collisions in the
1183 This function also assumes there are no deleted entries in the table.
1184 HASH is the hash value for the element to be inserted. */
1186 template<typename Descriptor
, template<typename Type
> class Allocator
>
1187 typename hash_table
<Descriptor
, Allocator
, true>::value_type
*
1188 hash_table
<Descriptor
, Allocator
, true>
1189 ::find_empty_slot_for_expand (hashval_t hash
)
1191 hashval_t index
= hash_table_mod1 (hash
, m_size_prime_index
);
1192 size_t size
= m_size
;
1193 value_type
*slot
= m_entries
+ index
;
1196 if (is_empty (*slot
))
1198 else if (is_deleted (*slot
))
1201 hash2
= hash_table_mod2 (hash
, m_size_prime_index
);
1208 slot
= m_entries
+ index
;
1209 if (is_empty (*slot
))
1211 else if (is_deleted (*slot
))
1216 /* The following function changes size of memory allocated for the
1217 entries and repeatedly inserts the table elements. The occupancy
1218 of the table after the call will be about 50%. Naturally the hash
1219 table must already exist. Remember also that the place of the
1220 table entries is changed. If memory allocation fails, this function
1223 template<typename Descriptor
, template<typename Type
> class Allocator
>
1225 hash_table
<Descriptor
, Allocator
, true>::expand ()
1227 value_type
*oentries
= m_entries
;
1228 unsigned int oindex
= m_size_prime_index
;
1229 size_t osize
= size ();
1230 value_type
*olimit
= oentries
+ osize
;
1231 size_t elts
= elements ();
1233 /* Resize only when table after removal of unused elements is either
1234 too full or too empty. */
1235 unsigned int nindex
;
1237 if (elts
* 2 > osize
|| (elts
* 8 < osize
&& osize
> 32))
1239 nindex
= hash_table_higher_prime_index (elts
* 2);
1240 nsize
= prime_tab
[nindex
].prime
;
1248 value_type
*nentries
= Allocator
<value_type
> ::data_alloc (nsize
);
1249 gcc_assert (nentries
!= NULL
);
1250 m_entries
= nentries
;
1252 m_size_prime_index
= nindex
;
1253 m_n_elements
-= m_n_deleted
;
1256 value_type
*p
= oentries
;
1261 if (!is_empty (x
) && !is_deleted (x
))
1263 value_type
*q
= find_empty_slot_for_expand (Descriptor::hash (x
));
1272 Allocator
<value_type
> ::data_free (oentries
);
1275 template<typename Descriptor
, template<typename Type
> class Allocator
>
1277 hash_table
<Descriptor
, Allocator
, true>::empty ()
1279 size_t size
= m_size
;
1280 value_type
*entries
= m_entries
;
1283 for (i
= size
- 1; i
>= 0; i
--)
1284 if (!is_empty (entries
[i
]) && !is_deleted (entries
[i
]))
1285 Descriptor::remove (entries
[i
]);
1287 /* Instead of clearing megabyte, downsize the table. */
1288 if (size
> 1024*1024 / sizeof (PTR
))
1290 int nindex
= hash_table_higher_prime_index (1024 / sizeof (PTR
));
1291 int nsize
= prime_tab
[nindex
].prime
;
1293 Allocator
<value_type
> ::data_free (m_entries
);
1294 m_entries
= Allocator
<value_type
> ::data_alloc (nsize
);
1296 m_size_prime_index
= nindex
;
1299 memset (entries
, 0, size
* sizeof (value_type
));
1304 /* This function clears a specified SLOT in a hash table. It is
1305 useful when you've already done the lookup and don't want to do it
1308 template<typename Descriptor
, template<typename Type
> class Allocator
>
1310 hash_table
<Descriptor
, Allocator
, true>::clear_slot (value_type
*slot
)
1312 if (slot
< m_entries
|| slot
>= m_entries
+ size ()
1313 || is_empty (*slot
) || is_deleted (*slot
))
1316 Descriptor::remove (*slot
);
1318 mark_deleted (*slot
);
1322 /* This function searches for a hash table entry equal to the given
1323 COMPARABLE element starting with the given HASH value. It cannot
1324 be used to insert or delete an element. */
1326 template<typename Descriptor
, template<typename Type
> class Allocator
>
1327 typename hash_table
<Descriptor
, Allocator
, true>::value_type
&
1328 hash_table
<Descriptor
, Allocator
, true>
1329 ::find_with_hash (const compare_type
&comparable
, hashval_t hash
)
1332 size_t size
= m_size
;
1333 hashval_t index
= hash_table_mod1 (hash
, m_size_prime_index
);
1335 value_type
*entry
= &m_entries
[index
];
1336 if (is_empty (*entry
)
1337 || (!is_deleted (*entry
) && Descriptor::equal (*entry
, comparable
)))
1340 hashval_t hash2
= hash_table_mod2 (hash
, m_size_prime_index
);
1348 entry
= &m_entries
[index
];
1349 if (is_empty (*entry
)
1350 || (!is_deleted (*entry
) && Descriptor::equal (*entry
, comparable
)))
1355 /* This function searches for a hash table slot containing an entry
1356 equal to the given COMPARABLE element and starting with the given
1357 HASH. To delete an entry, call this with insert=NO_INSERT, then
1358 call clear_slot on the slot returned (possibly after doing some
1359 checks). To insert an entry, call this with insert=INSERT, then
1360 write the value you want into the returned slot. When inserting an
1361 entry, NULL may be returned if memory allocation fails. */
1363 template<typename Descriptor
, template<typename Type
> class Allocator
>
1364 typename hash_table
<Descriptor
, Allocator
, true>::value_type
*
1365 hash_table
<Descriptor
, Allocator
, true>
1366 ::find_slot_with_hash (const compare_type
&comparable
, hashval_t hash
,
1367 enum insert_option insert
)
1369 if (insert
== INSERT
&& m_size
* 3 <= m_n_elements
* 4)
1374 value_type
*first_deleted_slot
= NULL
;
1375 hashval_t index
= hash_table_mod1 (hash
, m_size_prime_index
);
1376 hashval_t hash2
= hash_table_mod2 (hash
, m_size_prime_index
);
1377 value_type
*entry
= &m_entries
[index
];
1378 size_t size
= m_size
;
1379 if (is_empty (*entry
))
1381 else if (is_deleted (*entry
))
1382 first_deleted_slot
= &m_entries
[index
];
1383 else if (Descriptor::equal (*entry
, comparable
))
1384 return &m_entries
[index
];
1393 entry
= &m_entries
[index
];
1394 if (is_empty (*entry
))
1396 else if (is_deleted (*entry
))
1398 if (!first_deleted_slot
)
1399 first_deleted_slot
= &m_entries
[index
];
1401 else if (Descriptor::equal (*entry
, comparable
))
1402 return &m_entries
[index
];
1406 if (insert
== NO_INSERT
)
1409 if (first_deleted_slot
)
1412 mark_empty (*first_deleted_slot
);
1413 return first_deleted_slot
;
1417 return &m_entries
[index
];
1420 /* This function deletes an element with the given COMPARABLE value
1421 from hash table starting with the given HASH. If there is no
1422 matching element in the hash table, this function does nothing. */
1424 template<typename Descriptor
, template<typename Type
> class Allocator
>
1426 hash_table
<Descriptor
, Allocator
, true>
1427 ::remove_elt_with_hash (const compare_type
&comparable
, hashval_t hash
)
1429 value_type
*slot
= find_slot_with_hash (comparable
, hash
, NO_INSERT
);
1430 if (is_empty (*slot
))
1433 Descriptor::remove (*slot
);
1435 mark_deleted (*slot
);
1439 /* This function scans over the entire hash table calling CALLBACK for
1440 each live entry. If CALLBACK returns false, the iteration stops.
1441 ARGUMENT is passed as CALLBACK's second argument. */
1443 template<typename Descriptor
,
1444 template<typename Type
> class Allocator
>
1445 template<typename Argument
,
1446 int (*Callback
) (typename hash_table
<Descriptor
, Allocator
,
1447 true>::value_type
*slot
,
1450 hash_table
<Descriptor
, Allocator
, true>::traverse_noresize (Argument argument
)
1452 value_type
*slot
= m_entries
;
1453 value_type
*limit
= slot
+ size ();
1457 value_type
&x
= *slot
;
1459 if (!is_empty (x
) && !is_deleted (x
))
1460 if (! Callback (slot
, argument
))
1463 while (++slot
< limit
);
1466 /* Like traverse_noresize, but does resize the table when it is too empty
1467 to improve effectivity of subsequent calls. */
1469 template <typename Descriptor
,
1470 template <typename Type
> class Allocator
>
1471 template <typename Argument
,
1472 int (*Callback
) (typename hash_table
<Descriptor
, Allocator
,
1473 true>::value_type
*slot
,
1476 hash_table
<Descriptor
, Allocator
, true>::traverse (Argument argument
)
1478 size_t size
= m_size
;
1479 if (elements () * 8 < size
&& size
> 32)
1482 traverse_noresize
<Argument
, Callback
> (argument
);
1485 /* Slide down the iterator slots until an active entry is found. */
1487 template<typename Descriptor
, template<typename Type
> class Allocator
>
1489 hash_table
<Descriptor
, Allocator
, true>::iterator::slide ()
1491 for ( ; m_slot
< m_limit
; ++m_slot
)
1493 value_type
&x
= *m_slot
;
1494 if (!is_empty (x
) && !is_deleted (x
))
1501 /* Bump the iterator. */
1503 template<typename Descriptor
, template<typename Type
> class Allocator
>
1504 inline typename hash_table
<Descriptor
, Allocator
, true>::iterator
&
1505 hash_table
<Descriptor
, Allocator
, true>::iterator::operator ++ ()
1513 /* Iterate through the elements of hash_table HTAB,
1514 using hash_table <....>::iterator ITER,
1515 storing each element in RESULT, which is of type TYPE. */
1517 #define FOR_EACH_HASH_TABLE_ELEMENT(HTAB, RESULT, TYPE, ITER) \
1518 for ((ITER) = (HTAB).begin (); \
1519 (ITER) != (HTAB).end () ? (RESULT = *(ITER) , true) : false; \
1522 #endif /* TYPED_HASHTAB_H */