Slice Ops
Version: v0.2.0
Status: Complete
Header: include/stratax/core/ops/Slice.hpp
Overview
ops/Slice.hpp implements slicing for vector, matrix, and tensor containers using stratax::core::Slice ranges.
All overloads return owning containers that copy selected elements from the source.
Responsibilities
The slice ops module is responsible for:
- Normalizing signed slice ranges against container extents
- Applying strided selection for 1D, 2D, and ND containers
- Building result shapes and copying selected elements
The slice ops module is not responsible for:
- Returning non-owning views
- Defining Slice semantics itself
- Broadcasting or advanced gather indexing
Relationships
slice(...) overloads
├── detail::normalize_slice(...)
├── core::Slice metadata
├── validation::require_rank(...) for tensor rank check
└── validation::checked_* for tensor offset arithmetic
Depends on:
Invariants
The following conditions are always true:
- Output containers are owning copies.
- Slice normalization is half-open and step-aware for both step directions.
- Out-of-range start/stop values are clamped, not rejected.
- Tensor slicing requires slice count equal to tensor rank.
- Empty output shapes return early without element copy loops.
Public Interface
Vector slicing
template<typename T>
Stores a rank-1 Stratax array in contiguous memory.
Represents a half-open strided range of indices.
Behavior
- Normalizes and clamps range against vec.size()
- Copies selected elements into a new vector
Throws
Complexity
- O(k), where k is output size
Matrix slicing
template<typename T>
slice(
Stores a rank-2 Stratax array in row-major order.
Behavior
- Normalizes/clamps row and column slices independently
- Copies rectangular strided region into a new matrix
Throws
Complexity
Tensor slicing
template<typename T, typename... Slices>
Stores an N-dimensional Stratax array in contiguous memory.
Behavior
- Requires all variadic arguments to be stratax::core::Slice
- Requires number of slices to match tensor rank
- Computes output shape from resolved slice sizes
- Copies result in flat order using source/result strides
Throws
Complexity
Tensor slicing (vector-based)
template<typename T>
slice(
const std::vector<stratax::core::Slice>& slices);
Behavior
- Accepts slices as a vector instead of variadic arguments
- Requires vector size to match tensor rank
- Computes output shape from resolved slice sizes
- Copies result in flat order using source/result strides
Usage
Tensor<int> t(Shape{2, 3, 4});
std::vector<Slice> slices{Slice{0, 2}, Slice{1, 3}, Slice{0, 4}};
auto result = slice(t, slices);
Throws
Complexity
Complexity Summary
| Operation | Complexity |
| normalize_slice | O(1) |
| Vector slice | O(k) |
| Matrix slice | O(r_out * c_out) |
| Tensor slice | O(n_out * rank) |
Examples
Design Notes
Normalization is permissive by design: start/stop values are clamped to legal bounds, which keeps slicing behavior predictable and pythonic.
Tensor slicing currently copies into new storage rather than producing a view to preserve ownership simplicity.
Future Improvements
- Add non-owning slice/view support
- Add richer slice composition helpers
- Evaluate vectorized copy kernels for dense slices
See Also