1 /*=============================================================================
2 Copyright (c) 2010 Tim Blechmann
4 Use, modification and distribution is subject to the Boost Software
5 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
12 #include "../../../boost/heap/fibonacci_heap.hpp"
16 using namespace boost::heap
;
19 // PriorityQueue is expected to be a max-heap of integer values
20 template <typename PriorityQueue
>
21 void basic_interface(void)
29 cout
<< "Priority Queue: popped elements" << endl
;
30 cout
<< pq
.top() << " "; // 3
32 cout
<< pq
.top() << " "; // 2
34 cout
<< pq
.top() << " "; // 1
40 //[ iterator_interface
41 // PriorityQueue is expected to be a max-heap of integer values
42 template <typename PriorityQueue
>
43 void iterator_interface(void)
51 typename
PriorityQueue::iterator begin
= pq
.begin();
52 typename
PriorityQueue::iterator end
= pq
.end();
54 cout
<< "Priority Queue: iteration" << endl
;
55 for (typename
PriorityQueue::iterator it
= begin
; it
!= end
; ++it
)
56 cout
<< *it
<< " "; // 1, 2, 3 in unspecified order
61 //[ ordered_iterator_interface
62 // PriorityQueue is expected to be a max-heap of integer values
63 template <typename PriorityQueue
>
64 void ordered_iterator_interface(void)
72 typename
PriorityQueue::ordered_iterator begin
= pq
.ordered_begin();
73 typename
PriorityQueue::ordered_iterator end
= pq
.ordered_end();
75 cout
<< "Priority Queue: ordered iteration" << endl
;
76 for (typename
PriorityQueue::ordered_iterator it
= begin
; it
!= end
; ++it
)
77 cout
<< *it
<< " "; // 3, 2, 1 (i.e. 1, 2, 3 in heap order)
84 // PriorityQueue is expected to be a max-heap of integer values
85 template <typename PriorityQueue
>
86 void merge_interface(void)
102 cout
<< "Priority Queue: merge" << endl
;
103 cout
<< "first queue: ";
104 while (!pq
.empty()) {
105 cout
<< pq
.top() << " "; // 5 4 3 2 1 0
110 cout
<< "second queue: ";
111 while (!pq2
.empty()) {
112 cout
<< pq2
.top() << " "; // 4 2 0
119 //[ heap_merge_algorithm
120 // PriorityQueue is expected to be a max-heap of integer values
121 template <typename PriorityQueue
>
122 void heap_merge_algorithm(void)
136 boost::heap::heap_merge(pq
, pq2
);
138 cout
<< "Priority Queue: merge" << endl
;
139 cout
<< "first queue: ";
140 while (!pq
.empty()) {
141 cout
<< pq
.top() << " "; // 5 4 3 2 1 0
146 cout
<< "second queue: ";
147 while (!pq2
.empty()) {
148 cout
<< pq2
.top() << " "; // 4 2 0
155 //[ mutable_interface
156 // PriorityQueue is expected to be a max-heap of integer values
157 template <typename PriorityQueue
>
158 void mutable_interface(void)
161 typedef typename
PriorityQueue::handle_type handle_t
;
163 handle_t t3
= pq
.push(3);
164 handle_t t5
= pq
.push(5);
165 handle_t t1
= pq
.push(1);
171 cout
<< "Priority Queue: update" << endl
;
172 while (!pq
.empty()) {
173 cout
<< pq
.top() << " "; // 7, 4, 0
180 //[ mutable_fixup_interface
181 // PriorityQueue is expected to be a max-heap of integer values
182 template <typename PriorityQueue
>
183 void mutable_fixup_interface(void)
186 typedef typename
PriorityQueue::handle_type handle_t
;
188 handle_t t3
= pq
.push(3);
189 handle_t t5
= pq
.push(5);
190 handle_t t1
= pq
.push(1);
201 cout
<< "Priority Queue: update with fixup" << endl
;
202 while (!pq
.empty()) {
203 cout
<< pq
.top() << " "; // 7, 4, 0
210 //[ mutable_interface_handle_in_value
213 fibonacci_heap
<heap_data
>::handle_type handle
;
220 bool operator<(heap_data
const & rhs
) const
222 return payload
< rhs
.payload
;
226 void mutable_interface_handle_in_value(void)
228 fibonacci_heap
<heap_data
> heap
;
231 fibonacci_heap
<heap_data
>::handle_type handle
= heap
.push(f
);
232 (*handle
).handle
= handle
; // store handle in node
239 using boost::heap::fibonacci_heap
;
241 cout
<< std::setw(2);
243 basic_interface
<fibonacci_heap
<int> >();
246 iterator_interface
<fibonacci_heap
<int> >();
249 ordered_iterator_interface
<fibonacci_heap
<int> >();
252 merge_interface
<fibonacci_heap
<int> >();
255 mutable_interface
<fibonacci_heap
<int> >();
258 mutable_fixup_interface
<fibonacci_heap
<int> >();
260 mutable_interface_handle_in_value();