header_utils
Loading...
Searching...
No Matches
Ranges

Operations on C++20-style ranges. More...

Concepts

concept  ghassanpl::range_predicate
 Concept to check if a function is a predicate on the range elements (i.e. returns boolable).

Typedefs

template<typename RANGE>
using ghassanpl::range_value
 Helper template to get the value type of a type that decays to a range.
template<typename RANGE>
using ghassanpl::range_iterator
 Helper template to get the iterator type of a type that decays to a range.

Functions

constexpr bool ghassanpl::valid_index (random_access_range auto &range, std::integral auto index)
 Returns whether or not a given integer is a valid index to a random access range.
constexpr auto ghassanpl::modulo_index (size_t range_size, std::integral auto index)
 Returns index converted to a valid index into a range of range_size as if using modulo arithmetic.
constexpr auto ghassanpl::modulo_index (random_access_range auto &range, std::integral auto index)
 Returns an valid index into range, created from index as if range is circular.
constexpr decltype(auto) ghassanpl::at (random_access_range auto &range, std::integral auto index)
 Returns a reference to the value at index of range.
constexpr auto ghassanpl::at_ptr (random_access_range auto &range, std::integral auto index) -> decltype(std::to_address(std::ranges::begin(range)+index))
 Returns a pointer to the value at index of range if index is valid, else returns null.
constexpr decltype(auto) ghassanpl::modulo_at (random_access_range auto &range, std::integral auto index)
 Returns a reference to the value at modulo_index of range.
template<random_access_range RANGE, typename T>
requires requires (T value, RANGE range) { { *std::ranges::begin(range) == value } -> std::convertible_to<bool>; }
constexpr auto ghassanpl::index_of (RANGE const &range, T &&value) -> std::iter_difference_t< range_iterator< RANGE > >
 Find a value in range and returns an index to it.
template<random_access_range RANGE, typename T = range_value<RANGE>>
constexpr auto ghassanpl::at_or_default (RANGE &&range, std::integral auto index, T &&default_value)
 Return the element at index or default_value if not a valid index.
template<random_access_range RANGE, typename FUNC>
constexpr auto ghassanpl::find_index (RANGE const &range, FUNC &&pred) -> std::iter_difference_t< range_iterator< RANGE > >
 Find a value in range that matches pred, and returns an index to it.
template<std::ranges::range RANGE, typename FUNC>
constexpr auto ghassanpl::find_ptr (RANGE &range, FUNC &&pred)
 Find a value in range that matches pred, and returns a pointer to it, or null if none found.
template<random_access_range RANGE, typename FUNC, typename DEF_TYPE = range_value<RANGE>>
auto ghassanpl::find_if_or_default (RANGE &range, FUNC &&pred, DEF_TYPE &&default_value=DEF_TYPE{})
 Find a value in range that matches pred, and returns it, or default_value if none found.
constexpr auto ghassanpl::to_index (random_access_iterator auto iterator, random_access_range auto const &range)
 Turns an iterator to range to an index.
template<contiguous_range RANGE>
constexpr bool ghassanpl::valid_address (RANGE &&range, range_value< RANGE > *pointer)
 Returns whether or not pointer is a valid pointer to an element in the contiguous range.

C++23 Compatibility

constexpr __contains_fn ghassanpl::contains
 contains(range, el)
constexpr __contains_subrange_fn ghassanpl::contains_subrange
 contains_subrange(range, subrange)
template<class CONTAINER, std::ranges::input_range RANGE, class... TYPES>
constexpr CONTAINER ghassanpl::to (RANGE &&range, TYPES &&... args)
 to<container>();
template<template< class... > class CONTAINER, std::ranges::input_range RANGE, class... TYPES, class _Deduced = std::remove_pointer_t<decltype(detail::to_helper<CONTAINER, RANGE, TYPES...>())>>
constexpr _Deduced ghassanpl::to (RANGE &&_Range, TYPES &&... ARGS)

Span Operations

template<typename T, size_t N = std::dynamic_extent>
constexpr std::pair< span< T >, span< T > > ghassanpl::split_at (span< T, N > spn, size_t index)
template<typename T, size_t N = std::dynamic_extent>
constexpr std::array< span< T >, 3 > ghassanpl::split_at (span< T, N > spn, size_t index, size_t size)
template<typename T, size_t N = std::dynamic_extent>
constexpr std::pair< T *, T * > ghassanpl::as_range (span< T, N > spn)
constexpr auto ghassanpl::as_span (std::ranges::range auto &range)
template<typename TO, typename FROM, size_t N = std::dynamic_extent>
auto ghassanpl::span_cast (span< FROM, N > bytes) noexcept
 Casts a span of objects of type FROM to a span of objects of type TO.
template<typename T1, size_t N1, typename T2, size_t N2>
constexpr bool ghassanpl::are_adjacent (span< T1, N1 > s1, span< T2, N2 > s2)
template<typename T1, size_t N1, typename T2, size_t N2>
constexpr bool ghassanpl::are_overlapping (span< T1, N1 > s1, span< T2, N2 > s2)
template<typename T, size_t N1, typename U, size_t N2>
constexpr bool ghassanpl::ends_with (span< T, N1 > s1, span< U, N2 > s2)
template<typename T, size_t N1, typename U, size_t N2>
constexpr bool ghassanpl::starts_with (span< T, N1 > s1, span< U, N2 > s2)

Array Operations

template<typename T, std::size_t... Ns>
constexpr auto ghassanpl::join (std::array< T, Ns >... arrays)
template<typename T, size_t LL, typename... ARGS>
constexpr auto ghassanpl::join (std::array< T, LL > rhs, ARGS &&... args)

Detailed Description

Operations on C++20-style ranges.

Typedef Documentation

◆ range_iterator

template<typename RANGE>
using ghassanpl::range_iterator

Helper template to get the iterator type of a type that decays to a range.

Definition at line 32 of file ranges.h.

◆ range_value

template<typename RANGE>
using ghassanpl::range_value

Helper template to get the value type of a type that decays to a range.

Definition at line 28 of file ranges.h.

Function Documentation

◆ are_adjacent()

template<typename T1, size_t N1, typename T2, size_t N2>
bool ghassanpl::are_adjacent ( span< T1, N1 > s1,
span< T2, N2 > s2 )
nodiscardconstexpr

Definition at line 187 of file ranges.h.

◆ are_overlapping()

template<typename T1, size_t N1, typename T2, size_t N2>
bool ghassanpl::are_overlapping ( span< T1, N1 > s1,
span< T2, N2 > s2 )
nodiscardconstexpr

Definition at line 194 of file ranges.h.

◆ as_range()

template<typename T, size_t N = std::dynamic_extent>
std::pair< T *, T * > ghassanpl::as_range ( span< T, N > spn)
nodiscardconstexpr

Definition at line 168 of file ranges.h.

◆ as_span()

auto ghassanpl::as_span ( std::ranges::range auto & range)
nodiscardconstexpr

Definition at line 173 of file ranges.h.

◆ at()

decltype(auto) ghassanpl::at ( random_access_range auto & range,
std::integral auto index )
nodiscardconstexpr

Returns a reference to the value at index of range.

Definition at line 61 of file ranges.h.

◆ at_or_default()

template<random_access_range RANGE, typename T = range_value<RANGE>>
auto ghassanpl::at_or_default ( RANGE && range,
std::integral auto index,
T && default_value )
nodiscardconstexpr

Return the element at index or default_value if not a valid index.

Definition at line 97 of file ranges.h.

◆ at_ptr()

auto ghassanpl::at_ptr ( random_access_range auto & range,
std::integral auto index ) -> decltype(std::to_address(std::ranges::begin(range)+index))
nodiscardconstexpr

Returns a pointer to the value at index of range if index is valid, else returns null.

Definition at line 69 of file ranges.h.

◆ ends_with()

template<typename T, size_t N1, typename U, size_t N2>
bool ghassanpl::ends_with ( span< T, N1 > s1,
span< U, N2 > s2 )
nodiscardconstexpr

Definition at line 201 of file ranges.h.

◆ find_if_or_default()

template<random_access_range RANGE, typename FUNC, typename DEF_TYPE = range_value<RANGE>>
auto ghassanpl::find_if_or_default ( RANGE & range,
FUNC && pred,
DEF_TYPE && default_value = DEF_TYPE{} )
nodiscard

Find a value in range that matches pred, and returns it, or default_value if none found.

Definition at line 126 of file ranges.h.

◆ find_index()

template<random_access_range RANGE, typename FUNC>
auto ghassanpl::find_index ( RANGE const & range,
FUNC && pred ) -> std::iter_difference_t< range_iterator< RANGE > >
nodiscardconstexpr

Find a value in range that matches pred, and returns an index to it.

Definition at line 106 of file ranges.h.

◆ find_ptr()

template<std::ranges::range RANGE, typename FUNC>
auto ghassanpl::find_ptr ( RANGE & range,
FUNC && pred )
nodiscardconstexpr

Find a value in range that matches pred, and returns a pointer to it, or null if none found.

Definition at line 117 of file ranges.h.

◆ index_of()

template<random_access_range RANGE, typename T>
requires requires (T value, RANGE range) { { *std::ranges::begin(range) == value } -> std::convertible_to<bool>; }
auto ghassanpl::index_of ( RANGE const & range,
T && value ) -> std::iter_difference_t< range_iterator< RANGE > >
nodiscardconstexpr

Find a value in range and returns an index to it.

Definition at line 86 of file ranges.h.

◆ join() [1/2]

template<typename T, size_t LL, typename... ARGS>
auto ghassanpl::join ( std::array< T, LL > rhs,
ARGS &&... args )
nodiscardconstexpr

Definition at line 231 of file ranges.h.

◆ join() [2/2]

template<typename T, std::size_t... Ns>
auto ghassanpl::join ( std::array< T, Ns >... arrays)
nodiscardconstexpr

Definition at line 221 of file ranges.h.

◆ modulo_at()

decltype(auto) ghassanpl::modulo_at ( random_access_range auto & range,
std::integral auto index )
nodiscardconstexpr

Returns a reference to the value at modulo_index of range.

Precondition
range must not be empty

Definition at line 79 of file ranges.h.

◆ modulo_index() [1/2]

auto ghassanpl::modulo_index ( random_access_range auto & range,
std::integral auto index )
nodiscardconstexpr

Returns an valid index into range, created from index as if range is circular.

Precondition
range must not be empty

Definition at line 54 of file ranges.h.

◆ modulo_index() [2/2]

auto ghassanpl::modulo_index ( size_t range_size,
std::integral auto index )
nodiscardconstexpr

Returns index converted to a valid index into a range of range_size as if using modulo arithmetic.

Precondition
range_size > 0

Definition at line 46 of file ranges.h.

◆ span_cast()

template<typename TO, typename FROM, size_t N = std::dynamic_extent>
auto ghassanpl::span_cast ( span< FROM, N > bytes)
nodiscardnoexcept

Casts a span of objects of type FROM to a span of objects of type TO.

Does not perform any checks, specifically, no alignment or size checks are performed.

Definition at line 181 of file ranges.h.

◆ split_at() [1/2]

template<typename T, size_t N = std::dynamic_extent>
std::pair< span< T >, span< T > > ghassanpl::split_at ( span< T, N > spn,
size_t index )
nodiscardconstexpr

Definition at line 152 of file ranges.h.

◆ split_at() [2/2]

template<typename T, size_t N = std::dynamic_extent>
std::array< span< T >, 3 > ghassanpl::split_at ( span< T, N > spn,
size_t index,
size_t size )
nodiscardconstexpr

Definition at line 160 of file ranges.h.

◆ starts_with()

template<typename T, size_t N1, typename U, size_t N2>
bool ghassanpl::starts_with ( span< T, N1 > s1,
span< U, N2 > s2 )
nodiscardconstexpr

Definition at line 209 of file ranges.h.

◆ to() [1/2]

template<template< class... > class CONTAINER, std::ranges::input_range RANGE, class... TYPES, class _Deduced = std::remove_pointer_t<decltype(detail::to_helper<CONTAINER, RANGE, TYPES...>())>>
_Deduced ghassanpl::to ( RANGE && _Range,
TYPES &&... ARGS )
nodiscardconstexpr

Definition at line 416 of file ranges.h.

◆ to() [2/2]

template<class CONTAINER, std::ranges::input_range RANGE, class... TYPES>
CONTAINER ghassanpl::to ( RANGE && range,
TYPES &&... args )
nodiscardconstexpr

to<container>();

Definition at line 386 of file ranges.h.

◆ to_index()

auto ghassanpl::to_index ( random_access_iterator auto iterator,
random_access_range auto const & range )
nodiscardconstexpr

Turns an iterator to range to an index.

Definition at line 135 of file ranges.h.

◆ valid_address()

template<contiguous_range RANGE>
bool ghassanpl::valid_address ( RANGE && range,
range_value< RANGE > * pointer )
nodiscardconstexpr

Returns whether or not pointer is a valid pointer to an element in the contiguous range.

Definition at line 142 of file ranges.h.

◆ valid_index()

bool ghassanpl::valid_index ( random_access_range auto & range,
std::integral auto index )
nodiscardconstexpr

Returns whether or not a given integer is a valid index to a random access range.

Definition at line 39 of file ranges.h.

Variable Documentation

◆ contains

__contains_fn ghassanpl::contains
inlineconstexpr

contains(range, el)

Definition at line 264 of file ranges.h.

◆ contains_subrange

__contains_subrange_fn ghassanpl::contains_subrange
inlineconstexpr

contains_subrange(range, subrange)

Definition at line 286 of file ranges.h.