OSDN Git Service

2010-10-28 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / functional_hash.h
1 // functional_hash.h header -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010 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 bits/functional_hash.h
26  *  This is an internal header file, included by other library headers.
27  *  You should not attempt to use it directly.
28  */
29
30 #ifndef _FUNCTIONAL_HASH_H
31 #define _FUNCTIONAL_HASH_H 1
32
33 #pragma GCC system_header
34
35 #include <bits/c++config.h>
36
37 namespace std
38 {
39   /** @defgroup hashes Hashes
40    *  @ingroup functors
41    *
42    *   Hashing functors taking a variable type and returning a @c std::size_t.
43    *
44    *  @{
45    */
46
47   template<typename _Result, typename _Arg>
48     struct __hash_base
49     {
50       typedef _Result     result_type;
51       typedef _Arg      argument_type;
52     };
53
54   /// Primary class template hash.
55   template<typename _Tp>
56     struct hash : public __hash_base<size_t, _Tp>
57     {
58       size_t
59       operator()(_Tp __val) const;
60     };
61
62   /// Partial specializations for pointer types.
63   template<typename _Tp>
64     struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
65     {
66       size_t
67       operator()(_Tp* __p) const
68       { return reinterpret_cast<size_t>(__p); }
69     };
70
71   // Explicit specializations for integer types.
72 #define _Cxx_hashtable_define_trivial_hash(_Tp)         \
73   template<>                                            \
74     inline size_t                                       \
75     hash<_Tp>::operator()(_Tp __val) const              \
76     { return static_cast<size_t>(__val); }
77
78   /// Explicit specialization for bool.
79   _Cxx_hashtable_define_trivial_hash(bool);
80
81   /// Explicit specialization for char.
82   _Cxx_hashtable_define_trivial_hash(char);
83
84   /// Explicit specialization for signed char.
85   _Cxx_hashtable_define_trivial_hash(signed char);
86
87   /// Explicit specialization for unsigned char.
88   _Cxx_hashtable_define_trivial_hash(unsigned char);
89
90   /// Explicit specialization for wchar_t.
91   _Cxx_hashtable_define_trivial_hash(wchar_t);
92
93   /// Explicit specialization for char16_t.
94   _Cxx_hashtable_define_trivial_hash(char16_t);
95
96   /// Explicit specialization for char32_t.
97   _Cxx_hashtable_define_trivial_hash(char32_t);
98
99   /// Explicit specialization for short.
100   _Cxx_hashtable_define_trivial_hash(short);
101
102   /// Explicit specialization for int.
103   _Cxx_hashtable_define_trivial_hash(int);
104
105   /// Explicit specialization for long.
106   _Cxx_hashtable_define_trivial_hash(long);
107
108   /// Explicit specialization for long long.
109   _Cxx_hashtable_define_trivial_hash(long long);
110
111   /// Explicit specialization for unsigned short.
112   _Cxx_hashtable_define_trivial_hash(unsigned short);
113
114   /// Explicit specialization for unsigned int.
115   _Cxx_hashtable_define_trivial_hash(unsigned int);
116
117   /// Explicit specialization for unsigned long.
118   _Cxx_hashtable_define_trivial_hash(unsigned long);
119
120   /// Explicit specialization for unsigned long long.
121   _Cxx_hashtable_define_trivial_hash(unsigned long long);
122
123 #undef _Cxx_hashtable_define_trivial_hash
124
125   // Hash function implementation for the nontrivial specialization.
126   // All of them are based on a primitive that hashes a pointer to
127   // a byte array. The actual hash algorithm is not guaranteed to
128   // stay the same from release to release -- it may be updated or
129   // tuned to improve hash quality or speed.
130   size_t
131   _Hash_bytes(const void* __ptr, size_t __len, size_t __seed);
132
133   // A similar hash primitive, using the FNV hash algorithm. This
134   // algorithm is guaranteed to stay the same from release to release.
135   // (although it might not produce the same values on different machines.)
136   size_t
137   _Fnv_hash_bytes(const void* __ptr, size_t __len, size_t __seed);
138
139   struct _Hash_impl
140   {
141     static size_t
142     hash(const void* __ptr, size_t __clength,
143          size_t __seed = static_cast<size_t>(0xc70f6907UL))
144     { return _Hash_bytes(__ptr, __clength, __seed); }
145
146     template<typename _Tp>
147       static size_t
148       hash(const _Tp& __val)
149       { return hash(&__val, sizeof(__val)); }
150
151     template<typename _Tp>
152       static size_t
153       __hash_combine(const _Tp& __val, size_t __hash)
154       { return hash(&__val, sizeof(__val), __hash); }
155   };
156
157   struct _Fnv_hash_impl
158   {
159     static size_t
160     hash(const void* __ptr, size_t __clength,
161          size_t __seed = static_cast<size_t>(2166136261UL))
162     { return _Fnv_hash_bytes(__ptr, __clength, __seed); }
163
164     template<typename _Tp>
165       static size_t
166       hash(const _Tp& __val)
167       { return hash(&__val, sizeof(__val)); }
168
169     template<typename _Tp>
170       static size_t
171       __hash_combine(const _Tp& __val, size_t __hash)
172       { return hash(&__val, sizeof(__val), __hash); }
173   };
174
175   /// Specialization for float.
176   template<>
177     inline size_t
178     hash<float>::operator()(float __val) const
179     {
180       // 0 and -0 both hash to zero.
181       return __val != 0.0f ? std::_Hash_impl::hash(__val) : 0;
182     }
183
184   /// Specialization for double.
185   template<>
186     inline size_t
187     hash<double>::operator()(double __val) const
188     {
189       // 0 and -0 both hash to zero.
190       return __val != 0.0 ? std::_Hash_impl::hash(__val) : 0;
191     }
192
193   /// Specialization for long double.
194   template<>
195     _GLIBCXX_PURE size_t
196     hash<long double>::operator()(long double __val) const;
197
198   // @} group hashes
199 }
200
201 #endif // _FUNCTIONAL_HASH_H