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