]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/ext/pb_ds/example/store_hash.cc
re PR target/32338 (Error: .prologue within prologue)
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / store_hash.cc
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
3// Copyright (C) 2005, 2006 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 2, 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 COPYING. If not, write to
18// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19// MA 02111-1307, USA.
20
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
29// Public License.
30
31// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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
40// warranty.
41
42/**
43 * @file store_hash_example.cpp
44 * An example showing how to store hash-values with
45 * each entry in a hash-based container.
46 */
47
48/**
49 * This example shows how to configure a hash-based container to store
50 * the hash value of each key along with each entry. This technique
51 * is useful for complex keys (e.g., strings in this example), since
52 * it lowers the cost of collisions. For simpler types (e.g., integers),
53 * where the cost of comparing keys is of the same order as the cost
54 * of comparing hash values, this technique adds unnecessary overhead.
55 */
56
57#include <functional>
58#include <string>
59#include <ext/pb_ds/assoc_container.hpp>
60#include <ext/pb_ds/hash_policy.hpp>
61
62using namespace std;
63using namespace pb_ds;
64
65// A string hash functor.
66struct string_hash : public unary_function<string, size_t>
67{
68 inline size_t
69 operator()(string str) const
70 {
71 string::const_iterator b = str.begin();
72 string::const_iterator e = str.end();
73
74 size_t hash = 0;
75 while (b != e)
76 {
77 hash *= 5;
78 hash += static_cast<size_t>(*b);
79 ++b;
80 }
81 return hash;
82 }
83};
84
85int main()
86{
87 // A collision-chaining hash table mapping strings to ints.
88 typedef
89 cc_hash_table<
90 string,
91 int,
92 string_hash,
93 equal_to<string>,
94 direct_mask_range_hashing<>,
95 hash_standard_resize_policy<>,
96 true>
97 map_t;
98
99 map_t h;
100
101 // Use regularly.
102 h["Hello, "] = 0;
103 h["world"] = 1;
104
105 return 0;
106}
107