Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

functional_hash.h 5.9KB

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