partition_copy.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ALGORITHM_PARTITION_COPY_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_PARTITION_COPY_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/functional.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/algorithm/copy_if.hpp>
  17. #include <boost/compute/type_traits/is_device_iterator.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// Copies all of the elements in the range [\p first, \p last) for which
  21. /// \p predicate returns \c true to the range beginning at \p first_true
  22. /// and all of the elements for which \p predicate returns \c false to
  23. /// the range beginning at \p first_false.
  24. ///
  25. /// Space complexity: \Omega(2n)
  26. ///
  27. /// \see partition()
  28. template<class InputIterator,
  29. class OutputIterator1,
  30. class OutputIterator2,
  31. class UnaryPredicate>
  32. inline std::pair<OutputIterator1, OutputIterator2>
  33. partition_copy(InputIterator first,
  34. InputIterator last,
  35. OutputIterator1 first_true,
  36. OutputIterator2 first_false,
  37. UnaryPredicate predicate,
  38. command_queue &queue = system::default_queue())
  39. {
  40. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  41. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator1>::value);
  42. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator2>::value);
  43. // copy true values
  44. OutputIterator1 last_true =
  45. ::boost::compute::copy_if(first,
  46. last,
  47. first_true,
  48. predicate,
  49. queue);
  50. // copy false values
  51. OutputIterator2 last_false =
  52. ::boost::compute::copy_if(first,
  53. last,
  54. first_false,
  55. not1(predicate),
  56. queue);
  57. // return iterators to the end of the true and the false ranges
  58. return std::make_pair(last_true, last_false);
  59. }
  60. } // end compute namespace
  61. } // end boost namespace
  62. #endif // BOOST_COMPUTE_ALGORITHM_PARTITION_COPY_HPP