Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:22

0001 /* 
0002    Copyright (c) Marshall Clow 2008-2012.
0003 
0004    Distributed under the Boost Software License, Version 1.0. (See accompanying
0005    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 */
0007 
0008 /// \file  copy_if.hpp
0009 /// \brief Copy a subset of a sequence to a new sequence
0010 /// \author Marshall Clow
0011 
0012 #ifndef BOOST_ALGORITHM_COPY_IF_HPP
0013 #define BOOST_ALGORITHM_COPY_IF_HPP
0014 
0015 #include <utility>    // for std::pair, std::make_pair
0016 
0017 #include <boost/config.hpp>
0018 #include <boost/range/begin.hpp>
0019 #include <boost/range/end.hpp>
0020 
0021 namespace boost { namespace algorithm {
0022 
0023 /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
0024 /// \brief Copies all the elements from the input range that satisfy the
0025 /// predicate to the output range.
0026 /// \return The updated output iterator
0027 /// 
0028 /// \param first    The start of the input sequence
0029 /// \param last     One past the end of the input sequence
0030 /// \param result   An output iterator to write the results into
0031 /// \param p        A predicate for testing the elements of the range
0032 /// \note           This function is part of the C++2011 standard library.
0033 template<typename InputIterator, typename OutputIterator, typename Predicate> 
0034 BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
0035 {
0036     for ( ; first != last; ++first )
0037         if (p(*first))
0038             *result++ = *first;
0039     return result;
0040 }
0041 
0042 /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
0043 /// \brief Copies all the elements from the input range that satisfy the
0044 /// predicate to the output range.
0045 /// \return The updated output iterator
0046 /// 
0047 /// \param r        The input range
0048 /// \param result   An output iterator to write the results into
0049 /// \param p        A predicate for testing the elements of the range
0050 ///
0051 template<typename Range, typename OutputIterator, typename Predicate>
0052 BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
0053 {
0054     return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
0055 }
0056 
0057 
0058 /// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
0059 /// \brief Copies all the elements at the start of the input range that
0060 ///     satisfy the predicate to the output range.
0061 /// \return The updated input and output iterators
0062 /// 
0063 /// \param first    The start of the input sequence
0064 /// \param last     One past the end of the input sequence
0065 /// \param result   An output iterator to write the results into
0066 /// \param p        A predicate for testing the elements of the range
0067 ///
0068 template<typename InputIterator, typename OutputIterator, typename Predicate> 
0069 BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
0070 copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
0071 {
0072     for ( ; first != last && p(*first); ++first )
0073         *result++ = *first;
0074     return std::make_pair(first, result);
0075 }
0076 
0077 /// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
0078 /// \brief Copies all the elements at the start of the input range that
0079 ///     satisfy the predicate to the output range.
0080 /// \return The updated input and output iterators
0081 /// 
0082 /// \param r        The input range
0083 /// \param result   An output iterator to write the results into
0084 /// \param p        A predicate for testing the elements of the range
0085 ///
0086 template<typename Range, typename OutputIterator, typename Predicate>
0087 BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator> 
0088 copy_while ( const Range &r, OutputIterator result, Predicate p )
0089 {
0090     return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
0091 }
0092 
0093 
0094 /// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
0095 /// \brief Copies all the elements at the start of the input range that do not
0096 ///     satisfy the predicate to the output range.
0097 /// \return The updated output iterator
0098 /// 
0099 /// \param first    The start of the input sequence
0100 /// \param last     One past the end of the input sequence
0101 /// \param result   An output iterator to write the results into
0102 /// \param p        A predicate for testing the elements of the range
0103 ///
0104 template<typename InputIterator, typename OutputIterator, typename Predicate> 
0105 BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
0106 copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
0107 {
0108     for ( ; first != last && !p(*first); ++first )
0109         *result++ = *first;
0110     return std::make_pair(first, result);
0111 }
0112 
0113 /// \fn copy_until ( const Range &r, OutputIterator result, Predicate p )
0114 /// \brief Copies all the elements at the start of the input range that do not
0115 ///     satisfy the predicate to the output range.
0116 /// \return The updated output iterator
0117 /// 
0118 /// \param r        The input range
0119 /// \param result   An output iterator to write the results into
0120 /// \param p        A predicate for testing the elements of the range
0121 ///
0122 template<typename Range, typename OutputIterator, typename Predicate>
0123 BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator> 
0124 copy_until ( const Range &r, OutputIterator result, Predicate p )
0125 {
0126     return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
0127 }
0128 
0129 /// \fn copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
0130 /// \brief Copies all the elements from the input range that satisfy the
0131 /// copy predicate to the output range while the termination predicate is
0132 /// satisfied.
0133 /// \return The updated output iterator
0134 ///
0135 /// \param first     The start of the input sequence
0136 /// \param last      One past the end of the input sequence
0137 /// \param result    An output iterator to write the results into
0138 /// \param copy_pred A predicate for testing whether to the current element
0139 /// \param term_pred A predicate for testing whether to end the copy operation
0140 template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
0141 BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
0142 copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
0143 {
0144     for ( ; first != last && term_pred(*first); ++first ) {
0145         if (copy_pred(*first)) {
0146             *result++ = *first;
0147         }
0148     }
0149     return std::make_pair(first, result);
0150 }
0151 
0152 /// \fn copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
0153 /// \brief Copies all the elements from the input range that satisfy the
0154 /// copy predicate to the output range while the termination predicate is
0155 /// satisfied.
0156 /// \return The updated output iterator
0157 ///
0158 /// \param r         The input range
0159 /// \param result    An output iterator to write the results into
0160 /// \param copy_pred A predicate for testing whether to the current element
0161 /// \param term_pred A predicate for testing whether to end the copy operation
0162 template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
0163 BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
0164 copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
0165 {
0166     return boost::algorithm::copy_if_while(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
0167 }
0168 
0169 /// \fn copy_if_until ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
0170 /// \brief Copies all the elements from the input range that satisfy the
0171 /// copy predicate to the output range until the termination predicate is
0172 /// satisfied.
0173 /// \return The updated output iterator
0174 ///
0175 /// \param first     The start of the input sequence
0176 /// \param last      One past the end of the input sequence
0177 /// \param result    An output iterator to write the results into
0178 /// \param copy_pred A predicate for testing whether to the current element
0179 /// \param term_pred A predicate for testing whether to end the copy operation
0180 template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
0181 BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
0182 copy_if_until ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
0183 {
0184     for ( ; first != last && !term_pred(*first); ++first ) {
0185         if (copy_pred(*first)) {
0186             *result++ = *first;
0187         }
0188     }
0189     return std::make_pair(first, result);
0190 }
0191 
0192 /// \fn copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
0193 /// \brief Copies all the elements from the input range that satisfy the
0194 /// copy predicate to the output range until the termination predicate is
0195 /// satisfied.
0196 /// \return The updated output iterator
0197 ///
0198 /// \param r         The input range
0199 /// \param result    An output iterator to write the results into
0200 /// \param copy_pred A predicate for testing whether to the current element
0201 /// \param term_pred A predicate for testing whether to end the copy operation
0202 template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
0203 BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
0204 copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
0205 {
0206     return boost::algorithm::copy_if_until(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
0207 }
0208 
0209 }} // namespace boost and algorithm
0210 
0211 #endif  // BOOST_ALGORITHM_COPY_IF_HPP