copy_if.hpp 5.4 KB

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