| 1 | // -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2005-2025 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. |
| 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 COPYING3. If not see |
| 18 | // <http://www.gnu.org/licenses/>. |
| 19 | |
| 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 basic_priority_queue_example.cpp |
| 34 | * A basic example showing how to use priority queues. |
| 35 | */ |
| 36 | |
| 37 | /** |
| 38 | * This example shows how to use priority queues. It defines a |
| 39 | * function performing a sequence of operations on |
| 40 | * a generic container. It then calls this function with some containers. |
| 41 | */ |
| 42 | |
| 43 | #include <cassert> |
| 44 | #include <iostream> |
| 45 | #include <ext/pb_ds/priority_queue.hpp> |
| 46 | |
| 47 | using namespace std; |
| 48 | using namespace __gnu_pbds; |
| 49 | |
| 50 | // The following function performs a sequence of operations on a |
| 51 | // priority queue object storing integers. |
| 52 | template<class Cntnr> |
| 53 | void |
| 54 | some_op_sequence(Cntnr& r_c) |
| 55 | { |
| 56 | assert(r_c.empty()); |
| 57 | assert(r_c.size() == 0); |
| 58 | |
| 59 | for (size_t i = 0; i < 10; ++i) |
| 60 | r_c.push(i); |
| 61 | cout << endl << "All values in the container:" << endl; |
| 62 | |
| 63 | typedef typename Cntnr::const_iterator const_iterator; |
| 64 | for (const_iterator it = r_c.begin(); it != r_c.end(); ++it) |
| 65 | cout << *it << endl; |
| 66 | assert(!r_c.empty()); |
| 67 | assert(r_c.size() == 10); |
| 68 | |
| 69 | cout << "Popping all values: " << endl; |
| 70 | while (!r_c.empty()) |
| 71 | { |
| 72 | cout << r_c.top() << endl; |
| 73 | r_c.pop(); |
| 74 | } |
| 75 | assert(r_c.empty()); |
| 76 | assert(r_c.size() == 0); |
| 77 | |
| 78 | cout << endl; |
| 79 | } |
| 80 | |
| 81 | int main() |
| 82 | { |
| 83 | { |
| 84 | // Perform operations on a pairing-heap queue. |
| 85 | cout << "Pairing heap" << endl; |
| 86 | __gnu_pbds::priority_queue<int, less<int>, pairing_heap_tag> c; |
| 87 | some_op_sequence(c); |
| 88 | } |
| 89 | |
| 90 | { |
| 91 | // Perform operations on a binomial-heap queue. |
| 92 | cout << "Binomial heap" << endl; |
| 93 | __gnu_pbds::priority_queue<int, less<int>, binomial_heap_tag> c; |
| 94 | some_op_sequence(c); |
| 95 | } |
| 96 | |
| 97 | { |
| 98 | // Perform operations on a binomial-heap queue. |
| 99 | cout << "Redundant-counter binomial heap" << endl; |
| 100 | __gnu_pbds::priority_queue<int, less<int>, rc_binomial_heap_tag> c; |
| 101 | some_op_sequence(c); |
| 102 | } |
| 103 | |
| 104 | { |
| 105 | // Perform operations on a binomial-heap queue. |
| 106 | cout << "Binary heap" << endl; |
| 107 | __gnu_pbds::priority_queue<int, less<int>, binary_heap_tag> c; |
| 108 | some_op_sequence(c); |
| 109 | } |
| 110 | |
| 111 | { |
| 112 | // Perform operations on a thin-heap queue. |
| 113 | cout << "Thin heap" << endl; |
| 114 | __gnu_pbds::priority_queue<int, less<int>, thin_heap_tag> c; |
| 115 | some_op_sequence(c); |
| 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |