1 /* A type-safe hash map.
2 Copyright (C) 2014-2019 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 template<typename KeyId
, typename Value
,
26 class GTY((user
)) hash_map
28 typedef typename
Traits::key_type Key
;
34 typedef hash_entry value_type
;
35 typedef Key compare_type
;
37 static hashval_t
hash (const hash_entry
&e
)
39 return Traits::hash (e
.m_key
);
42 static bool equal (const hash_entry
&a
, const Key
&b
)
44 return Traits::equal_keys (a
.m_key
, b
);
47 static void remove (hash_entry
&e
) { Traits::remove (e
); }
49 static void mark_deleted (hash_entry
&e
) { Traits::mark_deleted (e
); }
51 static bool is_deleted (const hash_entry
&e
)
53 return Traits::is_deleted (e
);
56 static void mark_empty (hash_entry
&e
) { Traits::mark_empty (e
); }
57 static bool is_empty (const hash_entry
&e
) { return Traits::is_empty (e
); }
59 static void ggc_mx (hash_entry
&e
)
62 gt_ggc_mx (e
.m_value
);
65 static void ggc_maybe_mx (hash_entry
&e
)
71 static void pch_nx (hash_entry
&e
)
74 gt_pch_nx (e
.m_value
);
77 static void pch_nx (hash_entry
&e
, gt_pointer_operator op
, void *c
)
79 pch_nx_helper (e
.m_key
, op
, c
);
80 pch_nx_helper (e
.m_value
, op
, c
);
83 static int keep_cache_entry (hash_entry
&e
)
85 return ggc_marked_p (e
.m_key
);
91 pch_nx_helper (T
&x
, gt_pointer_operator op
, void *cookie
)
93 gt_pch_nx (&x
, op
, cookie
);
97 pch_nx_helper (int, gt_pointer_operator
, void *)
102 pch_nx_helper (unsigned int, gt_pointer_operator
, void *)
107 pch_nx_helper (bool, gt_pointer_operator
, void *)
113 pch_nx_helper (T
*&x
, gt_pointer_operator op
, void *cookie
)
120 explicit hash_map (size_t n
= 13, bool ggc
= false,
121 bool sanitize_eq_and_hash
= true,
122 bool gather_mem_stats
= GATHER_STATISTICS
124 : m_table (n
, ggc
, sanitize_eq_and_hash
, gather_mem_stats
,
125 HASH_MAP_ORIGIN PASS_MEM_STAT
)
129 explicit hash_map (const hash_map
&h
, bool ggc
= false,
130 bool sanitize_eq_and_hash
= true,
131 bool gather_mem_stats
= GATHER_STATISTICS
133 : m_table (h
.m_table
, ggc
, sanitize_eq_and_hash
, gather_mem_stats
,
134 HASH_MAP_ORIGIN PASS_MEM_STAT
) {}
136 /* Create a hash_map in ggc memory. */
137 static hash_map
*create_ggc (size_t size
,
138 bool gather_mem_stats
= GATHER_STATISTICS
141 hash_map
*map
= ggc_alloc
<hash_map
> ();
142 new (map
) hash_map (size
, true, true, gather_mem_stats PASS_MEM_STAT
);
146 /* If key k isn't already in the map add key k with value v to the map, and
147 return false. Otherwise set the value of the entry for key k to be v and
150 bool put (const Key
&k
, const Value
&v
)
152 hash_entry
*e
= m_table
.find_slot_with_hash (k
, Traits::hash (k
),
154 bool existed
= !hash_entry::is_empty (*e
);
162 /* if the passed in key is in the map return its value otherwise NULL. */
164 Value
*get (const Key
&k
)
166 hash_entry
&e
= m_table
.find_with_hash (k
, Traits::hash (k
));
167 return Traits::is_empty (e
) ? NULL
: &e
.m_value
;
170 /* Return a reference to the value for the passed in key, creating the entry
171 if it doesn't already exist. If existed is not NULL then it is set to false
172 if the key was not previously in the map, and true otherwise. */
174 Value
&get_or_insert (const Key
&k
, bool *existed
= NULL
)
176 hash_entry
*e
= m_table
.find_slot_with_hash (k
, Traits::hash (k
),
178 bool ins
= Traits::is_empty (*e
);
188 void remove (const Key
&k
)
190 m_table
.remove_elt_with_hash (k
, Traits::hash (k
));
193 /* Call the call back on each pair of key and value with the passed in
196 template<typename Arg
, bool (*f
)(const typename
Traits::key_type
&,
198 void traverse (Arg a
) const
200 for (typename hash_table
<hash_entry
>::iterator iter
= m_table
.begin ();
201 iter
!= m_table
.end (); ++iter
)
202 f ((*iter
).m_key
, (*iter
).m_value
, a
);
205 template<typename Arg
, bool (*f
)(const typename
Traits::key_type
&,
207 void traverse (Arg a
) const
209 for (typename hash_table
<hash_entry
>::iterator iter
= m_table
.begin ();
210 iter
!= m_table
.end (); ++iter
)
211 if (!f ((*iter
).m_key
, &(*iter
).m_value
, a
))
215 size_t elements () const { return m_table
.elements (); }
217 void empty () { m_table
.empty(); }
219 /* Return true when there are no elements in this hash map. */
220 bool is_empty () const { return m_table
.is_empty (); }
225 explicit iterator (const typename hash_table
<hash_entry
>::iterator
&iter
) :
228 iterator
&operator++ ()
234 /* Can't use std::pair here, because GCC before 4.3 don't handle
235 std::pair where template parameters are references well.
237 struct reference_pair
{
241 reference_pair (const Key
&key
, Value
&value
) : first (key
), second (value
) {}
243 template <typename K
, typename V
>
244 operator std::pair
<K
, V
> () const { return std::pair
<K
, V
> (first
, second
); }
247 reference_pair
operator* ()
249 hash_entry
&e
= *m_iter
;
250 return reference_pair (e
.m_key
, e
.m_value
);
254 operator != (const iterator
&other
) const
256 return m_iter
!= other
.m_iter
;
260 typename hash_table
<hash_entry
>::iterator m_iter
;
263 /* Standard iterator retrieval methods. */
265 iterator
begin () const { return iterator (m_table
.begin ()); }
266 iterator
end () const { return iterator (m_table
.end ()); }
270 template<typename T
, typename U
, typename V
> friend void gt_ggc_mx (hash_map
<T
, U
, V
> *);
271 template<typename T
, typename U
, typename V
> friend void gt_pch_nx (hash_map
<T
, U
, V
> *);
272 template<typename T
, typename U
, typename V
> friend void gt_pch_nx (hash_map
<T
, U
, V
> *, gt_pointer_operator
, void *);
273 template<typename T
, typename U
, typename V
> friend void gt_cleare_cache (hash_map
<T
, U
, V
> *);
275 hash_table
<hash_entry
> m_table
;
278 /* ggc marking routines. */
280 template<typename K
, typename V
, typename H
>
282 gt_ggc_mx (hash_map
<K
, V
, H
> *h
)
284 gt_ggc_mx (&h
->m_table
);
287 template<typename K
, typename V
, typename H
>
289 gt_pch_nx (hash_map
<K
, V
, H
> *h
)
291 gt_pch_nx (&h
->m_table
);
294 template<typename K
, typename V
, typename H
>
296 gt_cleare_cache (hash_map
<K
, V
, H
> *h
)
299 gt_cleare_cache (&h
->m_table
);
302 template<typename K
, typename V
, typename H
>
304 gt_pch_nx (hash_map
<K
, V
, H
> *h
, gt_pointer_operator op
, void *cookie
)
306 op (&h
->m_table
.m_entries
, cookie
);