]>
Commit | Line | Data |
---|---|---|
6e2830c3 | 1 | /* A type-safe hash set. |
99dee823 | 2 | Copyright (C) 2014-2021 Free Software Foundation, Inc. |
6e2830c3 TS |
3 | |
4 | This file is part of GCC. | |
5 | ||
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 | |
9 | version. | |
10 | ||
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 | |
14 | for more details. | |
15 | ||
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/>. */ | |
19 | ||
20 | ||
21 | #ifndef hash_set_h | |
22 | #define hash_set_h | |
23 | ||
7b8795a1 MS |
24 | /* Class hash_set is a hash-value based container for objects of |
25 | KeyId type. | |
26 | KeyId may be a non-trivial (non-POD) type provided a suitabe Traits | |
27 | class. Default Traits specializations are provided for basic types | |
28 | such as integers, pointers, and std::pair. Inserted elements are | |
29 | value-initialized either to zero for POD types or by invoking their | |
30 | default ctor. Removed elements are destroyed by invoking their dtor. | |
31 | On hash_set destruction all elements are removed. Objects of | |
32 | hash_set type are copy-constructible but not assignable. */ | |
33 | ||
36a3a7a3 JJ |
34 | template<typename KeyId, bool Lazy = false, |
35 | typename Traits = default_hash_traits<KeyId> > | |
6e2830c3 TS |
36 | class hash_set |
37 | { | |
6e2830c3 | 38 | public: |
6bac43d7 | 39 | typedef typename Traits::value_type Key; |
2d44c7de | 40 | explicit hash_set (size_t n = 13, bool ggc = false CXX_MEM_STAT_INFO) |
510c9192 | 41 | : m_table (n, ggc, true, GATHER_STATISTICS, HASH_SET_ORIGIN PASS_MEM_STAT) {} |
b086d530 TS |
42 | |
43 | /* Create a hash_set in gc memory with space for at least n elements. */ | |
44 | ||
45 | static hash_set * | |
36a3a7a3 JJ |
46 | create_ggc (size_t n) |
47 | { | |
48 | hash_set *set = ggc_alloc<hash_set> (); | |
49 | new (set) hash_set (n, true); | |
50 | return set; | |
51 | } | |
6e2830c3 TS |
52 | |
53 | /* If key k isn't already in the map add it to the map, and | |
54 | return false. Otherwise return true. */ | |
55 | ||
56 | bool add (const Key &k) | |
57 | { | |
b32ca1df RS |
58 | Key *e = m_table.find_slot_with_hash (k, Traits::hash (k), INSERT); |
59 | bool existed = !Traits::is_empty (*e); | |
6e2830c3 | 60 | if (!existed) |
7b8795a1 | 61 | new (e) Key (k); |
6e2830c3 TS |
62 | |
63 | return existed; | |
64 | } | |
65 | ||
66 | /* if the passed in key is in the map return its value otherwise NULL. */ | |
67 | ||
68 | bool contains (const Key &k) | |
69 | { | |
36a3a7a3 JJ |
70 | if (Lazy) |
71 | return (m_table.find_slot_with_hash (k, Traits::hash (k), NO_INSERT) | |
72 | != NULL); | |
b32ca1df RS |
73 | Key &e = m_table.find_with_hash (k, Traits::hash (k)); |
74 | return !Traits::is_empty (e); | |
6e2830c3 TS |
75 | } |
76 | ||
ce7bc090 ML |
77 | void remove (const Key &k) |
78 | { | |
79 | m_table.remove_elt_with_hash (k, Traits::hash (k)); | |
80 | } | |
81 | ||
6e2830c3 TS |
82 | /* Call the call back on each pair of key and value with the passed in |
83 | arg. */ | |
84 | ||
1fcb9641 | 85 | template<typename Arg, bool (*f)(const typename Traits::value_type &, Arg)> |
6e2830c3 TS |
86 | void traverse (Arg a) const |
87 | { | |
36a3a7a3 | 88 | for (typename hash_table<Traits, Lazy>::iterator iter = m_table.begin (); |
6e2830c3 | 89 | iter != m_table.end (); ++iter) |
b32ca1df | 90 | f (*iter, a); |
6e2830c3 TS |
91 | } |
92 | ||
b086d530 TS |
93 | /* Return the number of elements in the set. */ |
94 | ||
95 | size_t elements () const { return m_table.elements (); } | |
96 | ||
cde30fe0 AH |
97 | /* Clear the hash table. */ |
98 | ||
99 | void empty () { m_table.empty (); } | |
100 | ||
b119c055 ML |
101 | /* Return true when there are no elements in this hash set. */ |
102 | bool is_empty () const { return m_table.is_empty (); } | |
103 | ||
ce7bc090 ML |
104 | class iterator |
105 | { | |
106 | public: | |
36a3a7a3 JJ |
107 | explicit iterator (const typename hash_table<Traits, |
108 | Lazy>::iterator &iter) : | |
ce7bc090 ML |
109 | m_iter (iter) {} |
110 | ||
111 | iterator &operator++ () | |
112 | { | |
113 | ++m_iter; | |
114 | return *this; | |
115 | } | |
116 | ||
117 | Key | |
118 | operator* () | |
119 | { | |
120 | return *m_iter; | |
121 | } | |
122 | ||
123 | bool | |
124 | operator != (const iterator &other) const | |
125 | { | |
126 | return m_iter != other.m_iter; | |
127 | } | |
128 | ||
129 | private: | |
36a3a7a3 | 130 | typename hash_table<Traits, Lazy>::iterator m_iter; |
ce7bc090 ML |
131 | }; |
132 | ||
133 | /* Standard iterator retrieval methods. */ | |
134 | ||
135 | iterator begin () const { return iterator (m_table.begin ()); } | |
136 | iterator end () const { return iterator (m_table.end ()); } | |
137 | ||
138 | ||
6e2830c3 | 139 | private: |
b086d530 | 140 | |
36a3a7a3 JJ |
141 | template<typename T, typename U> |
142 | friend void gt_ggc_mx (hash_set<T, false, U> *); | |
143 | template<typename T, typename U> | |
144 | friend void gt_pch_nx (hash_set<T, false, U> *); | |
145 | template<typename T, typename U> | |
146 | friend void gt_pch_nx (hash_set<T, false, U> *, gt_pointer_operator, void *); | |
b086d530 | 147 | |
36a3a7a3 | 148 | hash_table<Traits, Lazy> m_table; |
6e2830c3 TS |
149 | }; |
150 | ||
19a30b71 AH |
151 | /* Generic hash_set<TYPE> debug helper. |
152 | ||
153 | This needs to be instantiated for each hash_set<TYPE> used throughout | |
154 | the compiler like this: | |
155 | ||
156 | DEFINE_DEBUG_HASH_SET (TYPE) | |
157 | ||
158 | The reason we have a debug_helper() is because GDB can't | |
159 | disambiguate a plain call to debug(some_hash), and it must be called | |
160 | like debug<TYPE>(some_hash). */ | |
161 | template<typename T> | |
162 | void | |
163 | debug_helper (hash_set<T> &ref) | |
164 | { | |
165 | for (typename hash_set<T>::iterator it = ref.begin (); | |
166 | it != ref.end (); ++it) | |
167 | { | |
168 | debug_slim (*it); | |
169 | fputc ('\n', stderr); | |
170 | } | |
171 | } | |
172 | ||
173 | #define DEFINE_DEBUG_HASH_SET(T) \ | |
4c71819d | 174 | template void debug_helper (hash_set<T> &); \ |
19a30b71 AH |
175 | DEBUG_FUNCTION void \ |
176 | debug (hash_set<T> &ref) \ | |
177 | { \ | |
178 | debug_helper <T> (ref); \ | |
179 | } \ | |
180 | DEBUG_FUNCTION void \ | |
181 | debug (hash_set<T> *ptr) \ | |
182 | { \ | |
183 | if (ptr) \ | |
184 | debug (*ptr); \ | |
185 | else \ | |
186 | fprintf (stderr, "<nil>\n"); \ | |
187 | } | |
188 | ||
b086d530 TS |
189 | /* ggc marking routines. */ |
190 | ||
191 | template<typename K, typename H> | |
192 | static inline void | |
36a3a7a3 | 193 | gt_ggc_mx (hash_set<K, false, H> *h) |
b086d530 TS |
194 | { |
195 | gt_ggc_mx (&h->m_table); | |
196 | } | |
197 | ||
198 | template<typename K, typename H> | |
199 | static inline void | |
36a3a7a3 | 200 | gt_pch_nx (hash_set<K, false, H> *h) |
b086d530 TS |
201 | { |
202 | gt_pch_nx (&h->m_table); | |
203 | } | |
204 | ||
205 | template<typename K, typename H> | |
206 | static inline void | |
36a3a7a3 | 207 | gt_pch_nx (hash_set<K, false, H> *h, gt_pointer_operator op, void *cookie) |
b086d530 TS |
208 | { |
209 | op (&h->m_table.m_entries, cookie); | |
210 | } | |
211 | ||
6e2830c3 | 212 | #endif |