]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /* |
2 | Copyright (c) Marshall Clow 2011-2012. | |
3 | ||
4 | Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | */ | |
7 | ||
8 | /// \file partition_point.hpp | |
9 | /// \brief Find the partition point in a sequence | |
10 | /// \author Marshall Clow | |
11 | ||
12 | #ifndef BOOST_ALGORITHM_PARTITION_POINT_HPP | |
13 | #define BOOST_ALGORITHM_PARTITION_POINT_HPP | |
14 | ||
15 | #include <iterator> // for std::distance, advance | |
16 | ||
17 | #include <boost/range/begin.hpp> | |
18 | #include <boost/range/end.hpp> | |
19 | ||
20 | namespace boost { namespace algorithm { | |
21 | ||
22 | /// \fn partition_point ( ForwardIterator first, ForwardIterator last, Predicate p ) | |
23 | /// \brief Given a partitioned range, returns the partition point, i.e, the first element | |
24 | /// that does not satisfy p | |
25 | /// | |
26 | /// \param first The start of the input sequence | |
27 | /// \param last One past the end of the input sequence | |
28 | /// \param p The predicate to test the values with | |
29 | /// \note This function is part of the C++2011 standard library. | |
30 | template <typename ForwardIterator, typename Predicate> | |
31 | ForwardIterator partition_point ( ForwardIterator first, ForwardIterator last, Predicate p ) | |
32 | { | |
33 | std::size_t dist = std::distance ( first, last ); | |
34 | while ( first != last ) { | |
35 | std::size_t d2 = dist / 2; | |
36 | ForwardIterator ret_val = first; | |
37 | std::advance (ret_val, d2); | |
38 | if (p (*ret_val)) { | |
39 | first = ++ret_val; | |
40 | dist -= d2 + 1; | |
41 | } | |
42 | else { | |
43 | last = ret_val; | |
44 | dist = d2; | |
45 | } | |
46 | } | |
47 | return first; | |
48 | } | |
49 | ||
50 | /// \fn partition_point ( Range &r, Predicate p ) | |
51 | /// \brief Given a partitioned range, returns the partition point | |
52 | /// | |
53 | /// \param r The input range | |
54 | /// \param p The predicate to test the values with | |
55 | /// | |
56 | template <typename Range, typename Predicate> | |
57 | typename boost::range_iterator<Range>::type partition_point ( Range &r, Predicate p ) | |
58 | { | |
59 | return boost::algorithm::partition_point (boost::begin(r), boost::end(r), p); | |
60 | } | |
61 | ||
62 | ||
63 | }} | |
64 | ||
65 | #endif // BOOST_ALGORITHM_PARTITION_POINT_HPP |