ranges

Source code

Implementations of ranges that didn’t make C++20.

tl::enumerate_view/tl::views::enumerate

template <class V> class tl::enumerate_view : public std::ranges::view_interface<enumerate_view<V>>

Requires: std::ranges::input_range<V> && std::ranges::view<V>

A view which lets you iterate over the items in a range and their indices at the same time.

Examples:

std::vector<int> v;
for (auto&& [index, item] : tl::views::enumerate(v)) {
    //...
}
for (auto&& [index, item] : tl::enumerate_view(v)) {
    //...
}
for (auto&& [index, item] : v | tl::views::enumerate) {
    //...
}

Member Types

template<bool Const>
class sentinel

Member Types

private using Base = std::conditional_t<Const, const V, V>

Special Members

sentinel() = default
explicit constexpr sentinel(std::ranges::sentinel_t<Base> end)
constexpr sentinel(sentinel<!Const> other)

Requires: Const && std::convertible_to<std::ranges::sentinel_t<V>, std::ranges::sentinel_t<Base>>

Const-converting constructor.

Member Functions

constexpr std::ranges::sentinel_t<Base> base() const

Retrieve the base sentinel.

Friend Functions

friend constexpr bool operator==(const iterator<Const> &x, const sentinel &y)
friend constexpr std::ranges::range_difference_t<Base> operator-(const iterator<Const> &x, const sentinel &y)
friend constexpr std::ranges::range_difference_t<Base> operator-(const sentinel &x, const iterator<Const> &y)

Requires: std::sized_sentinel_for<std::ranges::sentinel_t<Base>, std::ranges::iterator_t<Base>>

template<bool Const>
class iterator

Member Types

private using Base = std::conditional_t<Const, const V, V>
private count_type = std::conditional_t<std::ranges::sized_range<V>, std::ranges::range_size_t<V>, std::make_unsigned_t<std::ranges::range_difference_t<V>>>;
using iterator_category = typename std::iterator_traits<std::ranges::iterator_t<Base>>::iterator_category
reference = std::pair<count_type, std::ranges::range_reference_t<Base>>;
using value_type = std::pair<count_type, std::ranges::range_value_t<Base>>
using difference_type = std::ranges::range_difference_t<Base>

Special Members

iterator() = default

Construct an iterator not tied to a base iterator.

explicit constexpr iterator(std::ranges::iterator_t<Base> current, difference_type pos)

Construct an iterator wrapping the given base iterator at the given index.

constexpr iterator(iterator<!Const> i)

Requires: Const && std::convertible_to<std::ranges::iterator_t<V>, std::ranges::iterator_t<Base>>

Const-converting constructor.

Member Functions

constexpr std::ranges::iterator_t<Base> base() const &

Requires: std::copyable<std::ranges::iterator_t<Base>>

Retrieve the base iterator.

constexpr std::ranges::iterator_t<Base> base() &&

Retrieve the base iterator.

constexpr reference operator*() const

Retrieve a pair of the current index and element.

constexpr reference operator[](difference_type x) const

Requires: std::ranges::random_access_range<Base>

Retrieve the pair of the index and element at an offset of x.

constexpr iterator &operator++()
constexpr void operator++(int)

Requires: !std::ranges::forward_range<Base>

iterator operator++(int)

Requires: std::ranges::forward_range<Base>

constexpr iterator &operator--()
constexpr iterator operator--(int)

Requires: std::ranges::bidirectional_range<Base>

constexpr iterator &operator+=(difference_type x)
constexpr iterator &operator-=(difference_type x)

Requires: std::ranges::random_access_range<Base>

Friend Functions

friend constexpr bool operator==(const iterator &x, const iterator &y)

Requires: std::equality_comparable<std::ranges::iterator_t<Base>>

friend constexpr bool operator<(const iterator &x, const iterator &y)
friend constexpr bool operator>(const iterator &x, const iterator &y)
friend constexpr bool operator<=(const iterator &x, const iterator &y)
friend constexpr bool operator>=(const iterator &x, const iterator &y)

Requires: std::ranges::random_access_range<Base>

friend constexpr auto operator<=>(const iterator& x, const iterator& y)

Requires: std::ranges::random_access_range<Base> && std::three_way_comparable<std::ranges::iterator_t<Base>>

friend constexpr iterator operator+(const iterator &x, difference_type y)
friend constexpr iterator operator+(difference_type x, const iterator &y)
friend constexpr iterator operator-(const iterator &x, difference_type y)
friend constexpr iterator operator-(difference_type x, const iterator &y)

Requires: std::ranges::random_access_range<Base>

Special Members

enumerate_view() = default
enumerate_view(V base)

Member Functions

constexpr iterator<false> begin()

Requires: !simple_view<V>

constexpr iterator<true> begin() const

Requires: simple_view<V>

constexpr sentinel<false> end()
constexpr iterator<false> end()

Requires: std::ranges::common_range<V>&& std::ranges::sized_range<V>

constexpr sentinel<true> end() const
constexpr iterator<true> end() const

Requires: std::ranges::common_range<V>&& std::ranges::sized_range<V>

constexpr std::ranges::size_t<V> size()

Requires: std::ranges::sized_range<V>

constexpr std::ranges::size_t<const V> size()

Requires: std::ranges::sized_range<const V>

constexpr V base() const &

Requires: std::copy_constructible<V>

constexpr V base() &&
class tl::views::detail::enumerate_fn
template<class V>
constexpr tl::enumerate_view<deduced> operator()(V &&v) const
template<class V>
friend constexpr auto operator|(V &&v, enumerate_fn)

Requires: std::ranges::viewable_range<V>

Create a tl::enumerate_view from the given range.

constexpr inline tl::views::detail::enumerate_fn enumerate

Instance of tl::views::detail::enumerate_fn to allow piping, e.g.:

for (auto&& [index, item] : v | tl::views::enumerate) {
    //...
}