permutation_iterator#
-
template<class _Iter, class _Index = _Iter>
class permutation_iterator# permutation_iteratoris an iterator which represents a pointer into a reordered view of a given range.permutation_iteratoris an imprecise name; the reordered view need not be a strict permutation. This iterator is useful for fusing a scatter or gather operation with other algorithms.This iterator takes two arguments:
an iterator to the range
Von which the “permutation” will be applied, referred to asiterbelowan iterator to a range of indices defining the reindexing scheme that determines how the elements of
Vwill be permuted, referred to asindexbelow
Note that
permutation_iteratoris not limited to strict permutations of the given rangeV. The distance between begin and end of the reindexing iterators is allowed to be smaller compared to the size of the rangeV, in which case thepermutation_iteratoronly provides a “permutation” of a subset ofV. The indices do not need to be unique. In this same context, it must be noted that the past-the-endpermutation_iteratoris completely defined by means of the past-the-end iterator to the indices.The following code snippet demonstrates how to create a
permutation_iteratorwhich represents a reordering of the contents of adevice_vector.#include <cuda/iterator> #include <thrust/device_vector.h> ... thrust::device_vector<float> values{10.0f, 20.0f, 30.0f, 40.0f, 50.0f, 60.0f, 70.0f, 80.0f}; thrust::device_vector<int> indices{2, 6, 1, 3}; using ElementIterator = thrust::device_vector<float>::iterator; using IndexIterator = thrust::device_vector<int>::iterator; cuda::permutation_iterator<ElementIterator,IndexIterator> iter(values.begin(), indices.begin()); *iter; // returns 30.0f; iter[0]; // returns 30.0f; iter[1]; // returns 70.0f; iter[2]; // returns 20.0f; iter[3]; // returns 40.0f; // iter[4] is an out-of-bounds error *iter = -1.0f; // sets values[2] to -1.0f; iter[0] = -1.0f; // sets values[2] to -1.0f; iter[1] = -1.0f; // sets values[6] to -1.0f; iter[2] = -1.0f; // sets values[1] to -1.0f; iter[3] = -1.0f; // sets values[3] to -1.0f; // values is now {10, -1, -1, -1, 50, 60, -1, 80}
Public Types
-
using iterator_concept = ::cuda::std::random_access_iterator_tag#
-
using iterator_category = ::cuda::std::random_access_iterator_tag#
Public Functions
-
constexpr permutation_iterator() = default#
Ensure that the user passes an iterator to something interger_like.
Ensure that the index value_type is convertible to difference_type To actually use operator+ we need the index iterator to be random access To actually use operator+ we need the base iterator to be random access
Default constructs an
permutation_iteratorwith a value initialized iterator and index
- inline constexpr permutation_iterator( ) noexcept(::cuda::std::is_nothrow_copy_constructible_v<_Iter> && ::cuda::std::is_nothrow_copy_constructible_v<_Index>)#
Constructs an
permutation_iteratorfrom an iterator and an optional index.- Parameters:
__iter – The iterator to to index from
__index – The iterator with the permutations
-
inline constexpr const _Iter &base() const & noexcept#
Returns a const reference to the stored base iterator
iter.
- inline constexpr _Iter base(
Extracts the stored base iterator
iter.
-
inline constexpr difference_type index() const noexcept#
Returns the current index.
- Returns:
Equivalent to
*index
- inline constexpr decltype(auto) operator*(
Dereferences the
permutation_iterator.- Returns:
Equivalent to
iter[*index]
-
template<class _Iter2 = _Iter, ::cuda::std::enable_if_t<::cuda::std::__dereferenceable<const _Iter2>, int> = 0>
inline constexpr decltype(auto) operator*( Dereferences the
permutation_iterator.- Returns:
Equivalent to
iter[*index]
- inline constexpr decltype(auto) operator[](
- difference_type __n,
Subscripts the
permutation_iteratorby an offset.- Parameters:
__n – The additional offset
- Returns:
Equivalent to
iter[index[__n]]
-
template<class _Iter2 = _Iter, ::cuda::std::enable_if_t<::cuda::std::__dereferenceable<const _Iter2>, int> = 0>
inline constexpr decltype(auto) operator[]( - difference_type __n,
Subscripts the
permutation_iteratorby an offset.- Parameters:
__n – The additional offset
- Returns:
Equivalent to
iter[index[__n]]
- inline constexpr permutation_iterator &operator++(
Increments the
permutation_iterator.- Returns:
Equivalent to
++index
- inline constexpr permutation_iterator operator++(
- int,
Increments the
permutation_iterator.- Returns:
Equivalent to
index++
- inline constexpr permutation_iterator &operator--(
Increments the
permutation_iterator.- Returns:
Equivalent to
--index
- inline constexpr permutation_iterator operator--(
- int,
Increments the
permutation_iterator.- Returns:
Equivalent to
index++
- inline constexpr permutation_iterator &operator+=(
- difference_type __n,
Advances the
permutation_iteratorby a given number of elements.- Parameters:
__n – The number of elements to advance
- Returns:
Equivalent to
index + __n
- inline constexpr permutation_iterator &operator-=(
- difference_type __n,
Decrements the
permutation_iteratorby a given number of elements.- Parameters:
__n – The number of elements to decrement
- Returns:
Equivalent to
index - __n
Friends
- inline friend constexpr permutation_iterator operator+ (const permutation_iterator &__iter, difference_type __n) noexcept(//noexcept(__iter.__index_+__n) &&::cuda::std::is_nothrow_copy_constructible_v< _Iter > &&::cuda::std::is_nothrow_copy_constructible_v< _Index >)
Advances a
permutation_iteratorby a given number of elements.- Parameters:
__iter – The original
permutation_iterator__n – The number of elements to advance
- Returns:
Equivalent to
permutation_iterator{iter, index + __n}
- inline friend constexpr permutation_iterator operator+(
- difference_type __n,
- const permutation_iterator &__iter,
Advances a
permutation_iteratorby a given number of elements.- Parameters:
__n – The number of elements to advance
__iter – The original
permutation_iterator
- Returns:
Equivalent to
permutation_iterator{iter, index + __n}
- inline friend constexpr permutation_iterator operator- (const permutation_iterator &__iter, difference_type __n) noexcept(//noexcept(__iter.__index_ - __n) &&::cuda::std::is_nothrow_copy_constructible_v< _Iter > &&::cuda::std::is_nothrow_copy_constructible_v< _Index >)
Decrements a
permutation_iteratorby a given number of elements.- Parameters:
__iter – The original
permutation_iterator__n – The number of elements to decrement
- Returns:
Equivalent to
permutation_iterator{iter, index - __n}
- inline friend constexpr difference_type operator-(
- const permutation_iterator &__lhs,
- const permutation_iterator &__rhs,
Returns the distance between two
permutation_iterators.- Returns:
Equivalent to
__lhs.index - __rhs.index
-
template<class _OtherIter, class _OtherOffset>
inline friend constexpr bool operator==( - const permutation_iterator &__lhs,
- const permutation_iterator<_OtherIter, _OtherOffset> &__rhs,
Compares two
permutation_iteratorfor equality by comparingindex.- Returns:
Equivalent to
__lhs.index == __rhs.index
-
template<class _OtherIter, class _OtherOffset>
inline friend constexpr bool operator<( - const permutation_iterator &__lhs,
- const permutation_iterator<_OtherIter, _OtherOffset> &__rhs,
Compares two
permutation_iteratorfor less than by comparingindex.- Returns:
Equivalent to
__lhs.index < __rhs.index
-
template<class _OtherIter, class _OtherOffset>
inline friend constexpr bool operator<=( - const permutation_iterator &__lhs,
- const permutation_iterator<_OtherIter, _OtherOffset> &__rhs,
Compares two
permutation_iteratorfor less equal by comparingindex.- Returns:
Equivalent to
__lhs.index <= __rhs.index
-
template<class _OtherIter, class _OtherOffset>
inline friend constexpr bool operator>( - const permutation_iterator &__lhs,
- const permutation_iterator<_OtherIter, _OtherOffset> &__rhs,
Compares two
permutation_iteratorfor greater than by comparingindex.- Returns:
Equivalent to
__lhs.index > __rhs.index
-
template<class _OtherIter, class _OtherOffset>
inline friend constexpr bool operator>=( - const permutation_iterator &__lhs,
- const permutation_iterator<_OtherIter, _OtherOffset> &__rhs,
Compares two
permutation_iteratorfor greater equal by comparingindex.- Returns:
Equivalent to
__lhs.index >= __rhs.index
-
template<class _Iter, class _Index>
inline constexpr permutation_iterator<_Iter, _Index> make_permutation_iterator(
) noexcept(::cuda::std::is_nothrow_copy_constructible_v<_Iter> && ::cuda::std::is_nothrow_copy_constructible_v<_Index>)# Creates an
permutation_iteratorfrom a base iterator and an iterator to an integral index.- Parameters:
__iter – The iterator
__index – The iterator to an integral index