]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/ext/pb_ds/example/priority_queue_split_join.cc
3 // Copyright (C) 2005-2019 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 priority_queue_split_join_example.cpp
34 * A basic example showing how to split and join priority queues.
38 * This example shows how to split and join priority queues.
44 #include <ext/pb_ds/priority_queue.hpp>
47 using namespace __gnu_pbds
;
52 // Two priority queue objects.
53 __gnu_pbds::priority_queue
<int> even_p
, odd_p
;
55 // First we insert some values: even ones into even_p, and odd ones
57 for (size_t i
= 0; i
< 10; ++i
)
63 // Check that each one contains the appropriate 10 values.
64 assert(even_p
.size() == 10);
65 assert(even_p
.top() == 18);
67 // Print out the values.
68 cout
<< "Initial values in even priority queue:" << endl
;
69 __gnu_pbds::priority_queue
<int>::const_iterator it
;
70 for (it
= even_p
.begin(); it
!= even_p
.end(); ++it
)
73 assert(odd_p
.size() == 10);
74 assert(odd_p
.top() == 19);
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
)
81 // Now join the queues.
84 // Check that each one contains the appropriate values.
86 assert(even_p
.size() == 20);
87 assert(even_p
.top() == 19);
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
)
94 assert(odd_p
.size() == 0);
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
)
101 // Now split the queues.
102 even_p
.split(bind2nd(modulus
<int>(), 2), odd_p
);
104 // Check that each one contains the appropriate 10 values.
106 assert(even_p
.size() == 10);
107 assert(even_p
.top() == 18);
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
)
114 assert(odd_p
.size() == 10);
115 assert(odd_p
.top() == 19);
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
)