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_hash_fn.hpp
44 * Contains a unified ranged hash functor, allowing the hash tables
45 * to deal with a single class for ranged hashing.
48 #ifndef PB_DS_RANGED_HASH_FN_HPP
49 #define PB_DS_RANGED_HASH_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_Hash_Fn
, bool Store_Hash
>
63 #define PB_DS_CLASS_T_DEC \
64 template<typename Key, typename Hash_Fn, typename Allocator, \
65 typename Comb_Hash_Fn>
67 #define PB_DS_CLASS_C_DEC \
68 ranged_hash_fn<Key, Hash_Fn, Allocator, Comb_Hash_Fn, false>
72 * The client supplies a hash function and a ranged hash function,
73 * and requests that hash values not be stored.
75 template<typename Key
, typename Hash_Fn
, typename Allocator
,
76 typename Comb_Hash_Fn
>
77 class ranged_hash_fn
< Key
, Hash_Fn
, Allocator
, Comb_Hash_Fn
, false>
78 : public Hash_Fn
, public Comb_Hash_Fn
81 typedef typename
Allocator::size_type size_type
;
82 typedef Hash_Fn hash_fn_base
;
83 typedef Comb_Hash_Fn comb_hash_fn_base
;
84 typedef typename
Allocator::template rebind
< Key
>::other key_allocator
;
85 typedef typename
key_allocator::const_reference const_key_reference
;
87 ranged_hash_fn(size_type
);
89 ranged_hash_fn(size_type
, const Hash_Fn
&);
91 ranged_hash_fn(size_type
, const Hash_Fn
&, const Comb_Hash_Fn
&);
94 swap(PB_DS_CLASS_C_DEC
&);
97 notify_resized(size_type
);
100 operator()(const_key_reference
) const;
105 ranged_hash_fn(size_type size
)
106 { Comb_Hash_Fn::notify_resized(size
); }
110 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
)
112 { Comb_Hash_Fn::notify_resized(size
); }
116 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
,
117 const Comb_Hash_Fn
& r_comb_hash_fn
)
118 : Hash_Fn(r_hash_fn
), Comb_Hash_Fn(r_comb_hash_fn
)
119 { comb_hash_fn_base::notify_resized(size
); }
124 swap(PB_DS_CLASS_C_DEC
& other
)
126 comb_hash_fn_base::swap(other
);
127 std::swap((Hash_Fn
& )(*this), (Hash_Fn
& )other
);
133 notify_resized(size_type size
)
134 { comb_hash_fn_base::notify_resized(size
); }
137 inline typename
PB_DS_CLASS_C_DEC::size_type
139 operator()(const_key_reference r_key
) const
140 { return (comb_hash_fn_base::operator()(hash_fn_base::operator()(r_key
)));}
142 #undef PB_DS_CLASS_T_DEC
143 #undef PB_DS_CLASS_C_DEC
145 #define PB_DS_CLASS_T_DEC \
146 template<typename Key, typename Hash_Fn, typename Allocator, \
147 typename Comb_Hash_Fn>
149 #define PB_DS_CLASS_C_DEC \
150 ranged_hash_fn<Key,Hash_Fn, Allocator, Comb_Hash_Fn, true>
154 * The client supplies a hash function and a ranged hash function,
155 * and requests that hash values be stored.
157 template<typename Key
, typename Hash_Fn
, typename Allocator
,
158 typename Comb_Hash_Fn
>
159 class ranged_hash_fn
<Key
, Hash_Fn
, Allocator
, Comb_Hash_Fn
, true>
160 : public Hash_Fn
, public Comb_Hash_Fn
163 typedef typename
Allocator::size_type size_type
;
164 typedef std::pair
<size_type
, size_type
> comp_hash
;
165 typedef Hash_Fn hash_fn_base
;
166 typedef Comb_Hash_Fn comb_hash_fn_base
;
167 typedef typename
Allocator::template rebind
<Key
>::other key_allocator
;
168 typedef typename
key_allocator::const_reference const_key_reference
;
170 ranged_hash_fn(size_type
);
172 ranged_hash_fn(size_type
, const Hash_Fn
&);
174 ranged_hash_fn(size_type
, const Hash_Fn
&, const Comb_Hash_Fn
&);
177 swap(PB_DS_CLASS_C_DEC
&);
180 notify_resized(size_type
);
183 operator()(const_key_reference
) const;
186 operator()(const_key_reference
, size_type
) const;
191 ranged_hash_fn(size_type size
)
192 { Comb_Hash_Fn::notify_resized(size
); }
196 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
) :
198 { Comb_Hash_Fn::notify_resized(size
); }
202 ranged_hash_fn(size_type size
, const Hash_Fn
& r_hash_fn
,
203 const Comb_Hash_Fn
& r_comb_hash_fn
)
204 : Hash_Fn(r_hash_fn
), Comb_Hash_Fn(r_comb_hash_fn
)
205 { comb_hash_fn_base::notify_resized(size
); }
210 swap(PB_DS_CLASS_C_DEC
& other
)
212 comb_hash_fn_base::swap(other
);
213 std::swap((Hash_Fn
& )(*this), (Hash_Fn
& )other
);
219 notify_resized(size_type size
)
220 { comb_hash_fn_base::notify_resized(size
); }
223 inline typename
PB_DS_CLASS_C_DEC::comp_hash
225 operator()(const_key_reference r_key
) const
227 const size_type hash
= hash_fn_base::operator()(r_key
);
228 return std::make_pair(comb_hash_fn_base::operator()(hash
), hash
);
232 inline typename
PB_DS_CLASS_C_DEC::comp_hash
235 #ifdef _GLIBCXX_DEBUG
236 (const_key_reference r_key
, size_type hash
) const
238 (const_key_reference
/*r_key*/, size_type hash
) const
241 _GLIBCXX_DEBUG_ASSERT(hash
== hash_fn_base::operator()(r_key
));
242 return std::make_pair(comb_hash_fn_base::operator()(hash
), hash
);
245 #undef PB_DS_CLASS_T_DEC
246 #undef PB_DS_CLASS_C_DEC
248 #define PB_DS_CLASS_T_DEC \
249 template<typename Key, typename Allocator, typename Comb_Hash_Fn>
251 #define PB_DS_CLASS_C_DEC \
252 ranged_hash_fn<Key, null_hash_fn, Allocator, Comb_Hash_Fn, false>
256 * The client does not supply a hash function (by specifying
257 * null_hash_fn as the Hash_Fn parameter), and requests that hash
258 * values not be stored.
260 template<typename Key
, typename Allocator
, typename Comb_Hash_Fn
>
261 class ranged_hash_fn
<Key
, null_hash_fn
, Allocator
, Comb_Hash_Fn
, false>
262 : public null_hash_fn
, public Comb_Hash_Fn
265 typedef typename
Allocator::size_type size_type
;
266 typedef Comb_Hash_Fn comb_hash_fn_base
;
268 ranged_hash_fn(size_type
);
270 ranged_hash_fn(size_type
, const Comb_Hash_Fn
&);
272 ranged_hash_fn(size_type
, const null_hash_fn
&, const Comb_Hash_Fn
&);
275 swap(PB_DS_CLASS_C_DEC
&);
280 ranged_hash_fn(size_type size
)
281 { Comb_Hash_Fn::notify_resized(size
); }
285 ranged_hash_fn(size_type size
, const Comb_Hash_Fn
& r_comb_hash_fn
) :
286 Comb_Hash_Fn(r_comb_hash_fn
)
291 ranged_hash_fn(size_type size
, const null_hash_fn
& r_null_hash_fn
,
292 const Comb_Hash_Fn
& r_comb_hash_fn
)
293 : Comb_Hash_Fn(r_comb_hash_fn
)
299 swap(PB_DS_CLASS_C_DEC
& other
)
300 { comb_hash_fn_base::swap(other
); }
302 #undef PB_DS_CLASS_T_DEC
303 #undef PB_DS_CLASS_C_DEC
305 #define PB_DS_CLASS_T_DEC \
306 template<typename Key, typename Allocator, typename Comb_Hash_Fn>
308 #define PB_DS_CLASS_C_DEC \
309 ranged_hash_fn<Key, null_hash_fn, Allocator, Comb_Hash_Fn, true>
313 * The client does not supply a hash function (by specifying
314 * null_hash_fn as the Hash_Fn parameter), and requests that hash
317 template<typename Key
, typename Allocator
, typename Comb_Hash_Fn
>
318 class ranged_hash_fn
<Key
, null_hash_fn
, Allocator
, Comb_Hash_Fn
, true>
319 : public null_hash_fn
, public Comb_Hash_Fn
322 typedef typename
Allocator::size_type size_type
;
323 typedef Comb_Hash_Fn comb_hash_fn_base
;
325 ranged_hash_fn(size_type
);
327 ranged_hash_fn(size_type
, const Comb_Hash_Fn
&);
329 ranged_hash_fn(size_type
, const null_hash_fn
&, const Comb_Hash_Fn
&);
332 swap(PB_DS_CLASS_C_DEC
&);
337 ranged_hash_fn(size_type size
)
338 { Comb_Hash_Fn::notify_resized(size
); }
342 ranged_hash_fn(size_type size
, const Comb_Hash_Fn
& r_comb_hash_fn
)
343 : Comb_Hash_Fn(r_comb_hash_fn
)
348 ranged_hash_fn(size_type size
, const null_hash_fn
& r_null_hash_fn
,
349 const Comb_Hash_Fn
& r_comb_hash_fn
)
350 : Comb_Hash_Fn(r_comb_hash_fn
)
356 swap(PB_DS_CLASS_C_DEC
& other
)
357 { comb_hash_fn_base::swap(other
); }
359 #undef PB_DS_CLASS_T_DEC
360 #undef PB_DS_CLASS_C_DEC
362 } // namespace detail
363 } // namespace __gnu_pbds