]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 2005-2021 Free Software Foundation, Inc. |
4569a895 AT |
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 | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
4569a895 AT |
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 | |
748086b7 JJ |
17 | // along with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
19 | ||
4569a895 AT |
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 priority_queue_split_join_example.cpp | |
34 | * A basic example showing how to split and join priority queues. | |
35 | */ | |
36 | ||
37 | /** | |
38 | * This example shows how to split and join priority queues. | |
39 | */ | |
40 | ||
41 | #include <functional> | |
42 | #include <iostream> | |
43 | #include <cassert> | |
44 | #include <ext/pb_ds/priority_queue.hpp> | |
45 | ||
46 | using namespace std; | |
5e11f978 | 47 | using namespace __gnu_pbds; |
4569a895 AT |
48 | |
49 | int | |
50 | main() | |
51 | { | |
52 | // Two priority queue objects. | |
5e11f978 | 53 | __gnu_pbds::priority_queue<int> even_p, odd_p; |
4569a895 AT |
54 | |
55 | // First we insert some values: even ones into even_p, and odd ones | |
56 | // into odd_p. | |
57 | for (size_t i = 0; i < 10; ++i) | |
58 | { | |
59 | even_p.push(2* i); | |
60 | odd_p.push(2* i + 1); | |
61 | } | |
62 | ||
63 | // Check that each one contains the appropriate 10 values. | |
64 | assert(even_p.size() == 10); | |
65 | assert(even_p.top() == 18); | |
66 | ||
67 | // Print out the values. | |
68 | cout << "Initial values in even priority queue:" << endl; | |
5e11f978 | 69 | __gnu_pbds::priority_queue<int>::const_iterator it; |
4569a895 AT |
70 | for (it = even_p.begin(); it != even_p.end(); ++it) |
71 | cout <<* it << endl; | |
72 | ||
73 | assert(odd_p.size() == 10); | |
74 | assert(odd_p.top() == 19); | |
75 | ||
76 | // Print out the values. | |
77 | cout << "Initial values in odd priority queue:" << endl; | |
78 | for (it = odd_p.begin(); it != odd_p.end(); ++it) | |
79 | cout <<* it << endl; | |
80 | ||
81 | // Now join the queues. | |
82 | even_p.join(odd_p); | |
83 | ||
84 | // Check that each one contains the appropriate values. | |
85 | ||
86 | assert(even_p.size() == 20); | |
87 | assert(even_p.top() == 19); | |
88 | ||
89 | // Print out the values. | |
90 | cout << "After-join values in even priority queue:" << endl; | |
91 | for (it = even_p.begin(); it != even_p.end(); ++it) | |
92 | cout <<* it << endl; | |
93 | ||
94 | assert(odd_p.size() == 0); | |
95 | ||
96 | // Print out the values. | |
97 | cout << "After-join values in odd priority queue:" << endl; | |
98 | for (it = odd_p.begin(); it != odd_p.end(); ++it) | |
99 | cout <<* it << endl; | |
100 | ||
101 | // Now split the queues. | |
102 | even_p.split(bind2nd(modulus<int>(), 2), odd_p); | |
103 | ||
104 | // Check that each one contains the appropriate 10 values. | |
105 | ||
106 | assert(even_p.size() == 10); | |
107 | assert(even_p.top() == 18); | |
108 | ||
109 | // Print out the values. | |
110 | cout << "After-split values in even priority queue:" << endl; | |
111 | for (it = even_p.begin(); it != even_p.end(); ++it) | |
112 | cout <<* it << endl; | |
113 | ||
114 | assert(odd_p.size() == 10); | |
115 | assert(odd_p.top() == 19); | |
116 | ||
117 | // Print out the values. | |
118 | cout << "After-split values in odd priority queue:" << endl; | |
119 | for (it = odd_p.begin(); it != odd_p.end(); ++it) | |
120 | cout <<* it << endl; | |
121 | ||
122 | return 0; | |
123 | } | |
124 |