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_copy.hpp
9 /// \brief Copy a subset of a sequence to a new sequence
10 /// \author Marshall Clow
11 
12 #ifndef BOOST_ALGORITHM_PARTITION_COPY_HPP
13 #define BOOST_ALGORITHM_PARTITION_COPY_HPP
14 
15 #include <algorithm>    // for std::partition_copy, if available
16 #include <utility>  // for make_pair
17 
18 #include <boost/range/begin.hpp>
19 #include <boost/range/end.hpp>
20 
21 namespace boost { namespace algorithm {
22 
23 /// \fn partition_copy ( InputIterator first, InputIterator last,
24 ///     OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
25 /// \brief Copies the elements that satisfy the predicate p from the range [first, last)
26 ///     to the range beginning at d_first_true, and
27 ///     copies the elements that do not satisfy p to the range beginning at d_first_false.
28 ///
29 ///
30 /// \param first     The start of the input sequence
31 /// \param last      One past the end of the input sequence
32 /// \param out_true  An output iterator to write the elements that satisfy the predicate into
33 /// \param out_false An output iterator to write the elements that do not satisfy the predicate into
34 /// \param p         A predicate for dividing the elements of the input sequence.
35 ///
36 /// \note            This function is part of the C++2011 standard library.
37 ///  We will use the standard one if it is available,
38 ///  otherwise we have our own implementation.
39 template <typename InputIterator,
40         typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
41 std::pair<OutputIterator1, OutputIterator2>
partition_copy(InputIterator first,InputIterator last,OutputIterator1 out_true,OutputIterator2 out_false,UnaryPredicate p)42 partition_copy ( InputIterator first, InputIterator last,
43         OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
44 {
45     for ( ; first != last; ++first )
46         if ( p (*first))
47             *out_true++ = *first;
48         else
49             *out_false++ = *first;
50     return std::pair<OutputIterator1, OutputIterator2> ( out_true, out_false );
51 }
52 
53 /// \fn partition_copy ( const Range &r,
54 ///     OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
55 ///
56 /// \param r         The input range
57 /// \param out_true  An output iterator to write the elements that satisfy the predicate into
58 /// \param out_false An output iterator to write the elements that do not satisfy the predicate into
59 /// \param p         A predicate for dividing the elements of the input sequence.
60 ///
61 template <typename Range, typename OutputIterator1, typename OutputIterator2,
62             typename UnaryPredicate>
63 std::pair<OutputIterator1, OutputIterator2>
partition_copy(const Range & r,OutputIterator1 out_true,OutputIterator2 out_false,UnaryPredicate p)64 partition_copy ( const Range &r, OutputIterator1 out_true, OutputIterator2 out_false,
65                                 UnaryPredicate p )
66 {
67     return boost::algorithm::partition_copy
68                       (boost::begin(r), boost::end(r), out_true, out_false, p );
69 }
70 
71 }} // namespace boost and algorithm
72 
73 #endif  // BOOST_ALGORITHM_PARTITION_COPY_HPP
74