Chris@16: /* Chris@16: Copyright (c) Marshall Clow 2008-2012. Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: Chris@16: /// \file copy_if.hpp Chris@16: /// \brief Copy a subset of a sequence to a new sequence Chris@16: /// \author Marshall Clow Chris@16: Chris@16: #ifndef BOOST_ALGORITHM_COPY_IF_HPP Chris@16: #define BOOST_ALGORITHM_COPY_IF_HPP Chris@16: Chris@16: #include // for std::copy_if, if available Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { namespace algorithm { Chris@16: Chris@16: /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) Chris@16: /// \brief Copies all the elements from the input range that satisfy the Chris@16: /// predicate to the output range. Chris@16: /// \return The updated output iterator Chris@16: /// Chris@16: /// \param first The start of the input sequence Chris@16: /// \param last One past the end of the input sequence Chris@16: /// \param result An output iterator to write the results into Chris@16: /// \param p A predicate for testing the elements of the range Chris@16: /// \note This function is part of the C++2011 standard library. Chris@16: /// We will use the standard one if it is available, Chris@16: /// otherwise we have our own implementation. Chris@16: template Chris@16: OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) Chris@16: { Chris@16: for ( ; first != last; ++first ) Chris@16: if (p(*first)) Chris@16: *result++ = *first; Chris@16: return result; Chris@16: } Chris@16: Chris@16: /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p ) Chris@16: /// \brief Copies all the elements from the input range that satisfy the Chris@16: /// predicate to the output range. Chris@16: /// \return The updated output iterator Chris@16: /// Chris@16: /// \param r The input range Chris@16: /// \param result An output iterator to write the results into Chris@16: /// \param p A predicate for testing the elements of the range Chris@16: /// Chris@16: template Chris@16: OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p ) Chris@16: { Chris@16: return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p); Chris@16: } Chris@16: Chris@16: Chris@16: /// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) Chris@16: /// \brief Copies all the elements at the start of the input range that Chris@16: /// satisfy the predicate to the output range. Chris@16: /// \return The updated input and output iterators Chris@16: /// Chris@16: /// \param first The start of the input sequence Chris@16: /// \param last One past the end of the input sequence Chris@16: /// \param result An output iterator to write the results into Chris@16: /// \param p A predicate for testing the elements of the range Chris@16: /// Chris@16: template Chris@16: std::pair Chris@16: copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) Chris@16: { Chris@16: for ( ; first != last && p(*first); ++first ) Chris@16: *result++ = *first; Chris@16: return std::make_pair(first, result); Chris@16: } Chris@16: Chris@16: /// \fn copy_while ( const Range &r, OutputIterator result, Predicate p ) Chris@16: /// \brief Copies all the elements at the start of the input range that Chris@16: /// satisfy the predicate to the output range. Chris@16: /// \return The updated input and output iterators Chris@16: /// Chris@16: /// \param r The input range Chris@16: /// \param result An output iterator to write the results into Chris@16: /// \param p A predicate for testing the elements of the range Chris@16: /// Chris@16: template Chris@16: std::pair::type, OutputIterator> Chris@16: copy_while ( const Range &r, OutputIterator result, Predicate p ) Chris@16: { Chris@16: return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p); Chris@16: } Chris@16: Chris@16: Chris@16: /// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) Chris@16: /// \brief Copies all the elements at the start of the input range that do not Chris@16: /// satisfy the predicate to the output range. Chris@16: /// \return The updated output iterator Chris@16: /// Chris@16: /// \param first The start of the input sequence Chris@16: /// \param last One past the end of the input sequence Chris@16: /// \param result An output iterator to write the results into Chris@16: /// \param p A predicate for testing the elements of the range Chris@16: /// Chris@16: template Chris@16: std::pair Chris@16: copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p ) Chris@16: { Chris@16: for ( ; first != last && !p(*first); ++first ) Chris@16: *result++ = *first; Chris@16: return std::make_pair(first, result); Chris@16: } Chris@16: Chris@16: /// \fn copy_until ( const Range &r, OutputIterator result, Predicate p ) Chris@16: /// \brief Copies all the elements at the start of the input range that do not Chris@16: /// satisfy the predicate to the output range. Chris@16: /// \return The updated output iterator Chris@16: /// Chris@16: /// \param r The input range Chris@16: /// \param result An output iterator to write the results into Chris@16: /// \param p A predicate for testing the elements of the range Chris@16: /// Chris@16: template Chris@16: std::pair::type, OutputIterator> Chris@16: copy_until ( const Range &r, OutputIterator result, Predicate p ) Chris@16: { Chris@16: return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p); Chris@16: } Chris@16: Chris@16: }} // namespace boost and algorithm Chris@16: Chris@16: #endif // BOOST_ALGORITHM_COPY_IF_HPP