1 /* A type-safe hash set.
2 Copyright (C) 2014-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
24 #include "hash-table.h" // sdcpp
25 #include "hash-traits.h" // sdcpp
27 /* Class hash_set is a hash-value based container for objects of
29 KeyId may be a non-trivial (non-POD) type provided a suitabe Traits
30 class. Default Traits specializations are provided for basic types
31 such as integers, pointers, and std::pair. Inserted elements are
32 value-initialized either to zero for POD types or by invoking their
33 default ctor. Removed elements are destroyed by invoking their dtor.
34 On hash_set destruction all elements are removed. Objects of
35 hash_set type are copy-constructible but not assignable. */
37 template<typename KeyId
, bool Lazy
= false,
38 typename Traits
= default_hash_traits
<KeyId
> >
42 typedef typename
Traits::value_type Key
;
43 explicit hash_set (size_t n
= 13, bool ggc
= false CXX_MEM_STAT_INFO
)
44 : m_table (n
, ggc
, true, GATHER_STATISTICS
, HASH_SET_ORIGIN PASS_MEM_STAT
) {}
46 /* Create a hash_set in gc memory with space for at least n elements. */
51 hash_set
*set
= ggc_alloc
<hash_set
> ();
52 new (set
) hash_set (n
, true);
56 /* If key k isn't already in the map add it to the map, and
57 return false. Otherwise return true. */
59 bool add (const Key
&k
)
61 Key
*e
= m_table
.find_slot_with_hash (k
, Traits::hash (k
), INSERT
);
62 bool existed
= !Traits::is_empty (*e
);
69 /* if the passed in key is in the map return its value otherwise NULL. */
71 bool contains (const Key
&k
)
74 return (m_table
.find_slot_with_hash (k
, Traits::hash (k
), NO_INSERT
)
76 Key
&e
= m_table
.find_with_hash (k
, Traits::hash (k
));
77 return !Traits::is_empty (e
);
80 void remove (const Key
&k
)
82 m_table
.remove_elt_with_hash (k
, Traits::hash (k
));
85 /* Call the call back on each pair of key and value with the passed in
88 template<typename Arg
, bool (*f
)(const typename
Traits::value_type
&, Arg
)>
89 void traverse (Arg a
) const
91 for (typename hash_table
<Traits
, Lazy
>::iterator iter
= m_table
.begin ();
92 iter
!= m_table
.end (); ++iter
)
96 /* Return the number of elements in the set. */
98 size_t elements () const { return m_table
.elements (); }
100 /* Clear the hash table. */
102 void empty () { m_table
.empty (); }
104 /* Return true when there are no elements in this hash set. */
105 bool is_empty () const { return m_table
.is_empty (); }
110 explicit iterator (const typename hash_table
<Traits
,
111 Lazy
>::iterator
&iter
) :
114 iterator
&operator++ ()
127 operator != (const iterator
&other
) const
129 return m_iter
!= other
.m_iter
;
133 typename hash_table
<Traits
, Lazy
>::iterator m_iter
;
136 /* Standard iterator retrieval methods. */
138 iterator
begin () const { return iterator (m_table
.begin ()); }
139 iterator
end () const { return iterator (m_table
.end ()); }
144 template<typename T
, typename U
>
145 friend void gt_ggc_mx (hash_set
<T
, false, U
> *);
146 template<typename T
, typename U
>
147 friend void gt_pch_nx (hash_set
<T
, false, U
> *);
148 template<typename T
, typename U
>
149 friend void gt_pch_nx (hash_set
<T
, false, U
> *, gt_pointer_operator
, void *);
151 hash_table
<Traits
, Lazy
> m_table
;
154 /* Generic hash_set<TYPE> debug helper.
156 This needs to be instantiated for each hash_set<TYPE> used throughout
157 the compiler like this:
159 DEFINE_DEBUG_HASH_SET (TYPE)
161 The reason we have a debug_helper() is because GDB can't
162 disambiguate a plain call to debug(some_hash), and it must be called
163 like debug<TYPE>(some_hash). */
166 debug_helper (hash_set
<T
> &ref
)
168 for (typename hash_set
<T
>::iterator it
= ref
.begin ();
169 it
!= ref
.end (); ++it
)
172 fputc ('\n', stderr
);
176 #define DEFINE_DEBUG_HASH_SET(T) \
177 template void debug_helper (hash_set<T> &); \
178 DEBUG_FUNCTION void \
179 debug (hash_set<T> &ref) \
181 debug_helper <T> (ref); \
183 DEBUG_FUNCTION void \
184 debug (hash_set<T> *ptr) \
189 fprintf (stderr, "<nil>\n"); \
192 /* ggc marking routines. */
194 template<typename K
, typename H
>
196 gt_ggc_mx (hash_set
<K
, false, H
> *h
)
198 gt_ggc_mx (&h
->m_table
);
201 template<typename K
, typename H
>
203 gt_pch_nx (hash_set
<K
, false, H
> *h
)
205 gt_pch_nx (&h
->m_table
);
208 template<typename K
, typename H
>
210 gt_pch_nx (hash_set
<K
, false, H
> *h
, gt_pointer_operator op
, void *cookie
)
212 op (&h
->m_table
.m_entries
, NULL
, cookie
);