annotate gcc/hash-set.h @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* A type-safe hash set.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2014-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #ifndef hash_set_h
kono
parents:
diff changeset
22 #define hash_set_h
kono
parents:
diff changeset
23
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
24 /* Class hash_set is a hash-value based container for objects of
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
25 KeyId type.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
26 KeyId may be a non-trivial (non-POD) type provided a suitabe Traits
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
27 class. Default Traits specializations are provided for basic types
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
28 such as integers, pointers, and std::pair. Inserted elements are
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
29 value-initialized either to zero for POD types or by invoking their
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
30 default ctor. Removed elements are destroyed by invoking their dtor.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
31 On hash_set destruction all elements are removed. Objects of
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
32 hash_set type are copy-constructible but not assignable. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
33
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
34 template<typename KeyId, bool Lazy = false,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
35 typename Traits = default_hash_traits<KeyId> >
111
kono
parents:
diff changeset
36 class hash_set
kono
parents:
diff changeset
37 {
kono
parents:
diff changeset
38 public:
kono
parents:
diff changeset
39 typedef typename Traits::value_type Key;
kono
parents:
diff changeset
40 explicit hash_set (size_t n = 13, bool ggc = false CXX_MEM_STAT_INFO)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
41 : m_table (n, ggc, true, GATHER_STATISTICS, HASH_SET_ORIGIN PASS_MEM_STAT) {}
111
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 /* Create a hash_set in gc memory with space for at least n elements. */
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 static hash_set *
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
46 create_ggc (size_t n)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
47 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
48 hash_set *set = ggc_alloc<hash_set> ();
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
49 new (set) hash_set (n, true);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
50 return set;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
51 }
111
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 /* If key k isn't already in the map add it to the map, and
kono
parents:
diff changeset
54 return false. Otherwise return true. */
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 bool add (const Key &k)
kono
parents:
diff changeset
57 {
kono
parents:
diff changeset
58 Key *e = m_table.find_slot_with_hash (k, Traits::hash (k), INSERT);
kono
parents:
diff changeset
59 bool existed = !Traits::is_empty (*e);
kono
parents:
diff changeset
60 if (!existed)
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
61 new (e) Key (k);
111
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 return existed;
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 /* if the passed in key is in the map return its value otherwise NULL. */
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 bool contains (const Key &k)
kono
parents:
diff changeset
69 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
70 if (Lazy)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
71 return (m_table.find_slot_with_hash (k, Traits::hash (k), NO_INSERT)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
72 != NULL);
111
kono
parents:
diff changeset
73 Key &e = m_table.find_with_hash (k, Traits::hash (k));
kono
parents:
diff changeset
74 return !Traits::is_empty (e);
kono
parents:
diff changeset
75 }
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 void remove (const Key &k)
kono
parents:
diff changeset
78 {
kono
parents:
diff changeset
79 m_table.remove_elt_with_hash (k, Traits::hash (k));
kono
parents:
diff changeset
80 }
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 /* Call the call back on each pair of key and value with the passed in
kono
parents:
diff changeset
83 arg. */
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 template<typename Arg, bool (*f)(const typename Traits::value_type &, Arg)>
kono
parents:
diff changeset
86 void traverse (Arg a) const
kono
parents:
diff changeset
87 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
88 for (typename hash_table<Traits, Lazy>::iterator iter = m_table.begin ();
111
kono
parents:
diff changeset
89 iter != m_table.end (); ++iter)
kono
parents:
diff changeset
90 f (*iter, a);
kono
parents:
diff changeset
91 }
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 /* Return the number of elements in the set. */
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 size_t elements () const { return m_table.elements (); }
kono
parents:
diff changeset
96
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
97 /* Clear the hash table. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
98
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
99 void empty () { m_table.empty (); }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
100
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
101 /* Return true when there are no elements in this hash set. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
102 bool is_empty () const { return m_table.is_empty (); }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
103
111
kono
parents:
diff changeset
104 class iterator
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 public:
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
107 explicit iterator (const typename hash_table<Traits,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
108 Lazy>::iterator &iter) :
111
kono
parents:
diff changeset
109 m_iter (iter) {}
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 iterator &operator++ ()
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 ++m_iter;
kono
parents:
diff changeset
114 return *this;
kono
parents:
diff changeset
115 }
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 Key
kono
parents:
diff changeset
118 operator* ()
kono
parents:
diff changeset
119 {
kono
parents:
diff changeset
120 return *m_iter;
kono
parents:
diff changeset
121 }
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 bool
kono
parents:
diff changeset
124 operator != (const iterator &other) const
kono
parents:
diff changeset
125 {
kono
parents:
diff changeset
126 return m_iter != other.m_iter;
kono
parents:
diff changeset
127 }
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 private:
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
130 typename hash_table<Traits, Lazy>::iterator m_iter;
111
kono
parents:
diff changeset
131 };
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 /* Standard iterator retrieval methods. */
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 iterator begin () const { return iterator (m_table.begin ()); }
kono
parents:
diff changeset
136 iterator end () const { return iterator (m_table.end ()); }
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 private:
kono
parents:
diff changeset
140
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
141 template<typename T, typename U>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
142 friend void gt_ggc_mx (hash_set<T, false, U> *);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
143 template<typename T, typename U>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
144 friend void gt_pch_nx (hash_set<T, false, U> *);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
145 template<typename T, typename U>
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
146 friend void gt_pch_nx (hash_set<T, false, U> *, gt_pointer_operator, void *);
111
kono
parents:
diff changeset
147
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
148 hash_table<Traits, Lazy> m_table;
111
kono
parents:
diff changeset
149 };
kono
parents:
diff changeset
150
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
151 /* Generic hash_set<TYPE> debug helper.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
152
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
153 This needs to be instantiated for each hash_set<TYPE> used throughout
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
154 the compiler like this:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
155
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
156 DEFINE_DEBUG_HASH_SET (TYPE)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
157
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
158 The reason we have a debug_helper() is because GDB can't
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
159 disambiguate a plain call to debug(some_hash), and it must be called
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
160 like debug<TYPE>(some_hash). */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
161 template<typename T>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
162 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
163 debug_helper (hash_set<T> &ref)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
164 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
165 for (typename hash_set<T>::iterator it = ref.begin ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
166 it != ref.end (); ++it)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
167 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
168 debug_slim (*it);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
169 fputc ('\n', stderr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
170 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
171 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
172
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
173 #define DEFINE_DEBUG_HASH_SET(T) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
174 template void debug_helper (hash_set<T> &); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
175 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
176 debug (hash_set<T> &ref) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
177 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
178 debug_helper <T> (ref); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
179 } \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180 DEBUG_FUNCTION void \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 debug (hash_set<T> *ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
182 { \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
183 if (ptr) \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
184 debug (*ptr); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
185 else \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
186 fprintf (stderr, "<nil>\n"); \
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
187 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
188
111
kono
parents:
diff changeset
189 /* ggc marking routines. */
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 template<typename K, typename H>
kono
parents:
diff changeset
192 static inline void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
193 gt_ggc_mx (hash_set<K, false, H> *h)
111
kono
parents:
diff changeset
194 {
kono
parents:
diff changeset
195 gt_ggc_mx (&h->m_table);
kono
parents:
diff changeset
196 }
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 template<typename K, typename H>
kono
parents:
diff changeset
199 static inline void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
200 gt_pch_nx (hash_set<K, false, H> *h)
111
kono
parents:
diff changeset
201 {
kono
parents:
diff changeset
202 gt_pch_nx (&h->m_table);
kono
parents:
diff changeset
203 }
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 template<typename K, typename H>
kono
parents:
diff changeset
206 static inline void
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
207 gt_pch_nx (hash_set<K, false, H> *h, gt_pointer_operator op, void *cookie)
111
kono
parents:
diff changeset
208 {
kono
parents:
diff changeset
209 op (&h->m_table.m_entries, cookie);
kono
parents:
diff changeset
210 }
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 #endif