Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
tannic::expression::Slice< Source, Indexes > Class Template Reference

Expression template representing a tensor slice or subview. More...

#include <slices.hpp>

Public Member Functions

constexpr Slice (typename Trait< Source >::Reference source, std::tuple< Indexes... > indexes)
 Create a slice from a source expression and an index tuple.
 
template<Integral Index>
constexpr auto operator[] (Index index) const
 Index into the slice with an integer.
 
constexpr auto operator[] (indexing::Range range) const
 Index into the slice with a range.
 
template<typename T >
void operator= (T value)
 Assigns a scalar value to all elements in the slice.
 
template<typename T >
bool operator== (T value) const
 Compares a scalar value to the element in a rank-0 slice.
 
constexpr auto dtype () const
 Returns the runtime data type of elements in this slice.
 
constexpr auto rank () const
 Returns the number of dimensions in this slice.
 
constexpr Shape const & shape () const
 Returns the shape (size in each dimension) of this slice.
 
constexpr Strides const & strides () const
 Returns the memory strides for this slice.
 
std::ptrdiff_t offset () const
 Returns the byte offset from the source tensor's data pointer.
 
std::byte * bytes ()
 
std::byte const * bytes () const
 
Tensor forward () const
 
void assign (std::byte const *value, std::ptrdiff_t offset)
 
void assign (bool const *, std::ptrdiff_t)
 
bool compare (std::byte const *value, std::ptrdiff_t offset) const
 

Detailed Description

template<Expression Source, class... Indexes>
class tannic::expression::Slice< Source, Indexes >

Expression template representing a tensor slice or subview.

Template Parameters
SourceThe expression type from which the slice is taken (e.g., a Tensor).
IndexesVariadic list of index types (integer or indexing::Range).

A Slice stores:

  • The source expression reference.
  • A tuple of index specifications.
  • The computed shape, strides, and offset for the slice.

It supports further slicing through operator[], element assignment, and scalar comparison for rank-0 slices.

This class does not own the underlying data; it merely refers to a subset of it.

Constructor & Destructor Documentation

◆ Slice()

template<Expression Source, class... Indexes>
constexpr tannic::expression::Slice< Source, Indexes >::Slice ( typename Trait< Source >::Reference  source,
std::tuple< Indexes... >  indexes 
)
inlineconstexpr

Create a slice from a source expression and an index tuple.

Parameters
sourceReference to the source expression.
indexesTuple containing integer indices or ranges.

The constructor computes the resulting shape, strides, and byte offset based on the given indexes.

Member Function Documentation

◆ assign() [1/2]

template<Expression Source, class... Indexes>
void tannic::expression::Slice< Source, Indexes >::assign ( bool const *  value,
std::ptrdiff_t  offset 
)

◆ assign() [2/2]

template<Expression Source, class... Indexes>
void tannic::expression::Slice< Source, Indexes >::assign ( std::byte const *  value,
std::ptrdiff_t  offset 
)

◆ bytes() [1/2]

template<Expression Source, class... Indexes>
std::byte * tannic::expression::Slice< Source, Indexes >::bytes ( )
inline

◆ bytes() [2/2]

template<Expression Source, class... Indexes>
std::byte const * tannic::expression::Slice< Source, Indexes >::bytes ( ) const
inline

◆ compare()

template<Expression Source, class... Indexes>
bool tannic::expression::Slice< Source, Indexes >::compare ( std::byte const *  value,
std::ptrdiff_t  offset 
) const

◆ dtype()

template<Expression Source, class... Indexes>
constexpr auto tannic::expression::Slice< Source, Indexes >::dtype ( ) const
inlineconstexpr

Returns the runtime data type of elements in this slice.

The data type (dtype_) is determined from the source expression or tensor at construction and remains constant for the lifetime of the slice.

Returns
The data type of the elements as a type enum.
Note
This is a runtime property, not a compile-time type.

◆ forward()

template<Expression Source, class... Indexes>
Tensor tannic::expression::Slice< Source, Indexes >::forward

◆ offset()

template<Expression Source, class... Indexes>
std::ptrdiff_t tannic::expression::Slice< Source, Indexes >::offset ( ) const
inline

Returns the byte offset from the source tensor's data pointer.

The offset is calculated as:

  1. Initialized to 0
  2. For each index:
    • If it's a range:
      • Add range.start * source_stride * element_size to offset
    • If it's an integer index:
      • Add index * source_stride * element_size to offset
  3. The offset is relative to the source tensor's data pointer

This represents the starting memory position of the slice within the original tensor's storage.

Returns
The byte offset from the source tensor's data pointer.

◆ operator=()

template<Expression Source, class... Indexes>
template<typename T >
void tannic::expression::Slice< Source, Indexes >::operator= ( value)

Assigns a scalar value to all elements in the slice.

This method writes the given value into every element referenced by the slice. If the slice has rank 0 (scalar), it directly assigns the value to the single element. If the slice has higher rank, it iterates over all elements in the slice and assigns the value.

Template Parameters
TA type convertible to the underlying element type of the tensor.
Parameters
valueThe scalar value to assign.
Note
The value is cast to the slice's runtime dtype_ before assignment.
This operation modifies the underlying tensor/expression in-place.
Tensor tensor({3, 3}); tensor.initialize();
tensor[{0,2}][1] = 42; // assign 42 to elements in rows 0..1, column 1
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:105
void initialize(Environment environment=Host{}) const
Allocates the memory buffer for the tensor.

◆ operator==()

template<Expression Source, class... Indexes>
template<typename T >
bool tannic::expression::Slice< Source, Indexes >::operator== ( value) const

Compares a scalar value to the element in a rank-0 slice.

This method checks if the scalar stored in the slice equals the given value. It only works for rank-0 (scalar) slices; otherwise, an assertion will fail.

Template Parameters
TA type convertible to the underlying element type of the tensor.
Parameters
valueThe scalar value to compare against.
Returns
true if the value matches the element in the slice, false otherwise.
Warning
Will assert if called on a slice with rank() > 0.
if (tensor[1][2] == 5) {
// Element at (1, 2) is equal to 5
}
else if(tensor[1,1] == 3) {
// Element at (1,1) is equal to 3
}

◆ operator[]() [1/2]

template<Expression Source, class... Indexes>
template<Integral Index>
constexpr auto tannic::expression::Slice< Source, Indexes >::operator[] ( Index  index) const
inlineconstexpr

Index into the slice with an integer.

Parameters
indexThe index into the current slice's first dimension.
Returns
A new Slice object with the updated index tuple.

◆ operator[]() [2/2]

template<Expression Source, class... Indexes>
constexpr auto tannic::expression::Slice< Source, Indexes >::operator[] ( indexing::Range  range) const
inlineconstexpr

Index into the slice with a range.

Parameters
rangeThe range to select from the current slice's first dimension.
Returns
A new Slice object with the updated index tuple.

◆ rank()

template<Expression Source, class... Indexes>
constexpr auto tannic::expression::Slice< Source, Indexes >::rank ( ) const
inlineconstexpr

Returns the number of dimensions in this slice.

The rank is computed after applying all indexing operations (ranges and/or integer indices) to the source tensor. Integral index removes the corresponding dimension from the rank while range based slices may preserve it.

Returns
The rank (number of dimensions) of the slice.
Note
Scalar slices have rank 0.

◆ shape()

template<Expression Source, class... Indexes>
constexpr Shape const & tannic::expression::Slice< Source, Indexes >::shape ( ) const
inlineconstexpr

Returns the shape (size in each dimension) of this slice.

The shape is derived from the source tensor after applying the given indexes according to these rules:

  1. For each index in the index tuple:
    • If it's a range (indexing::Range):
    • If it's an integer index:
      • The dimension is eliminated (not included in output shape)
  2. Any remaining dimensions from the source tensor (after processing all indices) are preserved with their original sizes

Example:

Tensor X({4,3,2}); // shape [4,3,2]
auto slice = X[range(1,3)][1];
// Resulting shape: [2, 2] (from range(1,3) and preserving last dimension)
indexing::Range range
Convenience alias for tannic::indexing::Range.
Definition: indexing.hpp:139
Returns
A constant reference to the Shape object describing this slice.

◆ strides()

template<Expression Source, class... Indexes>
constexpr Strides const & tannic::expression::Slice< Source, Indexes >::strides ( ) const
inlineconstexpr

Returns the memory strides for this slice.

Strides represent the number of elements to skip in each dimension to move to the next element along that axis. They are computed as:

  1. For each index in the tuple:
    • If it's a range:
      • The stride for this dimension is copied from the source tensor
    • If it's an integer index:
      • No stride is recorded (dimension is eliminated)
  2. Remaining dimensions keep their original strides

The strides are always computed in elements (not bytes) and are relative to the original tensor's memory layout.

Returns
A constant reference to the Strides object for this slice.

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