• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <utility>  // for std::pair
16 
17 #include <boost/config.hpp>
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 template <typename InputIterator,
38         typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
39 BOOST_CXX14_CONSTEXPR std::pair<OutputIterator1, OutputIterator2>
partition_copy(InputIterator first,InputIterator last,OutputIterator1 out_true,OutputIterator2 out_false,UnaryPredicate p)40 partition_copy ( InputIterator first, InputIterator last,
41         OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
42 {
43     for ( ; first != last; ++first )
44         if ( p (*first))
45             *out_true++ = *first;
46         else
47             *out_false++ = *first;
48     return std::pair<OutputIterator1, OutputIterator2> ( out_true, out_false );
49 }
50 
51 /// \fn partition_copy ( const Range &r,
52 ///     OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
53 ///
54 /// \param r         The input range
55 /// \param out_true  An output iterator to write the elements that satisfy the predicate into
56 /// \param out_false An output iterator to write the elements that do not satisfy the predicate into
57 /// \param p         A predicate for dividing the elements of the input sequence.
58 ///
59 template <typename Range, typename OutputIterator1, typename OutputIterator2,
60             typename UnaryPredicate>
61 BOOST_CXX14_CONSTEXPR std::pair<OutputIterator1, OutputIterator2>
partition_copy(const Range & r,OutputIterator1 out_true,OutputIterator2 out_false,UnaryPredicate p)62 partition_copy ( const Range &r, OutputIterator1 out_true, OutputIterator2 out_false,
63                                 UnaryPredicate p )
64 {
65     return boost::algorithm::partition_copy
66                       (boost::begin(r), boost::end(r), out_true, out_false, p );
67 }
68 
69 }} // namespace boost and algorithm
70 
71 #endif  // BOOST_ALGORITHM_PARTITION_COPY_HPP
72