]>
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 priority_queue_split_join_example.cpp | |
44 | * A basic example showing how to split and join priority queues. | |
45 | */ | |
46 | ||
47 | /** | |
48 | * This example shows how to split and join priority queues. | |
49 | */ | |
50 | ||
51 | #include <functional> | |
52 | #include <iostream> | |
53 | #include <cassert> | |
54 | #include <ext/pb_ds/priority_queue.hpp> | |
55 | ||
56 | using namespace std; | |
57 | using namespace pb_ds; | |
58 | using namespace pb_ds; | |
59 | ||
60 | int | |
61 | main() | |
62 | { | |
63 | // Two priority queue objects. | |
64 | pb_ds::priority_queue<int> even_p, odd_p; | |
65 | ||
66 | // First we insert some values: even ones into even_p, and odd ones | |
67 | // into odd_p. | |
68 | for (size_t i = 0; i < 10; ++i) | |
69 | { | |
70 | even_p.push(2* i); | |
71 | odd_p.push(2* i + 1); | |
72 | } | |
73 | ||
74 | // Check that each one contains the appropriate 10 values. | |
75 | assert(even_p.size() == 10); | |
76 | assert(even_p.top() == 18); | |
77 | ||
78 | // Print out the values. | |
79 | cout << "Initial values in even priority queue:" << endl; | |
80 | pb_ds::priority_queue<int>::const_iterator it; | |
81 | for (it = even_p.begin(); it != even_p.end(); ++it) | |
82 | cout <<* it << endl; | |
83 | ||
84 | assert(odd_p.size() == 10); | |
85 | assert(odd_p.top() == 19); | |
86 | ||
87 | // Print out the values. | |
88 | cout << "Initial values in odd priority queue:" << endl; | |
89 | for (it = odd_p.begin(); it != odd_p.end(); ++it) | |
90 | cout <<* it << endl; | |
91 | ||
92 | // Now join the queues. | |
93 | even_p.join(odd_p); | |
94 | ||
95 | // Check that each one contains the appropriate values. | |
96 | ||
97 | assert(even_p.size() == 20); | |
98 | assert(even_p.top() == 19); | |
99 | ||
100 | // Print out the values. | |
101 | cout << "After-join values in even priority queue:" << endl; | |
102 | for (it = even_p.begin(); it != even_p.end(); ++it) | |
103 | cout <<* it << endl; | |
104 | ||
105 | assert(odd_p.size() == 0); | |
106 | ||
107 | // Print out the values. | |
108 | cout << "After-join values in odd priority queue:" << endl; | |
109 | for (it = odd_p.begin(); it != odd_p.end(); ++it) | |
110 | cout <<* it << endl; | |
111 | ||
112 | // Now split the queues. | |
113 | even_p.split(bind2nd(modulus<int>(), 2), odd_p); | |
114 | ||
115 | // Check that each one contains the appropriate 10 values. | |
116 | ||
117 | assert(even_p.size() == 10); | |
118 | assert(even_p.top() == 18); | |
119 | ||
120 | // Print out the values. | |
121 | cout << "After-split values in even priority queue:" << endl; | |
122 | for (it = even_p.begin(); it != even_p.end(); ++it) | |
123 | cout <<* it << endl; | |
124 | ||
125 | assert(odd_p.size() == 10); | |
126 | assert(odd_p.top() == 19); | |
127 | ||
128 | // Print out the values. | |
129 | cout << "After-split values in odd priority queue:" << endl; | |
130 | for (it = odd_p.begin(); it != odd_p.end(); ++it) | |
131 | cout <<* it << endl; | |
132 | ||
133 | return 0; | |
134 | } | |
135 |