interface.cpp 5.5 KB

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