forked from boostorg/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_if.hpp
211 lines (191 loc) · 9.56 KB
/
copy_if.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
Copyright (c) Marshall Clow 2008-2012.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/// \file copy_if.hpp
/// \brief Copy a subset of a sequence to a new sequence
/// \author Marshall Clow
#ifndef BOOST_ALGORITHM_COPY_IF_HPP
#define BOOST_ALGORITHM_COPY_IF_HPP
#include <utility> // for std::pair, std::make_pair
#include <boost/config.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
namespace boost { namespace algorithm {
/// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
/// \brief Copies all the elements from the input range that satisfy the
/// predicate to the output range.
/// \return The updated output iterator
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
/// \note This function is part of the C++2011 standard library.
template<typename InputIterator, typename OutputIterator, typename Predicate>
BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last; ++first )
if (p(*first))
*result++ = *first;
return result;
}
/// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
/// \brief Copies all the elements from the input range that satisfy the
/// predicate to the output range.
/// \return The updated output iterator
///
/// \param r The input range
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
}
/// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
/// \brief Copies all the elements at the start of the input range that
/// satisfy the predicate to the output range.
/// \return The updated input and output iterators
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
///
template<typename InputIterator, typename OutputIterator, typename Predicate>
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last && p(*first); ++first )
*result++ = *first;
return std::make_pair(first, result);
}
/// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
/// \brief Copies all the elements at the start of the input range that
/// satisfy the predicate to the output range.
/// \return The updated input and output iterators
///
/// \param r The input range
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
copy_while ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
}
/// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
/// \brief Copies all the elements at the start of the input range that do not
/// satisfy the predicate to the output range.
/// \return The updated output iterator
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
///
template<typename InputIterator, typename OutputIterator, typename Predicate>
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
{
for ( ; first != last && !p(*first); ++first )
*result++ = *first;
return std::make_pair(first, result);
}
/// \fn copy_until ( const Range &r, OutputIterator result, Predicate p )
/// \brief Copies all the elements at the start of the input range that do not
/// satisfy the predicate to the output range.
/// \return The updated output iterator
///
/// \param r The input range
/// \param result An output iterator to write the results into
/// \param p A predicate for testing the elements of the range
///
template<typename Range, typename OutputIterator, typename Predicate>
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
copy_until ( const Range &r, OutputIterator result, Predicate p )
{
return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
}
/// \fn copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
/// \brief Copies all the elements from the input range that satisfy the
/// copy predicate to the output range while the termination predicate is
/// satisfied.
/// \return The updated output iterator
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param result An output iterator to write the results into
/// \param copy_pred A predicate for testing whether to the current element
/// \param term_pred A predicate for testing whether to end the copy operation
template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
{
for ( ; first != last && term_pred(*first); ++first ) {
if (copy_pred(*first)) {
*result++ = *first;
}
}
return std::make_pair(first, result);
}
/// \fn copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
/// \brief Copies all the elements from the input range that satisfy the
/// copy predicate to the output range while the termination predicate is
/// satisfied.
/// \return The updated output iterator
///
/// \param r The input range
/// \param result An output iterator to write the results into
/// \param copy_pred A predicate for testing whether to the current element
/// \param term_pred A predicate for testing whether to end the copy operation
template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
{
return boost::algorithm::copy_if_while(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
}
/// \fn copy_if_until ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
/// \brief Copies all the elements from the input range that satisfy the
/// copy predicate to the output range until the termination predicate is
/// satisfied.
/// \return The updated output iterator
///
/// \param first The start of the input sequence
/// \param last One past the end of the input sequence
/// \param result An output iterator to write the results into
/// \param copy_pred A predicate for testing whether to the current element
/// \param term_pred A predicate for testing whether to end the copy operation
template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
copy_if_until ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
{
for ( ; first != last && !term_pred(*first); ++first ) {
if (copy_pred(*first)) {
*result++ = *first;
}
}
return std::make_pair(first, result);
}
/// \fn copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
/// \brief Copies all the elements from the input range that satisfy the
/// copy predicate to the output range until the termination predicate is
/// satisfied.
/// \return The updated output iterator
///
/// \param r The input range
/// \param result An output iterator to write the results into
/// \param copy_pred A predicate for testing whether to the current element
/// \param term_pred A predicate for testing whether to end the copy operation
template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
{
return boost::algorithm::copy_if_until(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
}
}} // namespace boost and algorithm
#endif // BOOST_ALGORITHM_COPY_IF_HPP