2 Copyright (c) Marshall Clow 2008-2012.
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)
9 /// \brief Copy a subset of a sequence to a new sequence
10 /// \author Marshall Clow
12 #ifndef BOOST_ALGORITHM_COPY_IF_HPP
13 #define BOOST_ALGORITHM_COPY_IF_HPP
15 #include <utility> // for std::pair, std::make_pair
16 #include <boost/range/begin.hpp>
17 #include <boost/range/end.hpp>
19 namespace boost { namespace algorithm {
21 /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
22 /// \brief Copies all the elements from the input range that satisfy the
23 /// predicate to the output range.
24 /// \return The updated output iterator
26 /// \param first The start of the input sequence
27 /// \param last One past the end of the input sequence
28 /// \param result An output iterator to write the results into
29 /// \param p A predicate for testing the elements of the range
30 /// \note This function is part of the C++2011 standard library.
31 template<typename InputIterator, typename OutputIterator, typename Predicate>
32 OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
34 for ( ; first != last; ++first )
40 /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
41 /// \brief Copies all the elements from the input range that satisfy the
42 /// predicate to the output range.
43 /// \return The updated output iterator
45 /// \param r The input range
46 /// \param result An output iterator to write the results into
47 /// \param p A predicate for testing the elements of the range
49 template<typename Range, typename OutputIterator, typename Predicate>
50 OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
52 return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
56 /// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
57 /// \brief Copies all the elements at the start of the input range that
58 /// satisfy the predicate to the output range.
59 /// \return The updated input and output iterators
61 /// \param first The start of the input sequence
62 /// \param last One past the end of the input sequence
63 /// \param result An output iterator to write the results into
64 /// \param p A predicate for testing the elements of the range
66 template<typename InputIterator, typename OutputIterator, typename Predicate>
67 std::pair<InputIterator, OutputIterator>
68 copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
70 for ( ; first != last && p(*first); ++first )
72 return std::make_pair(first, result);
75 /// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
76 /// \brief Copies all the elements at the start of the input range that
77 /// satisfy the predicate to the output range.
78 /// \return The updated input and output iterators
80 /// \param r The input range
81 /// \param result An output iterator to write the results into
82 /// \param p A predicate for testing the elements of the range
84 template<typename Range, typename OutputIterator, typename Predicate>
85 std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
86 copy_while ( const Range &r, OutputIterator result, Predicate p )
88 return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
92 /// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
93 /// \brief Copies all the elements at the start of the input range that do not
94 /// satisfy the predicate to the output range.
95 /// \return The updated output iterator
97 /// \param first The start of the input sequence
98 /// \param last One past the end of the input sequence
99 /// \param result An output iterator to write the results into
100 /// \param p A predicate for testing the elements of the range
102 template<typename InputIterator, typename OutputIterator, typename Predicate>
103 std::pair<InputIterator, OutputIterator>
104 copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
106 for ( ; first != last && !p(*first); ++first )
108 return std::make_pair(first, result);
111 /// \fn copy_until ( const Range &r, OutputIterator result, Predicate p )
112 /// \brief Copies all the elements at the start of the input range that do not
113 /// satisfy the predicate to the output range.
114 /// \return The updated output iterator
116 /// \param r The input range
117 /// \param result An output iterator to write the results into
118 /// \param p A predicate for testing the elements of the range
120 template<typename Range, typename OutputIterator, typename Predicate>
121 std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
122 copy_until ( const Range &r, OutputIterator result, Predicate p )
124 return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
127 }} // namespace boost and algorithm
129 #endif // BOOST_ALGORITHM_COPY_IF_HPP