Dead
[official-gcc.git] / gomp-20050608-branch / libstdc++-v3 / testsuite / ext / pb_assoc / example / hash_load_set_change.cc
blob7c55c7a2e7d1e68802d8e5c92f42213cd63c53e9
1 // -*- C++ -*-
3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
40 /**
41 * @file hash_load_set_change_example.cpp
42 * An example of setting and changing the load factor of a hash-based
43 * container object.
46 // For cc_hash_assoc_cntnr.
47 #include <ext/pb_assoc/assoc_cntnr.hpp>
49 * For hash_standard_resize_policy, hash_exponential_size_policy,
50 * and direct_mask_range_hashing.
52 #include <ext/pb_assoc/hash_policy.hpp>
53 // For unary_function.
54 #include <functional>
55 // For assert
56 #include <cassert>
59 * A simple hash functor.
60 * std::hash could serve instead of this functor,
61 * but it is not yet standard everywhere.
63 struct int_hash : public std::unary_function<
64 int,
65 size_t>
67 inline size_t
68 operator()(int i) const
70 return (i);
74 int
75 main()
77 // A trigger policy type.
78 typedef pb_assoc::hash_load_check_resize_trigger< true> trigger_t;
80 // A resize policy type.
81 typedef
82 pb_assoc::hash_standard_resize_policy<
83 pb_assoc::hash_exponential_size_policy<>,
84 // Trigger type.
85 trigger_t,
86 /* Allow external access to size.
87 * This is not necessary for setting the load factor,
88 * but it allows to call get_actual_size.
90 true>
91 resize_t;
94 * A collision-chaining hash table mapping ints to chars.
96 typedef
97 pb_assoc::cc_hash_assoc_cntnr<
98 int,
99 char,
100 int_hash,
101 std::equal_to<int>,
102 // Combining function.
103 pb_assoc::direct_mask_range_hashing<>,
104 // Resize policy.
105 resize_t>
106 map_t;
108 // A trigger policy object with load between 0.3 and 0.8.
109 trigger_t trigger(static_cast<float>(0.3), static_cast<float>(0.8));
111 // A resize policy object with the above trigger.
112 resize_t resize(pb_assoc::hash_exponential_size_policy<>(),
113 trigger);
115 map_t c(int_hash(),
116 std::equal_to<int>(),
117 pb_assoc::direct_mask_range_hashing<>(),
118 resize);
120 c[1] = 'a';
122 // Check the loads and sizes.
124 assert(c.get_loads().first == static_cast<float>(0.3));
125 assert(c.get_loads().second == static_cast<float>(0.8));
127 assert(c.get_actual_size() == 8);
129 assert(c.size() == 1);
132 * Note that there is a discrepancy between the loads of the policy
133 * object and the actual size of the container object. This is
134 * because the container's construction performs an implicit external
135 * resize.
138 c[2] = 'b';
139 c[3] = 'c';
140 c[4] = 'd';
142 assert(c.get_actual_size() == 8);
144 // Change the loads. This causes (potentially) a resize.
145 c.set_loads(std::make_pair(
146 static_cast<float>(0.01),
147 static_cast<float>(0.05)));
149 // The loads actually changed.
150 assert(c.get_actual_size() > 8);