Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
tannic::indexing Namespace Reference

Classes

struct  Range
 Represents a half-open interval [start, stop) for slicing. More...
 

Functions

template<Integral Index, Integral Size>
constexpr Index normalize (Index index, Size bound)
 Normalize a possibly-negative index into the valid range [0, bound).
 
template<Integral Size>
constexpr Range normalize (Range range, Size size)
 Normalize a slicing range into valid [start, stop) indices.
 

Function Documentation

◆ normalize() [1/2]

template<Integral Index, Integral Size>
constexpr Index tannic::indexing::normalize ( Index  index,
Size  bound 
)
inlineconstexpr

Normalize a possibly-negative index into the valid range [0, bound).

Template Parameters
IndexIntegral type of the index
SizeIntegral type of the bound
Parameters
indexIndex to normalize (may be negative, counting from the end)
boundUpper bound (dimension size)
Returns
Normalized non-negative index
Exceptions
Assertionfailure if the normalized index is out of bounds

This function is used internally by the tensor library when processing indexing operations. It allows Python-like negative indices, where -1 refers to the last element of a dimension, -2 the second-to-last, etc.

Example:

int i = tannic::indexing::normalize(-1, 5); // → 4
int j = tannic::indexing::normalize(2, 5); // → 2
constexpr Index normalize(Index index, Size bound)
Normalize a possibly-negative index into the valid range [0, bound).
Definition: indexing.hpp:87
Note
End-users typically don’t call this directly; it underpins higher-level indexing (tensor[i]) and slicing operations.

◆ normalize() [2/2]

template<Integral Size>
constexpr Range tannic::indexing::normalize ( Range  range,
Size  size 
)
inlineconstexpr

Normalize a slicing range into valid [start, stop) indices.

Template Parameters
SizeIntegral type of the dimension size
Parameters
rangeA possibly-negative Range
sizeSize of the dimension being indexed
Returns
A new Range with non-negative, normalized indices

This function is used internally by the tensor library when converting user-provided slicing ranges into valid [start, stop) intervals. Negative start and stop values are supported and are interpreted relative to the end of the dimension:

  • start = -1 → last element
  • stop = -1 → one-past-the-end

Example:

auto r1 = tannic::indexing::normalize({-3, -1}, 10);
// r1.start = 7, r1.stop = 10
Note
End-users generally use tannic::range{} or slicing syntax; this function ensures those inputs are mapped into safe, non-negative indices before evaluation.