1 /* A type-safe hash table template.
3 Free Software Foundation, Inc.
4 Contributed by Lawrence Crowl <crowl@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This file implements a typed hash table.
24 The implementation borrows from libiberty's htab_t in hashtab.h.
29 Users of the hash table generally need to be aware of three types.
31 1. The type being placed into the hash table. This type is called
34 2. The type used to describe how to handle the value type within
35 the hash table. This descriptor type provides the hash table with
38 - A typedef named 'value_type' to the value type (from above).
40 - A static member function named 'hash' that takes a value_type
41 pointer and returns a hashval_t value.
43 - A typedef named 'compare_type' that is used to test when an value
44 is found. This type is the comparison type. Usually, it will be the
45 same as value_type. If it is not the same type, you must generally
46 explicitly compute hash values and pass them to the hash table.
48 - A static member function named 'equal' that takes a value_type
49 pointer and a compare_type pointer, and returns a bool.
51 - A static function named 'remove' that takes an value_type pointer
52 and frees the memory allocated by it. This function is used when
53 individual elements of the table need to be disposed of (e.g.,
54 when deleting a hash table, removing elements from the table, etc).
56 3. The type of the hash table itself. (More later.)
58 In very special circumstances, users may need to know about a fourth type.
60 4. The template type used to describe how hash table memory
61 is allocated. This type is called the allocator type. It is
62 parameterized on the value type. It provides four functions.
64 - A static member function named 'control_alloc'. This function
65 allocates the control data blocks for the table.
67 - A static member function named 'control_free'. This function
68 frees the control data blocks for the table.
70 - A static member function named 'data_alloc'. This function
71 allocates the data elements in the table.
73 - A static member function named 'data_free'. This function
74 deallocates the data elements in the table.
76 Hash table are instantiated with two type arguments.
78 * The descriptor type, (2) above.
80 * The allocator type, (4) above. In general, you will not need to
81 provide your own allocator type. By default, hash tables will use
82 the class template xcallocator, which uses malloc/free for allocation.
85 DEFINING A DESCRIPTOR TYPE
87 The first task in using the hash table is to describe the element type.
88 We compose this into a few steps.
90 1. Decide on a removal policy for values stored in the table.
91 This header provides class templates for the two most common
94 * typed_free_remove implements the static 'remove' member function
97 * typed_noop_remove implements the static 'remove' member function
100 You can use these policies by simply deriving the descriptor type
101 from one of those class template, with the appropriate argument.
103 Otherwise, you need to write the static 'remove' member function
104 in the descriptor class.
106 2. Choose a hash function. Write the static 'hash' member function.
108 3. Choose an equality testing function. In most cases, its two
109 arguments will be value_type pointers. If not, the first argument must
110 be a value_type pointer, and the second argument a compare_type pointer.
113 AN EXAMPLE DESCRIPTOR TYPE
115 Suppose you want to put some_type into the hash table. You could define
116 the descriptor type as follows.
118 struct some_type_hasher : typed_noop_remove <some_type>
119 // Deriving from typed_noop_remove means that we get a 'remove' that does
120 // nothing. This choice is good for raw values.
122 typedef some_type value_type;
123 typedef some_type compare_type;
124 static inline hashval_t hash (const value_type *);
125 static inline bool equal (const value_type *, const compare_type *);
129 some_type_hasher::hash (const value_type *e)
130 { ... compute and return a hash value for E ... }
133 some_type_hasher::equal (const value_type *p1, const compare_type *p2)
134 { ... compare P1 vs P2. Return true if they are the 'same' ... }
137 AN EXAMPLE HASH_TABLE DECLARATION
139 To instantiate a hash table for some_type:
141 hash_table <some_type_hasher> some_type_hash_table;
143 There is no need to mention some_type directly, as the hash table will
144 obtain it using some_type_hasher::value_type.
146 You can then used any of the functions in hash_table's public interface.
147 See hash_table for details. The interface is very similar to libiberty's
151 EASY DESCRIPTORS FOR POINTERS
153 The class template pointer_hash provides everything you need to hash
154 pointers (as opposed to what they point to). So, to instantiate a hash
155 table over pointers to whatever_type,
157 hash_table <pointer_hash <whatever_type>> whatever_type_hash_table;
162 #ifndef TYPED_HASHTAB_H
163 #define TYPED_HASHTAB_H
168 /* The ordinary memory allocator. */
169 /* FIXME (crowl): This allocator may be extracted for wider sharing later. */
171 template <typename Type
>
174 static Type
*control_alloc (size_t count
);
175 static Type
*data_alloc (size_t count
);
176 static void control_free (Type
*memory
);
177 static void data_free (Type
*memory
);
181 /* Allocate memory for COUNT control blocks. */
183 template <typename Type
>
185 xcallocator
<Type
>::control_alloc (size_t count
)
187 return static_cast <Type
*> (xcalloc (count
, sizeof (Type
)));
191 /* Allocate memory for COUNT data blocks. */
193 template <typename Type
>
195 xcallocator
<Type
>::data_alloc (size_t count
)
197 return static_cast <Type
*> (xcalloc (count
, sizeof (Type
)));
201 /* Free memory for control blocks. */
203 template <typename Type
>
205 xcallocator
<Type
>::control_free (Type
*memory
)
207 return ::free (memory
);
211 /* Free memory for data blocks. */
213 template <typename Type
>
215 xcallocator
<Type
>::data_free (Type
*memory
)
217 return ::free (memory
);
221 /* Helpful type for removing with free. */
223 template <typename Type
>
224 struct typed_free_remove
226 static inline void remove (Type
*p
);
230 /* Remove with free. */
232 template <typename Type
>
234 typed_free_remove
<Type
>::remove (Type
*p
)
240 /* Helpful type for a no-op remove. */
242 template <typename Type
>
243 struct typed_noop_remove
245 static inline void remove (Type
*p
);
249 /* Remove doing nothing. */
251 template <typename Type
>
253 typed_noop_remove
<Type
>::remove (Type
*p ATTRIBUTE_UNUSED
)
258 /* Pointer hash with a no-op remove method. */
260 template <typename Type
>
261 struct pointer_hash
: typed_noop_remove
<Type
>
263 typedef Type value_type
;
264 typedef Type compare_type
;
266 static inline hashval_t
267 hash (const value_type
*);
270 equal (const value_type
*existing
, const compare_type
*candidate
);
273 template <typename Type
>
275 pointer_hash
<Type
>::hash (const value_type
*candidate
)
277 /* This is a really poor hash function, but it is what the current code uses,
278 so I am reusing it to avoid an additional axis in testing. */
279 return (hashval_t
) ((intptr_t)candidate
>> 3);
282 template <typename Type
>
284 pointer_hash
<Type
>::equal (const value_type
*existing
,
285 const compare_type
*candidate
)
287 return existing
== candidate
;
291 /* Table of primes and their inversion information. */
297 hashval_t inv_m2
; /* inverse of prime-2 */
301 extern struct prime_ent
const prime_tab
[];
304 /* Functions for computing hash table indexes. */
306 extern unsigned int hash_table_higher_prime_index (unsigned long n
);
307 extern hashval_t
hash_table_mod1 (hashval_t hash
, unsigned int index
);
308 extern hashval_t
hash_table_mod2 (hashval_t hash
, unsigned int index
);
311 /* Internal implementation type. */
313 template <typename T
>
314 struct hash_table_control
319 /* Current size (in entries) of the hash table. */
322 /* Current number of elements including also deleted elements. */
325 /* Current number of deleted elements in the table. */
328 /* The following member is used for debugging. Its value is number
329 of all calls of `htab_find_slot' for the hash table. */
330 unsigned int searches
;
332 /* The following member is used for debugging. Its value is number
333 of collisions fixed for time of work with the hash table. */
334 unsigned int collisions
;
336 /* Current size (in entries) of the hash table, as an index into the
338 unsigned int size_prime_index
;
342 /* User-facing hash table type.
344 The table stores elements of type Descriptor::value_type.
346 It hashes values with the hash member function.
347 The table currently works with relatively weak hash functions.
348 Use typed_pointer_hash <Value> when hashing pointers instead of objects.
350 It compares elements with the equal member function.
351 Two elements with the same hash may not be equal.
352 Use typed_pointer_equal <Value> when hashing pointers instead of objects.
354 It removes elements with the remove member function.
355 This feature is useful for freeing memory.
356 Derive from typed_null_remove <Value> when not freeing objects.
357 Derive from typed_free_remove <Value> when doing a simple object free.
359 Specify the template Allocator to allocate and free memory.
360 The default is xcallocator.
364 template <typename Descriptor
,
365 template <typename Type
> class Allocator
= xcallocator
>
369 typedef typename
Descriptor::value_type value_type
;
370 typedef typename
Descriptor::compare_type compare_type
;
373 hash_table_control
<value_type
> *htab
;
375 value_type
**find_empty_slot_for_expand (hashval_t hash
);
380 void create (size_t initial_slots
);
383 value_type
*find (const compare_type
*comparable
);
384 value_type
*find_with_hash (const compare_type
*comparable
, hashval_t hash
);
385 value_type
**find_slot (const compare_type
*comparable
,
386 enum insert_option insert
);
387 value_type
**find_slot_with_hash (const compare_type
*comparable
,
388 hashval_t hash
, enum insert_option insert
);
390 void clear_slot (value_type
**slot
);
391 void remove_elt (const compare_type
*comparable
);
392 void remove_elt_with_hash (const compare_type
*comparable
, hashval_t hash
);
397 template <typename Argument
,
398 int (*Callback
) (value_type
**slot
, Argument argument
)>
399 void traverse_noresize (Argument argument
);
401 template <typename Argument
,
402 int (*Callback
) (value_type
**slot
, Argument argument
)>
403 void traverse (Argument argument
);
407 /* Construct the hash table. The only useful operation next is create. */
409 template <typename Descriptor
,
410 template <typename Type
> class Allocator
>
412 hash_table
<Descriptor
, Allocator
>::hash_table ()
418 /* See if the table has been created, as opposed to constructed. */
420 template <typename Descriptor
,
421 template <typename Type
> class Allocator
>
423 hash_table
<Descriptor
, Allocator
>::is_created ()
429 /* Like find_with_hash, but compute the hash value from the element. */
431 template <typename Descriptor
,
432 template <typename Type
> class Allocator
>
433 inline typename
Descriptor::value_type
*
434 hash_table
<Descriptor
, Allocator
>::find (const compare_type
*comparable
)
436 return find_with_hash (comparable
, Descriptor::hash (comparable
));
440 /* Like find_slot_with_hash, but compute the hash value from the element. */
442 template <typename Descriptor
,
443 template <typename Type
> class Allocator
>
444 inline typename
Descriptor::value_type
**
445 hash_table
<Descriptor
, Allocator
>
446 ::find_slot (const compare_type
*comparable
, enum insert_option insert
)
448 return find_slot_with_hash (comparable
, Descriptor::hash (comparable
), insert
);
452 /* Like remove_elt_with_hash, but compute the hash value from the element. */
454 template <typename Descriptor
,
455 template <typename Type
> class Allocator
>
457 hash_table
<Descriptor
, Allocator
>::remove_elt (const compare_type
*comparable
)
459 remove_elt_with_hash (comparable
, Descriptor::hash (comparable
));
463 /* Return the current size of this hash table. */
465 template <typename Descriptor
,
466 template <typename Type
> class Allocator
>
468 hash_table
<Descriptor
, Allocator
>::size()
474 /* Return the current number of elements in this hash table. */
476 template <typename Descriptor
,
477 template <typename Type
> class Allocator
>
479 hash_table
<Descriptor
, Allocator
>::elements()
481 return htab
->n_elements
- htab
->n_deleted
;
485 /* Return the fraction of fixed collisions during all work with given
488 template <typename Descriptor
,
489 template <typename Type
> class Allocator
>
491 hash_table
<Descriptor
, Allocator
>::collisions()
493 if (htab
->searches
== 0)
496 return static_cast <double> (htab
->collisions
) / htab
->searches
;
500 /* Create a hash table with at least the given number of INITIAL_SLOTS. */
502 template <typename Descriptor
,
503 template <typename Type
> class Allocator
>
505 hash_table
<Descriptor
, Allocator
>::create (size_t size
)
507 unsigned int size_prime_index
;
509 size_prime_index
= hash_table_higher_prime_index (size
);
510 size
= prime_tab
[size_prime_index
].prime
;
512 htab
= Allocator
<hash_table_control
<value_type
> > ::control_alloc (1);
513 gcc_assert (htab
!= NULL
);
514 htab
->entries
= Allocator
<value_type
*> ::data_alloc (size
);
515 gcc_assert (htab
->entries
!= NULL
);
517 htab
->size_prime_index
= size_prime_index
;
521 /* Dispose of a hash table. Free all memory and return this hash table to
522 the non-created state. Naturally the hash table must already exist. */
524 template <typename Descriptor
,
525 template <typename Type
> class Allocator
>
527 hash_table
<Descriptor
, Allocator
>::dispose ()
529 size_t size
= htab
->size
;
530 value_type
**entries
= htab
->entries
;
532 for (int i
= size
- 1; i
>= 0; i
--)
533 if (entries
[i
] != HTAB_EMPTY_ENTRY
&& entries
[i
] != HTAB_DELETED_ENTRY
)
534 Descriptor::remove (entries
[i
]);
536 Allocator
<value_type
*> ::data_free (entries
);
537 Allocator
<hash_table_control
<value_type
> > ::control_free (htab
);
542 /* Similar to find_slot, but without several unwanted side effects:
543 - Does not call equal when it finds an existing entry.
544 - Does not change the count of elements/searches/collisions in the
546 This function also assumes there are no deleted entries in the table.
547 HASH is the hash value for the element to be inserted. */
549 template <typename Descriptor
,
550 template <typename Type
> class Allocator
>
551 typename
Descriptor::value_type
**
552 hash_table
<Descriptor
, Allocator
>::find_empty_slot_for_expand (hashval_t hash
)
554 hashval_t index
= hash_table_mod1 (hash
, htab
->size_prime_index
);
555 size_t size
= htab
->size
;
556 value_type
**slot
= htab
->entries
+ index
;
559 if (*slot
== HTAB_EMPTY_ENTRY
)
561 else if (*slot
== HTAB_DELETED_ENTRY
)
564 hash2
= hash_table_mod2 (hash
, htab
->size_prime_index
);
571 slot
= htab
->entries
+ index
;
572 if (*slot
== HTAB_EMPTY_ENTRY
)
574 else if (*slot
== HTAB_DELETED_ENTRY
)
580 /* The following function changes size of memory allocated for the
581 entries and repeatedly inserts the table elements. The occupancy
582 of the table after the call will be about 50%. Naturally the hash
583 table must already exist. Remember also that the place of the
584 table entries is changed. If memory allocation fails, this function
587 template <typename Descriptor
,
588 template <typename Type
> class Allocator
>
590 hash_table
<Descriptor
, Allocator
>::expand ()
592 value_type
**oentries
;
595 value_type
**nentries
;
596 size_t nsize
, osize
, elts
;
597 unsigned int oindex
, nindex
;
599 oentries
= htab
->entries
;
600 oindex
= htab
->size_prime_index
;
602 olimit
= oentries
+ osize
;
605 /* Resize only when table after removal of unused elements is either
606 too full or too empty. */
607 if (elts
* 2 > osize
|| (elts
* 8 < osize
&& osize
> 32))
609 nindex
= hash_table_higher_prime_index (elts
* 2);
610 nsize
= prime_tab
[nindex
].prime
;
618 nentries
= Allocator
<value_type
*> ::data_alloc (nsize
);
619 gcc_assert (nentries
!= NULL
);
620 htab
->entries
= nentries
;
622 htab
->size_prime_index
= nindex
;
623 htab
->n_elements
-= htab
->n_deleted
;
631 if (x
!= HTAB_EMPTY_ENTRY
&& x
!= HTAB_DELETED_ENTRY
)
633 value_type
**q
= find_empty_slot_for_expand (Descriptor::hash (x
));
642 Allocator
<value_type
*> ::data_free (oentries
);
646 /* This function searches for a hash table entry equal to the given
647 COMPARABLE element starting with the given HASH value. It cannot
648 be used to insert or delete an element. */
650 template <typename Descriptor
,
651 template <typename Type
> class Allocator
>
652 typename
Descriptor::value_type
*
653 hash_table
<Descriptor
, Allocator
>
654 ::find_with_hash (const compare_type
*comparable
, hashval_t hash
)
656 hashval_t index
, hash2
;
662 index
= hash_table_mod1 (hash
, htab
->size_prime_index
);
664 entry
= htab
->entries
[index
];
665 if (entry
== HTAB_EMPTY_ENTRY
666 || (entry
!= HTAB_DELETED_ENTRY
&& Descriptor::equal (entry
, comparable
)))
669 hash2
= hash_table_mod2 (hash
, htab
->size_prime_index
);
677 entry
= htab
->entries
[index
];
678 if (entry
== HTAB_EMPTY_ENTRY
679 || (entry
!= HTAB_DELETED_ENTRY
680 && Descriptor::equal (entry
, comparable
)))
686 /* This function searches for a hash table slot containing an entry
687 equal to the given COMPARABLE element and starting with the given
688 HASH. To delete an entry, call this with insert=NO_INSERT, then
689 call clear_slot on the slot returned (possibly after doing some
690 checks). To insert an entry, call this with insert=INSERT, then
691 write the value you want into the returned slot. When inserting an
692 entry, NULL may be returned if memory allocation fails. */
694 template <typename Descriptor
,
695 template <typename Type
> class Allocator
>
696 typename
Descriptor::value_type
**
697 hash_table
<Descriptor
, Allocator
>
698 ::find_slot_with_hash (const compare_type
*comparable
, hashval_t hash
,
699 enum insert_option insert
)
701 value_type
**first_deleted_slot
;
702 hashval_t index
, hash2
;
707 if (insert
== INSERT
&& size
* 3 <= htab
->n_elements
* 4)
713 index
= hash_table_mod1 (hash
, htab
->size_prime_index
);
716 first_deleted_slot
= NULL
;
718 entry
= htab
->entries
[index
];
719 if (entry
== HTAB_EMPTY_ENTRY
)
721 else if (entry
== HTAB_DELETED_ENTRY
)
722 first_deleted_slot
= &htab
->entries
[index
];
723 else if (Descriptor::equal (entry
, comparable
))
724 return &htab
->entries
[index
];
726 hash2
= hash_table_mod2 (hash
, htab
->size_prime_index
);
734 entry
= htab
->entries
[index
];
735 if (entry
== HTAB_EMPTY_ENTRY
)
737 else if (entry
== HTAB_DELETED_ENTRY
)
739 if (!first_deleted_slot
)
740 first_deleted_slot
= &htab
->entries
[index
];
742 else if (Descriptor::equal (entry
, comparable
))
743 return &htab
->entries
[index
];
747 if (insert
== NO_INSERT
)
750 if (first_deleted_slot
)
753 *first_deleted_slot
= static_cast <value_type
*> (HTAB_EMPTY_ENTRY
);
754 return first_deleted_slot
;
758 return &htab
->entries
[index
];
762 /* This function clears all entries in the given hash table. */
764 template <typename Descriptor
,
765 template <typename Type
> class Allocator
>
767 hash_table
<Descriptor
, Allocator
>::empty ()
769 size_t size
= htab
->size
;
770 value_type
**entries
= htab
->entries
;
773 for (i
= size
- 1; i
>= 0; i
--)
774 if (entries
[i
] != HTAB_EMPTY_ENTRY
&& entries
[i
] != HTAB_DELETED_ENTRY
)
775 Descriptor::remove (entries
[i
]);
777 /* Instead of clearing megabyte, downsize the table. */
778 if (size
> 1024*1024 / sizeof (PTR
))
780 int nindex
= hash_table_higher_prime_index (1024 / sizeof (PTR
));
781 int nsize
= prime_tab
[nindex
].prime
;
783 Allocator
<value_type
*> ::data_free (htab
->entries
);
784 htab
->entries
= Allocator
<value_type
*> ::data_alloc (nsize
);
786 htab
->size_prime_index
= nindex
;
789 memset (entries
, 0, size
* sizeof (value_type
*));
791 htab
->n_elements
= 0;
795 /* This function clears a specified SLOT in a hash table. It is
796 useful when you've already done the lookup and don't want to do it
799 template <typename Descriptor
,
800 template <typename Type
> class Allocator
>
802 hash_table
<Descriptor
, Allocator
>::clear_slot (value_type
**slot
)
804 if (slot
< htab
->entries
|| slot
>= htab
->entries
+ htab
->size
805 || *slot
== HTAB_EMPTY_ENTRY
|| *slot
== HTAB_DELETED_ENTRY
)
808 Descriptor::remove (*slot
);
810 *slot
= static_cast <value_type
*> (HTAB_DELETED_ENTRY
);
815 /* This function deletes an element with the given COMPARABLE value
816 from hash table starting with the given HASH. If there is no
817 matching element in the hash table, this function does nothing. */
819 template <typename Descriptor
,
820 template <typename Type
> class Allocator
>
822 hash_table
<Descriptor
, Allocator
>
823 ::remove_elt_with_hash (const compare_type
*comparable
, hashval_t hash
)
827 slot
= find_slot_with_hash (comparable
, hash
, NO_INSERT
);
828 if (*slot
== HTAB_EMPTY_ENTRY
)
831 Descriptor::remove (*slot
);
833 *slot
= static_cast <value_type
*> (HTAB_DELETED_ENTRY
);
838 /* This function scans over the entire hash table calling CALLBACK for
839 each live entry. If CALLBACK returns false, the iteration stops.
840 ARGUMENT is passed as CALLBACK's second argument. */
842 template <typename Descriptor
,
843 template <typename Type
> class Allocator
>
844 template <typename Argument
,
845 int (*Callback
) (typename
Descriptor::value_type
**slot
, Argument argument
)>
847 hash_table
<Descriptor
, Allocator
>::traverse_noresize (Argument argument
)
852 slot
= htab
->entries
;
853 limit
= slot
+ htab
->size
;
857 value_type
*x
= *slot
;
859 if (x
!= HTAB_EMPTY_ENTRY
&& x
!= HTAB_DELETED_ENTRY
)
860 if (! Callback (slot
, argument
))
863 while (++slot
< limit
);
867 /* Like traverse_noresize, but does resize the table when it is too empty
868 to improve effectivity of subsequent calls. */
870 template <typename Descriptor
,
871 template <typename Type
> class Allocator
>
872 template <typename Argument
,
873 int (*Callback
) (typename
Descriptor::value_type
**slot
,
876 hash_table
<Descriptor
, Allocator
>::traverse (Argument argument
)
878 size_t size
= htab
->size
;
879 if (elements () * 8 < size
&& size
> 32)
882 traverse_noresize
<Argument
, Callback
> (argument
);
885 #endif /* TYPED_HASHTAB_H */