Chris@16
|
1 // Copyright Neil Groves 2009. Use, modification and
|
Chris@16
|
2 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
3 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
4 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5 //
|
Chris@16
|
6 //
|
Chris@16
|
7 // For more information, see http://www.boost.org/libs/range/
|
Chris@16
|
8 //
|
Chris@16
|
9 #ifndef BOOST_RANGE_ALGORITHM_NTH_ELEMENT_HPP_INCLUDED
|
Chris@16
|
10 #define BOOST_RANGE_ALGORITHM_NTH_ELEMENT_HPP_INCLUDED
|
Chris@16
|
11
|
Chris@16
|
12 #include <boost/concept_check.hpp>
|
Chris@16
|
13 #include <boost/range/begin.hpp>
|
Chris@16
|
14 #include <boost/range/end.hpp>
|
Chris@16
|
15 #include <boost/range/concepts.hpp>
|
Chris@16
|
16 #include <algorithm>
|
Chris@16
|
17
|
Chris@16
|
18 namespace boost
|
Chris@16
|
19 {
|
Chris@16
|
20 namespace range
|
Chris@16
|
21 {
|
Chris@16
|
22
|
Chris@16
|
23 /// \brief template function nth_element
|
Chris@16
|
24 ///
|
Chris@16
|
25 /// range-based version of the nth_element std algorithm
|
Chris@16
|
26 ///
|
Chris@16
|
27 /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept
|
Chris@16
|
28 /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
|
Chris@16
|
29 template<class RandomAccessRange>
|
Chris@16
|
30 inline RandomAccessRange& nth_element(RandomAccessRange& rng,
|
Chris@16
|
31 BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type nth)
|
Chris@16
|
32 {
|
Chris@16
|
33 BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
|
Chris@16
|
34 std::nth_element(boost::begin(rng), nth, boost::end(rng));
|
Chris@16
|
35 return rng;
|
Chris@16
|
36 }
|
Chris@16
|
37
|
Chris@16
|
38 /// \overload
|
Chris@16
|
39 template<class RandomAccessRange>
|
Chris@16
|
40 inline const RandomAccessRange& nth_element(const RandomAccessRange& rng,
|
Chris@16
|
41 BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type nth)
|
Chris@16
|
42 {
|
Chris@16
|
43 BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
|
Chris@16
|
44 std::nth_element(boost::begin(rng), nth, boost::end(rng));
|
Chris@16
|
45 return rng;
|
Chris@16
|
46 }
|
Chris@16
|
47
|
Chris@16
|
48 /// \overload
|
Chris@16
|
49 template<class RandomAccessRange, class BinaryPredicate>
|
Chris@16
|
50 inline RandomAccessRange& nth_element(RandomAccessRange& rng,
|
Chris@16
|
51 BOOST_DEDUCED_TYPENAME range_iterator<RandomAccessRange>::type nth,
|
Chris@16
|
52 BinaryPredicate sort_pred)
|
Chris@16
|
53 {
|
Chris@16
|
54 BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
|
Chris@16
|
55 std::nth_element(boost::begin(rng), nth, boost::end(rng), sort_pred);
|
Chris@16
|
56 return rng;
|
Chris@16
|
57 }
|
Chris@16
|
58
|
Chris@16
|
59 /// \overload
|
Chris@16
|
60 template<class RandomAccessRange, class BinaryPredicate>
|
Chris@16
|
61 inline const RandomAccessRange& nth_element(const RandomAccessRange& rng,
|
Chris@16
|
62 BOOST_DEDUCED_TYPENAME range_iterator<const RandomAccessRange>::type nth,
|
Chris@16
|
63 BinaryPredicate sort_pred)
|
Chris@16
|
64 {
|
Chris@16
|
65 BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
|
Chris@16
|
66 std::nth_element(boost::begin(rng), nth, boost::end(rng), sort_pred);
|
Chris@16
|
67 return rng;
|
Chris@16
|
68 }
|
Chris@16
|
69
|
Chris@16
|
70 } // namespace range
|
Chris@16
|
71 using range::nth_element;
|
Chris@16
|
72 } // namespace boost
|
Chris@16
|
73
|
Chris@16
|
74 #endif // include guard
|