annotate gcc/hash-map.h @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* A type-safe hash map.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2014-2018 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_map_h
kono
parents:
diff changeset
22 #define hash_map_h
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 template<typename KeyId, typename Value,
kono
parents:
diff changeset
25 typename Traits>
kono
parents:
diff changeset
26 class GTY((user)) hash_map
kono
parents:
diff changeset
27 {
kono
parents:
diff changeset
28 typedef typename Traits::key_type Key;
kono
parents:
diff changeset
29 struct hash_entry
kono
parents:
diff changeset
30 {
kono
parents:
diff changeset
31 Key m_key;
kono
parents:
diff changeset
32 Value m_value;
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 typedef hash_entry value_type;
kono
parents:
diff changeset
35 typedef Key compare_type;
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 static hashval_t hash (const hash_entry &e)
kono
parents:
diff changeset
38 {
kono
parents:
diff changeset
39 return Traits::hash (e.m_key);
kono
parents:
diff changeset
40 }
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 static bool equal (const hash_entry &a, const Key &b)
kono
parents:
diff changeset
43 {
kono
parents:
diff changeset
44 return Traits::equal_keys (a.m_key, b);
kono
parents:
diff changeset
45 }
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 static void remove (hash_entry &e) { Traits::remove (e); }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 static void mark_deleted (hash_entry &e) { Traits::mark_deleted (e); }
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 static bool is_deleted (const hash_entry &e)
kono
parents:
diff changeset
52 {
kono
parents:
diff changeset
53 return Traits::is_deleted (e);
kono
parents:
diff changeset
54 }
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 static void mark_empty (hash_entry &e) { Traits::mark_empty (e); }
kono
parents:
diff changeset
57 static bool is_empty (const hash_entry &e) { return Traits::is_empty (e); }
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 static void ggc_mx (hash_entry &e)
kono
parents:
diff changeset
60 {
kono
parents:
diff changeset
61 gt_ggc_mx (e.m_key);
kono
parents:
diff changeset
62 gt_ggc_mx (e.m_value);
kono
parents:
diff changeset
63 }
kono
parents:
diff changeset
64
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
65 static void ggc_maybe_mx (hash_entry &e)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
66 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
67 if (Traits::maybe_mx)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
68 ggc_mx (e);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
69 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
70
111
kono
parents:
diff changeset
71 static void pch_nx (hash_entry &e)
kono
parents:
diff changeset
72 {
kono
parents:
diff changeset
73 gt_pch_nx (e.m_key);
kono
parents:
diff changeset
74 gt_pch_nx (e.m_value);
kono
parents:
diff changeset
75 }
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 static void pch_nx (hash_entry &e, gt_pointer_operator op, void *c)
kono
parents:
diff changeset
78 {
kono
parents:
diff changeset
79 pch_nx_helper (e.m_key, op, c);
kono
parents:
diff changeset
80 pch_nx_helper (e.m_value, op, c);
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
83 static int keep_cache_entry (hash_entry &e)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
84 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
85 return ggc_marked_p (e.m_key);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
86 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
87
111
kono
parents:
diff changeset
88 private:
kono
parents:
diff changeset
89 template<typename T>
kono
parents:
diff changeset
90 static void
kono
parents:
diff changeset
91 pch_nx_helper (T &x, gt_pointer_operator op, void *cookie)
kono
parents:
diff changeset
92 {
kono
parents:
diff changeset
93 gt_pch_nx (&x, op, cookie);
kono
parents:
diff changeset
94 }
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 static void
kono
parents:
diff changeset
97 pch_nx_helper (int, gt_pointer_operator, void *)
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 static void
kono
parents:
diff changeset
102 pch_nx_helper (unsigned int, gt_pointer_operator, void *)
kono
parents:
diff changeset
103 {
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 static void
kono
parents:
diff changeset
107 pch_nx_helper (bool, gt_pointer_operator, void *)
kono
parents:
diff changeset
108 {
kono
parents:
diff changeset
109 }
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 template<typename T>
kono
parents:
diff changeset
112 static void
kono
parents:
diff changeset
113 pch_nx_helper (T *&x, gt_pointer_operator op, void *cookie)
kono
parents:
diff changeset
114 {
kono
parents:
diff changeset
115 op (&x, cookie);
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117 };
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 public:
kono
parents:
diff changeset
120 explicit hash_map (size_t n = 13, bool ggc = false,
kono
parents:
diff changeset
121 bool gather_mem_stats = GATHER_STATISTICS
kono
parents:
diff changeset
122 CXX_MEM_STAT_INFO)
kono
parents:
diff changeset
123 : m_table (n, ggc, gather_mem_stats, HASH_MAP_ORIGIN PASS_MEM_STAT) {}
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 explicit hash_map (const hash_map &h, bool ggc = false,
kono
parents:
diff changeset
126 bool gather_mem_stats = GATHER_STATISTICS
kono
parents:
diff changeset
127 CXX_MEM_STAT_INFO)
kono
parents:
diff changeset
128 : m_table (h.m_table, ggc, gather_mem_stats,
kono
parents:
diff changeset
129 HASH_MAP_ORIGIN PASS_MEM_STAT) {}
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 /* Create a hash_map in ggc memory. */
kono
parents:
diff changeset
132 static hash_map *create_ggc (size_t size,
kono
parents:
diff changeset
133 bool gather_mem_stats = GATHER_STATISTICS
kono
parents:
diff changeset
134 CXX_MEM_STAT_INFO)
kono
parents:
diff changeset
135 {
kono
parents:
diff changeset
136 hash_map *map = ggc_alloc<hash_map> ();
kono
parents:
diff changeset
137 new (map) hash_map (size, true, gather_mem_stats PASS_MEM_STAT);
kono
parents:
diff changeset
138 return map;
kono
parents:
diff changeset
139 }
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 /* If key k isn't already in the map add key k with value v to the map, and
kono
parents:
diff changeset
142 return false. Otherwise set the value of the entry for key k to be v and
kono
parents:
diff changeset
143 return true. */
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 bool put (const Key &k, const Value &v)
kono
parents:
diff changeset
146 {
kono
parents:
diff changeset
147 hash_entry *e = m_table.find_slot_with_hash (k, Traits::hash (k),
kono
parents:
diff changeset
148 INSERT);
kono
parents:
diff changeset
149 bool existed = !hash_entry::is_empty (*e);
kono
parents:
diff changeset
150 if (!existed)
kono
parents:
diff changeset
151 e->m_key = k;
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 e->m_value = v;
kono
parents:
diff changeset
154 return existed;
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 /* if the passed in key is in the map return its value otherwise NULL. */
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 Value *get (const Key &k)
kono
parents:
diff changeset
160 {
kono
parents:
diff changeset
161 hash_entry &e = m_table.find_with_hash (k, Traits::hash (k));
kono
parents:
diff changeset
162 return Traits::is_empty (e) ? NULL : &e.m_value;
kono
parents:
diff changeset
163 }
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 /* Return a reference to the value for the passed in key, creating the entry
kono
parents:
diff changeset
166 if it doesn't already exist. If existed is not NULL then it is set to false
kono
parents:
diff changeset
167 if the key was not previously in the map, and true otherwise. */
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 Value &get_or_insert (const Key &k, bool *existed = NULL)
kono
parents:
diff changeset
170 {
kono
parents:
diff changeset
171 hash_entry *e = m_table.find_slot_with_hash (k, Traits::hash (k),
kono
parents:
diff changeset
172 INSERT);
kono
parents:
diff changeset
173 bool ins = Traits::is_empty (*e);
kono
parents:
diff changeset
174 if (ins)
kono
parents:
diff changeset
175 e->m_key = k;
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 if (existed != NULL)
kono
parents:
diff changeset
178 *existed = !ins;
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 return e->m_value;
kono
parents:
diff changeset
181 }
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 void remove (const Key &k)
kono
parents:
diff changeset
184 {
kono
parents:
diff changeset
185 m_table.remove_elt_with_hash (k, Traits::hash (k));
kono
parents:
diff changeset
186 }
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 /* Call the call back on each pair of key and value with the passed in
kono
parents:
diff changeset
189 arg. */
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 template<typename Arg, bool (*f)(const typename Traits::key_type &,
kono
parents:
diff changeset
192 const Value &, Arg)>
kono
parents:
diff changeset
193 void traverse (Arg a) const
kono
parents:
diff changeset
194 {
kono
parents:
diff changeset
195 for (typename hash_table<hash_entry>::iterator iter = m_table.begin ();
kono
parents:
diff changeset
196 iter != m_table.end (); ++iter)
kono
parents:
diff changeset
197 f ((*iter).m_key, (*iter).m_value, a);
kono
parents:
diff changeset
198 }
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 template<typename Arg, bool (*f)(const typename Traits::key_type &,
kono
parents:
diff changeset
201 Value *, Arg)>
kono
parents:
diff changeset
202 void traverse (Arg a) const
kono
parents:
diff changeset
203 {
kono
parents:
diff changeset
204 for (typename hash_table<hash_entry>::iterator iter = m_table.begin ();
kono
parents:
diff changeset
205 iter != m_table.end (); ++iter)
kono
parents:
diff changeset
206 if (!f ((*iter).m_key, &(*iter).m_value, a))
kono
parents:
diff changeset
207 break;
kono
parents:
diff changeset
208 }
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 size_t elements () const { return m_table.elements (); }
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 void empty () { m_table.empty(); }
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 class iterator
kono
parents:
diff changeset
215 {
kono
parents:
diff changeset
216 public:
kono
parents:
diff changeset
217 explicit iterator (const typename hash_table<hash_entry>::iterator &iter) :
kono
parents:
diff changeset
218 m_iter (iter) {}
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 iterator &operator++ ()
kono
parents:
diff changeset
221 {
kono
parents:
diff changeset
222 ++m_iter;
kono
parents:
diff changeset
223 return *this;
kono
parents:
diff changeset
224 }
kono
parents:
diff changeset
225
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
226 std::pair<const Key&, Value&> operator* ()
111
kono
parents:
diff changeset
227 {
kono
parents:
diff changeset
228 hash_entry &e = *m_iter;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
229 return std::pair<const Key&, Value&> (e.m_key, e.m_value);
111
kono
parents:
diff changeset
230 }
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 bool
kono
parents:
diff changeset
233 operator != (const iterator &other) const
kono
parents:
diff changeset
234 {
kono
parents:
diff changeset
235 return m_iter != other.m_iter;
kono
parents:
diff changeset
236 }
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 private:
kono
parents:
diff changeset
239 typename hash_table<hash_entry>::iterator m_iter;
kono
parents:
diff changeset
240 };
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 /* Standard iterator retrieval methods. */
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 iterator begin () const { return iterator (m_table.begin ()); }
kono
parents:
diff changeset
245 iterator end () const { return iterator (m_table.end ()); }
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 private:
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 template<typename T, typename U, typename V> friend void gt_ggc_mx (hash_map<T, U, V> *);
kono
parents:
diff changeset
250 template<typename T, typename U, typename V> friend void gt_pch_nx (hash_map<T, U, V> *);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
251 template<typename T, typename U, typename V> friend void gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
252 template<typename T, typename U, typename V> friend void gt_cleare_cache (hash_map<T, U, V> *);
111
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 hash_table<hash_entry> m_table;
kono
parents:
diff changeset
255 };
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 /* ggc marking routines. */
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 template<typename K, typename V, typename H>
kono
parents:
diff changeset
260 static inline void
kono
parents:
diff changeset
261 gt_ggc_mx (hash_map<K, V, H> *h)
kono
parents:
diff changeset
262 {
kono
parents:
diff changeset
263 gt_ggc_mx (&h->m_table);
kono
parents:
diff changeset
264 }
kono
parents:
diff changeset
265
kono
parents:
diff changeset
266 template<typename K, typename V, typename H>
kono
parents:
diff changeset
267 static inline void
kono
parents:
diff changeset
268 gt_pch_nx (hash_map<K, V, H> *h)
kono
parents:
diff changeset
269 {
kono
parents:
diff changeset
270 gt_pch_nx (&h->m_table);
kono
parents:
diff changeset
271 }
kono
parents:
diff changeset
272
kono
parents:
diff changeset
273 template<typename K, typename V, typename H>
kono
parents:
diff changeset
274 static inline void
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
275 gt_cleare_cache (hash_map<K, V, H> *h)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
276 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
277 if (h)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
278 gt_cleare_cache (&h->m_table);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
279 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
280
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
281 template<typename K, typename V, typename H>
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
282 static inline void
111
kono
parents:
diff changeset
283 gt_pch_nx (hash_map<K, V, H> *h, gt_pointer_operator op, void *cookie)
kono
parents:
diff changeset
284 {
kono
parents:
diff changeset
285 op (&h->m_table.m_entries, cookie);
kono
parents:
diff changeset
286 }
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 #endif