re PR libstdc++/37144 (A bug in include/ext/pb_ds/detail/pat_trie_/constructors_destr...
[gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / hash_fn / ranged_hash_fn.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2009, 2011 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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
9 // version.
10
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // 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 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
34 // warranty.
35
36 /**
37 * @file ranged_hash_fn.hpp
38 * Contains a unified ranged hash functor, allowing the hash tables
39 * to deal with a single class for ranged hashing.
40 */
41
42 #ifndef PB_DS_RANGED_HASH_FN_HPP
43 #define PB_DS_RANGED_HASH_FN_HPP
44
45 #include <utility>
46 #include <debug/debug.h>
47
48 namespace __gnu_pbds
49 {
50 namespace detail
51 {
52 template<typename Key, typename Hash_Fn, typename _Alloc,
53 typename Comb_Hash_Fn, bool Store_Hash>
54 class ranged_hash_fn;
55
56 #define PB_DS_CLASS_T_DEC \
57 template<typename Key, typename Hash_Fn, typename _Alloc, \
58 typename Comb_Hash_Fn>
59
60 #define PB_DS_CLASS_C_DEC \
61 ranged_hash_fn<Key, Hash_Fn, _Alloc, Comb_Hash_Fn, false>
62
63 /**
64 * Specialization 1
65 * The client supplies a hash function and a ranged hash function,
66 * and requests that hash values not be stored.
67 **/
68 template<typename Key, typename Hash_Fn, typename _Alloc,
69 typename Comb_Hash_Fn>
70 class ranged_hash_fn< Key, Hash_Fn, _Alloc, Comb_Hash_Fn, false>
71 : public Hash_Fn, public Comb_Hash_Fn
72 {
73 protected:
74 typedef typename _Alloc::size_type size_type;
75 typedef Hash_Fn hash_fn_base;
76 typedef Comb_Hash_Fn comb_hash_fn_base;
77 typedef typename _Alloc::template rebind< Key>::other key_allocator;
78 typedef typename key_allocator::const_reference key_const_reference;
79
80 ranged_hash_fn(size_type);
81
82 ranged_hash_fn(size_type, const Hash_Fn&);
83
84 ranged_hash_fn(size_type, const Hash_Fn&, const Comb_Hash_Fn&);
85
86 void
87 swap(PB_DS_CLASS_C_DEC&);
88
89 void
90 notify_resized(size_type);
91
92 inline size_type
93 operator()(key_const_reference) const;
94 };
95
96 PB_DS_CLASS_T_DEC
97 PB_DS_CLASS_C_DEC::
98 ranged_hash_fn(size_type size)
99 { Comb_Hash_Fn::notify_resized(size); }
100
101 PB_DS_CLASS_T_DEC
102 PB_DS_CLASS_C_DEC::
103 ranged_hash_fn(size_type size, const Hash_Fn& r_hash_fn)
104 : Hash_Fn(r_hash_fn)
105 { Comb_Hash_Fn::notify_resized(size); }
106
107 PB_DS_CLASS_T_DEC
108 PB_DS_CLASS_C_DEC::
109 ranged_hash_fn(size_type size, const Hash_Fn& r_hash_fn,
110 const Comb_Hash_Fn& r_comb_hash_fn)
111 : Hash_Fn(r_hash_fn), Comb_Hash_Fn(r_comb_hash_fn)
112 { comb_hash_fn_base::notify_resized(size); }
113
114 PB_DS_CLASS_T_DEC
115 void
116 PB_DS_CLASS_C_DEC::
117 swap(PB_DS_CLASS_C_DEC& other)
118 {
119 comb_hash_fn_base::swap(other);
120 std::swap((Hash_Fn& )(*this), (Hash_Fn& )other);
121 }
122
123 PB_DS_CLASS_T_DEC
124 void
125 PB_DS_CLASS_C_DEC::
126 notify_resized(size_type size)
127 { comb_hash_fn_base::notify_resized(size); }
128
129 PB_DS_CLASS_T_DEC
130 inline typename PB_DS_CLASS_C_DEC::size_type
131 PB_DS_CLASS_C_DEC::
132 operator()(key_const_reference r_key) const
133 { return (comb_hash_fn_base::operator()(hash_fn_base::operator()(r_key)));}
134
135 #undef PB_DS_CLASS_T_DEC
136 #undef PB_DS_CLASS_C_DEC
137
138 #define PB_DS_CLASS_T_DEC \
139 template<typename Key, typename Hash_Fn, typename _Alloc, \
140 typename Comb_Hash_Fn>
141
142 #define PB_DS_CLASS_C_DEC \
143 ranged_hash_fn<Key,Hash_Fn, _Alloc, Comb_Hash_Fn, true>
144
145 /**
146 * Specialization 2
147 * The client supplies a hash function and a ranged hash function,
148 * and requests that hash values be stored.
149 **/
150 template<typename Key, typename Hash_Fn, typename _Alloc,
151 typename Comb_Hash_Fn>
152 class ranged_hash_fn<Key, Hash_Fn, _Alloc, Comb_Hash_Fn, true>
153 : public Hash_Fn, public Comb_Hash_Fn
154 {
155 protected:
156 typedef typename _Alloc::size_type size_type;
157 typedef std::pair<size_type, size_type> comp_hash;
158 typedef Hash_Fn hash_fn_base;
159 typedef Comb_Hash_Fn comb_hash_fn_base;
160 typedef typename _Alloc::template rebind<Key>::other key_allocator;
161 typedef typename key_allocator::const_reference key_const_reference;
162
163 ranged_hash_fn(size_type);
164
165 ranged_hash_fn(size_type, const Hash_Fn&);
166
167 ranged_hash_fn(size_type, const Hash_Fn&, const Comb_Hash_Fn&);
168
169 void
170 swap(PB_DS_CLASS_C_DEC&);
171
172 void
173 notify_resized(size_type);
174
175 inline comp_hash
176 operator()(key_const_reference) const;
177
178 inline comp_hash
179 operator()(key_const_reference, size_type) const;
180 };
181
182 PB_DS_CLASS_T_DEC
183 PB_DS_CLASS_C_DEC::
184 ranged_hash_fn(size_type size)
185 { Comb_Hash_Fn::notify_resized(size); }
186
187 PB_DS_CLASS_T_DEC
188 PB_DS_CLASS_C_DEC::
189 ranged_hash_fn(size_type size, const Hash_Fn& r_hash_fn) :
190 Hash_Fn(r_hash_fn)
191 { Comb_Hash_Fn::notify_resized(size); }
192
193 PB_DS_CLASS_T_DEC
194 PB_DS_CLASS_C_DEC::
195 ranged_hash_fn(size_type size, const Hash_Fn& r_hash_fn,
196 const Comb_Hash_Fn& r_comb_hash_fn)
197 : Hash_Fn(r_hash_fn), Comb_Hash_Fn(r_comb_hash_fn)
198 { comb_hash_fn_base::notify_resized(size); }
199
200 PB_DS_CLASS_T_DEC
201 void
202 PB_DS_CLASS_C_DEC::
203 swap(PB_DS_CLASS_C_DEC& other)
204 {
205 comb_hash_fn_base::swap(other);
206 std::swap((Hash_Fn& )(*this), (Hash_Fn& )other);
207 }
208
209 PB_DS_CLASS_T_DEC
210 void
211 PB_DS_CLASS_C_DEC::
212 notify_resized(size_type size)
213 { comb_hash_fn_base::notify_resized(size); }
214
215 PB_DS_CLASS_T_DEC
216 inline typename PB_DS_CLASS_C_DEC::comp_hash
217 PB_DS_CLASS_C_DEC::
218 operator()(key_const_reference r_key) const
219 {
220 const size_type hash = hash_fn_base::operator()(r_key);
221 return std::make_pair(comb_hash_fn_base::operator()(hash), hash);
222 }
223
224 PB_DS_CLASS_T_DEC
225 inline typename PB_DS_CLASS_C_DEC::comp_hash
226 PB_DS_CLASS_C_DEC::
227 operator()
228 #ifdef _GLIBCXX_DEBUG
229 (key_const_reference r_key, size_type hash) const
230 #else
231 (key_const_reference /*r_key*/, size_type hash) const
232 #endif
233 {
234 _GLIBCXX_DEBUG_ASSERT(hash == hash_fn_base::operator()(r_key));
235 return std::make_pair(comb_hash_fn_base::operator()(hash), hash);
236 }
237
238 #undef PB_DS_CLASS_T_DEC
239 #undef PB_DS_CLASS_C_DEC
240
241 #define PB_DS_CLASS_T_DEC \
242 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
243
244 #define PB_DS_CLASS_C_DEC \
245 ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, false>
246
247 /**
248 * Specialization 3
249 * The client does not supply a hash function (by specifying
250 * null_type as the Hash_Fn parameter), and requests that hash
251 * values not be stored.
252 **/
253 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
254 class ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, false>
255 : public Comb_Hash_Fn
256 {
257 protected:
258 typedef typename _Alloc::size_type size_type;
259 typedef Comb_Hash_Fn comb_hash_fn_base;
260
261 ranged_hash_fn(size_type);
262
263 ranged_hash_fn(size_type, const Comb_Hash_Fn&);
264
265 ranged_hash_fn(size_type, const null_type&, const Comb_Hash_Fn&);
266
267 void
268 swap(PB_DS_CLASS_C_DEC&);
269 };
270
271 PB_DS_CLASS_T_DEC
272 PB_DS_CLASS_C_DEC::
273 ranged_hash_fn(size_type size)
274 { Comb_Hash_Fn::notify_resized(size); }
275
276 PB_DS_CLASS_T_DEC
277 PB_DS_CLASS_C_DEC::
278 ranged_hash_fn(size_type size, const Comb_Hash_Fn& r_comb_hash_fn) :
279 Comb_Hash_Fn(r_comb_hash_fn)
280 { }
281
282 PB_DS_CLASS_T_DEC
283 PB_DS_CLASS_C_DEC::
284 ranged_hash_fn(size_type size, const null_type& r_null_type,
285 const Comb_Hash_Fn& r_comb_hash_fn)
286 : Comb_Hash_Fn(r_comb_hash_fn)
287 { }
288
289 PB_DS_CLASS_T_DEC
290 void
291 PB_DS_CLASS_C_DEC::
292 swap(PB_DS_CLASS_C_DEC& other)
293 { comb_hash_fn_base::swap(other); }
294
295 #undef PB_DS_CLASS_T_DEC
296 #undef PB_DS_CLASS_C_DEC
297
298 #define PB_DS_CLASS_T_DEC \
299 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
300
301 #define PB_DS_CLASS_C_DEC \
302 ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, true>
303
304 /**
305 * Specialization 4
306 * The client does not supply a hash function (by specifying
307 * null_type as the Hash_Fn parameter), and requests that hash
308 * values be stored.
309 **/
310 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
311 class ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, true>
312 : public Comb_Hash_Fn
313 {
314 protected:
315 typedef typename _Alloc::size_type size_type;
316 typedef Comb_Hash_Fn comb_hash_fn_base;
317
318 ranged_hash_fn(size_type);
319
320 ranged_hash_fn(size_type, const Comb_Hash_Fn&);
321
322 ranged_hash_fn(size_type, const null_type&, const Comb_Hash_Fn&);
323
324 void
325 swap(PB_DS_CLASS_C_DEC&);
326 };
327
328 PB_DS_CLASS_T_DEC
329 PB_DS_CLASS_C_DEC::
330 ranged_hash_fn(size_type size)
331 { Comb_Hash_Fn::notify_resized(size); }
332
333 PB_DS_CLASS_T_DEC
334 PB_DS_CLASS_C_DEC::
335 ranged_hash_fn(size_type size, const Comb_Hash_Fn& r_comb_hash_fn)
336 : Comb_Hash_Fn(r_comb_hash_fn)
337 { }
338
339 PB_DS_CLASS_T_DEC
340 PB_DS_CLASS_C_DEC::
341 ranged_hash_fn(size_type size, const null_type& r_null_type,
342 const Comb_Hash_Fn& r_comb_hash_fn)
343 : Comb_Hash_Fn(r_comb_hash_fn)
344 { }
345
346 PB_DS_CLASS_T_DEC
347 void
348 PB_DS_CLASS_C_DEC::
349 swap(PB_DS_CLASS_C_DEC& other)
350 { comb_hash_fn_base::swap(other); }
351
352 #undef PB_DS_CLASS_T_DEC
353 #undef PB_DS_CLASS_C_DEC
354
355 } // namespace detail
356 } // namespace __gnu_pbds
357
358 #endif