3 // Copyright (C) 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file ranged_probe_fn.hpp
44 * Contains a unified ranged probe functor, allowing the probe tables to deal with
45 * a single class for ranged probeing.
48 #ifndef PB_DS_RANGED_PROBE_FN_HPP
49 #define PB_DS_RANGED_PROBE_FN_HPP
51 #include <ext/pb_ds/detail/basic_types.hpp>
53 #include <debug/debug.h>
59 template<typename Key, typename Hash_Fn, typename Allocator,
60 typename Comb_Probe_Fn, typename Probe_Fn, bool Store_Hash>
61 class ranged_probe_fn;
63 #define PB_DS_CLASS_T_DEC \
64 template<typename Key, typename Hash_Fn, typename Allocator, \
65 typename Comb_Probe_Fn, typename Probe_Fn>
67 #define PB_DS_CLASS_C_DEC \
68 ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, false>
72 * The client supplies a probe function and a ranged probe
73 * function, and requests that hash values not be stored.
75 template<typename Key, typename Hash_Fn, typename Allocator,
76 typename Comb_Probe_Fn, typename Probe_Fn>
77 class ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn,
79 : public Hash_Fn, public Comb_Probe_Fn, public Probe_Fn
82 typedef typename Allocator::size_type size_type;
83 typedef Comb_Probe_Fn comb_probe_fn_base;
84 typedef Hash_Fn hash_fn_base;
85 typedef Probe_Fn probe_fn_base;
86 typedef typename Allocator::template rebind<Key>::other key_allocator;
87 typedef typename key_allocator::const_reference const_key_reference;
89 ranged_probe_fn(size_type);
91 ranged_probe_fn(size_type, const Hash_Fn&);
93 ranged_probe_fn(size_type, const Hash_Fn&, const Comb_Probe_Fn&);
95 ranged_probe_fn(size_type, const Hash_Fn&, const Comb_Probe_Fn&,
99 swap(PB_DS_CLASS_C_DEC&);
102 notify_resized(size_type);
105 operator()(const_key_reference) const;
108 operator()(const_key_reference, size_type, size_type) const;
113 ranged_probe_fn(size_type size)
114 { Comb_Probe_Fn::notify_resized(size); }
118 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn)
120 { Comb_Probe_Fn::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 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn)
127 { comb_probe_fn_base::notify_resized(size); }
131 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn,
132 const Comb_Probe_Fn& r_comb_probe_fn,
133 const Probe_Fn& r_probe_fn)
134 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn), Probe_Fn(r_probe_fn)
135 { comb_probe_fn_base::notify_resized(size); }
140 swap(PB_DS_CLASS_C_DEC& other)
142 comb_probe_fn_base::swap(other);
143 std::swap((Hash_Fn& )(*this), (Hash_Fn&)other);
149 notify_resized(size_type size)
150 { comb_probe_fn_base::notify_resized(size); }
153 inline typename PB_DS_CLASS_C_DEC::size_type
155 operator()(const_key_reference r_key) const
156 { return comb_probe_fn_base::operator()(hash_fn_base::operator()(r_key)); }
159 inline typename PB_DS_CLASS_C_DEC::size_type
161 operator()(const_key_reference, size_type hash, size_type i) const
163 return comb_probe_fn_base::operator()(hash + probe_fn_base::operator()(i));
166 #undef PB_DS_CLASS_T_DEC
167 #undef PB_DS_CLASS_C_DEC
169 #define PB_DS_CLASS_T_DEC \
170 template<typename Key, typename Hash_Fn, typename Allocator, \
171 typename Comb_Probe_Fn, typename Probe_Fn>
173 #define PB_DS_CLASS_C_DEC \
174 ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, true>
177 * Specialization 2- The client supplies a probe function and a ranged
178 * probe function, and requests that hash values not be stored.
180 template<typename Key, typename Hash_Fn, typename Allocator,
181 typename Comb_Probe_Fn, typename Probe_Fn>
182 class ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn,
184 : public Hash_Fn, public Comb_Probe_Fn, public Probe_Fn
187 typedef typename Allocator::size_type size_type;
188 typedef std::pair<size_type, size_type> comp_hash;
189 typedef Comb_Probe_Fn comb_probe_fn_base;
190 typedef Hash_Fn hash_fn_base;
191 typedef Probe_Fn probe_fn_base;
192 typedef typename Allocator::template rebind<Key>::other key_allocator;
193 typedef typename key_allocator::const_reference const_key_reference;
195 ranged_probe_fn(size_type);
197 ranged_probe_fn(size_type, const Hash_Fn&);
199 ranged_probe_fn(size_type, const Hash_Fn&,
200 const Comb_Probe_Fn&);
202 ranged_probe_fn(size_type, const Hash_Fn&, const Comb_Probe_Fn&,
206 swap(PB_DS_CLASS_C_DEC&);
209 notify_resized(size_type);
212 operator()(const_key_reference) const;
215 operator()(const_key_reference, size_type, size_type) const;
218 operator()(const_key_reference, size_type) const;
223 ranged_probe_fn(size_type size)
224 { Comb_Probe_Fn::notify_resized(size); }
228 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn)
230 { Comb_Probe_Fn::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 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn)
237 { comb_probe_fn_base::notify_resized(size); }
241 ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn,
242 const Comb_Probe_Fn& r_comb_probe_fn,
243 const Probe_Fn& r_probe_fn)
244 : Hash_Fn(r_hash_fn), Comb_Probe_Fn(r_comb_probe_fn), Probe_Fn(r_probe_fn)
245 { comb_probe_fn_base::notify_resized(size); }
250 swap(PB_DS_CLASS_C_DEC& other)
252 comb_probe_fn_base::swap(other);
253 std::swap((Hash_Fn& )(*this), (Hash_Fn& )other);
259 notify_resized(size_type size)
260 { comb_probe_fn_base::notify_resized(size); }
263 inline typename PB_DS_CLASS_C_DEC::comp_hash
265 operator()(const_key_reference r_key) const
267 const size_type hash = hash_fn_base::operator()(r_key);
268 return std::make_pair(comb_probe_fn_base::operator()(hash), hash);
272 inline typename PB_DS_CLASS_C_DEC::size_type
274 operator()(const_key_reference, size_type hash, size_type i) const
276 return comb_probe_fn_base::operator()(hash + probe_fn_base::operator()(i));
280 inline typename PB_DS_CLASS_C_DEC::size_type
283 #ifdef _GLIBCXX_DEBUG
284 (const_key_reference r_key, size_type hash) const
286 (const_key_reference /*r_key*/, size_type hash) const
289 _GLIBCXX_DEBUG_ASSERT(hash == hash_fn_base::operator()(r_key));
293 #undef PB_DS_CLASS_T_DEC
294 #undef PB_DS_CLASS_C_DEC
297 * Specialization 3 and 4
298 * The client does not supply a hash function or probe function,
299 * and requests that hash values not be stored.
301 template<typename Key, typename Allocator, typename Comb_Probe_Fn>
302 class ranged_probe_fn<Key, null_hash_fn, Allocator, Comb_Probe_Fn,
303 null_probe_fn, false>
304 : public Comb_Probe_Fn, public null_hash_fn, public null_probe_fn
307 typedef typename Allocator::size_type size_type;
308 typedef Comb_Probe_Fn comb_probe_fn_base;
309 typedef typename Allocator::template rebind<Key>::other key_allocator;
310 typedef typename key_allocator::const_reference const_key_reference;
312 ranged_probe_fn(size_type size)
313 { Comb_Probe_Fn::notify_resized(size); }
315 ranged_probe_fn(size_type, const Comb_Probe_Fn& r_comb_probe_fn)
316 : Comb_Probe_Fn(r_comb_probe_fn)
319 ranged_probe_fn(size_type, const null_hash_fn&,
320 const Comb_Probe_Fn& r_comb_probe_fn,
321 const null_probe_fn&)
322 : Comb_Probe_Fn(r_comb_probe_fn)
326 swap(ranged_probe_fn& other)
327 { comb_probe_fn_base::swap(other); }
329 } // namespace detail