libstdc++
functional_hash.h
Go to the documentation of this file.
00001 // functional_hash.h header -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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 : public __hash_base<size_t, _Tp>
00059     {
00060       static_assert(sizeof(_Tp) < 0,
00061             "std::hash is not specialized for this type");
00062       size_t operator()(const _Tp&) const noexcept;
00063     };
00064 
00065   /// Partial specializations for pointer types.
00066   template<typename _Tp>
00067     struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
00068     {
00069       size_t
00070       operator()(_Tp* __p) const noexcept
00071       { return reinterpret_cast<size_t>(__p); }
00072     };
00073 
00074   // Explicit specializations for integer types.
00075 #define _Cxx_hashtable_define_trivial_hash(_Tp)     \
00076   template<>                        \
00077     struct hash<_Tp> : public __hash_base<size_t, _Tp>  \
00078     {                                                   \
00079       size_t                                            \
00080       operator()(_Tp __val) const noexcept              \
00081       { return static_cast<size_t>(__val); }            \
00082     };
00083 
00084   /// Explicit specialization for bool.
00085   _Cxx_hashtable_define_trivial_hash(bool)
00086 
00087   /// Explicit specialization for char.
00088   _Cxx_hashtable_define_trivial_hash(char)
00089 
00090   /// Explicit specialization for signed char.
00091   _Cxx_hashtable_define_trivial_hash(signed char)
00092 
00093   /// Explicit specialization for unsigned char.
00094   _Cxx_hashtable_define_trivial_hash(unsigned char)
00095 
00096   /// Explicit specialization for wchar_t.
00097   _Cxx_hashtable_define_trivial_hash(wchar_t)
00098 
00099   /// Explicit specialization for char16_t.
00100   _Cxx_hashtable_define_trivial_hash(char16_t)
00101 
00102   /// Explicit specialization for char32_t.
00103   _Cxx_hashtable_define_trivial_hash(char32_t)
00104 
00105   /// Explicit specialization for short.
00106   _Cxx_hashtable_define_trivial_hash(short)
00107 
00108   /// Explicit specialization for int.
00109   _Cxx_hashtable_define_trivial_hash(int)
00110 
00111   /// Explicit specialization for long.
00112   _Cxx_hashtable_define_trivial_hash(long)
00113 
00114   /// Explicit specialization for long long.
00115   _Cxx_hashtable_define_trivial_hash(long long)
00116 
00117   /// Explicit specialization for unsigned short.
00118   _Cxx_hashtable_define_trivial_hash(unsigned short)
00119 
00120   /// Explicit specialization for unsigned int.
00121   _Cxx_hashtable_define_trivial_hash(unsigned int)
00122 
00123   /// Explicit specialization for unsigned long.
00124   _Cxx_hashtable_define_trivial_hash(unsigned long)
00125 
00126   /// Explicit specialization for unsigned long long.
00127   _Cxx_hashtable_define_trivial_hash(unsigned long long)
00128 
00129 #undef _Cxx_hashtable_define_trivial_hash
00130 
00131   struct _Hash_impl
00132   {
00133     static size_t
00134     hash(const void* __ptr, size_t __clength,
00135      size_t __seed = static_cast<size_t>(0xc70f6907UL))
00136     { return _Hash_bytes(__ptr, __clength, __seed); }
00137 
00138     template<typename _Tp>
00139       static size_t
00140       hash(const _Tp& __val)
00141       { return hash(&__val, sizeof(__val)); }
00142 
00143     template<typename _Tp>
00144       static size_t
00145       __hash_combine(const _Tp& __val, size_t __hash)
00146       { return hash(&__val, sizeof(__val), __hash); }
00147   };
00148 
00149   struct _Fnv_hash_impl
00150   {
00151     static size_t
00152     hash(const void* __ptr, size_t __clength,
00153      size_t __seed = static_cast<size_t>(2166136261UL))
00154     { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
00155 
00156     template<typename _Tp>
00157       static size_t
00158       hash(const _Tp& __val)
00159       { return hash(&__val, sizeof(__val)); }
00160 
00161     template<typename _Tp>
00162       static size_t
00163       __hash_combine(const _Tp& __val, size_t __hash)
00164       { return hash(&__val, sizeof(__val), __hash); }
00165   };
00166 
00167   /// Specialization for float.
00168   template<>
00169     struct hash<float> : public __hash_base<size_t, float>
00170     {
00171       size_t
00172       operator()(float __val) const noexcept
00173       {
00174     // 0 and -0 both hash to zero.
00175     return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
00176       }
00177     };
00178 
00179   /// Specialization for double.
00180   template<>
00181     struct hash<double> : public __hash_base<size_t, double>
00182     {
00183       size_t
00184       operator()(double __val) const noexcept
00185       {
00186     // 0 and -0 both hash to zero.
00187     return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
00188       }
00189     };
00190 
00191   /// Specialization for long double.
00192   template<>
00193     struct hash<long double>
00194     : public __hash_base<size_t, long double>
00195     {
00196       _GLIBCXX_PURE size_t
00197       operator()(long double __val) const noexcept;
00198     };
00199 
00200   // @} group hashes
00201 
00202 _GLIBCXX_END_NAMESPACE_VERSION
00203 } // namespace
00204 
00205 #endif // _FUNCTIONAL_HASH_H