replace_copy.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_REPLACE_COPY_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_REPLACE_COPY_HPP
  12. #include <iterator>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/algorithm/copy.hpp>
  17. #include <boost/compute/algorithm/replace.hpp>
  18. #include <boost/compute/type_traits/is_device_iterator.hpp>
  19. namespace boost {
  20. namespace compute {
  21. /// Copies the value in the range [\p first, \p last) to the range
  22. /// beginning at \p result while replacing each instance of \p old_value
  23. /// with \p new_value.
  24. ///
  25. /// Space complexity: \Omega(1)
  26. ///
  27. /// \see replace()
  28. template<class InputIterator, class OutputIterator, class T>
  29. inline OutputIterator
  30. replace_copy(InputIterator first,
  31. InputIterator last,
  32. OutputIterator result,
  33. const T &old_value,
  34. const T &new_value,
  35. command_queue &queue = system::default_queue())
  36. {
  37. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  38. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  39. typedef typename std::iterator_traits<OutputIterator>::difference_type difference_type;
  40. difference_type count = std::distance(first, last);
  41. if(count == 0){
  42. return result;
  43. }
  44. // copy data to result
  45. ::boost::compute::copy(first, last, result, queue);
  46. // replace in result
  47. ::boost::compute::replace(result,
  48. result + count,
  49. old_value,
  50. new_value,
  51. queue);
  52. // return iterator to the end of result
  53. return result + count;
  54. }
  55. } // end compute namespace
  56. } // end boost namespace
  57. #endif // BOOST_COMPUTE_ALGORITHM_REPLACE_COPY_HPP