]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/hash_fn/ranged_hash_fn.hpp
re PR libstdc++/37144 (A bug in include/ext/pb_ds/detail/pat_trie_/constructors_destr...
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / hash_fn / ranged_hash_fn.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
a345e45d 3// Copyright (C) 2005, 2006, 2009, 2011 Free Software Foundation, Inc.
4569a895
AT
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
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
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
748086b7
JJ
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.
4569a895 19
748086b7
JJ
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/>.
4569a895
AT
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
1f1a03ef
BK
38 * Contains a unified ranged hash functor, allowing the hash tables
39 * to deal with a single class for ranged hashing.
4569a895
AT
40 */
41
42#ifndef PB_DS_RANGED_HASH_FN_HPP
43#define PB_DS_RANGED_HASH_FN_HPP
44
4569a895 45#include <utility>
47bea7b8 46#include <debug/debug.h>
4569a895 47
5e11f978 48namespace __gnu_pbds
4569a895
AT
49{
50 namespace detail
51 {
a345e45d 52 template<typename Key, typename Hash_Fn, typename _Alloc,
1f1a03ef 53 typename Comb_Hash_Fn, bool Store_Hash>
4569a895
AT
54 class ranged_hash_fn;
55
1f1a03ef 56#define PB_DS_CLASS_T_DEC \
a345e45d 57 template<typename Key, typename Hash_Fn, typename _Alloc, \
1f1a03ef
BK
58 typename Comb_Hash_Fn>
59
60#define PB_DS_CLASS_C_DEC \
a345e45d 61 ranged_hash_fn<Key, Hash_Fn, _Alloc, Comb_Hash_Fn, false>
4569a895
AT
62
63 /**
1f1a03ef
BK
64 * Specialization 1
65 * The client supplies a hash function and a ranged hash function,
66 * and requests that hash values not be stored.
4569a895 67 **/
a345e45d 68 template<typename Key, typename Hash_Fn, typename _Alloc,
1f1a03ef 69 typename Comb_Hash_Fn>
a345e45d 70 class ranged_hash_fn< Key, Hash_Fn, _Alloc, Comb_Hash_Fn, false>
1f1a03ef 71 : public Hash_Fn, public Comb_Hash_Fn
4569a895
AT
72 {
73 protected:
a345e45d 74 typedef typename _Alloc::size_type size_type;
4569a895 75 typedef Hash_Fn hash_fn_base;
4569a895 76 typedef Comb_Hash_Fn comb_hash_fn_base;
a345e45d
BK
77 typedef typename _Alloc::template rebind< Key>::other key_allocator;
78 typedef typename key_allocator::const_reference key_const_reference;
4569a895 79
1f1a03ef 80 ranged_hash_fn(size_type);
4569a895 81
1f1a03ef 82 ranged_hash_fn(size_type, const Hash_Fn&);
4569a895 83
1f1a03ef 84 ranged_hash_fn(size_type, const Hash_Fn&, const Comb_Hash_Fn&);
4569a895
AT
85
86 void
1f1a03ef 87 swap(PB_DS_CLASS_C_DEC&);
4569a895
AT
88
89 void
1f1a03ef 90 notify_resized(size_type);
4569a895
AT
91
92 inline size_type
a345e45d 93 operator()(key_const_reference) const;
4569a895
AT
94 };
95
96 PB_DS_CLASS_T_DEC
97 PB_DS_CLASS_C_DEC::
98 ranged_hash_fn(size_type size)
1f1a03ef 99 { Comb_Hash_Fn::notify_resized(size); }
4569a895
AT
100
101 PB_DS_CLASS_T_DEC
102 PB_DS_CLASS_C_DEC::
1f1a03ef
BK
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); }
4569a895
AT
106
107 PB_DS_CLASS_T_DEC
108 PB_DS_CLASS_C_DEC::
1f1a03ef
BK
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); }
4569a895
AT
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);
4569a895
AT
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)
1f1a03ef 127 { comb_hash_fn_base::notify_resized(size); }
4569a895
AT
128
129 PB_DS_CLASS_T_DEC
130 inline typename PB_DS_CLASS_C_DEC::size_type
131 PB_DS_CLASS_C_DEC::
a345e45d 132 operator()(key_const_reference r_key) const
1f1a03ef 133 { return (comb_hash_fn_base::operator()(hash_fn_base::operator()(r_key)));}
4569a895
AT
134
135#undef PB_DS_CLASS_T_DEC
136#undef PB_DS_CLASS_C_DEC
137
1f1a03ef 138#define PB_DS_CLASS_T_DEC \
a345e45d 139 template<typename Key, typename Hash_Fn, typename _Alloc, \
1f1a03ef
BK
140 typename Comb_Hash_Fn>
141
142#define PB_DS_CLASS_C_DEC \
a345e45d 143 ranged_hash_fn<Key,Hash_Fn, _Alloc, Comb_Hash_Fn, true>
4569a895
AT
144
145 /**
1f1a03ef
BK
146 * Specialization 2
147 * The client supplies a hash function and a ranged hash function,
148 * and requests that hash values be stored.
4569a895 149 **/
a345e45d 150 template<typename Key, typename Hash_Fn, typename _Alloc,
1f1a03ef 151 typename Comb_Hash_Fn>
a345e45d 152 class ranged_hash_fn<Key, Hash_Fn, _Alloc, Comb_Hash_Fn, true>
1f1a03ef 153 : public Hash_Fn, public Comb_Hash_Fn
4569a895
AT
154 {
155 protected:
a345e45d 156 typedef typename _Alloc::size_type size_type;
1f1a03ef 157 typedef std::pair<size_type, size_type> comp_hash;
4569a895 158 typedef Hash_Fn hash_fn_base;
4569a895 159 typedef Comb_Hash_Fn comb_hash_fn_base;
a345e45d
BK
160 typedef typename _Alloc::template rebind<Key>::other key_allocator;
161 typedef typename key_allocator::const_reference key_const_reference;
4569a895 162
1f1a03ef 163 ranged_hash_fn(size_type);
4569a895 164
1f1a03ef 165 ranged_hash_fn(size_type, const Hash_Fn&);
4569a895 166
1f1a03ef 167 ranged_hash_fn(size_type, const Hash_Fn&, const Comb_Hash_Fn&);
4569a895
AT
168
169 void
1f1a03ef 170 swap(PB_DS_CLASS_C_DEC&);
4569a895
AT
171
172 void
1f1a03ef 173 notify_resized(size_type);
4569a895
AT
174
175 inline comp_hash
a345e45d 176 operator()(key_const_reference) const;
4569a895
AT
177
178 inline comp_hash
a345e45d 179 operator()(key_const_reference, size_type) const;
4569a895
AT
180 };
181
182 PB_DS_CLASS_T_DEC
183 PB_DS_CLASS_C_DEC::
184 ranged_hash_fn(size_type size)
47bea7b8 185 { Comb_Hash_Fn::notify_resized(size); }
4569a895
AT
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)
47bea7b8 191 { Comb_Hash_Fn::notify_resized(size); }
4569a895
AT
192
193 PB_DS_CLASS_T_DEC
194 PB_DS_CLASS_C_DEC::
1f1a03ef
BK
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)
47bea7b8 198 { comb_hash_fn_base::notify_resized(size); }
4569a895
AT
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);
4569a895
AT
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)
47bea7b8 213 { comb_hash_fn_base::notify_resized(size); }
4569a895
AT
214
215 PB_DS_CLASS_T_DEC
216 inline typename PB_DS_CLASS_C_DEC::comp_hash
217 PB_DS_CLASS_C_DEC::
a345e45d 218 operator()(key_const_reference r_key) const
4569a895
AT
219 {
220 const size_type hash = hash_fn_base::operator()(r_key);
47bea7b8 221 return std::make_pair(comb_hash_fn_base::operator()(hash), hash);
4569a895
AT
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()
47bea7b8 228#ifdef _GLIBCXX_DEBUG
a345e45d 229 (key_const_reference r_key, size_type hash) const
47bea7b8 230#else
a345e45d 231 (key_const_reference /*r_key*/, size_type hash) const
47bea7b8 232#endif
4569a895 233 {
47bea7b8
BK
234 _GLIBCXX_DEBUG_ASSERT(hash == hash_fn_base::operator()(r_key));
235 return std::make_pair(comb_hash_fn_base::operator()(hash), hash);
4569a895
AT
236 }
237
238#undef PB_DS_CLASS_T_DEC
239#undef PB_DS_CLASS_C_DEC
240
1f1a03ef 241#define PB_DS_CLASS_T_DEC \
a345e45d 242 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
4569a895 243
1f1a03ef 244#define PB_DS_CLASS_C_DEC \
a345e45d 245 ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, false>
4569a895
AT
246
247 /**
1f1a03ef
BK
248 * Specialization 3
249 * The client does not supply a hash function (by specifying
a345e45d 250 * null_type as the Hash_Fn parameter), and requests that hash
1f1a03ef
BK
251 * values not be stored.
252 **/
a345e45d
BK
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
4569a895
AT
256 {
257 protected:
a345e45d 258 typedef typename _Alloc::size_type size_type;
4569a895
AT
259 typedef Comb_Hash_Fn comb_hash_fn_base;
260
1f1a03ef 261 ranged_hash_fn(size_type);
4569a895 262
1f1a03ef 263 ranged_hash_fn(size_type, const Comb_Hash_Fn&);
4569a895 264
a345e45d 265 ranged_hash_fn(size_type, const null_type&, const Comb_Hash_Fn&);
4569a895
AT
266
267 void
1f1a03ef 268 swap(PB_DS_CLASS_C_DEC&);
4569a895
AT
269 };
270
271 PB_DS_CLASS_T_DEC
272 PB_DS_CLASS_C_DEC::
273 ranged_hash_fn(size_type size)
47bea7b8 274 { Comb_Hash_Fn::notify_resized(size); }
4569a895
AT
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::
a345e45d 284 ranged_hash_fn(size_type size, const null_type& r_null_type,
1f1a03ef
BK
285 const Comb_Hash_Fn& r_comb_hash_fn)
286 : Comb_Hash_Fn(r_comb_hash_fn)
4569a895
AT
287 { }
288
289 PB_DS_CLASS_T_DEC
290 void
291 PB_DS_CLASS_C_DEC::
292 swap(PB_DS_CLASS_C_DEC& other)
47bea7b8 293 { comb_hash_fn_base::swap(other); }
4569a895
AT
294
295#undef PB_DS_CLASS_T_DEC
296#undef PB_DS_CLASS_C_DEC
297
1f1a03ef 298#define PB_DS_CLASS_T_DEC \
a345e45d 299 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
4569a895 300
1f1a03ef 301#define PB_DS_CLASS_C_DEC \
a345e45d 302 ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, true>
4569a895
AT
303
304 /**
1f1a03ef
BK
305 * Specialization 4
306 * The client does not supply a hash function (by specifying
a345e45d 307 * null_type as the Hash_Fn parameter), and requests that hash
1f1a03ef
BK
308 * values be stored.
309 **/
a345e45d
BK
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
4569a895
AT
313 {
314 protected:
a345e45d 315 typedef typename _Alloc::size_type size_type;
4569a895
AT
316 typedef Comb_Hash_Fn comb_hash_fn_base;
317
1f1a03ef 318 ranged_hash_fn(size_type);
4569a895 319
1f1a03ef 320 ranged_hash_fn(size_type, const Comb_Hash_Fn&);
4569a895 321
a345e45d 322 ranged_hash_fn(size_type, const null_type&, const Comb_Hash_Fn&);
4569a895
AT
323
324 void
1f1a03ef 325 swap(PB_DS_CLASS_C_DEC&);
4569a895
AT
326 };
327
328 PB_DS_CLASS_T_DEC
329 PB_DS_CLASS_C_DEC::
330 ranged_hash_fn(size_type size)
47bea7b8 331 { Comb_Hash_Fn::notify_resized(size); }
4569a895
AT
332
333 PB_DS_CLASS_T_DEC
334 PB_DS_CLASS_C_DEC::
1f1a03ef
BK
335 ranged_hash_fn(size_type size, const Comb_Hash_Fn& r_comb_hash_fn)
336 : Comb_Hash_Fn(r_comb_hash_fn)
4569a895
AT
337 { }
338
339 PB_DS_CLASS_T_DEC
340 PB_DS_CLASS_C_DEC::
a345e45d 341 ranged_hash_fn(size_type size, const null_type& r_null_type,
1f1a03ef
BK
342 const Comb_Hash_Fn& r_comb_hash_fn)
343 : Comb_Hash_Fn(r_comb_hash_fn)
4569a895
AT
344 { }
345
346 PB_DS_CLASS_T_DEC
347 void
348 PB_DS_CLASS_C_DEC::
349 swap(PB_DS_CLASS_C_DEC& other)
47bea7b8 350 { comb_hash_fn_base::swap(other); }
4569a895
AT
351
352#undef PB_DS_CLASS_T_DEC
353#undef PB_DS_CLASS_C_DEC
354
4569a895 355 } // namespace detail
5e11f978 356} // namespace __gnu_pbds
4569a895 357
47bea7b8 358#endif