3 // Copyright (C) 2005, 2006, 2009 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 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
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.
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/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
33 * @file hash_illegal_resize_example.cpp
34 * An example of illegally
35 * externally resizing a hash-based container object.
39 * This example shows the case where a hash-based container object is
40 * resized to a value which it cannot accomodate at runtime. Illegal
41 * runtime resizes cause an exception.
44 #include <ext/pb_ds/assoc_container.hpp>
45 #include <ext/pb_ds/hash_policy.hpp>
46 #include <ext/pb_ds/exception.hpp>
50 using namespace __gnu_pbds
;
52 // A simple hash functor.
53 // hash could serve instead of this functor, but it is not yet
54 // standard everywhere.
55 struct int_hash
: public unary_function
<int, size_t>
58 operator()(const int& r_i
) const
65 // A probing hash table mapping ints to chars.
73 // Combining function.
74 direct_mod_range_hashing
<>,
78 hash_standard_resize_policy
<
79 hash_prime_size_policy
,
80 hash_load_check_resize_trigger
<>,
81 /* Allow external access to size.
82 * Without setting this to true, external resizing
90 // Insert some elements.
93 for (i
= 0; i
< 1000; ++i
)
97 assert(g
.size() == 1000);
98 for (i
= 0; i
< 1000; ++i
)
99 assert(g
.find(i
) != g
.end()&& g
.find(i
)->second
== 2* i
);
101 // Now attempt to resize the table to 200 (impossible).
102 bool ex_thrown
= false;
108 catch(__gnu_pbds::resize_error
& )
113 // Assert an exception was thrown. A probing table cannot contain
114 // 1000 entries in less than 1000 places.
117 // Irrespective of the fact that the resize was not successful, the
118 // container object should still be in a valid state; the following
121 assert(g
.size() == 1000);
122 for (i
= 0; i
< 1000; ++i
)
123 assert(g
.find(i
) != g
.end()&& g
.find(i
)->second
== 2* i
);