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