nth_element.hpp 2.4 KB

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