]>
Commit | Line | Data |
---|---|---|
fd1e1726 BK |
1 | // -*- C++ -*- |
2 | ||
4569a895 | 3 | // Copyright (C) 2005, 2006 Free Software Foundation, Inc. |
fd1e1726 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
4569a895 AT |
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 | |
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 | // 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. | |
20 | ||
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 | |
29 | // Public License. | |
fd1e1726 BK |
30 | |
31 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. | |
32 | ||
33 | // Permission to use, copy, modify, sell, and distribute this software | |
34 | // is hereby granted without fee, provided that the above copyright | |
4569a895 AT |
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 | |
fd1e1726 | 38 | // representation about the suitability of this software for any |
4569a895 AT |
39 | // purpose. It is provided "as is" without express or implied |
40 | // warranty. | |
fd1e1726 BK |
41 | |
42 | /** | |
43 | * @file ranged_probe_fn.hpp | |
44 | * Contains a unified ranged probe functor, allowing the probe tables to deal with | |
4569a895 | 45 | * a single class for ranged probeing. |
fd1e1726 BK |
46 | */ |
47 | ||
4569a895 AT |
48 | #ifndef PB_DS_RANGED_PROBE_FN_HPP |
49 | #define PB_DS_RANGED_PROBE_FN_HPP | |
fd1e1726 | 50 | |
4569a895 | 51 | #include <ext/pb_ds/detail/basic_types.hpp> |
fd1e1726 | 52 | #include <utility> |
47bea7b8 | 53 | #include <debug/debug.h> |
fd1e1726 | 54 | |
4569a895 | 55 | namespace pb_ds |
fd1e1726 | 56 | { |
fd1e1726 BK |
57 | namespace detail |
58 | { | |
fd1e1726 | 59 | template<typename Key, |
3441f106 BK |
60 | typename Hash_Fn, |
61 | typename Allocator, | |
62 | typename Comb_Probe_Fn, | |
63 | typename Probe_Fn, | |
fd1e1726 BK |
64 | bool Store_Hash> |
65 | class ranged_probe_fn; | |
66 | ||
3441f106 BK |
67 | #define PB_DS_CLASS_T_DEC \ |
68 | template<typename Key, typename Hash_Fn, typename Allocator, \ | |
69 | typename Comb_Probe_Fn, typename Probe_Fn> | |
70 | ||
71 | #define PB_DS_CLASS_C_DEC \ | |
72 | ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, false> | |
fd1e1726 BK |
73 | |
74 | /** | |
75 | * Specialization 1- The client supplies a probe function and a ranged | |
4569a895 | 76 | * probe function, and requests that hash values not be stored. |
fd1e1726 | 77 | **/ |
3441f106 BK |
78 | template<typename Key, typename Hash_Fn, typename Allocator, |
79 | typename Comb_Probe_Fn, typename Probe_Fn> | |
80 | class ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, | |
81 | Probe_Fn, false> | |
82 | : public Hash_Fn, public Comb_Probe_Fn, public Probe_Fn | |
fd1e1726 BK |
83 | { |
84 | protected: | |
85 | typedef typename Allocator::size_type size_type; | |
86 | ||
4569a895 | 87 | typedef Comb_Probe_Fn comb_probe_fn_base; |
fd1e1726 | 88 | |
4569a895 | 89 | typedef Hash_Fn hash_fn_base; |
fd1e1726 | 90 | |
4569a895 | 91 | typedef Probe_Fn probe_fn_base; |
fd1e1726 | 92 | |
4569a895 | 93 | typedef typename Allocator::template rebind<Key>::other key_allocator; |
fd1e1726 BK |
94 | |
95 | typedef typename key_allocator::const_reference const_key_reference; | |
96 | ||
97 | protected: | |
98 | ranged_probe_fn(size_type size); | |
99 | ||
100 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn); | |
101 | ||
102 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, const Comb_Probe_Fn& r_comb_probe_fn); | |
103 | ||
104 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, const Comb_Probe_Fn& r_comb_probe_fn, const Probe_Fn& r_probe_fn); | |
105 | ||
106 | void | |
4569a895 | 107 | swap(PB_DS_CLASS_C_DEC& other); |
fd1e1726 BK |
108 | |
109 | void | |
110 | notify_resized(size_type size); | |
111 | ||
112 | inline size_type | |
113 | operator()(const_key_reference r_key) const; | |
114 | ||
115 | inline size_type | |
116 | operator()(const_key_reference r_key, size_type hash, size_type i) const; | |
117 | }; | |
118 | ||
4569a895 AT |
119 | PB_DS_CLASS_T_DEC |
120 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 | 121 | ranged_probe_fn(size_type size) |
3441f106 | 122 | { Comb_Probe_Fn::notify_resized(size); } |
fd1e1726 | 123 | |
4569a895 AT |
124 | PB_DS_CLASS_T_DEC |
125 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 BK |
126 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn) : |
127 | Hash_Fn(r_hash_fn) | |
3441f106 | 128 | { Comb_Probe_Fn::notify_resized(size); } |
fd1e1726 | 129 | |
4569a895 AT |
130 | PB_DS_CLASS_T_DEC |
131 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 BK |
132 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, const Comb_Probe_Fn& r_comb_probe_fn) : |
133 | Hash_Fn(r_hash_fn), | |
134 | Comb_Probe_Fn(r_comb_probe_fn) | |
3441f106 | 135 | { comb_probe_fn_base::notify_resized(size); } |
fd1e1726 | 136 | |
4569a895 AT |
137 | PB_DS_CLASS_T_DEC |
138 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 BK |
139 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, const Comb_Probe_Fn& r_comb_probe_fn, const Probe_Fn& r_probe_fn) : |
140 | Hash_Fn(r_hash_fn), | |
141 | Comb_Probe_Fn(r_comb_probe_fn), | |
142 | Probe_Fn(r_probe_fn) | |
3441f106 | 143 | { comb_probe_fn_base::notify_resized(size); } |
fd1e1726 | 144 | |
4569a895 | 145 | PB_DS_CLASS_T_DEC |
fd1e1726 | 146 | void |
4569a895 AT |
147 | PB_DS_CLASS_C_DEC:: |
148 | swap(PB_DS_CLASS_C_DEC& other) | |
fd1e1726 | 149 | { |
4569a895 | 150 | comb_probe_fn_base::swap(other); |
3441f106 | 151 | std::swap((Hash_Fn& )(*this), (Hash_Fn&)other); |
fd1e1726 BK |
152 | } |
153 | ||
4569a895 | 154 | PB_DS_CLASS_T_DEC |
fd1e1726 | 155 | void |
4569a895 | 156 | PB_DS_CLASS_C_DEC:: |
fd1e1726 | 157 | notify_resized(size_type size) |
3441f106 | 158 | { comb_probe_fn_base::notify_resized(size); } |
fd1e1726 | 159 | |
4569a895 AT |
160 | PB_DS_CLASS_T_DEC |
161 | inline typename PB_DS_CLASS_C_DEC::size_type | |
162 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 | 163 | operator()(const_key_reference r_key) const |
3441f106 | 164 | { return comb_probe_fn_base::operator()(hash_fn_base::operator()(r_key)); } |
fd1e1726 | 165 | |
4569a895 AT |
166 | PB_DS_CLASS_T_DEC |
167 | inline typename PB_DS_CLASS_C_DEC::size_type | |
168 | PB_DS_CLASS_C_DEC:: | |
3441f106 | 169 | operator()(const_key_reference, size_type hash, size_type i) const |
fd1e1726 | 170 | { |
3441f106 | 171 | return comb_probe_fn_base::operator()(hash + probe_fn_base::operator()(i)); |
fd1e1726 BK |
172 | } |
173 | ||
4569a895 AT |
174 | #undef PB_DS_CLASS_T_DEC |
175 | #undef PB_DS_CLASS_C_DEC | |
176 | ||
3441f106 BK |
177 | #define PB_DS_CLASS_T_DEC \ |
178 | template<typename Key, class Hash_Fn, class Allocator, \ | |
179 | class Comb_Probe_Fn, class Probe_Fn> | |
180 | ||
181 | #define PB_DS_CLASS_C_DEC \ | |
182 | ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, Probe_Fn, true> | |
fd1e1726 BK |
183 | |
184 | /** | |
185 | * Specialization 2- The client supplies a probe function and a ranged | |
4569a895 | 186 | * probe function, and requests that hash values not be stored. |
fd1e1726 | 187 | **/ |
3441f106 BK |
188 | template<typename Key, typename Hash_Fn, typename Allocator, |
189 | typename Comb_Probe_Fn, typename Probe_Fn> | |
190 | class ranged_probe_fn<Key, Hash_Fn, Allocator, Comb_Probe_Fn, | |
191 | Probe_Fn, true> | |
192 | : public Hash_Fn, public Comb_Probe_Fn, public Probe_Fn | |
fd1e1726 BK |
193 | { |
194 | protected: | |
195 | typedef typename Allocator::size_type size_type; | |
196 | ||
4569a895 | 197 | typedef typename comp_hash_<size_type>::comp_hash comp_hash; |
fd1e1726 | 198 | |
4569a895 | 199 | typedef Comb_Probe_Fn comb_probe_fn_base; |
fd1e1726 | 200 | |
4569a895 | 201 | typedef Hash_Fn hash_fn_base; |
fd1e1726 | 202 | |
4569a895 | 203 | typedef Probe_Fn probe_fn_base; |
fd1e1726 BK |
204 | |
205 | typedef typename Allocator::template rebind<Key>::other key_allocator; | |
206 | ||
207 | typedef typename key_allocator::const_reference const_key_reference; | |
208 | ||
fd1e1726 BK |
209 | ranged_probe_fn(size_type size); |
210 | ||
211 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn); | |
212 | ||
3441f106 BK |
213 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, |
214 | const Comb_Probe_Fn& r_comb_probe_fn); | |
fd1e1726 | 215 | |
3441f106 BK |
216 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, |
217 | const Comb_Probe_Fn& r_comb_probe_fn, | |
218 | const Probe_Fn& r_probe_fn); | |
fd1e1726 BK |
219 | |
220 | void | |
4569a895 | 221 | swap(PB_DS_CLASS_C_DEC& other); |
fd1e1726 BK |
222 | |
223 | void | |
224 | notify_resized(size_type size); | |
225 | ||
226 | inline comp_hash | |
227 | operator()(const_key_reference r_key) const; | |
228 | ||
229 | inline size_type | |
230 | operator()(const_key_reference r_key, size_type hash, size_type i) const; | |
231 | ||
232 | inline size_type | |
233 | operator()(const_key_reference r_key, size_type hash) const; | |
234 | }; | |
235 | ||
4569a895 AT |
236 | PB_DS_CLASS_T_DEC |
237 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 | 238 | ranged_probe_fn(size_type size) |
47bea7b8 | 239 | { Comb_Probe_Fn::notify_resized(size); } |
fd1e1726 | 240 | |
4569a895 AT |
241 | PB_DS_CLASS_T_DEC |
242 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 BK |
243 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn) : |
244 | Hash_Fn(r_hash_fn) | |
47bea7b8 | 245 | { Comb_Probe_Fn::notify_resized(size); } |
fd1e1726 | 246 | |
4569a895 AT |
247 | PB_DS_CLASS_T_DEC |
248 | PB_DS_CLASS_C_DEC:: | |
3441f106 BK |
249 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, |
250 | const Comb_Probe_Fn& r_comb_probe_fn) : | |
fd1e1726 BK |
251 | Hash_Fn(r_hash_fn), |
252 | Comb_Probe_Fn(r_comb_probe_fn) | |
47bea7b8 | 253 | { comb_probe_fn_base::notify_resized(size); } |
fd1e1726 | 254 | |
4569a895 AT |
255 | PB_DS_CLASS_T_DEC |
256 | PB_DS_CLASS_C_DEC:: | |
3441f106 BK |
257 | ranged_probe_fn(size_type size, const Hash_Fn& r_hash_fn, |
258 | const Comb_Probe_Fn& r_comb_probe_fn, | |
259 | const Probe_Fn& r_probe_fn) : | |
fd1e1726 BK |
260 | Hash_Fn(r_hash_fn), |
261 | Comb_Probe_Fn(r_comb_probe_fn), | |
262 | Probe_Fn(r_probe_fn) | |
47bea7b8 | 263 | { comb_probe_fn_base::notify_resized(size); } |
fd1e1726 | 264 | |
4569a895 | 265 | PB_DS_CLASS_T_DEC |
fd1e1726 | 266 | void |
4569a895 AT |
267 | PB_DS_CLASS_C_DEC:: |
268 | swap(PB_DS_CLASS_C_DEC& other) | |
fd1e1726 | 269 | { |
4569a895 | 270 | comb_probe_fn_base::swap(other); |
4569a895 | 271 | std::swap((Hash_Fn& )(*this), (Hash_Fn& )other); |
fd1e1726 BK |
272 | } |
273 | ||
4569a895 | 274 | PB_DS_CLASS_T_DEC |
fd1e1726 | 275 | void |
4569a895 | 276 | PB_DS_CLASS_C_DEC:: |
fd1e1726 | 277 | notify_resized(size_type size) |
47bea7b8 | 278 | { comb_probe_fn_base::notify_resized(size); } |
fd1e1726 | 279 | |
4569a895 AT |
280 | PB_DS_CLASS_T_DEC |
281 | inline typename PB_DS_CLASS_C_DEC::comp_hash | |
282 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 BK |
283 | operator()(const_key_reference r_key) const |
284 | { | |
4569a895 | 285 | const size_type hash = hash_fn_base::operator()(r_key); |
47bea7b8 | 286 | return std::make_pair(comb_probe_fn_base::operator()(hash), hash); |
fd1e1726 BK |
287 | } |
288 | ||
4569a895 AT |
289 | PB_DS_CLASS_T_DEC |
290 | inline typename PB_DS_CLASS_C_DEC::size_type | |
291 | PB_DS_CLASS_C_DEC:: | |
3441f106 | 292 | operator()(const_key_reference, size_type hash, size_type i) const |
fd1e1726 | 293 | { |
47bea7b8 | 294 | return comb_probe_fn_base::operator()(hash + probe_fn_base::operator()(i)); |
fd1e1726 BK |
295 | } |
296 | ||
4569a895 AT |
297 | PB_DS_CLASS_T_DEC |
298 | inline typename PB_DS_CLASS_C_DEC::size_type | |
299 | PB_DS_CLASS_C_DEC:: | |
fd1e1726 | 300 | operator() |
47bea7b8 | 301 | #ifdef _GLIBCXX_DEBUG |
fd1e1726 | 302 | (const_key_reference r_key, size_type hash) const |
47bea7b8 | 303 | #else |
fd1e1726 | 304 | (const_key_reference /*r_key*/, size_type hash) const |
47bea7b8 | 305 | #endif |
fd1e1726 | 306 | { |
47bea7b8 BK |
307 | _GLIBCXX_DEBUG_ASSERT(hash == hash_fn_base::operator()(r_key)); |
308 | return hash; | |
fd1e1726 BK |
309 | } |
310 | ||
4569a895 AT |
311 | #undef PB_DS_CLASS_T_DEC |
312 | #undef PB_DS_CLASS_C_DEC | |
fd1e1726 | 313 | |
fd1e1726 BK |
314 | /** |
315 | * Specialization 3 and 4- The client does not supply a hash function or | |
4569a895 | 316 | * probe function, and requests that hash values not be stored. |
fd1e1726 | 317 | **/ |
3441f106 BK |
318 | template<typename Key, typename Allocator, typename Comb_Probe_Fn> |
319 | class ranged_probe_fn<Key, null_hash_fn, Allocator, Comb_Probe_Fn, | |
320 | null_probe_fn, false> | |
321 | : public Comb_Probe_Fn, public null_hash_fn, public null_probe_fn | |
fd1e1726 BK |
322 | { |
323 | protected: | |
324 | typedef typename Allocator::size_type size_type; | |
325 | ||
4569a895 | 326 | typedef Comb_Probe_Fn comb_probe_fn_base; |
fd1e1726 BK |
327 | |
328 | typedef typename Allocator::template rebind<Key>::other key_allocator; | |
329 | ||
330 | typedef typename key_allocator::const_reference const_key_reference; | |
331 | ||
3441f106 BK |
332 | ranged_probe_fn(size_type size) |
333 | { Comb_Probe_Fn::notify_resized(size); } | |
fd1e1726 | 334 | |
3441f106 BK |
335 | ranged_probe_fn(size_type size, const Comb_Probe_Fn& r_comb_probe_fn) |
336 | : Comb_Probe_Fn(r_comb_probe_fn) | |
337 | { } | |
fd1e1726 | 338 | |
3441f106 BK |
339 | ranged_probe_fn(size_type size, const null_hash_fn& r_null_hash_fn, |
340 | const Comb_Probe_Fn& r_comb_probe_fn, | |
341 | const null_probe_fn& r_null_probe_fn) | |
342 | : Comb_Probe_Fn(r_comb_probe_fn) | |
343 | { } | |
fd1e1726 BK |
344 | |
345 | void | |
3441f106 BK |
346 | swap(ranged_probe_fn& other) |
347 | { comb_probe_fn_base::swap(other); } | |
fd1e1726 | 348 | }; |
fd1e1726 | 349 | } // namespace detail |
4569a895 | 350 | } // namespace pb_ds |
fd1e1726 | 351 | |
47bea7b8 | 352 | #endif |
fd1e1726 | 353 |