| 1 | // -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 4 | // |
| 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 |
| 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 COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | |
| 21 | // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. |
| 22 | |
| 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 |
| 30 | // warranty. |
| 31 | |
| 32 | /** |
| 33 | * @file store_hash_example.cpp |
| 34 | * An example showing how to store hash-values with |
| 35 | * each entry in a hash-based container. |
| 36 | */ |
| 37 | |
| 38 | /** |
| 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. |
| 45 | */ |
| 46 | |
| 47 | #include <functional> |
| 48 | #include <string> |
| 49 | #include <ext/pb_ds/assoc_container.hpp> |
| 50 | #include <ext/pb_ds/hash_policy.hpp> |
| 51 | |
| 52 | using namespace std; |
| 53 | using namespace __gnu_pbds; |
| 54 | |
| 55 | // A string hash functor. |
| 56 | struct string_hash |
| 57 | { |
| 58 | size_t |
| 59 | operator()(string str) const |
| 60 | { |
| 61 | string::const_iterator b = str.begin(); |
| 62 | string::const_iterator e = str.end(); |
| 63 | |
| 64 | size_t hash = 0; |
| 65 | while (b != e) |
| 66 | { |
| 67 | hash *= 5; |
| 68 | hash += static_cast<size_t>(*b); |
| 69 | ++b; |
| 70 | } |
| 71 | return hash; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | int main() |
| 76 | { |
| 77 | // A collision-chaining hash table mapping strings to ints. |
| 78 | typedef |
| 79 | cc_hash_table< |
| 80 | string, |
| 81 | int, |
| 82 | string_hash, |
| 83 | equal_to<string>, |
| 84 | direct_mask_range_hashing<>, |
| 85 | hash_standard_resize_policy<>, |
| 86 | true> |
| 87 | map_t; |
| 88 | |
| 89 | map_t h; |
| 90 | |
| 91 | // Use regularly. |
| 92 | h["Hello, "] = 0; |
| 93 | h["world"] = 1; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 |