3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file ranged_hash_fn.hpp
42 * Contains a unified ranged hash functor, allowing the hash tables to deal with
43 * a single class for ranged hashing.
46 #ifndef RANGED_HASH_FN_HPP
47 #define RANGED_HASH_FN_HPP
57 #ifdef PB_ASSOC_RANGED_HASH_FN_DEBUG
58 #define PB_ASSOC_DBG_ASSERT(X) assert(X)
59 #define PB_ASSOC_DBG_VERIFY(X) assert(X)
60 #define PB_ASSOC_DBG_ONLY(X) X
61 #else // #ifdef PB_ASSOC_RANGED_HASH_FN_DEBUG
62 #define PB_ASSOC_DBG_ASSERT(X)
63 #define PB_ASSOC_DBG_VERIFY(X) {if((X)==0);}
64 #define PB_ASSOC_DBG_ONLY(X) ;
65 #endif // #ifdef PB_ASSOC_RANGED_HASH_FN_DEBUG
67 template<typename Key
,
74 #define PB_ASSOC_CLASS_T_DEC \
81 #define PB_ASSOC_CLASS_C_DEC \
90 * Specialization 1- The client supplies a hash function and a ranged
91 * hash function, and requests that hash values not be stored.
93 template<typename Key
,
97 class ranged_hash_fn
<Key
, Hash_Fn
, Allocator
, Comb_Hash_Fn
, false> : public Hash_Fn
,
101 typedef typename
Allocator::size_type size_type
;
103 typedef Hash_Fn my_hash_fn_base
;
105 typedef Comb_Hash_Fn my_comb_hash_fn_base
;
107 typedef typename
Allocator::template rebind
<Key
>::other key_allocator
;
109 typedef typename
key_allocator::const_reference const_key_reference
;
112 ranged_hash_fn(size_type size
);
114 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
);
116 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
);
119 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
122 notify_resized(size_type size
);
125 operator()(const_key_reference r_key
) const;
129 PB_ASSOC_CLASS_C_DEC::
130 ranged_hash_fn(size_type size
)
132 Comb_Hash_Fn::notify_resized(size
);
136 PB_ASSOC_CLASS_C_DEC::
137 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
) :
140 Comb_Hash_Fn::notify_resized(size
);
144 PB_ASSOC_CLASS_C_DEC::
145 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
) :
147 Comb_Hash_Fn(r_comb_hash_fn
)
149 my_comb_hash_fn_base::notify_resized(size
);
154 PB_ASSOC_CLASS_C_DEC::
155 swap(PB_ASSOC_CLASS_C_DEC
& r_other
)
157 my_comb_hash_fn_base::swap(r_other
);
159 std::swap((Hash_Fn
& )(*this), (Hash_Fn
& )r_other
);
164 PB_ASSOC_CLASS_C_DEC::
165 notify_resized(size_type size
)
167 my_comb_hash_fn_base::notify_resized(size
);
171 inline typename
PB_ASSOC_CLASS_C_DEC::size_type
172 PB_ASSOC_CLASS_C_DEC::
173 operator()(const_key_reference r_key
) const
175 return (my_comb_hash_fn_base::operator()(
176 my_hash_fn_base::operator()(r_key
)));
179 #undef PB_ASSOC_CLASS_T_DEC
180 #undef PB_ASSOC_CLASS_C_DEC
182 #define PB_ASSOC_CLASS_T_DEC \
189 #define PB_ASSOC_CLASS_C_DEC \
198 * Specialization 2- The client supplies a hash function and a ranged
199 * hash function, and requests that hash values be stored.
201 template<typename Key
,
205 class ranged_hash_fn
<Key
, Hash_Fn
, Allocator
, Comb_Hash_Fn
, true> :
210 typedef typename
Allocator::size_type size_type
;
212 typedef std::pair
<size_type
, size_type
> comp_hash
;
214 typedef Hash_Fn my_hash_fn_base
;
216 typedef Comb_Hash_Fn my_comb_hash_fn_base
;
218 typedef typename
Allocator::template rebind
<Key
>::other key_allocator
;
220 typedef typename
key_allocator::const_reference const_key_reference
;
223 ranged_hash_fn(size_type size
);
225 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
);
227 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
);
230 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
233 notify_resized(size_type size
);
236 operator()(const_key_reference r_key
) const;
239 operator()(const_key_reference r_key
, size_type hash
) const;
243 PB_ASSOC_CLASS_C_DEC::
244 ranged_hash_fn(size_type size
)
246 Comb_Hash_Fn::notify_resized(size
);
250 PB_ASSOC_CLASS_C_DEC::
251 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
) :
254 Comb_Hash_Fn::notify_resized(size
);
258 PB_ASSOC_CLASS_C_DEC::
259 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
) :
261 Comb_Hash_Fn(r_comb_hash_fn
)
263 my_comb_hash_fn_base::notify_resized(size
);
268 PB_ASSOC_CLASS_C_DEC::
269 swap(PB_ASSOC_CLASS_C_DEC
& r_other
)
271 my_comb_hash_fn_base::swap(r_other
);
273 std::swap((Hash_Fn
& )(*this), (Hash_Fn
& )r_other
);
278 PB_ASSOC_CLASS_C_DEC::
279 notify_resized(size_type size
)
281 my_comb_hash_fn_base::notify_resized(size
);
285 inline typename
PB_ASSOC_CLASS_C_DEC::comp_hash
286 PB_ASSOC_CLASS_C_DEC::
287 operator()(const_key_reference r_key
) const
289 const size_type hash
= my_hash_fn_base::operator()(r_key
);
291 return (std::make_pair(my_comb_hash_fn_base::operator()(hash
), hash
));
295 inline typename
PB_ASSOC_CLASS_C_DEC::comp_hash
296 PB_ASSOC_CLASS_C_DEC::
298 #ifdef PB_ASSOC_RANGED_HASH_FN_DEBUG
299 (const_key_reference r_key
, size_type hash
) const
300 #else // #ifdef PB_ASSOC_RANGED_HASH_FN_DEBUG
301 (const_key_reference
/*r_key*/, size_type hash
) const
302 #endif // #ifdef PB_ASSOC_RANGED_HASH_FN_DEBUG
304 PB_ASSOC_DBG_ASSERT(hash
== my_hash_fn_base::operator()(r_key
));
306 return (std::make_pair(my_comb_hash_fn_base::operator()(hash
), hash
));
309 #undef PB_ASSOC_CLASS_T_DEC
310 #undef PB_ASSOC_CLASS_C_DEC
312 #define PB_ASSOC_CLASS_T_DEC \
313 template<typename Key, class Allocator, class Comb_Hash_Fn>
315 #define PB_ASSOC_CLASS_C_DEC \
324 * Specialization 3- The client does not supply a hash function
325 * (by specifying null_hash_fn as the Hash_Fn parameter),
326 * and requests that hash values not be stored.
329 template<typename Key
, class Allocator
, class Comb_Hash_Fn
>
330 class ranged_hash_fn
<Key
, null_hash_fn
, Allocator
,
331 Comb_Hash_Fn
, false> :
337 typedef typename
Allocator::size_type size_type
;
339 typedef Comb_Hash_Fn my_comb_hash_fn_base
;
342 ranged_hash_fn(size_type size
);
344 ranged_hash_fn(size_type size
, const Comb_Hash_Fn
& r_comb_hash_fn
);
346 ranged_hash_fn(size_type size
, const null_hash_fn
&r_null_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
);
349 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
353 PB_ASSOC_CLASS_C_DEC::
354 ranged_hash_fn(size_type size
)
356 Comb_Hash_Fn::notify_resized(size
);
360 PB_ASSOC_CLASS_C_DEC::
361 ranged_hash_fn(size_type size
, const Comb_Hash_Fn
& r_comb_hash_fn
) :
362 Comb_Hash_Fn(r_comb_hash_fn
)
366 PB_ASSOC_CLASS_C_DEC::
367 ranged_hash_fn(size_type size
, const null_hash_fn
& r_null_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
) :
368 Comb_Hash_Fn(r_comb_hash_fn
)
373 PB_ASSOC_CLASS_C_DEC::
374 swap(PB_ASSOC_CLASS_C_DEC
& r_other
)
376 my_comb_hash_fn_base::swap(r_other
);
379 #undef PB_ASSOC_CLASS_T_DEC
380 #undef PB_ASSOC_CLASS_C_DEC
382 #define PB_ASSOC_CLASS_T_DEC \
383 template<typename Key, class Allocator, class Comb_Hash_Fn>
385 #define PB_ASSOC_CLASS_C_DEC \
394 * Specialization 4- The client does not supply a hash function
395 * (by specifying null_hash_fn as the Hash_Fn parameter),
396 * and requests that hash values be stored.
399 template<typename Key
, class Allocator
, class Comb_Hash_Fn
>
400 class ranged_hash_fn
<Key
, null_hash_fn
, Allocator
,
401 Comb_Hash_Fn
, true> :
406 typedef typename
Allocator::size_type size_type
;
408 typedef Comb_Hash_Fn my_comb_hash_fn_base
;
411 ranged_hash_fn(size_type size
);
413 ranged_hash_fn(size_type size
, const Comb_Hash_Fn
& r_comb_hash_fn
);
415 ranged_hash_fn(size_type size
, const null_hash_fn
&r_null_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
);
418 swap(PB_ASSOC_CLASS_C_DEC
& r_other
);
422 PB_ASSOC_CLASS_C_DEC::
423 ranged_hash_fn(size_type size
)
425 Comb_Hash_Fn::notify_resized(size
);
429 PB_ASSOC_CLASS_C_DEC::
430 ranged_hash_fn(size_type size
, const Comb_Hash_Fn
& r_comb_hash_fn
) :
431 Comb_Hash_Fn(r_comb_hash_fn
)
435 PB_ASSOC_CLASS_C_DEC::
436 ranged_hash_fn(size_type size
, const null_hash_fn
&r_null_hash_fn
, const Comb_Hash_Fn
& r_comb_hash_fn
) :
437 Comb_Hash_Fn(r_comb_hash_fn
)
442 PB_ASSOC_CLASS_C_DEC::
443 swap(PB_ASSOC_CLASS_C_DEC
& r_other
)
445 my_comb_hash_fn_base::swap(r_other
);
448 #undef PB_ASSOC_CLASS_T_DEC
449 #undef PB_ASSOC_CLASS_C_DEC
451 #undef PB_ASSOC_DBG_ASSERT
452 #undef PB_ASSOC_DBG_VERIFY
453 #undef PB_ASSOC_DBG_ONLY
455 } // namespace detail
457 } // namespace pb_assoc
459 #endif // #ifndef RANGED_HASH_FN_HPP