libstdc++
|
00001 // functional_hash.h header -*- C++ -*- 00002 00003 // Copyright (C) 2007-2013 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/functional_hash.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{functional} 00028 */ 00029 00030 #ifndef _FUNCTIONAL_HASH_H 00031 #define _FUNCTIONAL_HASH_H 1 00032 00033 #pragma GCC system_header 00034 00035 #include <bits/hash_bytes.h> 00036 00037 namespace std _GLIBCXX_VISIBILITY(default) 00038 { 00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00040 00041 /** @defgroup hashes Hashes 00042 * @ingroup functors 00043 * 00044 * Hashing functors taking a variable type and returning a @c std::size_t. 00045 * 00046 * @{ 00047 */ 00048 00049 template<typename _Result, typename _Arg> 00050 struct __hash_base 00051 { 00052 typedef _Result result_type; 00053 typedef _Arg argument_type; 00054 }; 00055 00056 /// Primary class template hash. 00057 template<typename _Tp> 00058 struct hash; 00059 00060 /// Partial specializations for pointer types. 00061 template<typename _Tp> 00062 struct hash<_Tp*> : public __hash_base<size_t, _Tp*> 00063 { 00064 size_t 00065 operator()(_Tp* __p) const noexcept 00066 { return reinterpret_cast<size_t>(__p); } 00067 }; 00068 00069 // Explicit specializations for integer types. 00070 #define _Cxx_hashtable_define_trivial_hash(_Tp) \ 00071 template<> \ 00072 struct hash<_Tp> : public __hash_base<size_t, _Tp> \ 00073 { \ 00074 size_t \ 00075 operator()(_Tp __val) const noexcept \ 00076 { return static_cast<size_t>(__val); } \ 00077 }; 00078 00079 /// Explicit specialization for bool. 00080 _Cxx_hashtable_define_trivial_hash(bool) 00081 00082 /// Explicit specialization for char. 00083 _Cxx_hashtable_define_trivial_hash(char) 00084 00085 /// Explicit specialization for signed char. 00086 _Cxx_hashtable_define_trivial_hash(signed char) 00087 00088 /// Explicit specialization for unsigned char. 00089 _Cxx_hashtable_define_trivial_hash(unsigned char) 00090 00091 /// Explicit specialization for wchar_t. 00092 _Cxx_hashtable_define_trivial_hash(wchar_t) 00093 00094 /// Explicit specialization for char16_t. 00095 _Cxx_hashtable_define_trivial_hash(char16_t) 00096 00097 /// Explicit specialization for char32_t. 00098 _Cxx_hashtable_define_trivial_hash(char32_t) 00099 00100 /// Explicit specialization for short. 00101 _Cxx_hashtable_define_trivial_hash(short) 00102 00103 /// Explicit specialization for int. 00104 _Cxx_hashtable_define_trivial_hash(int) 00105 00106 /// Explicit specialization for long. 00107 _Cxx_hashtable_define_trivial_hash(long) 00108 00109 /// Explicit specialization for long long. 00110 _Cxx_hashtable_define_trivial_hash(long long) 00111 00112 /// Explicit specialization for unsigned short. 00113 _Cxx_hashtable_define_trivial_hash(unsigned short) 00114 00115 /// Explicit specialization for unsigned int. 00116 _Cxx_hashtable_define_trivial_hash(unsigned int) 00117 00118 /// Explicit specialization for unsigned long. 00119 _Cxx_hashtable_define_trivial_hash(unsigned long) 00120 00121 /// Explicit specialization for unsigned long long. 00122 _Cxx_hashtable_define_trivial_hash(unsigned long long) 00123 00124 #undef _Cxx_hashtable_define_trivial_hash 00125 00126 struct _Hash_impl 00127 { 00128 static size_t 00129 hash(const void* __ptr, size_t __clength, 00130 size_t __seed = static_cast<size_t>(0xc70f6907UL)) 00131 { return _Hash_bytes(__ptr, __clength, __seed); } 00132 00133 template<typename _Tp> 00134 static size_t 00135 hash(const _Tp& __val) 00136 { return hash(&__val, sizeof(__val)); } 00137 00138 template<typename _Tp> 00139 static size_t 00140 __hash_combine(const _Tp& __val, size_t __hash) 00141 { return hash(&__val, sizeof(__val), __hash); } 00142 }; 00143 00144 struct _Fnv_hash_impl 00145 { 00146 static size_t 00147 hash(const void* __ptr, size_t __clength, 00148 size_t __seed = static_cast<size_t>(2166136261UL)) 00149 { return _Fnv_hash_bytes(__ptr, __clength, __seed); } 00150 00151 template<typename _Tp> 00152 static size_t 00153 hash(const _Tp& __val) 00154 { return hash(&__val, sizeof(__val)); } 00155 00156 template<typename _Tp> 00157 static size_t 00158 __hash_combine(const _Tp& __val, size_t __hash) 00159 { return hash(&__val, sizeof(__val), __hash); } 00160 }; 00161 00162 /// Specialization for float. 00163 template<> 00164 struct hash<float> : public __hash_base<size_t, float> 00165 { 00166 size_t 00167 operator()(float __val) const noexcept 00168 { 00169 // 0 and -0 both hash to zero. 00170 return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0; 00171 } 00172 }; 00173 00174 /// Specialization for double. 00175 template<> 00176 struct hash<double> : public __hash_base<size_t, double> 00177 { 00178 size_t 00179 operator()(double __val) const noexcept 00180 { 00181 // 0 and -0 both hash to zero. 00182 return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0; 00183 } 00184 }; 00185 00186 /// Specialization for long double. 00187 template<> 00188 struct hash<long double> 00189 : public __hash_base<size_t, long double> 00190 { 00191 _GLIBCXX_PURE size_t 00192 operator()(long double __val) const noexcept; 00193 }; 00194 00195 // @} group hashes 00196 00197 // Hint about performance of hash functor. If not fast the hash based 00198 // containers will cache the hash code. 00199 // Default behavior is to consider that hasher are fast unless specified 00200 // otherwise. 00201 template<typename _Hash> 00202 struct __is_fast_hash : public std::true_type 00203 { }; 00204 00205 template<> 00206 struct __is_fast_hash<hash<long double>> : public std::false_type 00207 { }; 00208 00209 _GLIBCXX_END_NAMESPACE_VERSION 00210 } // namespace 00211 00212 #endif // _FUNCTIONAL_HASH_H