MostlyHarmless 0.0.1
 
Loading...
Searching...
No Matches
marvin::containers::StrideView< T, Stride > Class Template Referencefinal

Non owning view into an array-like, with a configurable step size. More...

#include <marvin_StrideView.h>

Classes

class  const_iterator
 Custom const_iterator for a StrideView. More...
 
class  iterator
 Custom iterator for StrideView. More...
 

Public Member Functions

 StrideView (T *data, size_t size)
 
template<std::input_or_output_iterator Iter>
requires std::is_same_v<typename Iter::value_type, T>
 StrideView (Iter begin, Iter end)
 
 StrideView (std::span< T > arrayLike)
 
size_t size () const noexcept
 
T & operator[] (size_t idx)
 
const T & operator[] (size_t idx) const
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator cbegin () const
 
iterator end ()
 
const_iterator end () const
 
const_iterator cend () const
 
std::span< T > underlying ()
 

Detailed Description

template<typename T, size_t Stride>
requires (Stride > 0)
class marvin::containers::StrideView< T, Stride >

Non owning view into an array-like, with a configurable step size.

Essentially exists because std::ranges::views isn't implemented (or at least isn't widespread) on Apple Clang at the time of writing. A Stride of 2 would mean iterating over the StrideView would return every second value. Stride cannot == 0.
Does not require Stride to be even. Usage Example:

std::vector<int> vec{ 0, 1, 2, 3, 4, 5, 6, 7 };
marvin::utils::StrideView<int, 2> evenView{ vec };
for(const auto& el : evenView) {
std::cout << el << "\n";
}
=> [0, 2, 4, 6]
auto* offsetPtr = vec.data() + 1;
const auto size = vec.size() - 1;
for(const auto& el : oddView) {
std::cout << el << "\n";
}
=> [1, 3, 5, 7]
Non owning view into an array-like, with a configurable step size.
Definition marvin_StrideView.h:48
size_t size() const noexcept
Definition marvin_StrideView.h:516

Constructor & Destructor Documentation

◆ StrideView() [1/3]

template<typename T, size_t Stride>
marvin::containers::StrideView< T, Stride >::StrideView ( T * data,
size_t size )
inline

Constructs a StrideView with a T* and size.

Parameters
dataA T* to the data to wrap.
sizeThe number of elements to wrap.

◆ StrideView() [2/3]

template<typename T, size_t Stride>
template<std::input_or_output_iterator Iter>
requires std::is_same_v<typename Iter::value_type, T>
marvin::containers::StrideView< T, Stride >::StrideView ( Iter begin,
Iter end )
inline

Constructs a StrideView from a start and end iterator. The passed iterators must satisfy std::input_or_output_iterator, and must have a ::value_type typedef that is the same as T.

Parameters
beginThe start iterator.
endThe sentinel iterator.

◆ StrideView() [3/3]

template<typename T, size_t Stride>
marvin::containers::StrideView< T, Stride >::StrideView ( std::span< T > arrayLike)
inlineexplicit

Constructs a StrideView with any array-like that can bind to std::span.

Parameters
arrayLikeA span to bind to.

Member Function Documentation

◆ begin() [1/2]

template<typename T, size_t Stride>
iterator marvin::containers::StrideView< T, Stride >::begin ( )
inline

Creates an iterator to the first element in the view.

Returns
An iterator to the beginning of the view.

◆ begin() [2/2]

template<typename T, size_t Stride>
const_iterator marvin::containers::StrideView< T, Stride >::begin ( ) const
inline

Creates a const iterator to the first element in the view.

Returns
An const iterator to the beginning of the view.

◆ cbegin()

template<typename T, size_t Stride>
const_iterator marvin::containers::StrideView< T, Stride >::cbegin ( ) const
inline

Creates a const iterator to the first element in the view.

Returns
An const iterator to the beginning of the view.

◆ cend()

template<typename T, size_t Stride>
const_iterator marvin::containers::StrideView< T, Stride >::cend ( ) const
inline

Creates a const iterator to the last element in the view.

Returns
A const iterator to the end of the view.

◆ end() [1/2]

template<typename T, size_t Stride>
iterator marvin::containers::StrideView< T, Stride >::end ( )
inline

Creates an iterator to the last element in the view.

Returns
An iterator to the end of the view.

◆ end() [2/2]

template<typename T, size_t Stride>
const_iterator marvin::containers::StrideView< T, Stride >::end ( ) const
inline

Creates a const iterator to the last element in the view.

Returns
A const iterator to the end of the view.

◆ operator[]() [1/2]

template<typename T, size_t Stride>
T & marvin::containers::StrideView< T, Stride >::operator[] ( size_t idx)
inline

Subscript operator. The index passed here is with respect to stride - ie if the wrapped array is [0, 1, 2, 3] and Stride == 2, then operator[](1) will return 2.

Parameters
idxThe index to retrieve.
Returns
A reference to the element at idx.

◆ operator[]() [2/2]

template<typename T, size_t Stride>
const T & marvin::containers::StrideView< T, Stride >::operator[] ( size_t idx) const
inline

Subscript operator. The index passed here is with respect to stride - ie if the wrapped array is [0, 1, 2, 3] and Stride == 2, then operator[](1) will return 2.

Parameters
idxThe index to retrieve.
Returns
A const reference to the element at idx.

◆ size()

template<typename T, size_t Stride>
size_t marvin::containers::StrideView< T, Stride >::size ( ) const
inlinenodiscardnoexcept

Retrieves the number of elements in the view (with respect to Stride).

Returns
The number of elements in the view.

◆ underlying()

template<typename T, size_t Stride>
std::span< T > marvin::containers::StrideView< T, Stride >::underlying ( )
inlinenodiscard

Retrieves the underlying data for the current view.

Returns
The wrapped data.

The documentation for this class was generated from the following file: