OSDN Git Service

14604d73dce4a112df7c149195cf58ede0619bc3
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / ext / pb_ds / example / priority_queue_split_join.cc
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 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 2, 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 COPYING.  If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction.  Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License.  This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
41
42 /**
43  * @file priority_queue_split_join_example.cpp
44  * A basic example showing how to split and join priority queues.
45  */
46
47 /**
48  * This example shows how to split and join priority queues.
49  */
50
51 #include <functional>
52 #include <iostream>
53 #include <cassert>
54 #include <ext/pb_ds/priority_queue.hpp>
55
56 using namespace std;
57 using namespace __gnu_pbds;
58
59 int
60 main()
61 {
62   // Two priority queue objects.
63   __gnu_pbds::priority_queue<int> even_p, odd_p;
64
65   // First we insert some values: even ones into even_p, and odd ones
66   // into odd_p.
67   for (size_t i = 0; i < 10; ++i)
68     {
69       even_p.push(2*  i);
70       odd_p.push(2*  i + 1);
71     }
72
73   // Check that each one contains the appropriate 10 values.
74   assert(even_p.size() == 10);
75   assert(even_p.top() == 18);
76
77   // Print out the values.
78   cout << "Initial values in even priority queue:" << endl;
79   __gnu_pbds::priority_queue<int>::const_iterator it;
80   for (it = even_p.begin(); it != even_p.end(); ++it)
81     cout <<* it << endl;
82
83   assert(odd_p.size() == 10);
84   assert(odd_p.top() == 19);
85
86   // Print out the values.
87   cout << "Initial values in odd priority queue:" << endl;
88   for (it = odd_p.begin(); it != odd_p.end(); ++it)
89     cout <<* it << endl;
90
91   // Now join the queues.
92   even_p.join(odd_p);
93
94   // Check that each one contains the appropriate values.
95
96   assert(even_p.size() == 20);
97   assert(even_p.top() == 19);
98
99   // Print out the values.
100   cout << "After-join values in even priority queue:" << endl;
101   for (it = even_p.begin(); it != even_p.end(); ++it)
102     cout <<* it << endl;
103
104   assert(odd_p.size() == 0);
105
106   // Print out the values.
107   cout << "After-join values in odd priority queue:" << endl;
108   for (it = odd_p.begin(); it != odd_p.end(); ++it)
109     cout <<* it << endl;
110
111   // Now split the queues.
112   even_p.split(bind2nd(modulus<int>(), 2), odd_p);
113
114   // Check that each one contains the appropriate 10 values.
115
116   assert(even_p.size() == 10);
117   assert(even_p.top() == 18);
118
119   // Print out the values.
120   cout << "After-split values in even priority queue:" << endl;
121   for (it = even_p.begin(); it != even_p.end(); ++it)
122     cout <<* it << endl;
123
124   assert(odd_p.size() == 10);
125   assert(odd_p.top() == 19);
126
127   // Print out the values.
128   cout << "After-split values in odd priority queue:" << endl;
129   for (it = odd_p.begin(); it != odd_p.end(); ++it)
130     cout <<* it << endl;
131
132   return 0;
133 }
134