hash-map.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* A type-safe hash map.
  2. Copyright (C) 2014-2019 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. template<typename KeyId, typename Value,
  18. typename Traits>
  19. class GTY((user)) hash_map
  20. {
  21. typedef typename Traits::key_type Key;
  22. struct hash_entry
  23. {
  24. Key m_key;
  25. Value m_value;
  26. typedef hash_entry value_type;
  27. typedef Key compare_type;
  28. static hashval_t hash (const hash_entry &e)
  29. {
  30. return Traits::hash (e.m_key);
  31. }
  32. static bool equal (const hash_entry &a, const Key &b)
  33. {
  34. return Traits::equal_keys (a.m_key, b);
  35. }
  36. static void remove (hash_entry &e) { Traits::remove (e); }
  37. static void mark_deleted (hash_entry &e) { Traits::mark_deleted (e); }
  38. static bool is_deleted (const hash_entry &e)
  39. {
  40. return Traits::is_deleted (e);
  41. }
  42. static void mark_empty (hash_entry &e) { Traits::mark_empty (e); }
  43. static bool is_empty (const hash_entry &e) { return Traits::is_empty (e); }
  44. static void ggc_mx (hash_entry &e)
  45. {
  46. gt_ggc_mx (e.m_key);
  47. gt_ggc_mx (e.m_value);
  48. }
  49. static void ggc_maybe_mx (hash_entry &e)
  50. {
  51. if (Traits::maybe_mx)
  52. ggc_mx (e);
  53. }
  54. static void pch_nx (hash_entry &e)
  55. {
  56. gt_pch_nx (e.m_key);
  57. gt_pch_nx (e.m_value);
  58. }
  59. static void pch_nx (hash_entry &e, gt_pointer_operator op, void *c)
  60. {
  61. pch_nx_helper (e.m_key, op, c);
  62. pch_nx_helper (e.m_value, op, c);
  63. }
  64. static int keep_cache_entry (hash_entry &e)
  65. {
  66. return ggc_marked_p (e.m_key);
  67. }
  68. private:
  69. template<typename T>
  70. static void
  71. pch_nx_helper (T &x, gt_pointer_operator op, void *cookie)
  72. {
  73. gt_pch_nx (&x, op, cookie);
  74. }
  75. static void
  76. pch_nx_helper (int, gt_pointer_operator, void *)
  77. {
  78. }
  79. static void
  80. pch_nx_helper (unsigned int, gt_pointer_operator, void *)
  81. {
  82. }
  83. static void
  84. pch_nx_helper (bool, gt_pointer_operator, void *)
  85. {
  86. }
  87. template<typename T>
  88. static void
  89. pch_nx_helper (T *&x, gt_pointer_operator op, void *cookie)
  90. {
  91. op (&x, cookie);
  92. }
  93. };
  94. public:
  95. explicit hash_map (size_t n = 13, bool ggc = false,
  96. bool gather_mem_stats = GATHER_STATISTICS
  97. CXX_MEM_STAT_INFO)
  98. : m_table (n, ggc, gather_mem_stats, HASH_MAP_ORIGIN PASS_MEM_STAT) {}
  99. explicit hash_map (const hash_map &h, bool ggc = false,
  100. bool gather_mem_stats = GATHER_STATISTICS
  101. CXX_MEM_STAT_INFO)
  102. : m_table (h.m_table, ggc, gather_mem_stats,
  103. HASH_MAP_ORIGIN PASS_MEM_STAT) {}
  104. /* Create a hash_map in ggc memory. */
  105. static hash_map *create_ggc (size_t size,
  106. bool gather_mem_stats = GATHER_STATISTICS
  107. CXX_MEM_STAT_INFO)
  108. {
  109. hash_map *map = ggc_alloc<hash_map> ();
  110. new (map) hash_map (size, true, gather_mem_stats PASS_MEM_STAT);
  111. return map;
  112. }
  113. /* If key k isn't already in the map add key k with value v to the map, and
  114. return false. Otherwise set the value of the entry for key k to be v and
  115. return true. */
  116. bool put (const Key &k, const Value &v)
  117. {
  118. hash_entry *e = m_table.find_slot_with_hash (k, Traits::hash (k),
  119. INSERT);
  120. bool existed = !hash_entry::is_empty (*e);
  121. if (!existed)
  122. e->m_key = k;
  123. e->m_value = v;
  124. return existed;
  125. }
  126. /* if the passed in key is in the map return its value otherwise NULL. */
  127. Value *get (const Key &k)
  128. {
  129. hash_entry &e = m_table.find_with_hash (k, Traits::hash (k));
  130. return Traits::is_empty (e) ? NULL : &e.m_value;
  131. }
  132. /* Return a reference to the value for the passed in key, creating the entry
  133. if it doesn't already exist. If existed is not NULL then it is set to false
  134. if the key was not previously in the map, and true otherwise. */
  135. Value &get_or_insert (const Key &k, bool *existed = NULL)
  136. {
  137. hash_entry *e = m_table.find_slot_with_hash (k, Traits::hash (k),
  138. INSERT);
  139. bool ins = Traits::is_empty (*e);
  140. if (ins)
  141. e->m_key = k;
  142. if (existed != NULL)
  143. *existed = !ins;
  144. return e->m_value;
  145. }
  146. void remove (const Key &k)
  147. {
  148. m_table.remove_elt_with_hash (k, Traits::hash (k));
  149. }
  150. /* Call the call back on each pair of key and value with the passed in
  151. arg. */
  152. template<typename Arg, bool (*f)(const typename Traits::key_type &,
  153. const Value &, Arg)>
  154. void traverse (Arg a) const
  155. {
  156. for (typename hash_table<hash_entry>::iterator iter = m_table.begin ();
  157. iter != m_table.end (); ++iter)
  158. f ((*iter).m_key, (*iter).m_value, a);
  159. }
  160. template<typename Arg, bool (*f)(const typename Traits::key_type &,
  161. Value *, Arg)>
  162. void traverse (Arg a) const
  163. {
  164. for (typename hash_table<hash_entry>::iterator iter = m_table.begin ();
  165. iter != m_table.end (); ++iter)
  166. if (!f ((*iter).m_key, &(*iter).m_value, a))
  167. break;
  168. }
  169. size_t elements () const { return m_table.elements (); }
  170. void empty () { m_table.empty(); }
  171. class iterator
  172. {
  173. public:
  174. explicit iterator (const typename hash_table<hash_entry>::iterator &iter) :
  175. m_iter (iter) {}
  176. iterator &operator++ ()
  177. {
  178. ++m_iter;
  179. return *this;
  180. }
  181. /* Can't use std::pair here, because GCC before 4.3 don't handle
  182. std::pair where template parameters are references well.
  183. See PR86739. */
  184. struct reference_pair {
  185. const Key &first;
  186. Value &second;
  187. reference_pair (const Key &key, Value &value) : first (key), second (value) {}
  188. template <typename K, typename V>
  189. operator std::pair<K, V> () const { return std::pair<K, V> (first, second); }
  190. };
  191. reference_pair operator* ()
  192. {
  193. hash_entry &e = *m_iter;
  194. return reference_pair (e.m_key, e.m_value);
  195. }
  196. bool
  197. operator != (const iterator &other) const
  198. {
  199. return m_iter != other.m_iter;
  200. }
  201. private:
  202. typename hash_table<hash_entry>::iterator m_iter;
  203. };
  204. /* Standard iterator retrieval methods. */
  205. iterator begin () const { return iterator (m_table.begin ()); }
  206. iterator end () const { return iterator (m_table.end ()); }
  207. private:
  208. template<typename T, typename U, typename V> friend void gt_ggc_mx (hash_map<T, U, V> *);
  209. template<typename T, typename U, typename V> friend void gt_pch_nx (hash_map<T, U, V> *);
  210. template<typename T, typename U, typename V> friend void gt_pch_nx (hash_map<T, U, V> *, gt_pointer_operator, void *);
  211. template<typename T, typename U, typename V> friend void gt_cleare_cache (hash_map<T, U, V> *);
  212. hash_table<hash_entry> m_table;
  213. };
  214. /* ggc marking routines. */
  215. template<typename K, typename V, typename H>
  216. static inline void
  217. gt_ggc_mx (hash_map<K, V, H> *h)
  218. {
  219. gt_ggc_mx (&h->m_table);
  220. }
  221. template<typename K, typename V, typename H>
  222. static inline void
  223. gt_pch_nx (hash_map<K, V, H> *h)
  224. {
  225. gt_pch_nx (&h->m_table);
  226. }
  227. template<typename K, typename V, typename H>
  228. static inline void
  229. gt_cleare_cache (hash_map<K, V, H> *h)
  230. {
  231. if (h)
  232. gt_cleare_cache (&h->m_table);
  233. }
  234. template<typename K, typename V, typename H>
  235. static inline void
  236. gt_pch_nx (hash_map<K, V, H> *h, gt_pointer_operator op, void *cookie)
  237. {
  238. op (&h->m_table.m_entries, cookie);
  239. }
  240. #endif