Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::utils::SlideView< Iter > Class Template Reference

Adapter that flattens nested ranges into a single sequence. More...

#include <functional_adapters.hpp>

Public Types

using iterator = Iter
using const_iterator = Iter
using value_type = std::iter_value_t<Iter>
using reference = decltype(*std::declval<Iter>())
using size_type = size_t

Public Member Functions

constexpr SlideView (Iter begin, size_t size) noexcept(std::is_nothrow_copy_constructible_v< Iter >)
 Constructs a SlideView.
constexpr SlideView (const SlideView &) noexcept(std::is_nothrow_copy_constructible_v< Iter >)=default
constexpr SlideView (SlideView &&) noexcept(std::is_nothrow_move_constructible_v< Iter >)=default
constexpr ~SlideView () noexcept=default
constexpr SlideViewoperator= (const SlideView &) noexcept(std::is_nothrow_copy_assignable_v< Iter >)=default
constexpr SlideViewoperator= (SlideView &&) noexcept(std::is_nothrow_move_assignable_v< Iter >)=default
constexpr auto Collect () const -> std::vector< value_type >
 Collects the window elements into a vector.
template<typename Allocator>
requires (!std::derived_from<std::remove_pointer_t<Allocator>, std::pmr::memory_resource>)
constexpr auto CollectWith(Allocator allocator={}) const -> std::vector< value_type, Allocator > std constexpr auto CollectWith(std::pmr::memory_resource *resource) const -> std::pmr::vector< value_type > std auto CollectWith (std::nullptr_t) const -> std::pmr::vector< value_type >=delete
 Collects the window elements into a vector with a custom allocator.
constexpr reference operator[] (size_t index) const noexcept(noexcept(*std::declval< Iter >()) &&noexcept(++std::declval< Iter & >()))
 Accesses an element by index.
constexpr bool operator== (const SlideView &other) const
 Compares two SlideViews for equality.
template<std::ranges::sized_range R>
constexpr bool operator== (const R &range) const
 Compares SlideView with a vector for equality.
constexpr bool Empty () const noexcept
 Checks if the window is empty.
constexpr size_t Size () const noexcept
 Returns the size of the window.
constexpr Iter begin () const noexcept(std::is_nothrow_copy_constructible_v< Iter >)
 Returns an iterator to the beginning.
constexpr Iter end () const noexcept(std::is_nothrow_copy_constructible_v< Iter > &&noexcept(++std::declval< Iter & >()))
 Returns an iterator to the end.
template<typename Allocator>
requires (!std::derived_from<std::remove_pointer_t<Allocator>, std::pmr::memory_resource>)
constexpr auto CollectWith (Allocator allocator) const -> std::vector< value_type, Allocator > std

Detailed Description

template<IteratorLike Iter>
class helios::utils::SlideView< Iter >

Adapter that flattens nested ranges into a single sequence.

Iterates through an outer range of ranges, yielding elements from inner ranges.

Template Parameters
IterType of the outer iterator
// Join queries of `Position` and `Velocity` components
auto joined = query1.Join(query2);
joined.ForEach([](const Position& pos, const Velocity& vel) {
/
template <typename Iter>
class JoinAdapter final : public FunctionalAdapterBase<JoinAdapter<Iter>> {
public:
using iterator_category = details::adapter_iterator_category_t<Iter>;
using outer_value_type = std::iter_value_t<Iter>;
using inner_iterator_type = std::ranges::iterator_t<outer_value_type>;
using value_type = std::iter_value_t<inner_iterator_type>;
using difference_type = ptrdiff_t;
using pointer = value_type*;
/**
* @brief Constructs a join adapter.
* @param begin Iterator to the beginning of the outer range
* @param end Iterator to the end of the outer range
*/
constexpr JoinAdapter(Iter begin, Iter end) noexcept(
std::is_nothrow_move_constructible_v<Iter> &&
std::is_nothrow_copy_constructible_v<Iter> &&
std::is_nothrow_move_constructible_v<inner_iterator_type> &&
noexcept(std::declval<Iter&>() != std::declval<Iter&>()) &&
noexcept(*std::declval<inner_iterator_type&>()) &&
noexcept(AdvanceToValid()));
/**
* @brief Constructs a join adapter from a range.
* @tparam R The type of the range
* @param range The range to adapt
*/
template <ExternalRange R>
explicit constexpr JoinAdapter(R& range) noexcept(
noexcept(JoinAdapter(std::ranges::begin(range), std::ranges::end(range))))
: JoinAdapter(std::ranges::begin(range), std::ranges::end(range)) {}
/**
* @brief Constructs a join adapter from a range.
* @tparam R The type of the range
* @param range The range to adapt
*/
template <ExternalRange R>
explicit constexpr JoinAdapter(const R& range) noexcept(noexcept(
JoinAdapter(std::ranges::cbegin(range), std::ranges::cend(range))))
: JoinAdapter(std::ranges::cbegin(range), std::ranges::cend(range)) {}
constexpr JoinAdapter(const JoinAdapter&) noexcept(
std::is_nothrow_copy_constructible_v<Iter> &&
std::is_nothrow_copy_constructible_v<inner_iterator_type>) = default;
constexpr JoinAdapter(JoinAdapter&&) noexcept(
std::is_nothrow_move_constructible_v<Iter> &&
std::is_nothrow_move_constructible_v<inner_iterator_type>) = default;
constexpr ~JoinAdapter() noexcept(
std::is_nothrow_destructible_v<Iter> &&
std::is_nothrow_destructible_v<inner_iterator_type>) = default;
constexpr JoinAdapter& operator=(const JoinAdapter&) noexcept(
std::is_nothrow_copy_assignable_v<Iter> &&
std::is_nothrow_copy_assignable_v<inner_iterator_type>) = default;
constexpr JoinAdapter& operator=(JoinAdapter&&) noexcept(
std::is_nothrow_move_assignable_v<Iter> &&
std::is_nothrow_move_assignable_v<inner_iterator_type>) = default;
constexpr JoinAdapter& operator++() noexcept(
noexcept(std::declval<inner_iterator_type&>() !=
std::declval<inner_iterator_type&>()) &&
noexcept(++std::declval<inner_iterator_type&>()) &&
noexcept(AdvanceToValid()));
[[nodiscard]] constexpr JoinAdapter operator++(int) noexcept(
std::is_nothrow_copy_constructible_v<JoinAdapter> &&
noexcept(++std::declval<JoinAdapter&>()));
[[nodiscard]] constexpr reference operator*() const
noexcept(noexcept(*std::declval<const inner_iterator_type&>())) {
return *inner_current_;
}
pointer operator->() const = delete;
[[nodiscard]] constexpr bool operator==(const JoinAdapter& other) const
noexcept(noexcept(std::declval<const Iter&>() ==
std::declval<const Iter&>()) &&
noexcept(std::declval<const inner_iterator_type&>() ==
std::declval<const inner_iterator_type&>()));
[[nodiscard]] constexpr bool operator!=(const JoinAdapter& other) const
noexcept(noexcept(std::declval<JoinAdapter>() ==
std::declval<JoinAdapter>())) {
return !(*this == other);
}
[[nodiscard]] constexpr JoinAdapter begin() const noexcept(
std::is_nothrow_constructible_v<JoinAdapter, const Iter&, const Iter&>) {
return {outer_begin_, outer_end_};
}
[[nodiscard]] constexpr JoinAdapter end() const
noexcept(std::is_nothrow_copy_constructible_v<JoinAdapter> &&
std::is_nothrow_copy_assignable_v<Iter>);
private:
constexpr void AdvanceToValid() noexcept(
noexcept(std::declval<Iter&>() == std::declval<Iter&>()) &&
noexcept(std::declval<inner_iterator_type&>() ==
std::declval<inner_iterator_type&>()) &&
noexcept(++std::declval<Iter&>()) && noexcept(*std::declval<Iter&>()) &&
std::is_nothrow_move_assignable_v<inner_iterator_type>);
Iter outer_begin_;
Iter outer_current_;
Iter outer_end_;
inner_iterator_type inner_current_;
inner_iterator_type inner_end_;
};
template <ExternalRange R>
requires JoinAdapterRequirements<std::ranges::iterator_t<R>>
JoinAdapter(R&) -> JoinAdapter<std::ranges::iterator_t<R>>;
template <ExternalRange R>
requires JoinAdapterRequirements<std::ranges::iterator_t<const R>>
JoinAdapter(const R&) -> JoinAdapter<std::ranges::iterator_t<const R>>;
template <typename Iter>
requires JoinAdapterRequirements<Iter>
constexpr JoinAdapter<Iter>::JoinAdapter(Iter begin, Iter end) noexcept(
std::is_nothrow_move_constructible_v<Iter> &&
std::is_nothrow_copy_constructible_v<Iter> &&
std::is_nothrow_move_constructible_v<inner_iterator_type> &&
noexcept(std::declval<Iter&>() != std::declval<Iter&>()) &&
noexcept(*std::declval<inner_iterator_type&>()) &&
noexcept(AdvanceToValid()))
: outer_begin_(std::move(begin)),
outer_current_(outer_begin_),
outer_end_(std::move(end)) {
if (outer_current_ != outer_end_) {
auto& inner_range = *outer_current_;
inner_current_ = std::ranges::begin(inner_range);
inner_end_ = std::ranges::end(inner_range);
}
AdvanceToValid();
}
template <typename Iter>
requires JoinAdapterRequirements<Iter>
constexpr auto JoinAdapter<Iter>::operator++() noexcept(
noexcept(std::declval<inner_iterator_type&>() !=
std::declval<inner_iterator_type&>()) &&
noexcept(++std::declval<inner_iterator_type&>()) &&
noexcept(AdvanceToValid())) -> JoinAdapter& {
if (inner_current_ != inner_end_) {
++inner_current_;
}
AdvanceToValid();
return *this;
}
template <typename Iter>
requires JoinAdapterRequirements<Iter>
constexpr auto JoinAdapter<Iter>::operator++(int) noexcept(
std::is_nothrow_copy_constructible_v<JoinAdapter> &&
noexcept(++std::declval<JoinAdapter&>())) -> JoinAdapter {
auto temp = *this;
++(*this);
return temp;
}
template <typename Iter>
requires JoinAdapterRequirements<Iter>
constexpr bool JoinAdapter<Iter>::operator==(const JoinAdapter& other) const
noexcept(noexcept(std::declval<const Iter&>() ==
std::declval<const Iter&>()) &&
noexcept(std::declval<const inner_iterator_type&>() ==
std::declval<const inner_iterator_type&>())) {
if (outer_current_ != other.outer_current_) {
return false;
}
if (outer_current_ == outer_end_) {
return true;
}
return inner_current_ == other.inner_current_;
}
template <typename Iter>
requires JoinAdapterRequirements<Iter>
constexpr auto JoinAdapter<Iter>::end() const
noexcept(std::is_nothrow_copy_constructible_v<JoinAdapter> &&
std::is_nothrow_copy_assignable_v<Iter>) -> JoinAdapter {
auto result = *this;
result.outer_current_ = result.outer_end_;
return result;
}
template <typename Iter>
requires JoinAdapterRequirements<Iter>
constexpr void JoinAdapter<Iter>::AdvanceToValid() noexcept(
noexcept(std::declval<Iter&>() == std::declval<Iter&>()) &&
noexcept(std::declval<inner_iterator_type&>() ==
std::declval<inner_iterator_type&>()) &&
noexcept(++std::declval<Iter&>()) && noexcept(*std::declval<Iter&>()) &&
std::is_nothrow_move_assignable_v<inner_iterator_type>) {
while (outer_current_ != outer_end_) {
if (inner_current_ == inner_end_) {
++outer_current_;
if (outer_current_ == outer_end_) {
return;
}
auto& inner_range = *outer_current_;
inner_current_ = std::ranges::begin(inner_range);
inner_end_ = std::ranges::end(inner_range);
}
if (inner_current_ != inner_end_) {
return;
}
}
}
/**
* @brief A non-owning view over a sliding window of elements.
* @details Provides a lightweight, non-allocating view over a contiguous window
* of elements.
* @tparam Iter Type of the underlying iterator
*
* @code
* std::vector<int> data = {1, 2, 3, 4, 5};
* auto slide = SlideAdapterFromRange(data, 3);
* for (const auto& window : slide) {
* // window is a SlideView, iterate over it
* for (int val : window) {
* std::cout << val << " ";
* }
* std::cout << "\n";
* }
* // Output:
* // 1 2 3
* // 2 3 4
* // 3 4 5
*
CRTP base class providing common adapter operations.
constexpr Iter end() const noexcept(std::is_nothrow_copy_constructible_v< Iter > &&noexcept(++std::declval< Iter & >()))
Returns an iterator to the end.
std::iter_value_t< Iter > value_type
constexpr Iter begin() const noexcept(std::is_nothrow_copy_constructible_v< Iter >)
Returns an iterator to the beginning.
decltype(*std::declval< Iter >()) reference
Concept to validate JoinAdapter requirements.
typename adapter_iterator_traits< Iter >::iterator_category adapter_iterator_category_t
Gets the iterator category from Iter.
typename adapter_iterator_traits< Iter >::iterator_concept adapter_iterator_concept_t
Gets the iterator concept from Iter.
STL namespace.

Definition at line 2757 of file functional_adapters.hpp.

Member Typedef Documentation

◆ const_iterator

template<IteratorLike Iter>
using helios::utils::SlideView< Iter >::const_iterator = Iter

Definition at line 2760 of file functional_adapters.hpp.

◆ iterator

template<IteratorLike Iter>
using helios::utils::SlideView< Iter >::iterator = Iter

Definition at line 2759 of file functional_adapters.hpp.

◆ reference

template<IteratorLike Iter>
using helios::utils::SlideView< Iter >::reference = decltype(*std::declval<Iter>())

Definition at line 2762 of file functional_adapters.hpp.

◆ size_type

template<IteratorLike Iter>
using helios::utils::SlideView< Iter >::size_type = size_t

Definition at line 2763 of file functional_adapters.hpp.

◆ value_type

template<IteratorLike Iter>
using helios::utils::SlideView< Iter >::value_type = std::iter_value_t<Iter>

Definition at line 2761 of file functional_adapters.hpp.

Constructor & Destructor Documentation

◆ SlideView() [1/3]

template<IteratorLike Iter>
helios::utils::SlideView< Iter >::SlideView ( Iter begin,
size_t size )
inlineconstexprnoexcept

Constructs a SlideView.

Parameters
beginIterator to the beginning of the window
sizeSize of the window

Definition at line 2770 of file functional_adapters.hpp.

◆ SlideView() [2/3]

template<IteratorLike Iter>
helios::utils::SlideView< Iter >::SlideView ( const SlideView< Iter > & ) const
constexprdefaultnoexcept

◆ SlideView() [3/3]

template<IteratorLike Iter>
helios::utils::SlideView< Iter >::SlideView ( SlideView< Iter > && ) const
constexprdefaultnoexcept

◆ ~SlideView()

template<IteratorLike Iter>
helios::utils::SlideView< Iter >::~SlideView ( )
constexprdefaultnoexcept

Member Function Documentation

◆ begin()

template<IteratorLike Iter>
Iter helios::utils::SlideView< Iter >::begin ( ) const
inlinenodiscardconstexprnoexcept

Returns an iterator to the beginning.

Returns
Iterator to the first element

Definition at line 2865 of file functional_adapters.hpp.

◆ Collect()

template<IteratorLike Iter>
requires std::copy_constructible<value_type>
auto helios::utils::SlideView< Iter >::Collect ( ) const -> std::vector< value_type >
nodiscardconstexpr

Collects the window elements into a vector.

Use when you need ownership of the elements.

Returns
Vector containing copies of the window elements

Definition at line 2884 of file functional_adapters.hpp.

◆ CollectWith() [1/2]

template<IteratorLike Iter>
template<typename Allocator>
requires (!std::derived_from<std::remove_pointer_t<Allocator>, std::pmr::memory_resource>)
auto helios::utils::SlideView< Iter >::CollectWith ( Allocator allocator) const -> std::vector< value_type, Allocator >
constexpr

Definition at line 2900 of file functional_adapters.hpp.

◆ CollectWith() [2/2]

template<IteratorLike Iter>
template<typename Allocator>
requires (!std::derived_from<std::remove_pointer_t<Allocator>, std::pmr::memory_resource>)
auto CollectWith(Allocator allocator={}) const -> std::vector< value_type, Allocator > std constexpr auto CollectWith(std::pmr::memory_resource *resource) const -> std::pmr::vector< value_type > std auto helios::utils::SlideView< Iter >::CollectWith ( std::nullptr_t ) const -> std::pmr::vector< value_type >=delete
nodiscardconstexprdelete

Collects the window elements into a vector with a custom allocator.

Use when you need ownership of the elements with a specific allocator.

Template Parameters
AllocatorAllocator type
Parameters
allocatorAllocator to use for the vector
Returns
Vector containing copies of the window elements

Collects the window elements into a vector with a specific memory resource.

Use when you need ownership of the elements with a specific memory resource.

Parameters
resourceMemory resource to use for the vector
Returns
Vector containing copies of the window elements

◆ Empty()

template<IteratorLike Iter>
bool helios::utils::SlideView< Iter >::Empty ( ) const
inlinenodiscardconstexprnoexcept

Checks if the window is empty.

Returns
True if the window has no elements

Definition at line 2853 of file functional_adapters.hpp.

◆ end()

template<IteratorLike Iter>
Iter helios::utils::SlideView< Iter >::end ( ) const &
nodiscardconstexprnoexcept

Returns an iterator to the end.

Returns
Iterator past the last element

◆ operator=() [1/2]

template<IteratorLike Iter>
SlideView & helios::utils::SlideView< Iter >::operator= ( const SlideView< Iter > & )
constexprdefaultnoexcept

◆ operator=() [2/2]

template<IteratorLike Iter>
SlideView & helios::utils::SlideView< Iter >::operator= ( SlideView< Iter > && )
constexprdefaultnoexcept

◆ operator==() [1/2]

template<IteratorLike Iter>
template<std::ranges::sized_range R>
bool helios::utils::SlideView< Iter >::operator== ( const R & range) const
nodiscardconstexpr

Compares SlideView with a vector for equality.

Template Parameters
R
Parameters
vecVector to compare with
Returns
True if the view has the same elements as the vector

◆ operator==() [2/2]

template<IteratorLike Iter>
bool helios::utils::SlideView< Iter >::operator== ( const SlideView< Iter > & other) const
nodiscardconstexpr

Compares two SlideViews for equality.

Parameters
otherSlideView to compare with
Returns
True if both views have the same elements

◆ operator[]()

template<IteratorLike Iter>
reference helios::utils::SlideView< Iter >::operator[] ( size_t index) const &
nodiscardconstexprnoexcept

Accesses an element by index.

Warning
Index must be less than size
Parameters
indexIndex of the element
Returns
Reference to the element

◆ Size()

template<IteratorLike Iter>
size_t helios::utils::SlideView< Iter >::Size ( ) const
inlinenodiscardconstexprnoexcept

Returns the size of the window.

Returns
Number of elements in the window

Definition at line 2859 of file functional_adapters.hpp.