Stratax 0.4.0rc1
Loading...
Searching...
No Matches
Slicing.hpp File Reference

Positive-step non-owning slice views with clamped bounds. More...

#include <stratax/containers/Matrix.hpp>
#include <stratax/containers/Tensor.hpp>
#include <stratax/containers/Vector.hpp>
#include <stratax/exceptions/Exceptions.hpp>
#include <stratax/core/Shape.hpp>
#include <stratax/core/Slice.hpp>
#include <stratax/core/ArrayView.hpp>
#include <array>
#include <algorithm>
#include <cstddef>
#include <limits>
#include <vector>
#include <type_traits>
#include <utility>
#include <concepts>
Include dependency graph for Slicing.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  stratax::indexing::detail::ResolvedSlice
 Normalized first index, signed step, and selected element count. More...
 

Typedefs

template<typename Source >
using stratax::indexing::detail::view_element_t = std::conditional_t< std::is_const_v< std::remove_reference_t< Source > >, const typename std::remove_cvref_t< Source >::value_type, typename std::remove_cvref_t< Source >::value_type >
 Element type preserving the source array constness.
 

Functions

ResolvedSlice stratax::indexing::detail::normalize_slice (const stratax::core::Slice &slice, size_type extent)
 Normalizes explicit bounds against an extent without accessing storage.
 
template<typename Vector >
auto stratax::indexing::detail::make_vector_slice_view (Vector &vec, const stratax::core::Slice &slice)
 Constructs a one-axis view; see the public Vector slice overload.
 
template<typename Matrix >
auto stratax::indexing::detail::make_matrix_slice_view (Matrix &mat, const stratax::core::Slice &rows, const stratax::core::Slice &cols)
 Constructs a two-axis view; see the public Matrix slice overload.
 
template<typename T >
auto stratax::indexing::slice (stratax::container::Vector< T > &vec, const stratax::core::Slice &slice)
 Returns a non-owning Vector slice view (mutable when T is non-const).
 
template<typename T >
auto stratax::indexing::slice (const stratax::container::Vector< T > &vec, const stratax::core::Slice &slice)
 Returns a non-owning Vector slice view (read-only).
 
template<typename T >
auto stratax::indexing::slice (stratax::container::Matrix< T > &mat, const stratax::core::Slice &rows, const stratax::core::Slice &cols)
 Returns a non-owning Matrix slice view (mutable when T is non-const).
 
template<typename T >
auto stratax::indexing::slice (const stratax::container::Matrix< T > &mat, const stratax::core::Slice &rows, const stratax::core::Slice &cols)
 Returns a non-owning Matrix slice view (read-only).
 
template<typename Tensor , typename... Slices>
requires ( std::same_as< std::remove_cvref_t<Slices>, stratax::core::Slice > && ... )
auto stratax::indexing::detail::make_tensor_slice_view (Tensor &tensor, Slices... slices)
 Constructs a view with one slice per axis; see the public Tensor slice overloads.
 
template<typename Tensor >
auto stratax::indexing::detail::make_tensor_slice_view (Tensor &tensor, const std::vector< stratax::core::Slice > &slices)
 Constructs a view with one slice per axis; see the public Tensor slice overloads.
 
template<typename T , typename... Slices>
requires ( std::same_as< std::remove_cvref_t<Slices>, stratax::core::Slice > && ... )
auto stratax::indexing::slice (stratax::container::Tensor< T > &tensor, Slices... slices)
 Returns a non-owning Tensor slice view (mutable when T is non-const).
 
template<typename T , typename... Slices>
requires ( std::same_as< std::remove_cvref_t<Slices>, stratax::core::Slice > && ... )
auto stratax::indexing::slice (const stratax::container::Tensor< T > &tensor, Slices... slices)
 Returns a non-owning Tensor slice view (read-only).
 
template<typename T >
auto stratax::indexing::slice (stratax::container::Tensor< T > &tensor, const std::vector< stratax::core::Slice > &slices)
 Returns a non-owning Tensor slice view (mutable when T is non-const).
 
template<typename T >
auto stratax::indexing::slice (const stratax::container::Tensor< T > &tensor, const std::vector< stratax::core::Slice > &slices)
 Returns a non-owning Tensor slice view (read-only).
 
template<typename T >
stratax::core::ArrayView< T > stratax::indexing::slice (stratax::core::ArrayView< T > &view, const std::vector< stratax::core::Slice > &slices)
 Returns a non-owning ArrayView slice view (mutable when T is non-const).
 

Detailed Description

Positive-step non-owning slice views with clamped bounds.

Definition in file Slicing.hpp.

Typedef Documentation

◆ view_element_t

template<typename Source >
using stratax::indexing::detail::view_element_t = typedef std::conditional_t< std::is_const_v<std::remove_reference_t<Source> >, const typename std::remove_cvref_t<Source>::value_type, typename std::remove_cvref_t<Source>::value_type>

Element type preserving the source array constness.

Definition at line 50 of file Slicing.hpp.

Function Documentation

◆ make_matrix_slice_view()

template<typename Matrix >
auto stratax::indexing::detail::make_matrix_slice_view ( Matrix &  mat,
const stratax::core::Slice &  rows,
const stratax::core::Slice &  cols 
)

Constructs a two-axis view; see the public Matrix slice overload.

Definition at line 162 of file Slicing.hpp.

◆ make_tensor_slice_view() [1/2]

template<typename Tensor >
auto stratax::indexing::detail::make_tensor_slice_view ( Tensor &  tensor,
const std::vector< stratax::core::Slice > &  slices 
)

Constructs a view with one slice per axis; see the public Tensor slice overloads.

Definition at line 403 of file Slicing.hpp.

◆ make_tensor_slice_view() [2/2]

template<typename Tensor , typename... Slices>
requires ( std::same_as< std::remove_cvref_t<Slices>, stratax::core::Slice > && ... )
auto stratax::indexing::detail::make_tensor_slice_view ( Tensor &  tensor,
Slices...  slices 
)

Constructs a view with one slice per axis; see the public Tensor slice overloads.

Definition at line 313 of file Slicing.hpp.

◆ make_vector_slice_view()

template<typename Vector >
auto stratax::indexing::detail::make_vector_slice_view ( Vector &  vec,
const stratax::core::Slice &  slice 
)

Constructs a one-axis view; see the public Vector slice overload.

Definition at line 128 of file Slicing.hpp.

◆ normalize_slice()

ResolvedSlice stratax::indexing::detail::normalize_slice ( const stratax::core::Slice &  slice,
size_type  extent 
)
inline

Normalizes explicit bounds against an extent without accessing storage.

Parameters
sliceRaw range; a negative-step stop of -1 is the reverse sentinel.
extentAxis length, representable by difference_type.
Returns
Clamped start, unchanged step, and selected element count.
Exceptions
Exceptions::IndexErrorIf the extent exceeds PTRDIFF_MAX.
Note
Negative steps can be normalized, but public slice views reject them.
Complexity
O(1).

Definition at line 64 of file Slicing.hpp.

◆ slice() [1/9]

template<typename T >
auto stratax::indexing::slice ( const stratax::container::Matrix< T > &  mat,
const stratax::core::Slice &  rows,
const stratax::core::Slice &  cols 
)

Returns a non-owning Matrix slice view (read-only).

Parameters
matSource Matrix whose storage is aliased.
rowsPositive-step row range.
colsPositive-step column range.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(1).

Definition at line 295 of file Slicing.hpp.

◆ slice() [2/9]

template<typename T >
auto stratax::indexing::slice ( const stratax::container::Tensor< T > &  tensor,
const std::vector< stratax::core::Slice > &  slices 
)

Returns a non-owning Tensor slice view (read-only).

Parameters
tensorSource Tensor whose storage is aliased.
slicesOne explicit positive-step range per axis.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::RankErrorIf the number of slices differs from the source rank.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(tensor.rank()).

Definition at line 579 of file Slicing.hpp.

◆ slice() [3/9]

template<typename T , typename... Slices>
requires ( std::same_as< std::remove_cvref_t<Slices>, stratax::core::Slice > && ... )
auto stratax::indexing::slice ( const stratax::container::Tensor< T > &  tensor,
Slices...  slices 
)

Returns a non-owning Tensor slice view (read-only).

Parameters
tensorSource Tensor whose storage is aliased.
slicesOne explicit positive-step range per axis.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::RankErrorIf the number of slices differs from the source rank.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(tensor.rank()).

Definition at line 535 of file Slicing.hpp.

◆ slice() [4/9]

template<typename T >
auto stratax::indexing::slice ( const stratax::container::Vector< T > &  vec,
const stratax::core::Slice &  slice 
)

Returns a non-owning Vector slice view (read-only).

Parameters
vecSource Vector whose storage is aliased.
sliceExplicit positive-step range, normalized and clamped to the vector size.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(1).

Definition at line 248 of file Slicing.hpp.

◆ slice() [5/9]

template<typename T >
auto stratax::indexing::slice ( stratax::container::Matrix< T > &  mat,
const stratax::core::Slice &  rows,
const stratax::core::Slice &  cols 
)

Returns a non-owning Matrix slice view (mutable when T is non-const).

Parameters
matSource Matrix whose storage is aliased.
rowsPositive-step row range.
colsPositive-step column range.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(1).

Definition at line 271 of file Slicing.hpp.

◆ slice() [6/9]

template<typename T >
auto stratax::indexing::slice ( stratax::container::Tensor< T > &  tensor,
const std::vector< stratax::core::Slice > &  slices 
)

Returns a non-owning Tensor slice view (mutable when T is non-const).

Parameters
tensorSource Tensor whose storage is aliased.
slicesOne explicit positive-step range per axis.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::RankErrorIf the number of slices differs from the source rank.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(tensor.rank()).

Definition at line 556 of file Slicing.hpp.

◆ slice() [7/9]

template<typename T , typename... Slices>
requires ( std::same_as< std::remove_cvref_t<Slices>, stratax::core::Slice > && ... )
auto stratax::indexing::slice ( stratax::container::Tensor< T > &  tensor,
Slices...  slices 
)

Returns a non-owning Tensor slice view (mutable when T is non-const).

Parameters
tensorSource Tensor whose storage is aliased.
slicesOne explicit positive-step range per axis.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::RankErrorIf the number of slices differs from the source rank.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(tensor.rank()).

Definition at line 508 of file Slicing.hpp.

◆ slice() [8/9]

template<typename T >
auto stratax::indexing::slice ( stratax::container::Vector< T > &  vec,
const stratax::core::Slice &  slice 
)

Returns a non-owning Vector slice view (mutable when T is non-const).

Parameters
vecSource Vector whose storage is aliased.
sliceExplicit positive-step range, normalized and clamped to the vector size.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(1).

Definition at line 226 of file Slicing.hpp.

◆ slice() [9/9]

template<typename T >
stratax::core::ArrayView< T > stratax::indexing::slice ( stratax::core::ArrayView< T > &  view,
const std::vector< stratax::core::Slice > &  slices 
)

Returns a non-owning ArrayView slice view (mutable when T is non-const).

Parameters
viewSource ArrayView whose storage is aliased.
slicesOne explicit positive-step range per axis.
Returns
ArrayView with selected extents and source strides multiplied by each step. Every source axis is retained, including singleton and zero extents.
Precondition
The source allocation outlives the result and is not invalidated by assignment.
Exceptions
Exceptions::IndexErrorIf a step is negative or an extent exceeds PTRDIFF_MAX.
Exceptions::RankErrorIf the number of slices differs from the source rank.
Exceptions::DimensionErrorIf stride/offset arithmetic or metadata validation overflows.
std::bad_allocIf metadata allocation fails.
Note
Bounds are clamped; a slice may be empty. Empty results retain the source data pointer.
No elements are copied. C++ views do not keep the source owner alive.
Complexity
O(view.rank()).

Definition at line 603 of file Slicing.hpp.