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