hash-traits.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* Traits for hashable types.
  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_traits_h
  16. #define hash_traits_h
  17. /* Helpful type for removing with free. */
  18. template <typename Type>
  19. struct typed_free_remove
  20. {
  21. static inline void remove (Type *p);
  22. };
  23. /* Remove with free. */
  24. template <typename Type>
  25. inline void
  26. typed_free_remove <Type>::remove (Type *p)
  27. {
  28. free (p);
  29. }
  30. /* Helpful type for removing with delete. */
  31. template <typename Type>
  32. struct typed_delete_remove
  33. {
  34. static inline void remove (Type *p);
  35. };
  36. /* Remove with delete. */
  37. template <typename Type>
  38. inline void
  39. typed_delete_remove <Type>::remove (Type *p)
  40. {
  41. delete p;
  42. }
  43. /* Helpful type for a no-op remove. */
  44. template <typename Type>
  45. struct typed_noop_remove
  46. {
  47. static inline void remove (Type &);
  48. };
  49. /* Remove doing nothing. */
  50. template <typename Type>
  51. inline void
  52. typed_noop_remove <Type>::remove (Type &)
  53. {
  54. }
  55. /* Hasher for integer type Type in which Empty is a spare value that can be
  56. used to mark empty slots. If Deleted != Empty then Deleted is another
  57. spare value that can be used for deleted slots; if Deleted == Empty then
  58. hash table entries cannot be deleted. */
  59. template <typename Type, Type Empty, Type Deleted = Empty>
  60. struct int_hash : typed_noop_remove <Type>
  61. {
  62. typedef Type value_type;
  63. typedef Type compare_type;
  64. static inline hashval_t hash (value_type);
  65. static inline bool equal (value_type existing, value_type candidate);
  66. static inline void mark_deleted (Type &);
  67. static inline void mark_empty (Type &);
  68. static inline bool is_deleted (Type);
  69. static inline bool is_empty (Type);
  70. };
  71. template <typename Type, Type Empty, Type Deleted>
  72. inline hashval_t
  73. int_hash <Type, Empty, Deleted>::hash (value_type x)
  74. {
  75. return x;
  76. }
  77. template <typename Type, Type Empty, Type Deleted>
  78. inline bool
  79. int_hash <Type, Empty, Deleted>::equal (value_type x, value_type y)
  80. {
  81. return x == y;
  82. }
  83. template <typename Type, Type Empty, Type Deleted>
  84. inline void
  85. int_hash <Type, Empty, Deleted>::mark_deleted (Type &x)
  86. {
  87. gcc_assert (Empty != Deleted);
  88. x = Deleted;
  89. }
  90. template <typename Type, Type Empty, Type Deleted>
  91. inline void
  92. int_hash <Type, Empty, Deleted>::mark_empty (Type &x)
  93. {
  94. x = Empty;
  95. }
  96. template <typename Type, Type Empty, Type Deleted>
  97. inline bool
  98. int_hash <Type, Empty, Deleted>::is_deleted (Type x)
  99. {
  100. return Empty != Deleted && x == Deleted;
  101. }
  102. template <typename Type, Type Empty, Type Deleted>
  103. inline bool
  104. int_hash <Type, Empty, Deleted>::is_empty (Type x)
  105. {
  106. return x == Empty;
  107. }
  108. /* Pointer hasher based on pointer equality. Other types of pointer hash
  109. can inherit this and override the hash and equal functions with some
  110. other form of equality (such as string equality). */
  111. template <typename Type>
  112. struct pointer_hash
  113. {
  114. typedef Type *value_type;
  115. typedef Type *compare_type;
  116. static inline hashval_t hash (const value_type &);
  117. static inline bool equal (const value_type &existing,
  118. const compare_type &candidate);
  119. static inline void mark_deleted (Type *&);
  120. static inline void mark_empty (Type *&);
  121. static inline bool is_deleted (Type *);
  122. static inline bool is_empty (Type *);
  123. };
  124. template <typename Type>
  125. inline hashval_t
  126. pointer_hash <Type>::hash (const value_type &candidate)
  127. {
  128. /* This is a really poor hash function, but it is what the current code uses,
  129. so I am reusing it to avoid an additional axis in testing. */
  130. return (hashval_t) ((intptr_t)candidate >> 3);
  131. }
  132. template <typename Type>
  133. inline bool
  134. pointer_hash <Type>::equal (const value_type &existing,
  135. const compare_type &candidate)
  136. {
  137. return existing == candidate;
  138. }
  139. template <typename Type>
  140. inline void
  141. pointer_hash <Type>::mark_deleted (Type *&e)
  142. {
  143. e = reinterpret_cast<Type *> (1);
  144. }
  145. template <typename Type>
  146. inline void
  147. pointer_hash <Type>::mark_empty (Type *&e)
  148. {
  149. e = NULL;
  150. }
  151. template <typename Type>
  152. inline bool
  153. pointer_hash <Type>::is_deleted (Type *e)
  154. {
  155. return e == reinterpret_cast<Type *> (1);
  156. }
  157. template <typename Type>
  158. inline bool
  159. pointer_hash <Type>::is_empty (Type *e)
  160. {
  161. return e == NULL;
  162. }
  163. /* Hasher for "const char *" strings, using string rather than pointer
  164. equality. */
  165. struct string_hash : pointer_hash <const char>
  166. {
  167. static inline hashval_t hash (const char *);
  168. static inline bool equal (const char *, const char *);
  169. };
  170. inline hashval_t
  171. string_hash::hash (const char *id)
  172. {
  173. return htab_hash_string (id);
  174. }
  175. inline bool
  176. string_hash::equal (const char *id1, const char *id2)
  177. {
  178. return strcmp (id1, id2) == 0;
  179. }
  180. /* Remover and marker for entries in gc memory. */
  181. template<typename T>
  182. struct ggc_remove
  183. {
  184. static void remove (T &) {}
  185. static void
  186. ggc_mx (T &p)
  187. {
  188. extern void gt_ggc_mx (T &);
  189. gt_ggc_mx (p);
  190. }
  191. /* Overridden in ggc_cache_remove. */
  192. static void
  193. ggc_maybe_mx (T &p)
  194. {
  195. ggc_mx (p);
  196. }
  197. static void
  198. pch_nx (T &p)
  199. {
  200. extern void gt_pch_nx (T &);
  201. gt_pch_nx (p);
  202. }
  203. static void
  204. pch_nx (T &p, gt_pointer_operator op, void *cookie)
  205. {
  206. op (&p, cookie);
  207. }
  208. };
  209. /* Remover and marker for "cache" entries in gc memory. These entries can
  210. be deleted if there are no non-cache references to the data. */
  211. template<typename T>
  212. struct ggc_cache_remove : ggc_remove<T>
  213. {
  214. /* Entries are weakly held because this is for caches. */
  215. static void ggc_maybe_mx (T &) {}
  216. static int
  217. keep_cache_entry (T &e)
  218. {
  219. return ggc_marked_p (e) ? -1 : 0;
  220. }
  221. };
  222. /* Traits for pointer elements that should not be freed when an element
  223. is deleted. */
  224. template <typename T>
  225. struct nofree_ptr_hash : pointer_hash <T>, typed_noop_remove <T *> {};
  226. /* Traits for pointer elements that should be freed via free() when an
  227. element is deleted. */
  228. template <typename T>
  229. struct free_ptr_hash : pointer_hash <T>, typed_free_remove <T> {};
  230. /* Traits for pointer elements that should be freed via delete operand when an
  231. element is deleted. */
  232. template <typename T>
  233. struct delete_ptr_hash : pointer_hash <T>, typed_delete_remove <T> {};
  234. /* Traits for elements that point to gc memory. The pointed-to data
  235. must be kept across collections. */
  236. template <typename T>
  237. struct ggc_ptr_hash : pointer_hash <T>, ggc_remove <T *> {};
  238. /* Traits for elements that point to gc memory. The elements don't
  239. in themselves keep the pointed-to data alive and they can be deleted
  240. if the pointed-to data is going to be collected. */
  241. template <typename T>
  242. struct ggc_cache_ptr_hash : pointer_hash <T>, ggc_cache_remove <T *> {};
  243. /* Traits for string elements that should not be freed when an element
  244. is deleted. */
  245. struct nofree_string_hash : string_hash, typed_noop_remove <const char *> {};
  246. /* Traits for pairs of values, using the first to record empty and
  247. deleted slots. */
  248. template <typename T1, typename T2>
  249. struct pair_hash
  250. {
  251. typedef std::pair <typename T1::value_type,
  252. typename T2::value_type> value_type;
  253. typedef std::pair <typename T1::compare_type,
  254. typename T2::compare_type> compare_type;
  255. static inline hashval_t hash (const value_type &);
  256. static inline bool equal (const value_type &, const compare_type &);
  257. static inline void remove (value_type &);
  258. static inline void mark_deleted (value_type &);
  259. static inline void mark_empty (value_type &);
  260. static inline bool is_deleted (const value_type &);
  261. static inline bool is_empty (const value_type &);
  262. };
  263. template <typename T1, typename T2>
  264. inline hashval_t
  265. pair_hash <T1, T2>::hash (const value_type &x)
  266. {
  267. return iterative_hash_hashval_t (T1::hash (x.first), T2::hash (x.second));
  268. }
  269. template <typename T1, typename T2>
  270. inline bool
  271. pair_hash <T1, T2>::equal (const value_type &x, const compare_type &y)
  272. {
  273. return T1::equal (x.first, y.first) && T2::equal (x.second, y.second);
  274. }
  275. template <typename T1, typename T2>
  276. inline void
  277. pair_hash <T1, T2>::remove (value_type &x)
  278. {
  279. T1::remove (x.first);
  280. T2::remove (x.second);
  281. }
  282. template <typename T1, typename T2>
  283. inline void
  284. pair_hash <T1, T2>::mark_deleted (value_type &x)
  285. {
  286. T1::mark_deleted (x.first);
  287. }
  288. template <typename T1, typename T2>
  289. inline void
  290. pair_hash <T1, T2>::mark_empty (value_type &x)
  291. {
  292. T1::mark_empty (x.first);
  293. }
  294. template <typename T1, typename T2>
  295. inline bool
  296. pair_hash <T1, T2>::is_deleted (const value_type &x)
  297. {
  298. return T1::is_deleted (x.first);
  299. }
  300. template <typename T1, typename T2>
  301. inline bool
  302. pair_hash <T1, T2>::is_empty (const value_type &x)
  303. {
  304. return T1::is_empty (x.first);
  305. }
  306. template <typename T> struct default_hash_traits : T {};
  307. template <typename T>
  308. struct default_hash_traits <T *> : ggc_ptr_hash <T> {};
  309. #endif