1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
* Copyright 2020 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*! \file thrust/iterator/transform_input_output_iterator.h
* \brief An iterator which adapts another iterator by applying transform
* functions when reading and writing dereferenced values.
*/
#pragma once
#include <thrust/detail/config.h>
#include <thrust/iterator/detail/transform_input_output_iterator.inl>
THRUST_NAMESPACE_BEGIN
/*! \addtogroup iterators
* \{
*/
/*! \addtogroup fancyiterator Fancy Iterators
* \ingroup iterators
* \{
*/
/*! \p transform_input_output_iterator is a special kind of iterator which applies
* transform functions when reading from or writing to dereferenced values.
* This iterator is useful for algorithms that operate on a type that needs to
* be serialized/deserialized from values in another iterator, avoiding the
* need to materialize intermediate results in memory. This also enables the
* transform functions to be fused with the operations that read and write to
* the `transform_input_output_iterator`.
*
* The following code snippet demonstrates how to create a
* \p transform_input_output_iterator which performs different transformations when
* reading from and writing to the iterator.
*
* \code
* #include <thrust/iterator/transform_input_output_iterator.h>
* #include <thrust/device_vector.h>
*
* int main()
* {
* const size_t size = 4;
* thrust::device_vector<float> v(size);
*
* // Write 1.0f, 2.0f, 3.0f, 4.0f to vector
* thrust::sequence(v.begin(), v.end(), 1);
*
* // Iterator that returns negated values and writes squared values
* auto iter = thrust::make_transform_input_output_iterator(v.begin(),
* thrust::negate<float>{}, thrust::square<float>{});
*
* // Iterator negates values when reading
* std::cout << iter[0] << " "; // -1.0f;
* std::cout << iter[1] << " "; // -2.0f;
* std::cout << iter[2] << " "; // -3.0f;
* std::cout << iter[3] << "\n"; // -4.0f;
*
* // Write 1.0f, 2.0f, 3.0f, 4.0f to iterator
* thrust::sequence(iter, iter + size, 1);
*
* // Values were squared before writing to vector
* std::cout << v[0] << " "; // 1.0f;
* std::cout << v[1] << " "; // 4.0f;
* std::cout << v[2] << " "; // 9.0f;
* std::cout << v[3] << "\n"; // 16.0f;
*
* }
* \endcode
*
* \see make_transform_input_output_iterator
*/
template <typename InputFunction, typename OutputFunction, typename Iterator>
class transform_input_output_iterator
: public detail::transform_input_output_iterator_base<InputFunction, OutputFunction, Iterator>::type
{
/*! \cond
*/
public:
typedef typename
detail::transform_input_output_iterator_base<InputFunction, OutputFunction, Iterator>::type
super_t;
friend class thrust::iterator_core_access;
/*! \endcond
*/
/*! This constructor takes as argument a \c Iterator an \c InputFunction and an
* \c OutputFunction and copies them to a new \p transform_input_output_iterator
*
* \param io An \c Iterator pointing to where the input to \c InputFunction
* will be read from and the result of \c OutputFunction will be written to
* \param input_function An \c InputFunction to be executed on values read from the iterator
* \param output_function An \c OutputFunction to be executed on values written to the iterator
*/
__host__ __device__
transform_input_output_iterator(Iterator const& io, InputFunction input_function, OutputFunction output_function)
: super_t(io), input_function(input_function), output_function(output_function)
{
}
/*! \cond
*/
private:
__host__ __device__
typename super_t::reference dereference() const
{
return detail::transform_input_output_iterator_proxy<
InputFunction, OutputFunction, Iterator
>(this->base_reference(), input_function, output_function);
}
InputFunction input_function;
OutputFunction output_function;
/*! \endcond
*/
}; // end transform_input_output_iterator
/*! \p make_transform_input_output_iterator creates a \p transform_input_output_iterator from
* an \c Iterator a \c InputFunction and a \c OutputFunction
*
* \param io An \c Iterator pointing to where the input to \c InputFunction
* will be read from and the result of \c OutputFunction will be written to
* \param input_function An \c InputFunction to be executed on values read from the iterator
* \param output_function An \c OutputFunction to be executed on values written to the iterator
* \see transform_input_output_iterator
*/
template <typename InputFunction, typename OutputFunction, typename Iterator>
transform_input_output_iterator<InputFunction, OutputFunction, Iterator>
__host__ __device__
make_transform_input_output_iterator(Iterator io, InputFunction input_function, OutputFunction output_function)
{
return transform_input_output_iterator<InputFunction, OutputFunction, Iterator>(io, input_function, output_function);
} // end make_transform_input_output_iterator
/*! \} // end fancyiterators
*/
/*! \} // end iterators
*/
THRUST_NAMESPACE_END
|