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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
33 * @file hash_shift_mask_example.cpp
34 * An example showing how to write a range-hasing functor.
38 * In some rare cases, advance knowledge of the distribution of keys allows
39 * writing more efficient hash-related policies.
40 * In the rather simplistic case of the example, it is known in advance that
41 * all keys have 0 two lowest bits. The example shows how to write
42 * a range-hashing function disregarding the two lowest bits of the hash value.
46 #include <ext/pb_ds/assoc_container.hpp>
47 #include <ext/pb_ds/hash_policy.hpp>
50 using namespace __gnu_pbds
;
52 // A simple hash functor. hash could serve instead of this functor,
53 // but it is not yet standard everywhere.
54 struct simple_int_hash
: public unary_function
<int, size_t>
57 operator()(int i
) const
61 // A range-hashing function which shifts 2 bits right and then masks.
62 class shift_two_mask_range_hashing
: private direct_mask_range_hashing
<>
65 typedef size_t size_type
;
67 // Swaps with a different instant.
69 swap(shift_two_mask_range_hashing
& other
)
70 { direct_mask_range_hashing
<>::swap(other
); }
72 // Called by the container when internally resized.
74 notify_resized(size_type size
)
75 { direct_mask_range_hashing
<>::notify_resized(size
); }
77 // Given a hash value, returns a number in the range of the internal
78 // size of the container.
80 operator()(size_type hash
) const
81 { return direct_mask_range_hashing
<>::operator()(hash
>> 2); }
87 // A collision-chaining hash table mapping ints to chars.
95 // Range hashing function.
96 shift_two_mask_range_hashing
>