PR libstdc++/62045 fix O(N) insertion in pd_ds binary heap
[official-gcc.git] / libstdc++-v3 / testsuite / ext / pb_ds / regression / priority_queues.cc
blob6b27ac42fcc586a1c913712e5504b184439b1dca
1 // -*- C++ -*-
3 // Copyright (C) 2005-2017 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.
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
30 // warranty.
32 /**
33 * This example shows how to use priority queues. It defines a
34 * function performing a sequence of operations on
35 * a generic container. It then calls this function with some containers.
38 #include <ext/pb_ds/priority_queue.hpp>
39 #include <iostream>
40 #include <cassert>
42 using namespace std;
43 using namespace __gnu_pbds;
44 using namespace __gnu_pbds;
46 template<typename Cntnr>
47 void
48 some_op_sequence(Cntnr& r_c)
50 assert(r_c.empty());
51 assert(r_c.size() == 0);
53 for (size_t i = 0; i < 10; ++i)
54 r_c.push(i);
56 cout << endl << "All values in the container:" << endl;
57 for (typename Cntnr::const_iterator it = r_c.begin(); it != r_c.end();
58 ++it)
59 cout <<* it << endl;
61 assert(!r_c.empty());
62 assert(r_c.size() == 10);
64 cout << "Popping all values: " << endl;
66 while (!r_c.empty())
68 cout << r_c.top() << endl;
70 r_c.pop();
73 assert(r_c.empty());
74 assert(r_c.size() == 0);
76 cout << endl;
79 void
80 priority_queue_link_regression_test_0()
84 * Perform operations on a pairing-heap queue.
86 cout << "Pairing heap" << endl;
87 __gnu_pbds::priority_queue<int, less<int>, pairing_heap_tag> c;
88 some_op_sequence(c);
93 * Perform operations on a binomial-heap queue.
95 cout << "Binomial heap" << endl;
96 __gnu_pbds::priority_queue<int, less<int>, binomial_heap_tag> c;
97 some_op_sequence(c);
102 * Perform operations on a binomial-heap queue.
104 cout << "Redundant-counter binomial heap" << endl;
105 __gnu_pbds::priority_queue<int, less<int>, rc_binomial_heap_tag> c;
106 some_op_sequence(c);
111 * Perform operations on a binary-heap queue.
113 cout << "Binary heap" << endl;
114 __gnu_pbds::priority_queue<int, less<int>, binary_heap_tag> c;
115 some_op_sequence(c);
120 * Perform operations on a thin-heap queue.
122 cout << "Thin heap" << endl;
123 __gnu_pbds::priority_queue<int, less<int>, thin_heap_tag> c;
124 some_op_sequence(c);
129 void
130 priority_queue_link_regression_test_1()
134 * Perform operations on a pairing-heap queue.
136 cout << "Pairing heap" << endl;
137 __gnu_pbds::priority_queue<int, less<int>, pairing_heap_tag> c;
138 some_op_sequence(c);
143 * Perform operations on a binomial-heap queue.
145 cout << "Binomial heap" << endl;
146 __gnu_pbds::priority_queue<int, less<int>, binomial_heap_tag> c;
147 some_op_sequence(c);
152 * Perform operations on a binomial-heap queue.
154 cout << "Redundant-counter binomial heap" << endl;
155 __gnu_pbds::priority_queue<int, less<int>, rc_binomial_heap_tag> c;
156 some_op_sequence(c);
161 * Perform operations on a binomial-heap queue.
163 cout << "Binary heap" << endl;
164 __gnu_pbds::priority_queue<int, less<int>, binary_heap_tag> c;
165 some_op_sequence(c);
170 * Perform operations on a thin-heap queue.
172 cout << "Thin heap" << endl;
173 __gnu_pbds::priority_queue<int, less<int>, thin_heap_tag> c;
174 some_op_sequence(c);
179 main()
181 priority_queue_link_regression_test_0();
182 priority_queue_link_regression_test_1();
183 return 0;