3 // Copyright (C) 2005, 2006, 2009 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_probe_fn.hpp
38 * Contains a unified ranged probe functor, allowing the probe tables to deal with
39 * a single class for ranged probeing.
42 #ifndef PB_DS_RANGED_PROBE_FN_HPP
43 #define PB_DS_RANGED_PROBE_FN_HPP
46 #include <debug/debug.h>
52 template<typename Key, typename Hash_Fn, typename _Alloc,
53 typename Comb_Probe_Fn, typename Probe_Fn, bool Store_Hash>
54 class ranged_probe_fn;
56 #define PB_DS_CLASS_T_DEC \
57 template<typename Key, typename Hash_Fn, typename _Alloc, \
58 typename Comb_Probe_Fn, typename Probe_Fn>
60 #define PB_DS_CLASS_C_DEC \
61 ranged_probe_fn<Key, Hash_Fn, _Alloc, Comb_Probe_Fn, Probe_Fn, false>
65 * The client supplies a probe function and a ranged probe
66 * function, and requests that hash values not be stored.
68 template<typename Key, typename Hash_Fn, typename _Alloc,
69 typename Comb_Probe_Fn, typename Probe_Fn>
70 class ranged_probe_fn<Key, Hash_Fn, _Alloc, Comb_Probe_Fn,
72 : public Hash_Fn, public Comb_Probe_Fn, public Probe_Fn
75 typedef typename _Alloc::size_type size_type;
76 typedef Comb_Probe_Fn comb_probe_fn_base;
77 typedef Hash_Fn hash_fn_base;
78 typedef Probe_Fn probe_fn_base;
79 typedef typename _Alloc::template rebind<Key>::other key_allocator;
80 typedef typename key_allocator::const_reference key_const_reference;
82 ranged_probe_fn(size_type);
84 ranged_probe_fn(size_type, const Hash_Fn&);
86 ranged_probe_fn(size_type, const Hash_Fn&, const Comb_Probe_Fn&);
88 ranged_probe_fn(size_type, const Hash_Fn&, const Comb_Probe_Fn&,
92 swap(PB_DS_CLASS_C_DEC&);
95 notify_resized(size_type);
98 operator()(key_const_reference) const;
101 operator()(key_const_reference, size_type, size_type) const;
106 ranged_probe_fn(size_type size)
107 { Comb_Probe_Fn::notify_resized(size); }
111 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn)
113 { Comb_Probe_Fn::notify_resized(size); }
117 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn,
118 const Comb_Probe_Fn& r_comb_probe_fn)
119 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn)
120 { comb_probe_fn_base::notify_resized(size); }
124 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn,
125 const Comb_Probe_Fn& r_comb_probe_fn,
126 const Probe_Fn& r_probe_fn)
127 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn), Probe_Fn(r_probe_fn)
128 { comb_probe_fn_base::notify_resized(size); }
133 swap(PB_DS_CLASS_C_DEC& other)
135 comb_probe_fn_base::swap(other);
136 std::swap((Hash_Fn& )(*this), (Hash_Fn&)other);
142 notify_resized(size_type size)
143 { comb_probe_fn_base::notify_resized(size); }
146 inline typename PB_DS_CLASS_C_DEC::size_type
148 operator()(key_const_reference r_key) const
149 { return comb_probe_fn_base::operator()(hash_fn_base::operator()(r_key)); }
152 inline typename PB_DS_CLASS_C_DEC::size_type
154 operator()(key_const_reference, size_type hash, size_type i) const
156 return comb_probe_fn_base::operator()(hash + probe_fn_base::operator()(i));
159 #undef PB_DS_CLASS_T_DEC
160 #undef PB_DS_CLASS_C_DEC
162 #define PB_DS_CLASS_T_DEC \
163 template<typename Key, typename Hash_Fn, typename _Alloc, \
164 typename Comb_Probe_Fn, typename Probe_Fn>
166 #define PB_DS_CLASS_C_DEC \
167 ranged_probe_fn<Key, Hash_Fn, _Alloc, Comb_Probe_Fn, Probe_Fn, true>
170 * Specialization 2- The client supplies a probe function and a ranged
171 * probe function, and requests that hash values not be stored.
173 template<typename Key, typename Hash_Fn, typename _Alloc,
174 typename Comb_Probe_Fn, typename Probe_Fn>
175 class ranged_probe_fn<Key, Hash_Fn, _Alloc, Comb_Probe_Fn,
177 : public Hash_Fn, public Comb_Probe_Fn, public Probe_Fn
180 typedef typename _Alloc::size_type size_type;
181 typedef std::pair<size_type, size_type> comp_hash;
182 typedef Comb_Probe_Fn comb_probe_fn_base;
183 typedef Hash_Fn hash_fn_base;
184 typedef Probe_Fn probe_fn_base;
185 typedef typename _Alloc::template rebind<Key>::other key_allocator;
186 typedef typename key_allocator::const_reference key_const_reference;
188 ranged_probe_fn(size_type);
190 ranged_probe_fn(size_type, const Hash_Fn&);
192 ranged_probe_fn(size_type, const Hash_Fn&,
193 const Comb_Probe_Fn&);
195 ranged_probe_fn(size_type, const Hash_Fn&, const Comb_Probe_Fn&,
199 swap(PB_DS_CLASS_C_DEC&);
202 notify_resized(size_type);
205 operator()(key_const_reference) const;
208 operator()(key_const_reference, size_type, size_type) const;
211 operator()(key_const_reference, size_type) const;
216 ranged_probe_fn(size_type size)
217 { Comb_Probe_Fn::notify_resized(size); }
221 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn)
223 { Comb_Probe_Fn::notify_resized(size); }
227 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn,
228 const Comb_Probe_Fn& r_comb_probe_fn)
229 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn)
230 { comb_probe_fn_base::notify_resized(size); }
234 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn,
235 const Comb_Probe_Fn& r_comb_probe_fn,
236 const Probe_Fn& r_probe_fn)
237 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn), Probe_Fn(r_probe_fn)
238 { comb_probe_fn_base::notify_resized(size); }
243 swap(PB_DS_CLASS_C_DEC& other)
245 comb_probe_fn_base::swap(other);
246 std::swap((Hash_Fn& )(*this), (Hash_Fn& )other);
252 notify_resized(size_type size)
253 { comb_probe_fn_base::notify_resized(size); }
256 inline typename PB_DS_CLASS_C_DEC::comp_hash
258 operator()(key_const_reference r_key) const
260 const size_type hash = hash_fn_base::operator()(r_key);
261 return std::make_pair(comb_probe_fn_base::operator()(hash), hash);
265 inline typename PB_DS_CLASS_C_DEC::size_type
267 operator()(key_const_reference, size_type hash, size_type i) const
269 return comb_probe_fn_base::operator()(hash + probe_fn_base::operator()(i));
273 inline typename PB_DS_CLASS_C_DEC::size_type
276 #ifdef _GLIBCXX_DEBUG
277 (key_const_reference r_key, size_type hash) const
279 (key_const_reference /*r_key*/, size_type hash) const
282 _GLIBCXX_DEBUG_ASSERT(hash == hash_fn_base::operator()(r_key));
286 #undef PB_DS_CLASS_T_DEC
287 #undef PB_DS_CLASS_C_DEC
290 * Specialization 3 and 4
291 * The client does not supply a hash function or probe function,
292 * and requests that hash values not be stored.
294 template<typename Key, typename _Alloc, typename Comb_Probe_Fn>
295 class ranged_probe_fn<Key, null_type, _Alloc, Comb_Probe_Fn,
297 : public Comb_Probe_Fn
300 typedef typename _Alloc::size_type size_type;
301 typedef Comb_Probe_Fn comb_probe_fn_base;
302 typedef typename _Alloc::template rebind<Key>::other key_allocator;
303 typedef typename key_allocator::const_reference key_const_reference;
305 ranged_probe_fn(size_type size)
306 { Comb_Probe_Fn::notify_resized(size); }
308 ranged_probe_fn(size_type, const Comb_Probe_Fn& r_comb_probe_fn)
309 : Comb_Probe_Fn(r_comb_probe_fn)
312 ranged_probe_fn(size_type, const null_type&,
313 const Comb_Probe_Fn& r_comb_probe_fn,
315 : Comb_Probe_Fn(r_comb_probe_fn)
319 swap(ranged_probe_fn& other)
320 { comb_probe_fn_base::swap(other); }
322 } // namespace detail
323 } // namespace __gnu_pbds