Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement copy_if_while and copy_if_until #100

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions doc/algorithm.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,24 @@ See below

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

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

[section:copy_if_until copy_if_until ]
[*[^[link header.boost.algorithm.cxx11.copy_if_hpp copy_if_until ] ]
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
[endsect:copy_if_until]

[section:copy_if_while copy_if_while ]
[*[^[link header.boost.algorithm.cxx11.copy_if_hpp copy_if_while ] ]
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
[endsect:copy_if_while]

[section:iota_n iota_n ]
[*[^[link boost.algorithm.iota_n iota_n] ] ]
Write a sequence of n increasing values to an output iterator
Expand Down
80 changes: 80 additions & 0 deletions include/boost/algorithm/cxx11/copy_if.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,86 @@ 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 link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You missed something here; shouldn't this be:
Copies all the elements from the input range that satisfy the predicate while the termination predicate is satisfied.

also, is TerminatePred the correct term to use here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the missing documentation here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if TerminatePred is correct - I definitely struggled with naming of the predicates to indicate what they were and how they were different - these were the best that I could come up with. If you have a better name, I am more than happy to make changes!

/// 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
Loading