partition_copy.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. Copyright (c) Marshall Clow 2011-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 partition_copy.hpp
  7. /// \brief Copy a subset of a sequence to a new sequence
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_PARTITION_COPY_HPP
  10. #define BOOST_ALGORITHM_PARTITION_COPY_HPP
  11. #include <algorithm> // for std::partition_copy, if available
  12. #include <utility> // for make_pair
  13. #include <boost/range/begin.hpp>
  14. #include <boost/range/end.hpp>
  15. namespace boost { namespace algorithm {
  16. #if __cplusplus >= 201103L
  17. // Use the C++11 versions of partition_copy if it is available
  18. using std::partition_copy; // Section 25.3.13
  19. #else
  20. /// \fn partition_copy ( InputIterator first, InputIterator last,
  21. /// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
  22. /// \brief Copies the elements that satisfy the predicate p from the range [first, last)
  23. /// to the range beginning at d_first_true, and
  24. /// copies the elements that do not satisfy p to the range beginning at d_first_false.
  25. ///
  26. ///
  27. /// \param first The start of the input sequence
  28. /// \param last One past the end of the input sequence
  29. /// \param out_true An output iterator to write the elements that satisfy the predicate into
  30. /// \param out_false An output iterator to write the elements that do not satisfy the predicate into
  31. /// \param p A predicate for dividing the elements of the input sequence.
  32. ///
  33. /// \note This function is part of the C++2011 standard library.
  34. /// We will use the standard one if it is available,
  35. /// otherwise we have our own implementation.
  36. template <typename InputIterator,
  37. typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
  38. std::pair<OutputIterator1, OutputIterator2>
  39. partition_copy ( InputIterator first, InputIterator last,
  40. OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
  41. {
  42. for ( ; first != last; ++first )
  43. if ( p (*first))
  44. *out_true++ = *first;
  45. else
  46. *out_false++ = *first;
  47. return std::pair<OutputIterator1, OutputIterator2> ( out_true, out_false );
  48. }
  49. #endif
  50. /// \fn partition_copy ( const Range &r,
  51. /// OutputIterator1 out_true, OutputIterator2 out_false, UnaryPredicate p )
  52. ///
  53. /// \param r The input range
  54. /// \param out_true An output iterator to write the elements that satisfy the predicate into
  55. /// \param out_false An output iterator to write the elements that do not satisfy the predicate into
  56. /// \param p A predicate for dividing the elements of the input sequence.
  57. ///
  58. template <typename Range, typename OutputIterator1, typename OutputIterator2,
  59. typename UnaryPredicate>
  60. std::pair<OutputIterator1, OutputIterator2>
  61. partition_copy ( const Range &r, OutputIterator1 out_true, OutputIterator2 out_false,
  62. UnaryPredicate p )
  63. {
  64. return boost::algorithm::partition_copy
  65. (boost::begin(r), boost::end(r), out_true, out_false, p );
  66. }
  67. }} // namespace boost and algorithm
  68. #endif // BOOST_ALGORITHM_PARTITION_COPY_HPP