copy_if.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Copyright (c) Marshall Clow 2008-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file copy_if.hpp
  7. /// \brief Copy a subset of a sequence to a new sequence
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_COPY_IF_HPP
  10. #define BOOST_ALGORITHM_COPY_IF_HPP
  11. #include <utility> // for std::pair, std::make_pair
  12. #include <boost/config.hpp>
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. namespace boost { namespace algorithm {
  16. /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  17. /// \brief Copies all the elements from the input range that satisfy the
  18. /// predicate to the output range.
  19. /// \return The updated output iterator
  20. ///
  21. /// \param first The start of the input sequence
  22. /// \param last One past the end of the input sequence
  23. /// \param result An output iterator to write the results into
  24. /// \param p A predicate for testing the elements of the range
  25. /// \note This function is part of the C++2011 standard library.
  26. template<typename InputIterator, typename OutputIterator, typename Predicate>
  27. BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  28. {
  29. for ( ; first != last; ++first )
  30. if (p(*first))
  31. *result++ = *first;
  32. return result;
  33. }
  34. /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
  35. /// \brief Copies all the elements from the input range that satisfy the
  36. /// predicate to the output range.
  37. /// \return The updated output iterator
  38. ///
  39. /// \param r The input range
  40. /// \param result An output iterator to write the results into
  41. /// \param p A predicate for testing the elements of the range
  42. ///
  43. template<typename Range, typename OutputIterator, typename Predicate>
  44. BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
  45. {
  46. return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
  47. }
  48. /// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  49. /// \brief Copies all the elements at the start of the input range that
  50. /// satisfy the predicate to the output range.
  51. /// \return The updated input and output iterators
  52. ///
  53. /// \param first The start of the input sequence
  54. /// \param last One past the end of the input sequence
  55. /// \param result An output iterator to write the results into
  56. /// \param p A predicate for testing the elements of the range
  57. ///
  58. template<typename InputIterator, typename OutputIterator, typename Predicate>
  59. BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
  60. copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  61. {
  62. for ( ; first != last && p(*first); ++first )
  63. *result++ = *first;
  64. return std::make_pair(first, result);
  65. }
  66. /// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
  67. /// \brief Copies all the elements at the start of the input range that
  68. /// satisfy the predicate to the output range.
  69. /// \return The updated input and output iterators
  70. ///
  71. /// \param r The input range
  72. /// \param result An output iterator to write the results into
  73. /// \param p A predicate for testing the elements of the range
  74. ///
  75. template<typename Range, typename OutputIterator, typename Predicate>
  76. BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
  77. copy_while ( const Range &r, OutputIterator result, Predicate p )
  78. {
  79. return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
  80. }
  81. /// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  82. /// \brief Copies all the elements at the start of the input range that do not
  83. /// satisfy the predicate to the output range.
  84. /// \return The updated output iterator
  85. ///
  86. /// \param first The start of the input sequence
  87. /// \param last One past the end of the input sequence
  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 InputIterator, typename OutputIterator, typename Predicate>
  92. BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
  93. copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  94. {
  95. for ( ; first != last && !p(*first); ++first )
  96. *result++ = *first;
  97. return std::make_pair(first, result);
  98. }
  99. /// \fn copy_until ( const Range &r, OutputIterator result, Predicate p )
  100. /// \brief Copies all the elements at the start of the input range that do not
  101. /// satisfy the predicate to the output range.
  102. /// \return The updated output iterator
  103. ///
  104. /// \param r The input range
  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 Range, typename OutputIterator, typename Predicate>
  109. BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
  110. copy_until ( const Range &r, OutputIterator result, Predicate p )
  111. {
  112. return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
  113. }
  114. /// \fn copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
  115. /// \brief Copies all the elements from the input range that satisfy the
  116. /// copy predicate to the output range while the termination predicate is
  117. /// satisfied.
  118. /// \return The updated output iterator
  119. ///
  120. /// \param first The start of the input sequence
  121. /// \param last One past the end of the input sequence
  122. /// \param result An output iterator to write the results into
  123. /// \param copy_pred A predicate for testing whether to the current element
  124. /// \param term_pred A predicate for testing whether to end the copy operation
  125. template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
  126. BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
  127. copy_if_while ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
  128. {
  129. for ( ; first != last && term_pred(*first); ++first ) {
  130. if (copy_pred(*first)) {
  131. *result++ = *first;
  132. }
  133. }
  134. return std::make_pair(first, result);
  135. }
  136. /// \fn copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
  137. /// \brief Copies all the elements from the input range that satisfy the
  138. /// copy predicate to the output range while the termination predicate is
  139. /// satisfied.
  140. /// \return The updated output iterator
  141. ///
  142. /// \param r The input range
  143. /// \param result An output iterator to write the results into
  144. /// \param copy_pred A predicate for testing whether to the current element
  145. /// \param term_pred A predicate for testing whether to end the copy operation
  146. template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
  147. BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
  148. copy_if_while ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
  149. {
  150. return boost::algorithm::copy_if_while(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
  151. }
  152. /// \fn copy_if_until ( InputIterator first, InputIterator last, 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 until the termination predicate is
  155. /// satisfied.
  156. /// \return The updated output iterator
  157. ///
  158. /// \param first The start of the input sequence
  159. /// \param last One past the end of the input sequence
  160. /// \param result An output iterator to write the results into
  161. /// \param copy_pred A predicate for testing whether to the current element
  162. /// \param term_pred A predicate for testing whether to end the copy operation
  163. template<typename InputIterator, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
  164. BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
  165. copy_if_until ( InputIterator first, InputIterator last, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
  166. {
  167. for ( ; first != last && !term_pred(*first); ++first ) {
  168. if (copy_pred(*first)) {
  169. *result++ = *first;
  170. }
  171. }
  172. return std::make_pair(first, result);
  173. }
  174. /// \fn copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred )
  175. /// \brief Copies all the elements from the input range that satisfy the
  176. /// copy predicate to the output range until the termination predicate is
  177. /// satisfied.
  178. /// \return The updated output iterator
  179. ///
  180. /// \param r The input range
  181. /// \param result An output iterator to write the results into
  182. /// \param copy_pred A predicate for testing whether to the current element
  183. /// \param term_pred A predicate for testing whether to end the copy operation
  184. template<typename Range, typename OutputIterator, typename CopyPredicate, typename TerminatePred>
  185. BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
  186. copy_if_until ( const Range& r, OutputIterator result, CopyPredicate copy_pred, TerminatePred term_pred)
  187. {
  188. return boost::algorithm::copy_if_until(boost::begin(r), boost::end(r), result, copy_pred, term_pred);
  189. }
  190. }} // namespace boost and algorithm
  191. #endif // BOOST_ALGORITHM_COPY_IF_HPP