]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_TRANSFORM_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP | |
13 | ||
14 | #include <boost/compute/system.hpp> | |
15 | #include <boost/compute/command_queue.hpp> | |
16 | #include <boost/compute/algorithm/copy.hpp> | |
17 | #include <boost/compute/iterator/transform_iterator.hpp> | |
18 | #include <boost/compute/iterator/zip_iterator.hpp> | |
19 | #include <boost/compute/functional/detail/unpack.hpp> | |
20 | ||
21 | namespace boost { | |
22 | namespace compute { | |
23 | ||
24 | /// Transforms the elements in the range [\p first, \p last) using | |
25 | /// operator \p op and stores the results in the range beginning at | |
26 | /// \p result. | |
27 | /// | |
28 | /// For example, to calculate the absolute value for each element in a vector: | |
29 | /// | |
30 | /// \snippet test/test_transform.cpp transform_abs | |
31 | /// | |
32 | /// \see copy() | |
33 | template<class InputIterator, class OutputIterator, class UnaryOperator> | |
34 | inline OutputIterator transform(InputIterator first, | |
35 | InputIterator last, | |
36 | OutputIterator result, | |
37 | UnaryOperator op, | |
38 | command_queue &queue = system::default_queue()) | |
39 | { | |
40 | return copy( | |
41 | ::boost::compute::make_transform_iterator(first, op), | |
42 | ::boost::compute::make_transform_iterator(last, op), | |
43 | result, | |
44 | queue | |
45 | ); | |
46 | } | |
47 | ||
48 | /// \overload | |
49 | template<class InputIterator1, | |
50 | class InputIterator2, | |
51 | class OutputIterator, | |
52 | class BinaryOperator> | |
53 | inline OutputIterator transform(InputIterator1 first1, | |
54 | InputIterator1 last1, | |
55 | InputIterator2 first2, | |
56 | OutputIterator result, | |
57 | BinaryOperator op, | |
58 | command_queue &queue = system::default_queue()) | |
59 | { | |
60 | typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type; | |
61 | ||
62 | difference_type n = std::distance(first1, last1); | |
63 | ||
64 | return transform( | |
65 | make_zip_iterator(boost::make_tuple(first1, first2)), | |
66 | make_zip_iterator(boost::make_tuple(last1, first2 + n)), | |
67 | result, | |
68 | detail::unpack(op), | |
69 | queue | |
70 | ); | |
71 | } | |
72 | ||
73 | } // end compute namespace | |
74 | } // end boost namespace | |
75 | ||
76 | #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP |