]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/ext/pb_ds/example/hash_load_set_change.cc
*: Change namespace pb_ds to __gnu_pbds.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / hash_load_set_change.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 hash_load_set_change_example.cpp
44 * An example of setting and changing the load factor of a hash-based
45 * container object.
46 */
47
48/**
49 * This example shows how to set and change the load-factor of
50 * a hash-based container object through its resize-policy object.
51 */
52
53#include <functional>
54#include <cassert>
55#include <ext/pb_ds/assoc_container.hpp>
56#include <ext/pb_ds/hash_policy.hpp>
57
58using namespace std;
5e11f978 59using namespace __gnu_pbds;
4569a895
AT
60
61// A simple hash functor.
62// hash could serve instead of this functor, but it is not yet
63// standard everywhere.
64struct int_hash : public unary_function<int, size_t>
65{
66 inline size_t
67 operator()(int i) const
68 { return i; }
69};
70
71int main()
72{
73 // A trigger policy type.
74 typedef hash_load_check_resize_trigger< true> trigger_t;
75
76 // A resize policy type.
77 typedef
78 hash_standard_resize_policy<
79 hash_exponential_size_policy<>,
80 // Trigger type.
81 trigger_t,
82 /* Allow external access to size.
83 * This is not necessary for setting the load factor,
84 * but it allows to call get_actual_size.
85 */
86 true>
87 resize_t;
88
89 // A collision-chaining hash table mapping ints to chars.
90 typedef
91 cc_hash_table<
92 int,
93 char,
94 int_hash,
95 equal_to<int>,
96 // Combining function.
97 direct_mask_range_hashing<>,
98 // Resize policy.
99 resize_t>
100 map_t;
101
102 // A trigger policy object with load between 0.3 and 0.8.
103 trigger_t trigger(static_cast<float>(0.3), static_cast<float>(0.8));
104
105 // A resize policy object with the above trigger.
106 resize_t resize(hash_exponential_size_policy<>(),
107 trigger);
108
109 map_t r_c(int_hash(),
110 equal_to<int>(),
111 direct_mask_range_hashing<>(),
112 resize);
113
114 r_c[1] = 'a';
115
116 // Check the loads and sizes.
117 assert(r_c.get_loads().first == static_cast<float>(0.3));
118 assert(r_c.get_loads().second == static_cast<float>(0.8));
119 assert(r_c.get_actual_size() == 8);
120 assert(r_c.size() == 1);
121
122 // Note that there is a discrepancy between the loads of the policy
123 // object and the actual size of the container object. This is
124 // because the container's construction performs an implicit
125 // external resize.
126 r_c[2] = 'b';
127 r_c[3] = 'c';
128 r_c[4] = 'd';
129
130 assert(r_c.get_actual_size() == 8);
131
132 // Change the loads. This causes (potentially) a resize.
133 r_c.set_loads(make_pair(static_cast<float>(0.01),
134 static_cast<float>(0.05)));
135
136 // The actual size should really change in this case.
137 assert(r_c.get_actual_size() > 8);
138
139 return 0;
140}
141