partial_sort.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_PARTIAL_SORT_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_PARTIAL_SORT_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 partial_sort
  21. ///
  22. /// range-based version of the partial_sort std algorithm
  23. ///
  24. /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept
  25. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  26. template<class RandomAccessRange>
  27. inline RandomAccessRange& partial_sort(RandomAccessRange& rng,
  28. BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type middle)
  29. {
  30. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  31. std::partial_sort(boost::begin(rng), middle, boost::end(rng));
  32. return rng;
  33. }
  34. /// \overload
  35. template<class RandomAccessRange>
  36. inline const RandomAccessRange& partial_sort(const RandomAccessRange& rng,
  37. BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type middle)
  38. {
  39. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  40. std::partial_sort(boost::begin(rng), middle, boost::end(rng));
  41. return rng;
  42. }
  43. /// \overload
  44. template<class RandomAccessRange, class BinaryPredicate>
  45. inline RandomAccessRange& partial_sort(RandomAccessRange& rng,
  46. BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type middle,
  47. BinaryPredicate sort_pred)
  48. {
  49. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  50. std::partial_sort(boost::begin(rng), middle, boost::end(rng),
  51. sort_pred);
  52. return rng;
  53. }
  54. /// \overload
  55. template<class RandomAccessRange, class BinaryPredicate>
  56. inline const RandomAccessRange& partial_sort(const RandomAccessRange& rng,
  57. BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type middle,
  58. BinaryPredicate sort_pred)
  59. {
  60. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  61. std::partial_sort(boost::begin(rng), middle, boost::end(rng),
  62. sort_pred);
  63. return rng;
  64. }
  65. } // namespace range
  66. using range::partial_sort;
  67. } // namespace boost
  68. #endif // include guard