OSDN Git Service

bab267c7afffa0bc483848591fa020576e263264
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / hash_illegal_resize.cc
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2009 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 3, 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 COPYING3.  If not see
18 // <http://www.gnu.org/licenses/>.
19
20
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
22
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
30 // warranty.
31
32 /**
33  * @file hash_illegal_resize_example.cpp
34  * An example of illegally
35  *    externally resizing a hash-based container object.
36  */
37
38 /**
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.
42  */
43
44 #include <ext/pb_ds/assoc_container.hpp>
45 #include <ext/pb_ds/hash_policy.hpp>
46 #include <ext/pb_ds/exception.hpp>
47 #include <cassert>
48
49 using namespace std;
50 using namespace __gnu_pbds;
51
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>
56 {
57   inline size_t
58   operator()(const int& r_i) const
59   { return r_i; }
60 };
61
62
63 int main()
64 {
65   // A probing hash table mapping ints to chars.
66   typedef
67     gp_hash_table<
68     int,
69     int,
70     int_hash,
71     equal_to<
72     int>,
73     // Combining function.
74     direct_mod_range_hashing<>,
75     // Probe function.
76     quadratic_probe_fn<>,
77     // Resize policy.
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
83      *     is not possible.
84      */
85     true> >
86     map_t;
87
88   map_t g;
89
90   // Insert some elements.
91   int i;
92
93   for (i = 0; i < 1000; ++i)
94     g[i] = 2*  i;
95
96   // Check all ok.
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);
100
101   // Now attempt to resize the table to 200 (impossible).
102   bool ex_thrown = false;
103
104   try
105     {
106       g.resize(200);
107     }
108   catch(__gnu_pbds::resize_error& )
109     {
110       ex_thrown = true;
111     }
112
113   // Assert an exception was thrown. A probing table cannot contain
114   // 1000 entries in less than 1000 places.
115   assert(ex_thrown);
116
117   // Irrespective of the fact that the resize was not successful, the
118   // container object should still be in a valid state; the following
119   // checks this.
120   // Check all ok.
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);
124
125   return 0;
126 }
127