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