copy_if.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. namespace boost { namespace algorithm {
  15. /// \fn copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  16. /// \brief Copies all the elements from the input range that satisfy the
  17. /// predicate to the output range.
  18. /// \return The updated output iterator
  19. ///
  20. /// \param first The start of the input sequence
  21. /// \param last One past the end of the input sequence
  22. /// \param result An output iterator to write the results into
  23. /// \param p A predicate for testing the elements of the range
  24. /// \note This function is part of the C++2011 standard library.
  25. template<typename InputIterator, typename OutputIterator, typename Predicate>
  26. BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  27. {
  28. for ( ; first != last; ++first )
  29. if (p(*first))
  30. *result++ = *first;
  31. return result;
  32. }
  33. /// \fn copy_if ( const Range &r, OutputIterator result, Predicate p )
  34. /// \brief Copies all the elements from the input range that satisfy the
  35. /// predicate to the output range.
  36. /// \return The updated output iterator
  37. ///
  38. /// \param r The input range
  39. /// \param result An output iterator to write the results into
  40. /// \param p A predicate for testing the elements of the range
  41. ///
  42. template<typename Range, typename OutputIterator, typename Predicate>
  43. BOOST_CXX14_CONSTEXPR OutputIterator copy_if ( const Range &r, OutputIterator result, Predicate p )
  44. {
  45. return boost::algorithm::copy_if (boost::begin (r), boost::end(r), result, p);
  46. }
  47. /// \fn copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  48. /// \brief Copies all the elements at the start of the input range that
  49. /// satisfy the predicate to the output range.
  50. /// \return The updated input and output iterators
  51. ///
  52. /// \param first The start of the input sequence
  53. /// \param last One past the end of the input sequence
  54. /// \param result An output iterator to write the results into
  55. /// \param p A predicate for testing the elements of the range
  56. ///
  57. template<typename InputIterator, typename OutputIterator, typename Predicate>
  58. BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
  59. copy_while ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  60. {
  61. for ( ; first != last && p(*first); ++first )
  62. *result++ = *first;
  63. return std::make_pair(first, result);
  64. }
  65. /// \fn copy_while ( const Range &r, OutputIterator result, Predicate p )
  66. /// \brief Copies all the elements at the start of the input range that
  67. /// satisfy the predicate to the output range.
  68. /// \return The updated input and output iterators
  69. ///
  70. /// \param r The input range
  71. /// \param result An output iterator to write the results into
  72. /// \param p A predicate for testing the elements of the range
  73. ///
  74. template<typename Range, typename OutputIterator, typename Predicate>
  75. BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
  76. copy_while ( const Range &r, OutputIterator result, Predicate p )
  77. {
  78. return boost::algorithm::copy_while (boost::begin (r), boost::end(r), result, p);
  79. }
  80. /// \fn copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  81. /// \brief Copies all the elements at the start of the input range that do not
  82. /// satisfy the predicate to the output range.
  83. /// \return The updated output iterator
  84. ///
  85. /// \param first The start of the input sequence
  86. /// \param last One past the end of the input sequence
  87. /// \param result An output iterator to write the results into
  88. /// \param p A predicate for testing the elements of the range
  89. ///
  90. template<typename InputIterator, typename OutputIterator, typename Predicate>
  91. BOOST_CXX14_CONSTEXPR std::pair<InputIterator, OutputIterator>
  92. copy_until ( InputIterator first, InputIterator last, OutputIterator result, Predicate p )
  93. {
  94. for ( ; first != last && !p(*first); ++first )
  95. *result++ = *first;
  96. return std::make_pair(first, result);
  97. }
  98. /// \fn copy_until ( const Range &r, 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 r The input range
  104. /// \param result An output iterator to write the results into
  105. /// \param p A predicate for testing the elements of the range
  106. ///
  107. template<typename Range, typename OutputIterator, typename Predicate>
  108. BOOST_CXX14_CONSTEXPR std::pair<typename boost::range_iterator<const Range>::type, OutputIterator>
  109. copy_until ( const Range &r, OutputIterator result, Predicate p )
  110. {
  111. return boost::algorithm::copy_until (boost::begin (r), boost::end(r), result, p);
  112. }
  113. }} // namespace boost and algorithm
  114. #endif // BOOST_ALGORITHM_COPY_IF_HPP