Skip to content

Commit 12ade33

Browse files
authored
Merge pull request #100 from jgopel/copy-if-while
Implement copy_if_while and copy_if_until
2 parents 055ebaa + eec00d8 commit 12ade33

File tree

3 files changed

+328
-7
lines changed

3 files changed

+328
-7
lines changed

doc/algorithm.qbk

+12-2
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,24 @@ See below
216216

217217
[section:copy_until copy_until ]
218218
[*[^[link header.boost.algorithm.cxx11.copy_if_hpp copy_until] ] ]
219-
Copy all the elements at the start of the input range that do not satisfy the predicate to the output range
219+
Copy all the elements from the start of the input range to the output range until the predicate is satisfied
220220
[endsect:copy_until]
221221

222222
[section:copy_while copy_while ]
223223
[*[^[link header.boost.algorithm.cxx11.copy_if_hpp copy_while] ] ]
224-
Copy all the elements at the start of the input range that satisfy the predicate to the output range
224+
Copy all the elements from the start of the input range to the output range while the predicate is satisfied
225225
[endsect:copy_while]
226226

227+
[section:copy_if_until copy_if_until ]
228+
[*[^[link header.boost.algorithm.cxx11.copy_if_hpp copy_if_until ] ]
229+
Copy all elements that satisfy the element predicate from the start of the input range to the output range until the termination predicate is satisfied
230+
[endsect:copy_if_until]
231+
232+
[section:copy_if_while copy_if_while ]
233+
[*[^[link header.boost.algorithm.cxx11.copy_if_hpp copy_if_while ] ]
234+
Copy all elements that satisfy the element predicate from the start of the input range to the output range while the termination predicate is satisfied
235+
[endsect:copy_if_while]
236+
227237
[section:iota_n iota_n ]
228238
[*[^[link boost.algorithm.iota_n iota_n] ] ]
229239
Write a sequence of n increasing values to an output iterator

include/boost/algorithm/cxx11/copy_if.hpp

+80
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,86 @@ copy_until ( const Range &r, OutputIterator result, Predicate p )
126126
return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
127127
}
128128

129+
/// \fn copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
130+
/// \brief Copies all the elements from the input range that satisfy the
131+
/// copy predicate to the output range while the termination predicate is
132+
/// satisfied.
133+
/// \return The updated output iterator
134+
///
135+
/// \param first The start of the input sequence
136+
/// \param last One past the end of the input sequence
137+
/// \param result An output iterator to write the results into
138+
/// \param copy_pred A predicate for testing whether to the current element
139+
/// \param term_pred A predicate for testing whether to end the copy operation
140+
template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
141+
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
142+
copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
143+
{
144+
for ( ; first != last && term_pred(*first); ++first ) {
145+
if (copy_pred(*first)) {
146+
*result++ = *first;
147+
}
148+
}
149+
return std::make_pair(first, result);
150+
}
151+
152+
/// \fn copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
153+
/// \brief Copies all the elements from the input range that satisfy the
154+
/// copy predicate to the output range while the termination predicate is
155+
/// satisfied.
156+
/// \return The updated output iterator
157+
///
158+
/// \param r The input range
159+
/// \param result An output iterator to write the results into
160+
/// \param copy_pred A predicate for testing whether to the current element
161+
/// \param term_pred A predicate for testing whether to end the copy operation
162+
template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
163+
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
164+
copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
165+
{
166+
return boost::algorithm::copy_if_while(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
167+
}
168+
169+
/// \fn copy_if_until ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
170+
/// \brief Copies all the elements from the input range that satisfy the
171+
/// copy predicate to the output range until the termination predicate is
172+
/// satisfied.
173+
/// \return The updated output iterator
174+
///
175+
/// \param first The start of the input sequence
176+
/// \param last One past the end of the input sequence
177+
/// \param result An output iterator to write the results into
178+
/// \param copy_pred A predicate for testing whether to the current element
179+
/// \param term_pred A predicate for testing whether to end the copy operation
180+
template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
181+
BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
182+
copy_if_until ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
183+
{
184+
for ( ; first != last && !term_pred(*first); ++first ) {
185+
if (copy_pred(*first)) {
186+
*result++ = *first;
187+
}
188+
}
189+
return std::make_pair(first, result);
190+
}
191+
192+
/// \fn copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
193+
/// \brief Copies all the elements from the input range that satisfy the
194+
/// copy predicate to the output range until the termination predicate is
195+
/// satisfied.
196+
/// \return The updated output iterator
197+
///
198+
/// \param r The input range
199+
/// \param result An output iterator to write the results into
200+
/// \param copy_pred A predicate for testing whether to the current element
201+
/// \param term_pred A predicate for testing whether to end the copy operation
202+
template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
203+
BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
204+
copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
205+
{
206+
return boost::algorithm::copy_if_until(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
207+
}
208+
129209
}} // namespace boost and algorithm
130210

131211
#endif // BOOST_ALGORITHM_COPY_IF_HPP

0 commit comments

Comments
 (0)