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_EXT_IS_SORTED_HPP_INCLUDED |
---|
10 | #define BOOST_RANGE_ALGORITHM_EXT_IS_SORTED_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 <boost/range/value_type.hpp> |
---|
17 | #include <algorithm> |
---|
18 | |
---|
19 | namespace boost |
---|
20 | { |
---|
21 | namespace range_detail |
---|
22 | { |
---|
23 | template<class ForwardIterator> |
---|
24 | inline bool is_sorted(ForwardIterator first, ForwardIterator last) |
---|
25 | { |
---|
26 | for (ForwardIterator next = first; first != last && ++next != last; ++first) |
---|
27 | if (*next < *first) |
---|
28 | return false; |
---|
29 | return true; |
---|
30 | } |
---|
31 | |
---|
32 | template<class ForwardIterator, class BinaryPredicate> |
---|
33 | inline bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate pred) |
---|
34 | { |
---|
35 | for (ForwardIterator next = first; first != last && ++next != last; ++first) |
---|
36 | if (pred(*next, *first)) |
---|
37 | return false; |
---|
38 | return true; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | namespace range |
---|
43 | { |
---|
44 | |
---|
45 | /// \brief template function count |
---|
46 | /// |
---|
47 | /// range-based version of the count std algorithm |
---|
48 | /// |
---|
49 | /// \pre SinglePassRange is a model of the SinglePassRangeConcept |
---|
50 | template<class SinglePassRange> |
---|
51 | inline bool is_sorted(const SinglePassRange& rng) |
---|
52 | { |
---|
53 | BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<const SinglePassRange>)); |
---|
54 | BOOST_RANGE_CONCEPT_ASSERT((LessThanComparableConcept<BOOST_DEDUCED_TYPENAME range_value<const SinglePassRange>::type>)); |
---|
55 | return range_detail::is_sorted(boost::begin(rng), boost::end(rng)); |
---|
56 | } |
---|
57 | |
---|
58 | /// \overload |
---|
59 | template<class SinglePassRange, class BinaryPredicate> |
---|
60 | inline bool is_sorted(const SinglePassRange& rng, BinaryPredicate pred) |
---|
61 | { |
---|
62 | BOOST_RANGE_CONCEPT_ASSERT((SinglePassRangeConcept<const SinglePassRange>)); |
---|
63 | BOOST_RANGE_CONCEPT_ASSERT((BinaryPredicateConcept<BinaryPredicate, BOOST_DEDUCED_TYPENAME range_value<const SinglePassRange>::type, BOOST_DEDUCED_TYPENAME range_value<const SinglePassRange>::type>)); |
---|
64 | return range_detail::is_sorted(boost::begin(rng), boost::end(rng), pred); |
---|
65 | } |
---|
66 | |
---|
67 | } // namespace range |
---|
68 | using range::is_sorted; |
---|
69 | } // namespace boost |
---|
70 | |
---|
71 | #endif // include guard |
---|