]>
Commit | Line | Data |
---|---|---|
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 tree_join_example.cpp | |
44 | * An example showing how to join splay_tree_map objects. | |
45 | * The code in the example is relevant to red-black trees as well. | |
46 | */ | |
47 | ||
48 | /** | |
49 | * This example shows how to join tree based containers, i.e., taking two | |
50 | * objects with non-overlapping sets of keys and combining them into | |
51 | * a single object. | |
52 | */ | |
53 | ||
54 | // For tree | |
55 | #include <ext/pb_ds/assoc_container.hpp> | |
56 | // For join_error exception. | |
57 | #include <ext/pb_ds/exception.hpp> | |
58 | // For assert | |
59 | #include <cassert> | |
60 | ||
61 | using namespace std; | |
5e11f978 BK |
62 | using namespace __gnu_pbds; |
63 | using namespace __gnu_pbds; | |
4569a895 AT |
64 | |
65 | int main() | |
66 | { | |
67 | /* | |
68 | * | |
69 | */ | |
70 | // A splay tree table mapping ints to chars. | |
5e11f978 | 71 | typedef tree<int, char, less<int>, splay_tree_tag> map_t; |
4569a895 AT |
72 | |
73 | // Two map_t object. | |
74 | map_t h0, h1; | |
75 | ||
76 | // Insert some values into the first table. | |
77 | for (int i0 = 0; i0 < 100; ++i0) | |
78 | h0.insert(make_pair(i0, 'a')); | |
79 | ||
80 | // Insert some values into the second table. | |
81 | for (int i1 = 0; i1 < 100; ++i1) | |
82 | h1.insert(make_pair(1000 + i1, 'b')); | |
83 | ||
84 | // Since all the elements in h0 are smaller than those in h1, it is | |
85 | // possible to join the two tables. This is exception free. | |
86 | h0.join(h1); | |
87 | ||
88 | // Check that h0 should now contain all entries, and h1 should be empty. | |
89 | assert(h0.size() == 200); | |
90 | assert(h1.empty()); | |
91 | ||
92 | ||
93 | // Two other map_t objects. | |
94 | map_t h2, h3; | |
95 | ||
96 | h2[1] = 'a'; | |
97 | h2[3] = 'c'; | |
98 | ||
99 | h3[2] = 'b'; | |
100 | ||
101 | // Now perform an illegal join. | |
102 | // It is not true that all elements in h2 are smaller than those in | |
103 | // h3, nor is it true that they are all larger. Hence, attempting to | |
104 | // join h2, and h3 should result in an exception. | |
105 | bool exception_thrown = false; | |
106 | try | |
107 | { | |
108 | h2.join(h3); | |
109 | } | |
5e11f978 | 110 | catch (__gnu_pbds::join_error& ) |
4569a895 AT |
111 | { |
112 | exception_thrown = true; | |
113 | } | |
114 | assert(exception_thrown); | |
115 | ||
116 | // Since the operation was not performed, the tables should be unaltered. | |
117 | assert(h2.size() == 2); | |
118 | assert(h3[2] == 'b'); | |
119 | ||
120 | return 0; | |
121 | } | |
122 |