nth_element.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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