Stratax 0.2.0
Loading...
Searching...
No Matches
Slice Ops

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>
slice(const stratax::container::Vector<T>& vec, const stratax::core::Slice& slice);
Stores a rank-1 Stratax array in contiguous memory.
Definition Vector.hpp:27
Represents a half-open strided range of indices.
Definition Slice.hpp:16

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(
const stratax::core::Slice& rows,
const stratax::core::Slice& cols);
Stores a rank-2 Stratax array in row-major order.
Definition Matrix.hpp:29

Behavior

  • Normalizes/clamps row and column slices independently
  • Copies rectangular strided region into a new matrix

Throws

Complexity

  • O(r_out * c_out)

Tensor slicing

template<typename T, typename... Slices>
slice(const stratax::container::Tensor<T>& tensor, Slices... slices);
Stores an N-dimensional Stratax array in contiguous memory.
Definition Tensor.hpp:31

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

  • O(n_out * rank)

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

  • O(n_out * rank)

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

const auto v2 = slice(v, stratax::core::Slice(0, 10, 2));
const auto m2 = slice(m, stratax::core::Slice(1, -1, 1), stratax::core::Slice(0, 5, 2));
const auto t2 = slice(t, stratax::core::Slice(0, 2), stratax::core::Slice(0, 3), stratax::core::Slice(0, 4));

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