counting_iterator#

template<class _Start, ::cuda::std::enable_if_t<::cuda::std::weakly_incrementable<_Start>, int> = 0, ::cuda::std::enable_if_t<::cuda::std::copyable<_Start>, int> = 0>
class counting_iterator : public __counting_iterator_category<_Start>#

A counting_iterator represents an iterator into a range of sequentially increasing values.

This iterator is useful for creating a range filled with a sequence without explicitly storing it in memory. Using counting_iterator saves memory capacity and bandwidth.

The following code snippet demonstrates how to create a counting_iterator whose value_type is int

#include <cuda/iterator>
...
// create iterators
cuda::counting_iterator first(10);
cuda::counting_iterator last = first + 3;

first[0]   // returns 10
first[1]   // returns 11
first[100] // returns 110

// sum of [first, last)
std::reduce(first, last);   // returns 33 (i.e. 10 + 11 + 12)

// initialize vector to [0,1,2,..]
cuda::counting_iterator iter(0);
std::vector<int> vec(500);
std::copy(iter, iter + vec.size(), vec.begin());
Template Parameters:

_Start – the value type of the counting_iterator.

Public Types

using iterator_concept = ::cuda::std::conditional_t<__advanceable<_Start>, ::cuda::std::random_access_iterator_tag, ::cuda::std::conditional_t<__decrementable<_Start>, ::cuda::std::bidirectional_iterator_tag, ::cuda::std::conditional_t<::cuda::std::incrementable<_Start>, ::cuda::std::forward_iterator_tag, ::cuda::std::input_iterator_tag>>>#
using value_type = _Start#
using difference_type = _IotaDiffT<_Start>#

Public Functions

template<class _Start2 = _Start, ::cuda::std::enable_if_t<::cuda::std::default_initializable<_Start2>, int> = 0>
inline constexpr counting_iterator(
) noexcept(::cuda::std::is_nothrow_default_constructible_v<_Start2>)#
inline explicit constexpr counting_iterator(
_Start __value,
) noexcept(::cuda::std::is_nothrow_move_constructible_v<_Start>)#

Creates a counting_iterator from an initial value.

Parameters:

__value – The value to store in the counting_iterator

inline constexpr _Start operator*(
) const noexcept(::cuda::std::is_nothrow_copy_constructible_v<_Start>)#

Returns the value currently stored in the counting_iterator.

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__advanceable<_Start2>, int> = 0>
inline constexpr _Start2 operator[](
difference_type __n,
) const noexcept(::cuda::std::is_nothrow_copy_constructible_v<_Start2> && noexcept(::cuda::std::declval<const _Start2&>() + __n))#

Returns the value currently stored in the counting_iterator advanced by a number of steps.

Parameters:

__n – The amount of elements to advance

inline constexpr counting_iterator &operator++(
) noexcept(noexcept(++::cuda::std::declval<_Start&>()))#

Increments the stored value.

inline constexpr auto operator++(
int,
) noexcept(noexcept(++::cuda::std::declval<_Start&>()) && ::cuda::std::is_nothrow_copy_constructible_v<_Start>)#

Increments the stored value.

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__decrementable<_Start2>, int> = 0>
inline constexpr counting_iterator &operator--(
) noexcept(noexcept(--::cuda::std::declval<_Start2&>()))#

Decrements the stored value.

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__decrementable<_Start2>, int> = 0>
inline constexpr counting_iterator operator--(
int,
) noexcept(noexcept(--::cuda::std::declval<_Start2&>()) && ::cuda::std::is_nothrow_copy_constructible_v<_Start>)#

Decrements the stored value.

inline constexpr counting_iterator &operator+=(
difference_type __n,
) noexcept(::cuda::std::__integer_like<_Start>)#

Increments the stored value by a given number of elements.

Parameters:

__n – The number of elements to increment

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__advanceable<_Start2>, int> = 0>
inline constexpr counting_iterator &operator-=(
difference_type __n,
) noexcept(::cuda::std::__integer_like<_Start2>)#

Decrements the stored value by a given number of elements.

Parameters:

__n – The amount of elements to decrement

Friends

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__advanceable<_Start2>, int> = 0>
inline friend constexpr counting_iterator operator+(
counting_iterator __iter,
difference_type __n,
) noexcept(::cuda::std::__integer_like<_Start2>)#

Creates a copy of a counting_iterator advanced by a given number of elements.

Parameters:
  • __iter – The counting_iterator to advance

  • __n – The amount of elements to advance

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__advanceable<_Start2>, int> = 0>
inline friend constexpr counting_iterator operator+(
difference_type __n,
counting_iterator __iter,
) noexcept(::cuda::std::__integer_like<_Start2>)#

Creates a copy of a counting_iterator advanced by a given number of elements.

Parameters:
  • __iter – The counting_iterator to advance

  • __n – The amount of elements to advance

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__advanceable<_Start2>, int> = 0>
inline friend constexpr counting_iterator operator-(
counting_iterator __iter,
difference_type __n,
) noexcept(::cuda::std::__integer_like<_Start2>)#

Creates a copy of a counting_iterator decremented by a given number of elements.

Parameters:
  • __iter – The counting_iterator to decrement

  • __n – The amount of elements to decrement

template<class _Start2 = _Start, ::cuda::std::enable_if_t<__advanceable<_Start2>, int> = 0>
inline friend constexpr difference_type operator-(
const counting_iterator &__x,
const counting_iterator &__y,
) noexcept(::cuda::std::__integer_like<_Start2>)#

Returns the distance between two counting_iterator.

Returns:

The difference between the stored values

template<class _Start2 = _Start, ::cuda::std::enable_if_t<::cuda::std::equality_comparable<_Start2>, int> = 0>
inline friend constexpr bool operator==(
const counting_iterator &__x,
const counting_iterator &__y,
) noexcept(noexcept(::cuda::std::declval<const _Start2&>() == ::cuda::std::declval<const _Start2&>()))#

Compares two counting_iterator for equality.

Returns:

True if the stored values compare equal

template<class _Start2 = _Start, ::cuda::std::enable_if_t<::cuda::std::totally_ordered<_Start2>, int> = 0>
inline friend constexpr bool operator<(
const counting_iterator &__x,
const counting_iterator &__y,
) noexcept(noexcept(::cuda::std::declval<const _Start2&>() < ::cuda::std::declval<const _Start2&>()))#

Compares two counting_iterator for less than.

Returns:

True if stored values compare less than

template<class _Start2 = _Start, ::cuda::std::enable_if_t<::cuda::std::totally_ordered<_Start2>, int> = 0>
inline friend constexpr bool operator>(
const counting_iterator &__x,
const counting_iterator &__y,
) noexcept(noexcept(::cuda::std::declval<const _Start2&>() < ::cuda::std::declval<const _Start2&>()))#

Compares two counting_iterator for greater than.

Returns:

True if stored values compare greater than

template<class _Start2 = _Start, ::cuda::std::enable_if_t<::cuda::std::totally_ordered<_Start2>, int> = 0>
inline friend constexpr bool operator<=(
const counting_iterator &__x,
const counting_iterator &__y,
) noexcept(noexcept(::cuda::std::declval<const _Start2&>() < ::cuda::std::declval<const _Start2&>()))#

Compares two counting_iterator for less equal.

Returns:

True if stored values compare less equal

template<class _Start2 = _Start, ::cuda::std::enable_if_t<::cuda::std::totally_ordered<_Start2>, int> = 0>
inline friend constexpr bool operator>=(
const counting_iterator &__x,
const counting_iterator &__y,
) noexcept(noexcept(::cuda::std::declval<const _Start2&>() < ::cuda::std::declval<const _Start2&>()))#

Compares two counting_iterator for greater equal.

Returns:

True if stored values compare greater equal

template<class _Start>
inline constexpr auto make_counting_iterator(
_Start __start,
)#

Creates a counting_iterator from an integer-like _Start.

Parameters:

__start – The integer-like _Start representing the initial count