cub::TransformInputIterator

Defined in cub/iterator/transform_input_iterator.cuh

template<typename ValueType, typename ConversionOp, typename InputIteratorT, typename OffsetT = ptrdiff_t>
class TransformInputIterator

A random-access input wrapper for transforming dereferenced values.

Overview

  • TransformInputIteratorTwraps a unary conversion functor of type ConversionOp and a random-access input iterator of type InputIteratorT, using the former to produce references of type ValueType from the latter.

  • Can be used with any data type.

  • Can be constructed, manipulated, and exchanged within and between host and device functions. Wrapped host memory can only be dereferenced on the host, and wrapped device memory can only be dereferenced on the device.

  • Compatible with Thrust API v1.7 or newer.

Snippet

The code snippet below illustrates the use of TransformInputIteratorTto dereference an array of integers, tripling the values and converting them to doubles.

#include <cub/cub.cuh>   // or equivalently <cub/iterator/transform_input_iterator.cuh>

// Functor for tripling integer values and converting to doubles
struct TripleDoubler
{
    __host__ __device__ __forceinline__
    double operator()(const int &a) const {
        return double(a * 3);
    }
};

// Declare, allocate, and initialize a device array
int *d_in;                   // e.g., [8, 6, 7, 5, 3, 0, 9]
TripleDoubler conversion_op;

// Create an iterator wrapper
cub::TransformInputIterator<double, TripleDoubler, int*> itr(d_in, conversion_op);

// Within device code:
printf("%f\n", itr[0]);  // 24.0
printf("%f\n", itr[1]);  // 18.0
printf("%f\n", itr[6]);  // 27.0

Template Parameters
  • ValueType – The value type of this iterator

  • ConversionOp – Unary functor type for mapping objects of type InputType to type ValueType. Must have member ValueType operator()(const InputType &datum).

  • InputIteratorT – The type of the wrapped input iterator

  • OffsetT – The difference type of this iterator (Default: ptrdiff_t)

Public Types

using self_type = TransformInputIterator

My own type.

using difference_type = OffsetT

Type to express the result of subtracting one iterator from another.

using value_type = ValueType

The type of the element the iterator can point to.

using pointer = ValueType*

The type of a pointer to an element the iterator can point to.

using reference = ValueType

The type of a reference to an element the iterator can point to.

using iterator_category = std::random_access_iterator_tag

The iterator category.

Public Functions

inline TransformInputIterator(InputIteratorT input_itr, ConversionOp conversion_op)
Parameters
  • input_itr – Input iterator to wrap

  • conversion_op – Conversion functor to wrap

inline self_type operator++(int)

Postfix increment.

inline self_type operator++()

Prefix increment.

inline reference operator*() const

Indirection.

template<typename Distance>
inline self_type operator+(Distance n) const

Addition.

template<typename Distance>
inline self_type &operator+=(Distance n)

Addition assignment.

template<typename Distance>
inline self_type operator-(Distance n) const

Subtraction.

template<typename Distance>
inline self_type &operator-=(Distance n)

Subtraction assignment.

inline difference_type operator-(self_type other) const

Distance.

template<typename Distance>
inline reference operator[](Distance n) const

Array subscript.

inline bool operator==(const self_type &rhs) const

Equal to.

inline bool operator!=(const self_type &rhs) const

Not equal to.

Friends

inline friend std::ostream &operator<<(std::ostream &os, const self_type&)

ostream operator