3 // Copyright (C) 2005 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
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
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,
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.
41 * @file hash_load_set_change_example.cpp
42 * An example of setting and changing the load factor of a hash-based
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.
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
<
68 operator()(int i
) const
77 // A trigger policy type.
78 typedef pb_assoc::hash_load_check_resize_trigger
< true> trigger_t
;
80 // A resize policy type.
82 pb_assoc::hash_standard_resize_policy
<
83 pb_assoc::hash_exponential_size_policy
<>,
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.
94 * A collision-chaining hash table mapping ints to chars.
97 pb_assoc::cc_hash_assoc_cntnr
<
102 // Combining function.
103 pb_assoc::direct_mask_range_hashing
<>,
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
<>(),
116 std::equal_to
<int>(),
117 pb_assoc::direct_mask_range_hashing
<>(),
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
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);