functional_hash.h

Go to the documentation of this file.
00001 // functional_hash.h header -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009, 2010 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  *  You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _FUNCTIONAL_HASH_H
00031 #define _FUNCTIONAL_HASH_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #include <bits/c++config.h>
00036 
00037 namespace std
00038 {
00039   /** @defgroup hashes Hashes
00040    *  @ingroup functors
00041    *
00042    *   Hashing functors taking a variable type and returning a @c std::size_t.
00043    *
00044    *  @{
00045    */
00046 
00047   template<typename _Result, typename _Arg>
00048     struct __hash_base
00049     {
00050       typedef _Result     result_type;
00051       typedef _Arg      argument_type;
00052     };
00053 
00054   /// Primary class template hash.
00055   template<typename _Tp>
00056     struct hash : public __hash_base<size_t, _Tp>
00057     {
00058       size_t
00059       operator()(_Tp __val) const;
00060     };
00061 
00062   /// Partial specializations for pointer types.
00063   template<typename _Tp>
00064     struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
00065     {
00066       size_t
00067       operator()(_Tp* __p) const
00068       { return reinterpret_cast<size_t>(__p); }
00069     };
00070 
00071   // Explicit specializations for integer types.
00072 #define _Cxx_hashtable_define_trivial_hash(_Tp)     \
00073   template<>                        \
00074     inline size_t                   \
00075     hash<_Tp>::operator()(_Tp __val) const      \
00076     { return static_cast<size_t>(__val); }
00077 
00078   /// Explicit specialization for bool.
00079   _Cxx_hashtable_define_trivial_hash(bool);
00080 
00081   /// Explicit specialization for char.
00082   _Cxx_hashtable_define_trivial_hash(char);
00083 
00084   /// Explicit specialization for signed char.
00085   _Cxx_hashtable_define_trivial_hash(signed char);
00086 
00087   /// Explicit specialization for unsigned char.
00088   _Cxx_hashtable_define_trivial_hash(unsigned char);
00089 
00090   /// Explicit specialization for wchar_t.
00091   _Cxx_hashtable_define_trivial_hash(wchar_t);
00092 
00093   /// Explicit specialization for char16_t.
00094   _Cxx_hashtable_define_trivial_hash(char16_t);
00095 
00096   /// Explicit specialization for char32_t.
00097   _Cxx_hashtable_define_trivial_hash(char32_t);
00098 
00099   /// Explicit specialization for short.
00100   _Cxx_hashtable_define_trivial_hash(short);
00101 
00102   /// Explicit specialization for int.
00103   _Cxx_hashtable_define_trivial_hash(int);
00104 
00105   /// Explicit specialization for long.
00106   _Cxx_hashtable_define_trivial_hash(long);
00107 
00108   /// Explicit specialization for long long.
00109   _Cxx_hashtable_define_trivial_hash(long long);
00110 
00111   /// Explicit specialization for unsigned short.
00112   _Cxx_hashtable_define_trivial_hash(unsigned short);
00113 
00114   /// Explicit specialization for unsigned int.
00115   _Cxx_hashtable_define_trivial_hash(unsigned int);
00116 
00117   /// Explicit specialization for unsigned long.
00118   _Cxx_hashtable_define_trivial_hash(unsigned long);
00119 
00120   /// Explicit specialization for unsigned long long.
00121   _Cxx_hashtable_define_trivial_hash(unsigned long long);
00122 
00123 #undef _Cxx_hashtable_define_trivial_hash
00124 
00125   // Hash function implementation for the nontrivial specialization.
00126   // All of them are based on a primitive that hashes a pointer to
00127   // a byte array. The actual hash algorithm is not guaranteed to
00128   // stay the same from release to release -- it may be updated or
00129   // tuned to improve hash quality or speed.
00130   size_t
00131   _Hash_bytes(const void* __ptr, size_t __len, size_t __seed);
00132 
00133   // A similar hash primitive, using the FNV hash algorithm. This
00134   // algorithm is guaranteed to stay the same from release to release.
00135   // (although it might not produce the same values on different machines.)
00136   size_t
00137   _Fnv_hash_bytes(const void* __ptr, size_t __len, size_t __seed);
00138 
00139   struct _Hash_impl
00140   {
00141     static size_t
00142     hash(const void* __ptr, size_t __clength,
00143      size_t __seed = static_cast<size_t>(0xc70f6907UL))
00144     { return _Hash_bytes(__ptr, __clength, __seed); }
00145 
00146     template<typename _Tp>
00147       static size_t
00148       hash(const _Tp& __val)
00149       { return hash(&__val, sizeof(__val)); }
00150 
00151     template<typename _Tp>
00152       static size_t
00153       __hash_combine(const _Tp& __val, size_t __hash)
00154       { return hash(&__val, sizeof(__val), __hash); }
00155   };
00156 
00157   struct _Fnv_hash_impl
00158   {
00159     static size_t
00160     hash(const void* __ptr, size_t __clength,
00161          size_t __seed = static_cast<size_t>(2166136261UL))
00162     { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
00163 
00164     template<typename _Tp>
00165       static size_t
00166       hash(const _Tp& __val)
00167       { return hash(&__val, sizeof(__val)); }
00168 
00169     template<typename _Tp>
00170       static size_t
00171       __hash_combine(const _Tp& __val, size_t __hash)
00172       { return hash(&__val, sizeof(__val), __hash); }
00173   };
00174 
00175   /// Specialization for float.
00176   template<>
00177     inline size_t
00178     hash<float>::operator()(float __val) const
00179     {
00180       // 0 and -0 both hash to zero.
00181       return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
00182     }
00183 
00184   /// Specialization for double.
00185   template<>
00186     inline size_t
00187     hash<double>::operator()(double __val) const
00188     {
00189       // 0 and -0 both hash to zero.
00190       return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
00191     }
00192 
00193   /// Specialization for long double.
00194   template<>
00195     _GLIBCXX_PURE size_t
00196     hash<long double>::operator()(long double __val) const;
00197 
00198   // @} group hashes
00199 }
00200 
00201 #endif // _FUNCTIONAL_HASH_H