Chris@16
|
1 /*
|
Chris@16
|
2 Copyright (c) Marshall Clow 2008-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 copy_if.hpp
|
Chris@16
|
9 /// \brief Copy a subset of a sequence to a new sequence
|
Chris@16
|
10 /// \author Marshall Clow
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_ALGORITHM_COPY_IF_HPP
|
Chris@16
|
13 #define BOOST_ALGORITHM_COPY_IF_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #include <algorithm> // for std::copy_if, if available
|
Chris@16
|
16 #include <boost/range/begin.hpp>
|
Chris@16
|
17 #include <boost/range/end.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 namespace boost { namespace algorithm {
|
Chris@16
|
20
|
Chris@16
|
21 /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
|
Chris@16
|
22 /// \brief Copies all the elements from the input range that satisfy the
|
Chris@16
|
23 /// predicate to the output range.
|
Chris@16
|
24 /// \return The updated output iterator
|
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 result An output iterator to write the results into
|
Chris@16
|
29 /// \param p A predicate for testing the elements of the range
|
Chris@16
|
30 /// \note This function is part of the C++2011 standard library.
|
Chris@16
|
31 /// We will use the standard one if it is available,
|
Chris@16
|
32 /// otherwise we have our own implementation.
|
Chris@16
|
33 template<typename InputIterator, typename OutputIterator, typename Predicate>
|
Chris@16
|
34 OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
|
Chris@16
|
35 {
|
Chris@16
|
36 for ( ; first != last; ++first )
|
Chris@16
|
37 if (p(*first))
|
Chris@16
|
38 *result++ = *first;
|
Chris@16
|
39 return result;
|
Chris@16
|
40 }
|
Chris@16
|
41
|
Chris@16
|
42 /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
|
Chris@16
|
43 /// \brief Copies all the elements from the input range that satisfy the
|
Chris@16
|
44 /// predicate to the output range.
|
Chris@16
|
45 /// \return The updated output iterator
|
Chris@16
|
46 ///
|
Chris@16
|
47 /// \param r The input range
|
Chris@16
|
48 /// \param result An output iterator to write the results into
|
Chris@16
|
49 /// \param p A predicate for testing the elements of the range
|
Chris@16
|
50 ///
|
Chris@16
|
51 template<typename Range, typename OutputIterator, typename Predicate>
|
Chris@16
|
52 OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
|
Chris@16
|
53 {
|
Chris@16
|
54 return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57
|
Chris@16
|
58 /// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
|
Chris@16
|
59 /// \brief Copies all the elements at the start of the input range that
|
Chris@16
|
60 /// satisfy the predicate to the output range.
|
Chris@16
|
61 /// \return The updated input and output iterators
|
Chris@16
|
62 ///
|
Chris@16
|
63 /// \param first The start of the input sequence
|
Chris@16
|
64 /// \param last One past the end of the input sequence
|
Chris@16
|
65 /// \param result An output iterator to write the results into
|
Chris@16
|
66 /// \param p A predicate for testing the elements of the range
|
Chris@16
|
67 ///
|
Chris@16
|
68 template<typename InputIterator, typename OutputIterator, typename Predicate>
|
Chris@16
|
69 std::pair<InputIterator, OutputIterator>
|
Chris@16
|
70 copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
|
Chris@16
|
71 {
|
Chris@16
|
72 for ( ; first != last && p(*first); ++first )
|
Chris@16
|
73 *result++ = *first;
|
Chris@16
|
74 return std::make_pair(first, result);
|
Chris@16
|
75 }
|
Chris@16
|
76
|
Chris@16
|
77 /// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
|
Chris@16
|
78 /// \brief Copies all the elements at the start of the input range that
|
Chris@16
|
79 /// satisfy the predicate to the output range.
|
Chris@16
|
80 /// \return The updated input and output iterators
|
Chris@16
|
81 ///
|
Chris@16
|
82 /// \param r The input range
|
Chris@16
|
83 /// \param result An output iterator to write the results into
|
Chris@16
|
84 /// \param p A predicate for testing the elements of the range
|
Chris@16
|
85 ///
|
Chris@16
|
86 template<typename Range, typename OutputIterator, typename Predicate>
|
Chris@16
|
87 std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
|
Chris@16
|
88 copy_while ( const Range &r, OutputIterator result, Predicate p )
|
Chris@16
|
89 {
|
Chris@16
|
90 return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
|
Chris@16
|
91 }
|
Chris@16
|
92
|
Chris@16
|
93
|
Chris@16
|
94 /// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
|
Chris@16
|
95 /// \brief Copies all the elements at the start of the input range that do not
|
Chris@16
|
96 /// satisfy the predicate to the output range.
|
Chris@16
|
97 /// \return The updated output iterator
|
Chris@16
|
98 ///
|
Chris@16
|
99 /// \param first The start of the input sequence
|
Chris@16
|
100 /// \param last One past the end of the input sequence
|
Chris@16
|
101 /// \param result An output iterator to write the results into
|
Chris@16
|
102 /// \param p A predicate for testing the elements of the range
|
Chris@16
|
103 ///
|
Chris@16
|
104 template<typename InputIterator, typename OutputIterator, typename Predicate>
|
Chris@16
|
105 std::pair<InputIterator, OutputIterator>
|
Chris@16
|
106 copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
|
Chris@16
|
107 {
|
Chris@16
|
108 for ( ; first != last && !p(*first); ++first )
|
Chris@16
|
109 *result++ = *first;
|
Chris@16
|
110 return std::make_pair(first, result);
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@16
|
113 /// \fn copy_until ( const Range &r, OutputIterator result, Predicate p )
|
Chris@16
|
114 /// \brief Copies all the elements at the start of the input range that do not
|
Chris@16
|
115 /// satisfy the predicate to the output range.
|
Chris@16
|
116 /// \return The updated output iterator
|
Chris@16
|
117 ///
|
Chris@16
|
118 /// \param r The input range
|
Chris@16
|
119 /// \param result An output iterator to write the results into
|
Chris@16
|
120 /// \param p A predicate for testing the elements of the range
|
Chris@16
|
121 ///
|
Chris@16
|
122 template<typename Range, typename OutputIterator, typename Predicate>
|
Chris@16
|
123 std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
|
Chris@16
|
124 copy_until ( const Range &r, OutputIterator result, Predicate p )
|
Chris@16
|
125 {
|
Chris@16
|
126 return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 }} // namespace boost and algorithm
|
Chris@16
|
130
|
Chris@16
|
131 #endif // BOOST_ALGORITHM_COPY_IF_HPP
|