hash-map.h 7.8 KB

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