copy_if.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[email protected]>
  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_COPY_IF_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_COPY_IF_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/algorithm/transform_if.hpp>
  14. #include <boost/compute/functional/identity.hpp>
  15. #include <boost/compute/type_traits/is_device_iterator.hpp>
  16. namespace boost {
  17. namespace compute {
  18. namespace detail {
  19. // like the copy_if() algorithm but writes the indices of the values for which
  20. // predicate returns true.
  21. template<class InputIterator, class OutputIterator, class Predicate>
  22. inline OutputIterator copy_index_if(InputIterator first,
  23. InputIterator last,
  24. OutputIterator result,
  25. Predicate predicate,
  26. command_queue &queue = system::default_queue())
  27. {
  28. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  29. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  30. typedef typename std::iterator_traits<InputIterator>::value_type T;
  31. return detail::transform_if_impl(
  32. first, last, result, identity<T>(), predicate, true, queue
  33. );
  34. }
  35. } // end detail namespace
  36. /// Copies each element in the range [\p first, \p last) for which
  37. /// \p predicate returns \c true to the range beginning at \p result.
  38. ///
  39. /// Space complexity: \Omega(2n)
  40. template<class InputIterator, class OutputIterator, class Predicate>
  41. inline OutputIterator copy_if(InputIterator first,
  42. InputIterator last,
  43. OutputIterator result,
  44. Predicate predicate,
  45. command_queue &queue = system::default_queue())
  46. {
  47. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  48. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  49. typedef typename std::iterator_traits<InputIterator>::value_type T;
  50. return ::boost::compute::transform_if(
  51. first, last, result, identity<T>(), predicate, queue
  52. );
  53. }
  54. } // end compute namespace
  55. } // end boost namespace
  56. #endif // BOOST_COMPUTE_ALGORITHM_COPY_IF_HPP