remove_if.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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_REMOVE_IF_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_REMOVE_IF_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/algorithm/copy_if.hpp>
  15. #include <boost/compute/container/vector.hpp>
  16. #include <boost/compute/functional/logical.hpp>
  17. #include <boost/compute/type_traits/is_device_iterator.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// Removes each element for which \p predicate returns \c true in the
  21. /// range [\p first, \p last).
  22. ///
  23. /// Space complexity: \Omega(3n)
  24. ///
  25. /// \see remove()
  26. template<class Iterator, class Predicate>
  27. inline Iterator remove_if(Iterator first,
  28. Iterator last,
  29. Predicate predicate,
  30. command_queue &queue = system::default_queue())
  31. {
  32. BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
  33. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  34. // temporary storage for the input data
  35. ::boost::compute::vector<value_type> tmp(first, last, queue);
  36. return ::boost::compute::copy_if(tmp.begin(),
  37. tmp.end(),
  38. first,
  39. not1(predicate),
  40. queue);
  41. }
  42. } // end compute namespace
  43. } // end boost namespace
  44. #endif // BOOST_COMPUTE_ALGORITHM_REMOVE_IF_HPP