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 | |
12 | #include <boost/concept_check.hpp> |
13 | #include <boost/range/begin.hpp> |
14 | #include <boost/range/end.hpp> |
15 | #include <boost/range/concepts.hpp> |
16 | #include <algorithm> |
17 | |
18 | namespace boost |
19 | { |
20 | namespace range |
21 | { |
22 | |
23 | /// \brief template function nth_element |
24 | /// |
25 | /// range-based version of the nth_element std algorithm |
26 | /// |
27 | /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept |
28 | /// \pre BinaryPredicate is a model of the BinaryPredicateConcept |
29 | template<class RandomAccessRange> |
30 | inline RandomAccessRange& nth_element(RandomAccessRange& rng, |
31 | BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type nth) |
32 | { |
33 | BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> )); |
34 | std::nth_element(boost::begin(rng), nth, boost::end(rng)); |
35 | return rng; |
36 | } |
37 | |
38 | /// \overload |
39 | template<class RandomAccessRange> |
40 | inline const RandomAccessRange& nth_element(const RandomAccessRange& rng, |
41 | BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type nth) |
42 | { |
43 | BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> )); |
44 | std::nth_element(boost::begin(rng), nth, boost::end(rng)); |
45 | return rng; |
46 | } |
47 | |
48 | /// \overload |
49 | template<class RandomAccessRange, class BinaryPredicate> |
50 | inline RandomAccessRange& nth_element(RandomAccessRange& rng, |
51 | BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type nth, |
52 | BinaryPredicate sort_pred) |
53 | { |
54 | BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> )); |
55 | std::nth_element(boost::begin(rng), nth, boost::end(rng), sort_pred); |
56 | return rng; |
57 | } |
58 | |
59 | /// \overload |
60 | template<class RandomAccessRange, class BinaryPredicate> |
61 | inline const RandomAccessRange& nth_element(const RandomAccessRange& rng, |
62 | BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type nth, |
63 | BinaryPredicate sort_pred) |
64 | { |
65 | BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> )); |
66 | std::nth_element(boost::begin(rng), nth, boost::end(rng), sort_pred); |
67 | return rng; |
68 | } |
69 | |
70 | } // namespace range |
71 | using range::nth_element; |
72 | } // namespace boost |
73 | |
74 | #endif // include guard |
75 | |