|
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 |
|
template<Expression Source, class... Indexes>
class tannic::expression::Slice< Source, Indexes >
Expression template representing a tensor slice or subview.
- Template Parameters
-
Source | The expression type from which the slice is taken (e.g., a Tensor). |
Indexes | Variadic 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.
template<Expression Source, class... Indexes>
Returns the byte offset from the source tensor's data pointer.
The offset is calculated as:
- Initialized to 0
- 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
- 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.
template<Expression Source, class... Indexes>
template<typename T >
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
-
T | A type convertible to the underlying element type of the tensor. |
- Parameters
-
value | The 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[{0,2}][1] = 42;
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:105
void initialize(Environment environment=Host{}) const
Allocates the memory buffer for the tensor.
template<Expression Source, class... Indexes>
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:
- 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)
- 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.