1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_REPLACE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_REPLACE_HPP
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/command_queue.hpp>
16 #include <boost/compute/detail/meta_kernel.hpp>
17 #include <boost/compute/detail/iterator_range_size.hpp>
23 template<class Iterator, class T>
24 class replace_kernel : public meta_kernel
28 : meta_kernel("replace")
33 void set_range(Iterator first, Iterator last)
35 m_count = detail::iterator_range_size(first, last);
38 "const uint i = get_global_id(0);\n" <<
39 "if(" << first[var<cl_uint>("i")] << " == " << var<T>("old_value") << ")\n" <<
40 " " << first[var<cl_uint>("i")] << '=' << var<T>("new_value") << ";\n";
43 void set_old_value(const T &old_value)
45 add_set_arg<T>("old_value", old_value);
48 void set_new_value(const T &new_value)
50 add_set_arg<T>("new_value", new_value);
53 void exec(command_queue &queue)
60 exec_1d(queue, 0, m_count);
67 } // end detail namespace
69 /// Replaces each instance of \p old_value in the range [\p first,
70 /// \p last) with \p new_value.
71 template<class Iterator, class T>
72 inline void replace(Iterator first,
76 command_queue &queue = system::default_queue())
78 detail::replace_kernel<Iterator, T> kernel;
80 kernel.set_range(first, last);
81 kernel.set_old_value(old_value);
82 kernel.set_new_value(new_value);
87 } // end compute namespace
88 } // end boost namespace
90 #endif // BOOST_COMPUTE_ALGORITHM_REPLACE_HPP