3 // Copyright (C) 2005-2017 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 store_hash_example.cpp
34 * An example showing how to store hash-values with
35 * each entry in a hash-based container.
39 * This example shows how to configure a hash-based container to store
40 * the hash value of each key along with each entry. This technique
41 * is useful for complex keys (e.g., strings in this example), since
42 * it lowers the cost of collisions. For simpler types (e.g., integers),
43 * where the cost of comparing keys is of the same order as the cost
44 * of comparing hash values, this technique adds unnecessary overhead.
49 #include <ext/pb_ds/assoc_container.hpp>
50 #include <ext/pb_ds/hash_policy.hpp>
53 using namespace __gnu_pbds
;
55 // A string hash functor.
56 struct string_hash
: public unary_function
<string
, size_t>
59 operator()(string str
) const
61 string::const_iterator b
= str
.begin();
62 string::const_iterator e
= str
.end();
68 hash
+= static_cast<size_t>(*b
);
77 // A collision-chaining hash table mapping strings to ints.
84 direct_mask_range_hashing
<>,
85 hash_standard_resize_policy
<>,