heap_algorithm.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_HEAP_ALGORITHM_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_HEAP_ALGORITHM_HPP_INCLUDED
  11. #include <boost/concept_check.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/concepts.hpp>
  15. #include <algorithm>
  16. namespace boost
  17. {
  18. namespace range
  19. {
  20. /// \brief template function push_heap
  21. ///
  22. /// range-based version of the push_heap std algorithm
  23. ///
  24. /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept
  25. /// \pre Compare is a model of the BinaryPredicateConcept
  26. template<class RandomAccessRange>
  27. inline RandomAccessRange& push_heap(RandomAccessRange& rng)
  28. {
  29. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  30. std::push_heap(boost::begin(rng), boost::end(rng));
  31. return rng;
  32. }
  33. /// \overload
  34. template<class RandomAccessRange>
  35. inline const RandomAccessRange& push_heap(const RandomAccessRange& rng)
  36. {
  37. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  38. std::push_heap(boost::begin(rng), boost::end(rng));
  39. return rng;
  40. }
  41. /// \overload
  42. template<class RandomAccessRange, class Compare>
  43. inline RandomAccessRange& push_heap(RandomAccessRange& rng, Compare comp_pred)
  44. {
  45. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  46. std::push_heap(boost::begin(rng), boost::end(rng), comp_pred);
  47. return rng;
  48. }
  49. /// \overload
  50. template<class RandomAccessRange, class Compare>
  51. inline const RandomAccessRange& push_heap(const RandomAccessRange& rng, Compare comp_pred)
  52. {
  53. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  54. std::push_heap(boost::begin(rng), boost::end(rng), comp_pred);
  55. return rng;
  56. }
  57. /// \brief template function pop_heap
  58. ///
  59. /// range-based version of the pop_heap std algorithm
  60. ///
  61. /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept
  62. /// \pre Compare is a model of the BinaryPredicateConcept
  63. template<class RandomAccessRange>
  64. inline RandomAccessRange& pop_heap(RandomAccessRange& rng)
  65. {
  66. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  67. std::pop_heap(boost::begin(rng), boost::end(rng));
  68. return rng;
  69. }
  70. /// \overload
  71. template<class RandomAccessRange>
  72. inline const RandomAccessRange& pop_heap(const RandomAccessRange& rng)
  73. {
  74. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  75. std::pop_heap(boost::begin(rng), boost::end(rng));
  76. return rng;
  77. }
  78. /// \overload
  79. template<class RandomAccessRange, class Compare>
  80. inline RandomAccessRange& pop_heap(RandomAccessRange& rng, Compare comp_pred)
  81. {
  82. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  83. std::pop_heap(boost::begin(rng), boost::end(rng), comp_pred);
  84. return rng;
  85. }
  86. /// \overload
  87. template<class RandomAccessRange, class Compare>
  88. inline const RandomAccessRange& pop_heap(const RandomAccessRange& rng, Compare comp_pred)
  89. {
  90. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  91. std::pop_heap(boost::begin(rng), boost::end(rng), comp_pred);
  92. return rng;
  93. }
  94. /// \brief template function make_heap
  95. ///
  96. /// range-based version of the make_heap std algorithm
  97. ///
  98. /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept
  99. /// \pre Compare is a model of the BinaryPredicateConcept
  100. template<class RandomAccessRange>
  101. inline RandomAccessRange& make_heap(RandomAccessRange& rng)
  102. {
  103. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  104. std::make_heap(boost::begin(rng), boost::end(rng));
  105. return rng;
  106. }
  107. /// \overload
  108. template<class RandomAccessRange>
  109. inline const RandomAccessRange& make_heap(const RandomAccessRange& rng)
  110. {
  111. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  112. std::make_heap(boost::begin(rng), boost::end(rng));
  113. return rng;
  114. }
  115. /// \overload
  116. template<class RandomAccessRange, class Compare>
  117. inline RandomAccessRange& make_heap(RandomAccessRange& rng, Compare comp_pred)
  118. {
  119. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  120. std::make_heap(boost::begin(rng), boost::end(rng), comp_pred);
  121. return rng;
  122. }
  123. /// \overload
  124. template<class RandomAccessRange, class Compare>
  125. inline const RandomAccessRange& make_heap(const RandomAccessRange& rng, Compare comp_pred)
  126. {
  127. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  128. std::make_heap(boost::begin(rng), boost::end(rng), comp_pred);
  129. return rng;
  130. }
  131. /// \brief template function sort_heap
  132. ///
  133. /// range-based version of the sort_heap std algorithm
  134. ///
  135. /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept
  136. /// \pre Compare is a model of the BinaryPredicateConcept
  137. template<class RandomAccessRange>
  138. inline RandomAccessRange& sort_heap(RandomAccessRange& rng)
  139. {
  140. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  141. std::sort_heap(boost::begin(rng), boost::end(rng));
  142. return rng;
  143. }
  144. /// \overload
  145. template<class RandomAccessRange>
  146. inline const RandomAccessRange& sort_heap(const RandomAccessRange& rng)
  147. {
  148. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  149. std::sort_heap(boost::begin(rng), boost::end(rng));
  150. return rng;
  151. }
  152. /// \overload
  153. template<class RandomAccessRange, class Compare>
  154. inline RandomAccessRange& sort_heap(RandomAccessRange& rng, Compare comp_pred)
  155. {
  156. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  157. std::sort_heap(boost::begin(rng), boost::end(rng), comp_pred);
  158. return rng;
  159. }
  160. /// \overload
  161. template<class RandomAccessRange, class Compare>
  162. inline const RandomAccessRange& sort_heap(const RandomAccessRange& rng, Compare comp_pred)
  163. {
  164. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  165. std::sort_heap(boost::begin(rng), boost::end(rng), comp_pred);
  166. return rng;
  167. }
  168. } // namespace range
  169. using range::push_heap;
  170. using range::pop_heap;
  171. using range::make_heap;
  172. using range::sort_heap;
  173. } // namespace boost
  174. #endif // include guard