您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

374 行
9.9KB

  1. /* A type-safe hash map.
  2. Copyright (C) 2014-2020 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef hash_map_h
  16. #define hash_map_h
  17. /* Class hash_map is a hash-value based container mapping objects of
  18. KeyId type to those of the Value type.
  19. Both KeyId and Value may be non-trivial (non-POD) types provided
  20. a suitabe Traits class. A few default Traits specializations are
  21. provided for basic types such as integers, pointers, and std::pair.
  22. Inserted elements are value-initialized either to zero for POD types
  23. or by invoking their default ctor. Removed elements are destroyed
  24. by invoking their dtor. On hash_map destruction all elements are
  25. removed. Objects of hash_map type are copy-constructible but not
  26. assignable. */
  27. const size_t default_hash_map_size = 13;
  28. template<typename KeyId, typename Value,
  29. typename Traits /* = simple_hashmap_traits<default_hash_traits<Key>,
  30. Value> */>
  31. class GTY((user)) hash_map
  32. {
  33. typedef typename Traits::key_type Key;
  34. struct hash_entry
  35. {
  36. Key m_key;
  37. Value m_value;
  38. typedef hash_entry value_type;
  39. typedef Key compare_type;
  40. static hashval_t hash (const hash_entry &e)
  41. {
  42. return Traits::hash (e.m_key);
  43. }
  44. static bool equal (const hash_entry &a, const Key &b)
  45. {
  46. return Traits::equal_keys (a.m_key, b);
  47. }
  48. static void remove (hash_entry &e) { Traits::remove (e); }
  49. static void mark_deleted (hash_entry &e) { Traits::mark_deleted (e); }
  50. static bool is_deleted (const hash_entry &e)
  51. {
  52. return Traits::is_deleted (e);
  53. }
  54. static const bool empty_zero_p = Traits::empty_zero_p;
  55. static void mark_empty (hash_entry &e) { Traits::mark_empty (e); }
  56. static bool is_empty (const hash_entry &e) { return Traits::is_empty (e); }
  57. static void ggc_mx (hash_entry &e)
  58. {
  59. gt_ggc_mx (e.m_key);
  60. gt_ggc_mx (e.m_value);
  61. }
  62. static void ggc_maybe_mx (hash_entry &e)
  63. {
  64. if (Traits::maybe_mx)
  65. ggc_mx (e);
  66. }
  67. static void pch_nx (hash_entry &e)
  68. {
  69. gt_pch_nx (e.m_key);
  70. gt_pch_nx (e.m_value);
  71. }
  72. static void pch_nx (hash_entry &e, gt_pointer_operator op, void *c)
  73. {
  74. pch_nx_helper (e.m_key, op, c);
  75. pch_nx_helper (e.m_value, op, c);
  76. }
  77. static int keep_cache_entry (hash_entry &e)
  78. {
  79. return ggc_marked_p (e.m_key);
  80. }
  81. private:
  82. template<typename T>
  83. static void
  84. pch_nx_helper (T &x, gt_pointer_operator op, void *cookie)
  85. {
  86. gt_pch_nx (&x, op, cookie);
  87. }
  88. static void
  89. pch_nx_helper (int, gt_pointer_operator, void *)
  90. {
  91. }
  92. static void
  93. pch_nx_helper (unsigned int, gt_pointer_operator, void *)
  94. {
  95. }
  96. static void
  97. pch_nx_helper (bool, gt_pointer_operator, void *)
  98. {
  99. }
  100. template<typename T>
  101. static void
  102. pch_nx_helper (T *&x, gt_pointer_operator op, void *cookie)
  103. {
  104. op (&x, cookie);
  105. }
  106. };
  107. public:
  108. explicit hash_map (size_t n = default_hash_map_size, bool ggc = false,
  109. bool sanitize_eq_and_hash = true,
  110. bool gather_mem_stats = GATHER_STATISTICS
  111. CXX_MEM_STAT_INFO)
  112. : m_table (n, ggc, sanitize_eq_and_hash, gather_mem_stats,
  113. HASH_MAP_ORIGIN PASS_MEM_STAT)
  114. {
  115. }
  116. explicit hash_map (const hash_map &h, bool ggc = false,
  117. bool sanitize_eq_and_hash = true,
  118. bool gather_mem_stats = GATHER_STATISTICS
  119. CXX_MEM_STAT_INFO)
  120. : m_table (h.m_table, ggc, sanitize_eq_and_hash, gather_mem_stats,
  121. HASH_MAP_ORIGIN PASS_MEM_STAT) {}
  122. /* Create a hash_map in ggc memory. */
  123. static hash_map *create_ggc (size_t size = default_hash_map_size,
  124. bool gather_mem_stats = GATHER_STATISTICS
  125. CXX_MEM_STAT_INFO)
  126. {
  127. hash_map *map = ggc_alloc<hash_map> ();
  128. new (map) hash_map (size, true, true, gather_mem_stats PASS_MEM_STAT);
  129. return map;
  130. }
  131. /* If key k isn't already in the map add key k with value v to the map, and
  132. return false. Otherwise set the value of the entry for key k to be v and
  133. return true. */
  134. bool put (const Key &k, const Value &v)
  135. {
  136. hash_entry *e = m_table.find_slot_with_hash (k, Traits::hash (k),
  137. INSERT);
  138. bool ins = hash_entry::is_empty (*e);
  139. if (ins)
  140. {
  141. e->m_key = k;
  142. new ((void *) &e->m_value) Value (v);
  143. }
  144. else
  145. e->m_value = v;
  146. return !ins;
  147. }
  148. /* if the passed in key is in the map return its value otherwise NULL. */
  149. Value *get (const Key &k)
  150. {
  151. hash_entry &e = m_table.find_with_hash (k, Traits::hash (k));
  152. return Traits::is_empty (e) ? NULL : &e.m_value;
  153. }
  154. /* Return a reference to the value for the passed in key, creating the entry
  155. if it doesn't already exist. If existed is not NULL then it is set to
  156. false if the key was not previously in the map, and true otherwise. */
  157. Value &get_or_insert (const Key &k, bool *existed = NULL)
  158. {
  159. hash_entry *e = m_table.find_slot_with_hash (k, Traits::hash (k),
  160. INSERT);
  161. bool ins = Traits::is_empty (*e);
  162. if (ins)
  163. {
  164. e->m_key = k;
  165. new ((void *)&e->m_value) Value ();
  166. }
  167. if (existed != NULL)
  168. *existed = !ins;
  169. return e->m_value;
  170. }
  171. void remove (const Key &k)
  172. {
  173. m_table.remove_elt_with_hash (k, Traits::hash (k));
  174. }
  175. /* Call the call back on each pair of key and value with the passed in
  176. arg. */
  177. template<typename Arg, bool (*f)(const typename Traits::key_type &,
  178. const Value &, Arg)>
  179. void traverse (Arg a) const
  180. {
  181. for (typename hash_table<hash_entry>::iterator iter = m_table.begin ();
  182. iter != m_table.end (); ++iter)
  183. f ((*iter).m_key, (*iter).m_value, a);
  184. }
  185. template<typename Arg, bool (*f)(const typename Traits::key_type &,
  186. Value *, Arg)>
  187. void traverse (Arg a) const
  188. {
  189. for (typename hash_table<hash_entry>::iterator iter = m_table.begin ();
  190. iter != m_table.end (); ++iter)
  191. if (!f ((*iter).m_key, &(*iter).m_value, a))
  192. break;
  193. }
  194. size_t elements () const { return m_table.elements (); }
  195. void empty () { m_table.empty(); }
  196. /* Return true when there are no elements in this hash map. */
  197. bool is_empty () const { return m_table.is_empty (); }
  198. class iterator
  199. {
  200. public:
  201. explicit iterator (const typename hash_table<hash_entry>::iterator &iter) :
  202. m_iter (iter) {}
  203. iterator &operator++ ()
  204. {
  205. ++m_iter;
  206. return *this;
  207. }
  208. /* Can't use std::pair here, because GCC before 4.3 don't handle
  209. std::pair where template parameters are references well.
  210. See PR86739. */
  211. class reference_pair {
  212. public:
  213. const Key &first;
  214. Value &second;
  215. reference_pair (const Key &key, Value &value) : first (key), second (value) {}
  216. template <typename K, typename V>
  217. operator std::pair<K, V> () const { return std::pair<K, V> (first, second); }
  218. };
  219. reference_pair operator* ()
  220. {
  221. hash_entry &e = *m_iter;
  222. return reference_pair (e.m_key, e.m_value);
  223. }
  224. bool
  225. operator != (const iterator &other) const
  226. {
  227. return m_iter != other.m_iter;
  228. }
  229. private:
  230. typename hash_table<hash_entry>::iterator m_iter;
  231. };
  232. /* Standard iterator retrieval methods. */
  233. iterator begin () const { return iterator (m_table.begin ()); }
  234. iterator end () const { return iterator (m_table.end ()); }
  235. private:
  236. template<typename T, typename U, typename V> friend void gt_ggc_mx (hash_map<T, U, V> *);
  237. template<typename T, typename U, typename V> friend void gt_pch_nx (hash_map<T, U, V> *);
  238. template<typename T, typename U, typename V> friend void gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *);
  239. template<typename T, typename U, typename V> friend void gt_cleare_cache (hash_map<T, U, V> *);
  240. hash_table<hash_entry> m_table;
  241. };
  242. /* ggc marking routines. */
  243. template<typename K, typename V, typename H>
  244. static inline void
  245. gt_ggc_mx (hash_map<K, V, H> *h)
  246. {
  247. gt_ggc_mx (&h->m_table);
  248. }
  249. template<typename K, typename V, typename H>
  250. static inline void
  251. gt_pch_nx (hash_map<K, V, H> *h)
  252. {
  253. gt_pch_nx (&h->m_table);
  254. }
  255. template<typename K, typename V, typename H>
  256. static inline void
  257. gt_cleare_cache (hash_map<K, V, H> *h)
  258. {
  259. if (h)
  260. gt_cleare_cache (&h->m_table);
  261. }
  262. template<typename K, typename V, typename H>
  263. static inline void
  264. gt_pch_nx (hash_map<K, V, H> *h, gt_pointer_operator op, void *cookie)
  265. {
  266. op (&h->m_table.m_entries, cookie);
  267. }
  268. enum hm_alloc { hm_heap = false, hm_ggc = true };
  269. template<bool ggc, typename K, typename V, typename H>
  270. inline hash_map<K,V,H> *
  271. hash_map_maybe_create (hash_map<K,V,H> *&h,
  272. size_t size = default_hash_map_size)
  273. {
  274. if (!h)
  275. {
  276. if (ggc)
  277. h = hash_map<K,V,H>::create_ggc (size);
  278. else
  279. h = new hash_map<K,V,H> (size);
  280. }
  281. return h;
  282. }
  283. /* Like h->get, but handles null h. */
  284. template<typename K, typename V, typename H>
  285. inline V*
  286. hash_map_safe_get (hash_map<K,V,H> *h, const K& k)
  287. {
  288. return h ? h->get (k) : NULL;
  289. }
  290. /* Like h->get, but handles null h. */
  291. template<bool ggc, typename K, typename V, typename H>
  292. inline V&
  293. hash_map_safe_get_or_insert (hash_map<K,V,H> *&h, const K& k, bool *e = NULL,
  294. size_t size = default_hash_map_size)
  295. {
  296. return hash_map_maybe_create<ggc> (h, size)->get_or_insert (k, e);
  297. }
  298. /* Like h->put, but handles null h. */
  299. template<bool ggc, typename K, typename V, typename H>
  300. inline bool
  301. hash_map_safe_put (hash_map<K,V,H> *&h, const K& k, const V& v,
  302. size_t size = default_hash_map_size)
  303. {
  304. return hash_map_maybe_create<ggc> (h, size)->put (k, v);
  305. }
  306. #endif