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