1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*=============================================================================
Copyright (c) 2010 Tim Blechmann
Use, modification and distribution is subject to the Boost Software
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <iomanip>
#include <iostream>
#include "../../../boost/heap/fibonacci_heap.hpp"
using std::cout;
using std::endl;
using namespace boost::heap;
//[ basic_interface
// PriorityQueue is expected to be a max-heap of integer values
template < typename PriorityQueue >
void basic_interface( void )
{
PriorityQueue pq;
pq.push( 2 );
pq.push( 3 );
pq.push( 1 );
cout << "Priority Queue: popped elements" << endl;
cout << pq.top() << " "; // 3
pq.pop();
cout << pq.top() << " "; // 2
pq.pop();
cout << pq.top() << " "; // 1
pq.pop();
cout << endl;
}
//]
//[ iterator_interface
// PriorityQueue is expected to be a max-heap of integer values
template < typename PriorityQueue >
void iterator_interface( void )
{
PriorityQueue pq;
pq.push( 2 );
pq.push( 3 );
pq.push( 1 );
typename PriorityQueue::iterator begin = pq.begin();
typename PriorityQueue::iterator end = pq.end();
cout << "Priority Queue: iteration" << endl;
for ( typename PriorityQueue::iterator it = begin; it != end; ++it )
cout << *it << " "; // 1, 2, 3 in unspecified order
cout << endl;
}
//]
//[ ordered_iterator_interface
// PriorityQueue is expected to be a max-heap of integer values
template < typename PriorityQueue >
void ordered_iterator_interface( void )
{
PriorityQueue pq;
pq.push( 2 );
pq.push( 3 );
pq.push( 1 );
typename PriorityQueue::ordered_iterator begin = pq.ordered_begin();
typename PriorityQueue::ordered_iterator end = pq.ordered_end();
cout << "Priority Queue: ordered iteration" << endl;
for ( typename PriorityQueue::ordered_iterator it = begin; it != end; ++it )
cout << *it << " "; // 3, 2, 1 (i.e. 1, 2, 3 in heap order)
cout << endl;
}
//]
//[ merge_interface
// PriorityQueue is expected to be a max-heap of integer values
template < typename PriorityQueue >
void merge_interface( void )
{
PriorityQueue pq;
pq.push( 3 );
pq.push( 5 );
pq.push( 1 );
PriorityQueue pq2;
pq2.push( 2 );
pq2.push( 4 );
pq2.push( 0 );
pq.merge( pq2 );
cout << "Priority Queue: merge" << endl;
cout << "first queue: ";
while ( !pq.empty() ) {
cout << pq.top() << " "; // 5 4 3 2 1 0
pq.pop();
}
cout << endl;
cout << "second queue: ";
while ( !pq2.empty() ) {
cout << pq2.top() << " "; // 4 2 0
pq2.pop();
}
cout << endl;
}
//]
//[ heap_merge_algorithm
// PriorityQueue is expected to be a max-heap of integer values
template < typename PriorityQueue >
void heap_merge_algorithm( void )
{
PriorityQueue pq;
pq.push( 3 );
pq.push( 5 );
pq.push( 1 );
PriorityQueue pq2;
pq2.push( 2 );
pq2.push( 4 );
pq2.push( 0 );
boost::heap::heap_merge( pq, pq2 );
cout << "Priority Queue: merge" << endl;
cout << "first queue: ";
while ( !pq.empty() ) {
cout << pq.top() << " "; // 5 4 3 2 1 0
pq.pop();
}
cout << endl;
cout << "second queue: ";
while ( !pq2.empty() ) {
cout << pq2.top() << " "; // 4 2 0
pq2.pop();
}
cout << endl;
}
//]
//[ mutable_interface
// PriorityQueue is expected to be a max-heap of integer values
template < typename PriorityQueue >
void mutable_interface( void )
{
PriorityQueue pq;
typedef typename PriorityQueue::handle_type handle_t;
handle_t t3 = pq.push( 3 );
handle_t t5 = pq.push( 5 );
handle_t t1 = pq.push( 1 );
pq.update( t3, 4 );
pq.increase( t5, 7 );
pq.decrease( t1, 0 );
cout << "Priority Queue: update" << endl;
while ( !pq.empty() ) {
cout << pq.top() << " "; // 7, 4, 0
pq.pop();
}
cout << endl;
}
//]
//[ mutable_fixup_interface
// PriorityQueue is expected to be a max-heap of integer values
template < typename PriorityQueue >
void mutable_fixup_interface( void )
{
PriorityQueue pq;
typedef typename PriorityQueue::handle_type handle_t;
handle_t t3 = pq.push( 3 );
handle_t t5 = pq.push( 5 );
handle_t t1 = pq.push( 1 );
*t3 = 4;
pq.update( t3 );
*t5 = 7;
pq.increase( t5 );
*t1 = 0;
pq.decrease( t1 );
cout << "Priority Queue: update with fixup" << endl;
while ( !pq.empty() ) {
cout << pq.top() << " "; // 7, 4, 0
pq.pop();
}
cout << endl;
}
//]
//[ mutable_interface_handle_in_value
struct heap_data
{
fibonacci_heap< heap_data >::handle_type handle;
int payload;
heap_data( int i ) :
payload( i )
{}
bool operator<( heap_data const& rhs ) const
{
return payload < rhs.payload;
}
};
void mutable_interface_handle_in_value( void )
{
fibonacci_heap< heap_data > heap;
heap_data f( 2 );
fibonacci_heap< heap_data >::handle_type handle = heap.push( f );
( *handle ).handle = handle; // store handle in node
}
//]
int main( void )
{
using boost::heap::fibonacci_heap;
cout << std::setw( 2 );
basic_interface< fibonacci_heap< int > >();
cout << endl;
iterator_interface< fibonacci_heap< int > >();
cout << endl;
ordered_iterator_interface< fibonacci_heap< int > >();
cout << endl;
merge_interface< fibonacci_heap< int > >();
cout << endl;
mutable_interface< fibonacci_heap< int > >();
cout << endl;
mutable_fixup_interface< fibonacci_heap< int > >();
mutable_interface_handle_in_value();
}
|