functional_hash.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // TR1 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 tr1/functional_hash.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{tr1/functional}
  23. */
  24. #ifndef _GLIBCXX_TR1_FUNCTIONAL_HASH_H
  25. #define _GLIBCXX_TR1_FUNCTIONAL_HASH_H 1
  26. #pragma GCC system_header
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. namespace tr1
  31. {
  32. // Ignore warnings about std::unary_function and std::binary_function.
  33. #pragma GCC diagnostic push
  34. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  35. /// Class template hash.
  36. // Declaration of default hash functor std::tr1::hash. The types for
  37. // which std::tr1::hash<T> is well-defined is in clause 6.3.3. of the PDTR.
  38. template<typename _Tp>
  39. struct hash : public std::unary_function<_Tp, size_t>
  40. {
  41. size_t
  42. operator()(_Tp __val) const;
  43. };
  44. /// Partial specializations for pointer types.
  45. template<typename _Tp>
  46. struct hash<_Tp*> : public std::unary_function<_Tp*, size_t>
  47. {
  48. size_t
  49. operator()(_Tp* __p) const
  50. { return reinterpret_cast<size_t>(__p); }
  51. };
  52. #pragma GCC diagnostic pop
  53. /// Explicit specializations for integer types.
  54. #define _TR1_hashtable_define_trivial_hash(_Tp) \
  55. template<> \
  56. inline size_t \
  57. hash<_Tp>::operator()(_Tp __val) const \
  58. { return static_cast<size_t>(__val); }
  59. _TR1_hashtable_define_trivial_hash(bool);
  60. _TR1_hashtable_define_trivial_hash(char);
  61. _TR1_hashtable_define_trivial_hash(signed char);
  62. _TR1_hashtable_define_trivial_hash(unsigned char);
  63. _TR1_hashtable_define_trivial_hash(wchar_t);
  64. _TR1_hashtable_define_trivial_hash(short);
  65. _TR1_hashtable_define_trivial_hash(int);
  66. _TR1_hashtable_define_trivial_hash(long);
  67. _TR1_hashtable_define_trivial_hash(long long);
  68. _TR1_hashtable_define_trivial_hash(unsigned short);
  69. _TR1_hashtable_define_trivial_hash(unsigned int);
  70. _TR1_hashtable_define_trivial_hash(unsigned long);
  71. _TR1_hashtable_define_trivial_hash(unsigned long long);
  72. #undef _TR1_hashtable_define_trivial_hash
  73. // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
  74. // (Used by the next specializations of std::tr1::hash.)
  75. // N.B. These functions should work on unsigned char, otherwise they do not
  76. // correctly implement the FNV-1a algorithm (see PR59406).
  77. // The existing behaviour is retained for backwards compatibility.
  78. /// Dummy generic implementation (for sizeof(size_t) != 4, 8).
  79. template<size_t>
  80. struct _Fnv_hash_base
  81. {
  82. template<typename _Tp>
  83. static size_t
  84. hash(const _Tp* __ptr, size_t __clength)
  85. {
  86. size_t __result = 0;
  87. const char* __cptr = reinterpret_cast<const char*>(__ptr);
  88. for (; __clength; --__clength)
  89. __result = (__result * 131) + *__cptr++;
  90. return __result;
  91. }
  92. };
  93. template<>
  94. struct _Fnv_hash_base<4>
  95. {
  96. template<typename _Tp>
  97. static size_t
  98. hash(const _Tp* __ptr, size_t __clength)
  99. {
  100. size_t __result = static_cast<size_t>(2166136261UL);
  101. const char* __cptr = reinterpret_cast<const char*>(__ptr);
  102. for (; __clength; --__clength)
  103. {
  104. __result ^= static_cast<size_t>(*__cptr++);
  105. __result *= static_cast<size_t>(16777619UL);
  106. }
  107. return __result;
  108. }
  109. };
  110. template<>
  111. struct _Fnv_hash_base<8>
  112. {
  113. template<typename _Tp>
  114. static size_t
  115. hash(const _Tp* __ptr, size_t __clength)
  116. {
  117. size_t __result
  118. = static_cast<size_t>(14695981039346656037ULL);
  119. const char* __cptr = reinterpret_cast<const char*>(__ptr);
  120. for (; __clength; --__clength)
  121. {
  122. __result ^= static_cast<size_t>(*__cptr++);
  123. __result *= static_cast<size_t>(1099511628211ULL);
  124. }
  125. return __result;
  126. }
  127. };
  128. struct _Fnv_hash
  129. : public _Fnv_hash_base<sizeof(size_t)>
  130. {
  131. using _Fnv_hash_base<sizeof(size_t)>::hash;
  132. template<typename _Tp>
  133. static size_t
  134. hash(const _Tp& __val)
  135. { return hash(&__val, sizeof(__val)); }
  136. };
  137. /// Explicit specializations for float.
  138. template<>
  139. inline size_t
  140. hash<float>::operator()(float __val) const
  141. {
  142. // 0 and -0 both hash to zero.
  143. return __val != 0.0f ? std::tr1::_Fnv_hash::hash(__val) : 0;
  144. }
  145. /// Explicit specializations for double.
  146. template<>
  147. inline size_t
  148. hash<double>::operator()(double __val) const
  149. {
  150. // 0 and -0 both hash to zero.
  151. return __val != 0.0 ? std::tr1::_Fnv_hash::hash(__val) : 0;
  152. }
  153. /// Explicit specializations for long double.
  154. template<>
  155. _GLIBCXX_PURE size_t
  156. hash<long double>::operator()(long double __val) const;
  157. /// Explicit specialization of member operator for non-builtin types.
  158. template<>
  159. _GLIBCXX_PURE size_t
  160. hash<string>::operator()(string) const;
  161. template<>
  162. _GLIBCXX_PURE size_t
  163. hash<const string&>::operator()(const string&) const;
  164. #ifdef _GLIBCXX_USE_WCHAR_T
  165. template<>
  166. _GLIBCXX_PURE size_t
  167. hash<wstring>::operator()(wstring) const;
  168. template<>
  169. _GLIBCXX_PURE size_t
  170. hash<const wstring&>::operator()(const wstring&) const;
  171. #endif
  172. }
  173. _GLIBCXX_END_NAMESPACE_VERSION
  174. }
  175. #endif // _GLIBCXX_TR1_FUNCTIONAL_HASH_H