functional_hash.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // functional_hash.h header -*- C++ -*-
  2. // Copyright (C) 2007-2022 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 <type_traits>
  28. #include <bits/hash_bytes.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  32. /** @defgroup hashes Hashes
  33. * @ingroup functors
  34. *
  35. * Hashing functors taking a variable type and returning a @c std::size_t.
  36. *
  37. * @{
  38. */
  39. template<typename _Result, typename _Arg>
  40. struct __hash_base
  41. {
  42. typedef _Result result_type _GLIBCXX17_DEPRECATED;
  43. typedef _Arg argument_type _GLIBCXX17_DEPRECATED;
  44. };
  45. /// Primary class template hash.
  46. template<typename _Tp>
  47. struct hash;
  48. template<typename _Tp, typename = void>
  49. struct __poison_hash
  50. {
  51. static constexpr bool __enable_hash_call = false;
  52. private:
  53. // Private rather than deleted to be non-trivially-copyable.
  54. __poison_hash(__poison_hash&&);
  55. ~__poison_hash();
  56. };
  57. template<typename _Tp>
  58. struct __poison_hash<_Tp, __void_t<decltype(hash<_Tp>()(declval<_Tp>()))>>
  59. {
  60. static constexpr bool __enable_hash_call = true;
  61. };
  62. // Helper struct for SFINAE-poisoning non-enum types.
  63. template<typename _Tp, bool = is_enum<_Tp>::value>
  64. struct __hash_enum
  65. {
  66. private:
  67. // Private rather than deleted to be non-trivially-copyable.
  68. __hash_enum(__hash_enum&&);
  69. ~__hash_enum();
  70. };
  71. // Helper struct for hash with enum types.
  72. template<typename _Tp>
  73. struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp>
  74. {
  75. size_t
  76. operator()(_Tp __val) const noexcept
  77. {
  78. using __type = typename underlying_type<_Tp>::type;
  79. return hash<__type>{}(static_cast<__type>(__val));
  80. }
  81. };
  82. /// Primary class template hash, usable for enum types only.
  83. // Use with non-enum types still SFINAES.
  84. template<typename _Tp>
  85. struct hash : __hash_enum<_Tp>
  86. { };
  87. /// Partial specializations for pointer types.
  88. template<typename _Tp>
  89. struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
  90. {
  91. size_t
  92. operator()(_Tp* __p) const noexcept
  93. { return reinterpret_cast<size_t>(__p); }
  94. };
  95. // Explicit specializations for integer types.
  96. #define _Cxx_hashtable_define_trivial_hash(_Tp) \
  97. template<> \
  98. struct hash<_Tp> : public __hash_base<size_t, _Tp> \
  99. { \
  100. size_t \
  101. operator()(_Tp __val) const noexcept \
  102. { return static_cast<size_t>(__val); } \
  103. };
  104. /// Explicit specialization for bool.
  105. _Cxx_hashtable_define_trivial_hash(bool)
  106. /// Explicit specialization for char.
  107. _Cxx_hashtable_define_trivial_hash(char)
  108. /// Explicit specialization for signed char.
  109. _Cxx_hashtable_define_trivial_hash(signed char)
  110. /// Explicit specialization for unsigned char.
  111. _Cxx_hashtable_define_trivial_hash(unsigned char)
  112. /// Explicit specialization for wchar_t.
  113. _Cxx_hashtable_define_trivial_hash(wchar_t)
  114. #ifdef _GLIBCXX_USE_CHAR8_T
  115. /// Explicit specialization for char8_t.
  116. _Cxx_hashtable_define_trivial_hash(char8_t)
  117. #endif
  118. /// Explicit specialization for char16_t.
  119. _Cxx_hashtable_define_trivial_hash(char16_t)
  120. /// Explicit specialization for char32_t.
  121. _Cxx_hashtable_define_trivial_hash(char32_t)
  122. /// Explicit specialization for short.
  123. _Cxx_hashtable_define_trivial_hash(short)
  124. /// Explicit specialization for int.
  125. _Cxx_hashtable_define_trivial_hash(int)
  126. /// Explicit specialization for long.
  127. _Cxx_hashtable_define_trivial_hash(long)
  128. /// Explicit specialization for long long.
  129. _Cxx_hashtable_define_trivial_hash(long long)
  130. /// Explicit specialization for unsigned short.
  131. _Cxx_hashtable_define_trivial_hash(unsigned short)
  132. /// Explicit specialization for unsigned int.
  133. _Cxx_hashtable_define_trivial_hash(unsigned int)
  134. /// Explicit specialization for unsigned long.
  135. _Cxx_hashtable_define_trivial_hash(unsigned long)
  136. /// Explicit specialization for unsigned long long.
  137. _Cxx_hashtable_define_trivial_hash(unsigned long long)
  138. #ifdef __GLIBCXX_TYPE_INT_N_0
  139. __extension__
  140. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0)
  141. __extension__
  142. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0 unsigned)
  143. #endif
  144. #ifdef __GLIBCXX_TYPE_INT_N_1
  145. __extension__
  146. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1)
  147. __extension__
  148. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1 unsigned)
  149. #endif
  150. #ifdef __GLIBCXX_TYPE_INT_N_2
  151. __extension__
  152. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2)
  153. __extension__
  154. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2 unsigned)
  155. #endif
  156. #ifdef __GLIBCXX_TYPE_INT_N_3
  157. __extension__
  158. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3)
  159. __extension__
  160. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3 unsigned)
  161. #endif
  162. #undef _Cxx_hashtable_define_trivial_hash
  163. struct _Hash_impl
  164. {
  165. static size_t
  166. hash(const void* __ptr, size_t __clength,
  167. size_t __seed = static_cast<size_t>(0xc70f6907UL))
  168. { return _Hash_bytes(__ptr, __clength, __seed); }
  169. template<typename _Tp>
  170. static size_t
  171. hash(const _Tp& __val)
  172. { return hash(&__val, sizeof(__val)); }
  173. template<typename _Tp>
  174. static size_t
  175. __hash_combine(const _Tp& __val, size_t __hash)
  176. { return hash(&__val, sizeof(__val), __hash); }
  177. };
  178. // A hash function similar to FNV-1a (see PR59406 for how it differs).
  179. struct _Fnv_hash_impl
  180. {
  181. static size_t
  182. hash(const void* __ptr, size_t __clength,
  183. size_t __seed = static_cast<size_t>(2166136261UL))
  184. { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
  185. template<typename _Tp>
  186. static size_t
  187. hash(const _Tp& __val)
  188. { return hash(&__val, sizeof(__val)); }
  189. template<typename _Tp>
  190. static size_t
  191. __hash_combine(const _Tp& __val, size_t __hash)
  192. { return hash(&__val, sizeof(__val), __hash); }
  193. };
  194. /// Specialization for float.
  195. template<>
  196. struct hash<float> : public __hash_base<size_t, float>
  197. {
  198. size_t
  199. operator()(float __val) const noexcept
  200. {
  201. // 0 and -0 both hash to zero.
  202. return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
  203. }
  204. };
  205. /// Specialization for double.
  206. template<>
  207. struct hash<double> : public __hash_base<size_t, double>
  208. {
  209. size_t
  210. operator()(double __val) const noexcept
  211. {
  212. // 0 and -0 both hash to zero.
  213. return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
  214. }
  215. };
  216. /// Specialization for long double.
  217. template<>
  218. struct hash<long double>
  219. : public __hash_base<size_t, long double>
  220. {
  221. _GLIBCXX_PURE size_t
  222. operator()(long double __val) const noexcept;
  223. };
  224. #if __cplusplus >= 201703L
  225. template<>
  226. struct hash<nullptr_t> : public __hash_base<size_t, nullptr_t>
  227. {
  228. size_t
  229. operator()(nullptr_t) const noexcept
  230. { return 0; }
  231. };
  232. #endif
  233. /// @} group hashes
  234. // Hint about performance of hash functor. If not fast the hash-based
  235. // containers will cache the hash code.
  236. // Default behavior is to consider that hashers are fast unless specified
  237. // otherwise.
  238. template<typename _Hash>
  239. struct __is_fast_hash : public std::true_type
  240. { };
  241. template<>
  242. struct __is_fast_hash<hash<long double>> : public std::false_type
  243. { };
  244. _GLIBCXX_END_NAMESPACE_VERSION
  245. } // namespace
  246. #endif // _FUNCTIONAL_HASH_H