You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
8.4KB

  1. // functional_hash.h header -*- C++ -*-
  2. // Copyright (C) 2007-2020 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. #ifdef _GLIBCXX_USE_CHAR8_T
  114. /// Explicit specialization for char8_t.
  115. _Cxx_hashtable_define_trivial_hash(char8_t)
  116. #endif
  117. /// Explicit specialization for char16_t.
  118. _Cxx_hashtable_define_trivial_hash(char16_t)
  119. /// Explicit specialization for char32_t.
  120. _Cxx_hashtable_define_trivial_hash(char32_t)
  121. /// Explicit specialization for short.
  122. _Cxx_hashtable_define_trivial_hash(short)
  123. /// Explicit specialization for int.
  124. _Cxx_hashtable_define_trivial_hash(int)
  125. /// Explicit specialization for long.
  126. _Cxx_hashtable_define_trivial_hash(long)
  127. /// Explicit specialization for long long.
  128. _Cxx_hashtable_define_trivial_hash(long long)
  129. /// Explicit specialization for unsigned short.
  130. _Cxx_hashtable_define_trivial_hash(unsigned short)
  131. /// Explicit specialization for unsigned int.
  132. _Cxx_hashtable_define_trivial_hash(unsigned int)
  133. /// Explicit specialization for unsigned long.
  134. _Cxx_hashtable_define_trivial_hash(unsigned long)
  135. /// Explicit specialization for unsigned long long.
  136. _Cxx_hashtable_define_trivial_hash(unsigned long long)
  137. #ifdef __GLIBCXX_TYPE_INT_N_0
  138. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0)
  139. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0 unsigned)
  140. #endif
  141. #ifdef __GLIBCXX_TYPE_INT_N_1
  142. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1)
  143. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1 unsigned)
  144. #endif
  145. #ifdef __GLIBCXX_TYPE_INT_N_2
  146. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2)
  147. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2 unsigned)
  148. #endif
  149. #ifdef __GLIBCXX_TYPE_INT_N_3
  150. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3)
  151. _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3 unsigned)
  152. #endif
  153. #undef _Cxx_hashtable_define_trivial_hash
  154. struct _Hash_impl
  155. {
  156. static size_t
  157. hash(const void* __ptr, size_t __clength,
  158. size_t __seed = static_cast<size_t>(0xc70f6907UL))
  159. { return _Hash_bytes(__ptr, __clength, __seed); }
  160. template<typename _Tp>
  161. static size_t
  162. hash(const _Tp& __val)
  163. { return hash(&__val, sizeof(__val)); }
  164. template<typename _Tp>
  165. static size_t
  166. __hash_combine(const _Tp& __val, size_t __hash)
  167. { return hash(&__val, sizeof(__val), __hash); }
  168. };
  169. // A hash function similar to FNV-1a (see PR59406 for how it differs).
  170. struct _Fnv_hash_impl
  171. {
  172. static size_t
  173. hash(const void* __ptr, size_t __clength,
  174. size_t __seed = static_cast<size_t>(2166136261UL))
  175. { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
  176. template<typename _Tp>
  177. static size_t
  178. hash(const _Tp& __val)
  179. { return hash(&__val, sizeof(__val)); }
  180. template<typename _Tp>
  181. static size_t
  182. __hash_combine(const _Tp& __val, size_t __hash)
  183. { return hash(&__val, sizeof(__val), __hash); }
  184. };
  185. /// Specialization for float.
  186. template<>
  187. struct hash<float> : public __hash_base<size_t, float>
  188. {
  189. size_t
  190. operator()(float __val) const noexcept
  191. {
  192. // 0 and -0 both hash to zero.
  193. return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
  194. }
  195. };
  196. /// Specialization for double.
  197. template<>
  198. struct hash<double> : public __hash_base<size_t, double>
  199. {
  200. size_t
  201. operator()(double __val) const noexcept
  202. {
  203. // 0 and -0 both hash to zero.
  204. return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
  205. }
  206. };
  207. /// Specialization for long double.
  208. template<>
  209. struct hash<long double>
  210. : public __hash_base<size_t, long double>
  211. {
  212. _GLIBCXX_PURE size_t
  213. operator()(long double __val) const noexcept;
  214. };
  215. #if __cplusplus >= 201703L
  216. template<>
  217. struct hash<nullptr_t> : public __hash_base<size_t, nullptr_t>
  218. {
  219. size_t
  220. operator()(nullptr_t) const noexcept
  221. { return 0; }
  222. };
  223. #endif
  224. // @} group hashes
  225. // Hint about performance of hash functor. If not fast the hash-based
  226. // containers will cache the hash code.
  227. // Default behavior is to consider that hashers are fast unless specified
  228. // otherwise.
  229. template<typename _Hash>
  230. struct __is_fast_hash : public std::true_type
  231. { };
  232. template<>
  233. struct __is_fast_hash<hash<long double>> : public std::false_type
  234. { };
  235. _GLIBCXX_END_NAMESPACE_VERSION
  236. } // namespace
  237. #endif // _FUNCTIONAL_HASH_H