functional_hash.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // functional_hash.h header -*- C++ -*-
  2. // Copyright (C) 2007-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/functional_hash.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{functional}
  23. */
  24. #ifndef _FUNCTIONAL_HASH_H
  25. #define _FUNCTIONAL_HASH_H 1
  26. #pragma GCC system_header
  27. #include <bits/hash_bytes.h>
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. /** @defgroup hashes Hashes
  32. * @ingroup functors
  33. *
  34. * Hashing functors taking a variable type and returning a @c std::size_t.
  35. *
  36. * @{
  37. */
  38. template<typename _Result, typename _Arg>
  39. struct __hash_base
  40. {
  41. typedef _Result result_type _GLIBCXX17_DEPRECATED;
  42. typedef _Arg argument_type _GLIBCXX17_DEPRECATED;
  43. };
  44. /// Primary class template hash.
  45. template<typename _Tp>
  46. struct hash;
  47. template<typename _Tp, typename = void>
  48. struct __poison_hash
  49. {
  50. static constexpr bool __enable_hash_call = false;
  51. private:
  52. // Private rather than deleted to be non-trivially-copyable.
  53. __poison_hash(__poison_hash&&);
  54. ~__poison_hash();
  55. };
  56. template<typename _Tp>
  57. struct __poison_hash<_Tp, __void_t<decltype(hash<_Tp>()(declval<_Tp>()))>>
  58. {
  59. static constexpr bool __enable_hash_call = true;
  60. };
  61. // Helper struct for SFINAE-poisoning non-enum types.
  62. template<typename _Tp, bool = is_enum<_Tp>::value>
  63. struct __hash_enum
  64. {
  65. private:
  66. // Private rather than deleted to be non-trivially-copyable.
  67. __hash_enum(__hash_enum&&);
  68. ~__hash_enum();
  69. };
  70. // Helper struct for hash with enum types.
  71. template<typename _Tp>
  72. struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp>
  73. {
  74. size_t
  75. operator()(_Tp __val) const noexcept
  76. {
  77. using __type = typename underlying_type<_Tp>::type;
  78. return hash<__type>{}(static_cast<__type>(__val));
  79. }
  80. };
  81. /// Primary class template hash, usable for enum types only.
  82. // Use with non-enum types still SFINAES.
  83. template<typename _Tp>
  84. struct hash : __hash_enum<_Tp>
  85. { };
  86. /// Partial specializations for pointer types.
  87. template<typename _Tp>
  88. struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
  89. {
  90. size_t
  91. operator()(_Tp* __p) const noexcept
  92. { return reinterpret_cast<size_t>(__p); }
  93. };
  94. // Explicit specializations for integer types.
  95. #define _Cxx_hashtable_define_trivial_hash(_Tp) \
  96. template<> \
  97. struct hash<_Tp> : public __hash_base<size_t, _Tp> \
  98. { \
  99. size_t \
  100. operator()(_Tp __val) const noexcept \
  101. { return static_cast<size_t>(__val); } \
  102. };
  103. /// Explicit specialization for bool.
  104. _Cxx_hashtable_define_trivial_hash(bool)
  105. /// Explicit specialization for char.
  106. _Cxx_hashtable_define_trivial_hash(char)
  107. /// Explicit specialization for signed char.
  108. _Cxx_hashtable_define_trivial_hash(signed char)
  109. /// Explicit specialization for unsigned char.
  110. _Cxx_hashtable_define_trivial_hash(unsigned char)
  111. /// Explicit specialization for wchar_t.
  112. _Cxx_hashtable_define_trivial_hash(wchar_t)
  113. /// Explicit specialization for char16_t.
  114. _Cxx_hashtable_define_trivial_hash(char16_t)
  115. /// Explicit specialization for char32_t.
  116. _Cxx_hashtable_define_trivial_hash(char32_t)
  117. /// Explicit specialization for short.
  118. _Cxx_hashtable_define_trivial_hash(short)
  119. /// Explicit specialization for int.
  120. _Cxx_hashtable_define_trivial_hash(int)
  121. /// Explicit specialization for long.
  122. _Cxx_hashtable_define_trivial_hash(long)
  123. /// Explicit specialization for long long.
  124. _Cxx_hashtable_define_trivial_hash(long long)
  125. /// Explicit specialization for unsigned short.
  126. _Cxx_hashtable_define_trivial_hash(unsigned short)
  127. /// Explicit specialization for unsigned int.
  128. _Cxx_hashtable_define_trivial_hash(unsigned int)
  129. /// Explicit specialization for unsigned long.
  130. _Cxx_hashtable_define_trivial_hash(unsigned long)
  131. /// Explicit specialization for unsigned long long.
  132. _Cxx_hashtable_define_trivial_hash(unsigned long long)
  133. #ifdef __GLIBCXX_TYPE_INT_N_0
  134. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0)
  135. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0 unsigned)
  136. #endif
  137. #ifdef __GLIBCXX_TYPE_INT_N_1
  138. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1)
  139. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1 unsigned)
  140. #endif
  141. #ifdef __GLIBCXX_TYPE_INT_N_2
  142. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2)
  143. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2 unsigned)
  144. #endif
  145. #ifdef __GLIBCXX_TYPE_INT_N_3
  146. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3)
  147. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3 unsigned)
  148. #endif
  149. #undef _Cxx_hashtable_define_trivial_hash
  150. struct _Hash_impl
  151. {
  152. static size_t
  153. hash(const void* __ptr, size_t __clength,
  154. size_t __seed = static_cast<size_t>(0xc70f6907UL))
  155. { return _Hash_bytes(__ptr, __clength, __seed); }
  156. template<typename _Tp>
  157. static size_t
  158. hash(const _Tp& __val)
  159. { return hash(&__val, sizeof(__val)); }
  160. template<typename _Tp>
  161. static size_t
  162. __hash_combine(const _Tp& __val, size_t __hash)
  163. { return hash(&__val, sizeof(__val), __hash); }
  164. };
  165. // A hash function similar to FNV-1a (see PR59406 for how it differs).
  166. struct _Fnv_hash_impl
  167. {
  168. static size_t
  169. hash(const void* __ptr, size_t __clength,
  170. size_t __seed = static_cast<size_t>(2166136261UL))
  171. { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
  172. template<typename _Tp>
  173. static size_t
  174. hash(const _Tp& __val)
  175. { return hash(&__val, sizeof(__val)); }
  176. template<typename _Tp>
  177. static size_t
  178. __hash_combine(const _Tp& __val, size_t __hash)
  179. { return hash(&__val, sizeof(__val), __hash); }
  180. };
  181. /// Specialization for float.
  182. template<>
  183. struct hash<float> : public __hash_base<size_t, float>
  184. {
  185. size_t
  186. operator()(float __val) const noexcept
  187. {
  188. // 0 and -0 both hash to zero.
  189. return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
  190. }
  191. };
  192. /// Specialization for double.
  193. template<>
  194. struct hash<double> : public __hash_base<size_t, double>
  195. {
  196. size_t
  197. operator()(double __val) const noexcept
  198. {
  199. // 0 and -0 both hash to zero.
  200. return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
  201. }
  202. };
  203. /// Specialization for long double.
  204. template<>
  205. struct hash<long double>
  206. : public __hash_base<size_t, long double>
  207. {
  208. _GLIBCXX_PURE size_t
  209. operator()(long double __val) const noexcept;
  210. };
  211. // @} group hashes
  212. // Hint about performance of hash functor. If not fast the hash-based
  213. // containers will cache the hash code.
  214. // Default behavior is to consider that hashers are fast unless specified
  215. // otherwise.
  216. template<typename _Hash>
  217. struct __is_fast_hash : public std::true_type
  218. { };
  219. template<>
  220. struct __is_fast_hash<hash<long double>> : public std::false_type
  221. { };
  222. _GLIBCXX_END_NAMESPACE_VERSION
  223. } // namespace
  224. #endif // _FUNCTIONAL_HASH_H