]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/hash-set.h
Update libbid according to the latest Intel Decimal Floating-Point Math Library.
[thirdparty/gcc.git] / gcc / hash-set.h
1 /* A type-safe hash set.
2 Copyright (C) 2014-2019 Free Software Foundation, Inc.
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
24 template<typename KeyId, bool Lazy = false,
25 typename Traits = default_hash_traits<KeyId> >
26 class hash_set
27 {
28 public:
29 typedef typename Traits::value_type Key;
30 explicit hash_set (size_t n = 13, bool ggc = false CXX_MEM_STAT_INFO)
31 : m_table (n, ggc, true, GATHER_STATISTICS, HASH_SET_ORIGIN PASS_MEM_STAT) {}
32
33 /* Create a hash_set in gc memory with space for at least n elements. */
34
35 static hash_set *
36 create_ggc (size_t n)
37 {
38 hash_set *set = ggc_alloc<hash_set> ();
39 new (set) hash_set (n, true);
40 return set;
41 }
42
43 /* If key k isn't already in the map add it to the map, and
44 return false. Otherwise return true. */
45
46 bool add (const Key &k)
47 {
48 Key *e = m_table.find_slot_with_hash (k, Traits::hash (k), INSERT);
49 bool existed = !Traits::is_empty (*e);
50 if (!existed)
51 *e = k;
52
53 return existed;
54 }
55
56 /* if the passed in key is in the map return its value otherwise NULL. */
57
58 bool contains (const Key &k)
59 {
60 if (Lazy)
61 return (m_table.find_slot_with_hash (k, Traits::hash (k), NO_INSERT)
62 != NULL);
63 Key &e = m_table.find_with_hash (k, Traits::hash (k));
64 return !Traits::is_empty (e);
65 }
66
67 void remove (const Key &k)
68 {
69 m_table.remove_elt_with_hash (k, Traits::hash (k));
70 }
71
72 /* Call the call back on each pair of key and value with the passed in
73 arg. */
74
75 template<typename Arg, bool (*f)(const typename Traits::value_type &, Arg)>
76 void traverse (Arg a) const
77 {
78 for (typename hash_table<Traits, Lazy>::iterator iter = m_table.begin ();
79 iter != m_table.end (); ++iter)
80 f (*iter, a);
81 }
82
83 /* Return the number of elements in the set. */
84
85 size_t elements () const { return m_table.elements (); }
86
87 /* Clear the hash table. */
88
89 void empty () { m_table.empty (); }
90
91 /* Return true when there are no elements in this hash set. */
92 bool is_empty () const { return m_table.is_empty (); }
93
94 class iterator
95 {
96 public:
97 explicit iterator (const typename hash_table<Traits,
98 Lazy>::iterator &iter) :
99 m_iter (iter) {}
100
101 iterator &operator++ ()
102 {
103 ++m_iter;
104 return *this;
105 }
106
107 Key
108 operator* ()
109 {
110 return *m_iter;
111 }
112
113 bool
114 operator != (const iterator &other) const
115 {
116 return m_iter != other.m_iter;
117 }
118
119 private:
120 typename hash_table<Traits, Lazy>::iterator m_iter;
121 };
122
123 /* Standard iterator retrieval methods. */
124
125 iterator begin () const { return iterator (m_table.begin ()); }
126 iterator end () const { return iterator (m_table.end ()); }
127
128
129 private:
130
131 template<typename T, typename U>
132 friend void gt_ggc_mx (hash_set<T, false, U> *);
133 template<typename T, typename U>
134 friend void gt_pch_nx (hash_set<T, false, U> *);
135 template<typename T, typename U>
136 friend void gt_pch_nx (hash_set<T, false, U> *, gt_pointer_operator, void *);
137
138 hash_table<Traits, Lazy> m_table;
139 };
140
141 /* Generic hash_set<TYPE> debug helper.
142
143 This needs to be instantiated for each hash_set<TYPE> used throughout
144 the compiler like this:
145
146 DEFINE_DEBUG_HASH_SET (TYPE)
147
148 The reason we have a debug_helper() is because GDB can't
149 disambiguate a plain call to debug(some_hash), and it must be called
150 like debug<TYPE>(some_hash). */
151 template<typename T>
152 void
153 debug_helper (hash_set<T> &ref)
154 {
155 for (typename hash_set<T>::iterator it = ref.begin ();
156 it != ref.end (); ++it)
157 {
158 debug_slim (*it);
159 fputc ('\n', stderr);
160 }
161 }
162
163 #define DEFINE_DEBUG_HASH_SET(T) \
164 template void debug_helper (hash_set<T> &); \
165 DEBUG_FUNCTION void \
166 debug (hash_set<T> &ref) \
167 { \
168 debug_helper <T> (ref); \
169 } \
170 DEBUG_FUNCTION void \
171 debug (hash_set<T> *ptr) \
172 { \
173 if (ptr) \
174 debug (*ptr); \
175 else \
176 fprintf (stderr, "<nil>\n"); \
177 }
178
179 /* ggc marking routines. */
180
181 template<typename K, typename H>
182 static inline void
183 gt_ggc_mx (hash_set<K, false, H> *h)
184 {
185 gt_ggc_mx (&h->m_table);
186 }
187
188 template<typename K, typename H>
189 static inline void
190 gt_pch_nx (hash_set<K, false, H> *h)
191 {
192 gt_pch_nx (&h->m_table);
193 }
194
195 template<typename K, typename H>
196 static inline void
197 gt_pch_nx (hash_set<K, false, H> *h, gt_pointer_operator op, void *cookie)
198 {
199 op (&h->m_table.m_entries, cookie);
200 }
201
202 #endif