3 // Copyright (C) 2005, 2006, 2009, 2011 Free Software Foundation, Inc.
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
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
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.
42 #ifndef PB_DS_RANGED_HASH_FN_HPP
43 #define PB_DS_RANGED_HASH_FN_HPP
46 #include <debug/debug.h>
52 template<typename Key, typename Hash_Fn, typename _Alloc,
53 typename Comb_Hash_Fn, bool Store_Hash>
56 #define PB_DS_CLASS_T_DEC \
57 template<typename Key, typename Hash_Fn, typename _Alloc, \
58 typename Comb_Hash_Fn>
60 #define PB_DS_CLASS_C_DEC \
61 ranged_hash_fn<Key, Hash_Fn, _Alloc, Comb_Hash_Fn, false>
65 * The client supplies a hash function and a ranged hash function,
66 * and requests that hash values not be stored.
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
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;
80 ranged_hash_fn(size_type);
82 ranged_hash_fn(size_type, const Hash_Fn&);
84 ranged_hash_fn(size_type, const Hash_Fn&, const Comb_Hash_Fn&);
87 swap(PB_DS_CLASS_C_DEC&);
90 notify_resized(size_type);
93 operator()(key_const_reference) const;
98 ranged_hash_fn(size_type size)
99 { Comb_Hash_Fn::notify_resized(size); }
103 ranged_hash_fn(size_type size, const Hash_Fn& r_hash_fn)
105 { Comb_Hash_Fn::notify_resized(size); }
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); }
117 swap(PB_DS_CLASS_C_DEC& other)
119 comb_hash_fn_base::swap(other);
120 std::swap((Hash_Fn& )(*this), (Hash_Fn& )other);
126 notify_resized(size_type size)
127 { comb_hash_fn_base::notify_resized(size); }
130 inline typename PB_DS_CLASS_C_DEC::size_type
132 operator()(key_const_reference r_key) const
133 { return (comb_hash_fn_base::operator()(hash_fn_base::operator()(r_key)));}
135 #undef PB_DS_CLASS_T_DEC
136 #undef PB_DS_CLASS_C_DEC
138 #define PB_DS_CLASS_T_DEC \
139 template<typename Key, typename Hash_Fn, typename _Alloc, \
140 typename Comb_Hash_Fn>
142 #define PB_DS_CLASS_C_DEC \
143 ranged_hash_fn<Key,Hash_Fn, _Alloc, Comb_Hash_Fn, true>
147 * The client supplies a hash function and a ranged hash function,
148 * and requests that hash values be stored.
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
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;
163 ranged_hash_fn(size_type);
165 ranged_hash_fn(size_type, const Hash_Fn&);
167 ranged_hash_fn(size_type, const Hash_Fn&, const Comb_Hash_Fn&);
170 swap(PB_DS_CLASS_C_DEC&);
173 notify_resized(size_type);
176 operator()(key_const_reference) const;
179 operator()(key_const_reference, size_type) const;
184 ranged_hash_fn(size_type size)
185 { Comb_Hash_Fn::notify_resized(size); }
189 ranged_hash_fn(size_type size, const Hash_Fn& r_hash_fn) :
191 { Comb_Hash_Fn::notify_resized(size); }
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); }
203 swap(PB_DS_CLASS_C_DEC& other)
205 comb_hash_fn_base::swap(other);
206 std::swap((Hash_Fn& )(*this), (Hash_Fn& )other);
212 notify_resized(size_type size)
213 { comb_hash_fn_base::notify_resized(size); }
216 inline typename PB_DS_CLASS_C_DEC::comp_hash
218 operator()(key_const_reference r_key) const
220 const size_type hash = hash_fn_base::operator()(r_key);
221 return std::make_pair(comb_hash_fn_base::operator()(hash), hash);
225 inline typename PB_DS_CLASS_C_DEC::comp_hash
228 #ifdef _GLIBCXX_DEBUG
229 (key_const_reference r_key, size_type hash) const
231 (key_const_reference /*r_key*/, size_type hash) const
234 _GLIBCXX_DEBUG_ASSERT(hash == hash_fn_base::operator()(r_key));
235 return std::make_pair(comb_hash_fn_base::operator()(hash), hash);
238 #undef PB_DS_CLASS_T_DEC
239 #undef PB_DS_CLASS_C_DEC
241 #define PB_DS_CLASS_T_DEC \
242 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
244 #define PB_DS_CLASS_C_DEC \
245 ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, false>
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.
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
258 typedef typename _Alloc::size_type size_type;
259 typedef Comb_Hash_Fn comb_hash_fn_base;
261 ranged_hash_fn(size_type);
263 ranged_hash_fn(size_type, const Comb_Hash_Fn&);
265 ranged_hash_fn(size_type, const null_type&, const Comb_Hash_Fn&);
268 swap(PB_DS_CLASS_C_DEC&);
273 ranged_hash_fn(size_type size)
274 { Comb_Hash_Fn::notify_resized(size); }
278 ranged_hash_fn(size_type size, const Comb_Hash_Fn& r_comb_hash_fn) :
279 Comb_Hash_Fn(r_comb_hash_fn)
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)
292 swap(PB_DS_CLASS_C_DEC& other)
293 { comb_hash_fn_base::swap(other); }
295 #undef PB_DS_CLASS_T_DEC
296 #undef PB_DS_CLASS_C_DEC
298 #define PB_DS_CLASS_T_DEC \
299 template<typename Key, typename _Alloc, typename Comb_Hash_Fn>
301 #define PB_DS_CLASS_C_DEC \
302 ranged_hash_fn<Key, null_type, _Alloc, Comb_Hash_Fn, true>
306 * The client does not supply a hash function (by specifying
307 * null_type as the Hash_Fn parameter), and requests that hash
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
315 typedef typename _Alloc::size_type size_type;
316 typedef Comb_Hash_Fn comb_hash_fn_base;
318 ranged_hash_fn(size_type);
320 ranged_hash_fn(size_type, const Comb_Hash_Fn&);
322 ranged_hash_fn(size_type, const null_type&, const Comb_Hash_Fn&);
325 swap(PB_DS_CLASS_C_DEC&);
330 ranged_hash_fn(size_type size)
331 { Comb_Hash_Fn::notify_resized(size); }
335 ranged_hash_fn(size_type size, const Comb_Hash_Fn& r_comb_hash_fn)
336 : Comb_Hash_Fn(r_comb_hash_fn)
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)
349 swap(PB_DS_CLASS_C_DEC& other)
350 { comb_hash_fn_base::swap(other); }
352 #undef PB_DS_CLASS_T_DEC
353 #undef PB_DS_CLASS_C_DEC
355 } // namespace detail
356 } // namespace __gnu_pbds