Home | History | Annotate | Line # | Download | only in tr1
      1 // TR1 functional_hash.h header -*- C++ -*-
      2 
      3 // Copyright (C) 2007-2024 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file tr1/functional_hash.h
     26  *  This is an internal header file, included by other library headers.
     27  *  Do not attempt to use it directly. @headername{tr1/functional}
     28  */
     29 
     30 #ifndef _GLIBCXX_TR1_FUNCTIONAL_HASH_H
     31 #define _GLIBCXX_TR1_FUNCTIONAL_HASH_H 1
     32 
     33 #pragma GCC system_header
     34 
     35 namespace std _GLIBCXX_VISIBILITY(default)
     36 {
     37 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     38 
     39 namespace tr1
     40 {
     41 // Ignore warnings about std::unary_function and std::binary_function.
     42 #pragma GCC diagnostic push
     43 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
     44 
     45   /// Class template hash.
     46   // Declaration of default hash functor std::tr1::hash.  The types for
     47   // which std::tr1::hash<T> is well-defined is in clause 6.3.3. of the PDTR.
     48   template<typename _Tp>
     49     struct hash : public std::unary_function<_Tp, size_t>
     50     {
     51       size_t
     52       operator()(_Tp __val) const;
     53     };
     54 
     55   /// Partial specializations for pointer types.
     56   template<typename _Tp>
     57     struct hash<_Tp*> : public std::unary_function<_Tp*, size_t>
     58     {
     59       size_t
     60       operator()(_Tp* __p) const
     61       { return reinterpret_cast<size_t>(__p); }
     62     };
     63 #pragma GCC diagnostic pop
     64 
     65   /// Explicit specializations for integer types.
     66 #define _TR1_hashtable_define_trivial_hash(_Tp) 	\
     67   template<>						\
     68     inline size_t					\
     69     hash<_Tp>::operator()(_Tp __val) const		\
     70     { return static_cast<size_t>(__val); }
     71 
     72 #pragma GCC diagnostic push
     73 #pragma GCC diagnostic ignored "-Wlong-long"
     74   _TR1_hashtable_define_trivial_hash(bool)
     75   _TR1_hashtable_define_trivial_hash(char)
     76   _TR1_hashtable_define_trivial_hash(signed char)
     77   _TR1_hashtable_define_trivial_hash(unsigned char)
     78   _TR1_hashtable_define_trivial_hash(wchar_t)
     79   _TR1_hashtable_define_trivial_hash(short)
     80   _TR1_hashtable_define_trivial_hash(int)
     81   _TR1_hashtable_define_trivial_hash(long)
     82   _TR1_hashtable_define_trivial_hash(long long)
     83   _TR1_hashtable_define_trivial_hash(unsigned short)
     84   _TR1_hashtable_define_trivial_hash(unsigned int)
     85   _TR1_hashtable_define_trivial_hash(unsigned long)
     86   _TR1_hashtable_define_trivial_hash(unsigned long long)
     87 #pragma GCC diagnostic pop
     88 
     89 #undef _TR1_hashtable_define_trivial_hash
     90 
     91   // Fowler / Noll / Vo (FNV) Hash (type FNV-1a)
     92   // (Used by the next specializations of std::tr1::hash.)
     93 
     94   // N.B. These functions should work on unsigned char, otherwise they do not
     95   // correctly implement the FNV-1a algorithm (see PR59406).
     96   // The existing behaviour is retained for backwards compatibility.
     97 
     98   /// Dummy generic implementation (for sizeof(size_t) != 4, 8).
     99   template<size_t>
    100     struct _Fnv_hash_base
    101     {
    102       template<typename _Tp>
    103         static size_t
    104         hash(const _Tp* __ptr, size_t __clength)
    105         {
    106 	  size_t __result = 0;
    107 	  const char* __cptr = reinterpret_cast<const char*>(__ptr);
    108 	  for (; __clength; --__clength)
    109 	    __result = (__result * 131) + *__cptr++;
    110 	  return __result;
    111 	}
    112     };
    113 
    114   template<>
    115     struct _Fnv_hash_base<4>
    116     {
    117       template<typename _Tp>
    118         static size_t
    119         hash(const _Tp* __ptr, size_t __clength)
    120         {
    121 	  size_t __result = static_cast<size_t>(2166136261UL);
    122 	  const char* __cptr = reinterpret_cast<const char*>(__ptr);
    123 	  for (; __clength; --__clength)
    124 	    {
    125 	      __result ^= static_cast<size_t>(*__cptr++);
    126 	      __result *= static_cast<size_t>(16777619UL);
    127 	    }
    128 	  return __result;
    129 	}
    130     };
    131 
    132   template<>
    133     struct _Fnv_hash_base<8>
    134     {
    135       template<typename _Tp>
    136         static size_t
    137         hash(const _Tp* __ptr, size_t __clength)
    138         {
    139 #pragma GCC diagnostic push
    140 #pragma GCC diagnostic ignored "-Wlong-long"
    141 	  size_t __result
    142 	    = static_cast<size_t>(14695981039346656037ULL);
    143 	  const char* __cptr = reinterpret_cast<const char*>(__ptr);
    144 	  for (; __clength; --__clength)
    145 	    {
    146 	      __result ^= static_cast<size_t>(*__cptr++);
    147 	      __result *= static_cast<size_t>(1099511628211ULL);
    148 	    }
    149 	  return __result;
    150 #pragma GCC diagnostic pop
    151 	}
    152     };
    153 
    154   struct _Fnv_hash
    155   : public _Fnv_hash_base<sizeof(size_t)>
    156   {
    157     using _Fnv_hash_base<sizeof(size_t)>::hash;
    158 
    159     template<typename _Tp>
    160       static size_t
    161       hash(const _Tp& __val)
    162       { return hash(&__val, sizeof(__val)); }
    163   };
    164 
    165   /// Explicit specializations for float.
    166   template<>
    167     inline size_t
    168     hash<float>::operator()(float __val) const
    169     {
    170       // 0 and -0 both hash to zero.
    171       return __val != 0.0f ? std::tr1::_Fnv_hash::hash(__val) : 0;
    172     }
    173 
    174   /// Explicit specializations for double.
    175   template<>
    176     inline size_t
    177     hash<double>::operator()(double __val) const
    178     {
    179       // 0 and -0 both hash to zero.
    180       return __val != 0.0 ? std::tr1::_Fnv_hash::hash(__val) : 0;
    181     }
    182 
    183   /// Explicit specializations for long double.
    184   template<>
    185     _GLIBCXX_PURE size_t
    186     hash<long double>::operator()(long double __val) const;
    187 
    188   /// Explicit specialization of member operator for non-builtin types.
    189   template<>
    190     _GLIBCXX_PURE size_t
    191     hash<string>::operator()(string) const;
    192 
    193   template<>
    194     _GLIBCXX_PURE size_t
    195     hash<const string&>::operator()(const string&) const;
    196 
    197 #ifdef _GLIBCXX_USE_WCHAR_T
    198   template<>
    199     _GLIBCXX_PURE size_t
    200     hash<wstring>::operator()(wstring) const;
    201 
    202   template<>
    203     _GLIBCXX_PURE size_t
    204     hash<const wstring&>::operator()(const wstring&) const;
    205 #endif
    206 }
    207 
    208 _GLIBCXX_END_NAMESPACE_VERSION
    209 }
    210 
    211 #endif // _GLIBCXX_TR1_FUNCTIONAL_HASH_H
    212