copy_if.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. }} // namespace boost and algorithm
  115. #endif // BOOST_ALGORITHM_COPY_IF_HPP