libstdc++
functional_hash.h
Go to the documentation of this file.
00001 // functional_hash.h header -*- C++ -*-
00002 
00003 // Copyright (C) 2007-2018 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 _GLIBCXX17_DEPRECATED;
00053       typedef _Arg      argument_type _GLIBCXX17_DEPRECATED;
00054     };
00055 
00056   /// Primary class template hash.
00057   template<typename _Tp>
00058     struct hash;
00059 
00060   template<typename _Tp, typename = void>
00061     struct __poison_hash
00062     {
00063       static constexpr bool __enable_hash_call = false;
00064     private:
00065       // Private rather than deleted to be non-trivially-copyable.
00066       __poison_hash(__poison_hash&&);
00067       ~__poison_hash();
00068     };
00069 
00070   template<typename _Tp>
00071     struct __poison_hash<_Tp, __void_t<decltype(hash<_Tp>()(declval<_Tp>()))>>
00072     {
00073       static constexpr bool __enable_hash_call = true;
00074     };
00075 
00076   // Helper struct for SFINAE-poisoning non-enum types.
00077   template<typename _Tp, bool = is_enum<_Tp>::value>
00078     struct __hash_enum
00079     {
00080     private:
00081       // Private rather than deleted to be non-trivially-copyable.
00082       __hash_enum(__hash_enum&&);
00083       ~__hash_enum();
00084     };
00085 
00086   // Helper struct for hash with enum types.
00087   template<typename _Tp>
00088     struct __hash_enum<_Tp, true> : public __hash_base<size_t, _Tp>
00089     {
00090       size_t
00091       operator()(_Tp __val) const noexcept
00092       {
00093        using __type = typename underlying_type<_Tp>::type;
00094        return hash<__type>{}(static_cast<__type>(__val));
00095       }
00096     };
00097 
00098   /// Primary class template hash, usable for enum types only.
00099   // Use with non-enum types still SFINAES.
00100   template<typename _Tp>
00101     struct hash : __hash_enum<_Tp>
00102     { };
00103 
00104   /// Partial specializations for pointer types.
00105   template<typename _Tp>
00106     struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
00107     {
00108       size_t
00109       operator()(_Tp* __p) const noexcept
00110       { return reinterpret_cast<size_t>(__p); }
00111     };
00112 
00113   // Explicit specializations for integer types.
00114 #define _Cxx_hashtable_define_trivial_hash(_Tp)         \
00115   template<>                                            \
00116     struct hash<_Tp> : public __hash_base<size_t, _Tp>  \
00117     {                                                   \
00118       size_t                                            \
00119       operator()(_Tp __val) const noexcept              \
00120       { return static_cast<size_t>(__val); }            \
00121     };
00122 
00123   /// Explicit specialization for bool.
00124   _Cxx_hashtable_define_trivial_hash(bool)
00125 
00126   /// Explicit specialization for char.
00127   _Cxx_hashtable_define_trivial_hash(char)
00128 
00129   /// Explicit specialization for signed char.
00130   _Cxx_hashtable_define_trivial_hash(signed char)
00131 
00132   /// Explicit specialization for unsigned char.
00133   _Cxx_hashtable_define_trivial_hash(unsigned char)
00134 
00135   /// Explicit specialization for wchar_t.
00136   _Cxx_hashtable_define_trivial_hash(wchar_t)
00137 
00138   /// Explicit specialization for char16_t.
00139   _Cxx_hashtable_define_trivial_hash(char16_t)
00140 
00141   /// Explicit specialization for char32_t.
00142   _Cxx_hashtable_define_trivial_hash(char32_t)
00143 
00144   /// Explicit specialization for short.
00145   _Cxx_hashtable_define_trivial_hash(short)
00146 
00147   /// Explicit specialization for int.
00148   _Cxx_hashtable_define_trivial_hash(int)
00149 
00150   /// Explicit specialization for long.
00151   _Cxx_hashtable_define_trivial_hash(long)
00152 
00153   /// Explicit specialization for long long.
00154   _Cxx_hashtable_define_trivial_hash(long long)
00155 
00156   /// Explicit specialization for unsigned short.
00157   _Cxx_hashtable_define_trivial_hash(unsigned short)
00158 
00159   /// Explicit specialization for unsigned int.
00160   _Cxx_hashtable_define_trivial_hash(unsigned int)
00161 
00162   /// Explicit specialization for unsigned long.
00163   _Cxx_hashtable_define_trivial_hash(unsigned long)
00164 
00165   /// Explicit specialization for unsigned long long.
00166   _Cxx_hashtable_define_trivial_hash(unsigned long long)
00167 
00168 #ifdef __GLIBCXX_TYPE_INT_N_0
00169   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0)
00170   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_0 unsigned)
00171 #endif
00172 #ifdef __GLIBCXX_TYPE_INT_N_1
00173   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1)
00174   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_1 unsigned)
00175 #endif
00176 #ifdef __GLIBCXX_TYPE_INT_N_2
00177   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2)
00178   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_2 unsigned)
00179 #endif
00180 #ifdef __GLIBCXX_TYPE_INT_N_3
00181   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3)
00182   _Cxx_hashtable_define_trivial_hash(__GLIBCXX_TYPE_INT_N_3 unsigned)
00183 #endif
00184 
00185 #undef _Cxx_hashtable_define_trivial_hash
00186 
00187   struct _Hash_impl
00188   {
00189     static size_t
00190     hash(const void* __ptr, size_t __clength,
00191          size_t __seed = static_cast<size_t>(0xc70f6907UL))
00192     { return _Hash_bytes(__ptr, __clength, __seed); }
00193 
00194     template<typename _Tp>
00195       static size_t
00196       hash(const _Tp& __val)
00197       { return hash(&__val, sizeof(__val)); }
00198 
00199     template<typename _Tp>
00200       static size_t
00201       __hash_combine(const _Tp& __val, size_t __hash)
00202       { return hash(&__val, sizeof(__val), __hash); }
00203   };
00204 
00205   // A hash function similar to FNV-1a (see PR59406 for how it differs).
00206   struct _Fnv_hash_impl
00207   {
00208     static size_t
00209     hash(const void* __ptr, size_t __clength,
00210          size_t __seed = static_cast<size_t>(2166136261UL))
00211     { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
00212 
00213     template<typename _Tp>
00214       static size_t
00215       hash(const _Tp& __val)
00216       { return hash(&__val, sizeof(__val)); }
00217 
00218     template<typename _Tp>
00219       static size_t
00220       __hash_combine(const _Tp& __val, size_t __hash)
00221       { return hash(&__val, sizeof(__val), __hash); }
00222   };
00223 
00224   /// Specialization for float.
00225   template<>
00226     struct hash<float> : public __hash_base<size_t, float>
00227     {
00228       size_t
00229       operator()(float __val) const noexcept
00230       {
00231         // 0 and -0 both hash to zero.
00232         return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
00233       }
00234     };
00235 
00236   /// Specialization for double.
00237   template<>
00238     struct hash<double> : public __hash_base<size_t, double>
00239     {
00240       size_t
00241       operator()(double __val) const noexcept
00242       {
00243         // 0 and -0 both hash to zero.
00244         return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
00245       }
00246     };
00247 
00248   /// Specialization for long double.
00249   template<>
00250     struct hash<long double>
00251     : public __hash_base<size_t, long double>
00252     {
00253       _GLIBCXX_PURE size_t
00254       operator()(long double __val) const noexcept;
00255     };
00256 
00257   // @} group hashes
00258 
00259   // Hint about performance of hash functor. If not fast the hash-based
00260   // containers will cache the hash code.
00261   // Default behavior is to consider that hashers are fast unless specified
00262   // otherwise.
00263   template<typename _Hash>
00264     struct __is_fast_hash : public std::true_type
00265     { };
00266 
00267   template<>
00268     struct __is_fast_hash<hash<long double>> : public std::false_type
00269     { };
00270 
00271 _GLIBCXX_END_NAMESPACE_VERSION
00272 } // namespace
00273 
00274 #endif // _FUNCTIONAL_HASH_H