TensorStorageBase
TensorStorageBase
CRTP base class for tensor storage implementations. More…
#include <TensorStorageBase.hpp>
Public Functions
| Name | |
|---|---|
| float | getValueAt(std::span< std::size_t const > indices) const Get a single float value by multi-dimensional index. |
| std::span< float const > | flatData() const Get a span over the flat (row-major) data. |
| std::vector< float > | sliceAlongAxis(std::size_t axis, std::size_t index) const Extract a sub-tensor by fixing one axis to a single index. |
| std::vector< float > | getColumn(std::size_t col) const Get a single column (axis 1 slice) across all rows (axis 0). |
| std::vector< std::size_t > | shape() const Get the shape as a vector of sizes (one per axis). |
| std::size_t | totalElements() const Get the total number of elements (product of shape). |
| bool | isContiguous() const Whether the underlying data is contiguous in memory. |
| TensorStorageType | getStorageType() const Get the storage type identifier. |
| TensorStorageCache | tryGetCache() const Try to get cached pointers for fast-path access. |
Protected Functions
| Name | |
|---|---|
| ~TensorStorageBase() =default |
Detailed Description
template <typename Derived >
class TensorStorageBase;CRTP base class for tensor storage implementations.
Uses the Curiously Recurring Template Pattern to eliminate virtual function overhead while maintaining a polymorphic interface. All concrete storage backends (Armadillo, Dense, LibTorch, View, Lazy) inherit from this and implement the *Impl() methods.
The interface is read-oriented. Mutation is handled at the TensorData level, which may replace the entire storage wrapper. This matches the project convention where views are immutable (same as ViewAnalogDataStorage, ViewRaggedStorage).
Row-major contract
All storage backends present a row-major interface to consumers:
[flatData()](/api_generated/Classes/classTensorStorageBase.md#function-flatdata)returns data in row-major ordergetValueAt(indices)interprets indices in row-major ordergetColumn(col)returns columncolacross all rows (axis 0)
Backends that store data in a different layout (e.g., Armadillo is column-major) handle the translation internally.
DerivedThe concrete storage implementation type
Public Functions Documentation
function getValueAt
inline float getValueAt(
std::span< std::size_t const > indices
) constGet a single float value by multi-dimensional index.
Parameters:
- indices One index per dimension, in row-major order
Exceptions:
- std::out_of_range if any index is out of bounds
- std::invalid_argument if indices.size() != ndim
Return: The float value at that position
function flatData
inline std::span< float const > flatData() constGet a span over the flat (row-major) data.
Exceptions:
- std::runtime_error if storage is not contiguous
Return: Span over the underlying float buffer
Only available for contiguous storage backends. Non-contiguous backends (views, lazy) throw std::runtime_error.
function sliceAlongAxis
inline std::vector< float > sliceAlongAxis(
std::size_t axis,
std::size_t index
) constExtract a sub-tensor by fixing one axis to a single index.
Parameters:
- axis Which axis to slice along
- index The index along that axis
Exceptions:
- std::out_of_range if axis >= ndim or index >= shape[axis]
Return: Flat vector of floats for the sub-tensor
Returns a new contiguous vector of floats. For example, on a 3D tensor with shape [T, C, F]:
- sliceAlongAxis(0, 5) returns the 2D slice at time=5 (shape [C, F])
- sliceAlongAxis(1, 2) returns all time/frequency data for channel 2
The returned data is always in row-major order for the remaining axes.
function getColumn
inline std::vector< float > getColumn(
std::size_t col
) constGet a single column (axis 1 slice) across all rows (axis 0).
Parameters:
- col Column index
Exceptions:
- std::out_of_range if col >= shape[1]
- std::invalid_argument if tensor is 0D or 1D (no column axis)
Return: Vector of floats (one per row for 2D, flattened for higher dims)
Equivalent to sliceAlongAxis(1, col) for 2D tensors, but optimized for the common case of extracting named feature columns.
For tensors with >2 dimensions, this extracts along axis 1 and flattens the remaining trailing dimensions.
function shape
inline std::vector< std::size_t > shape() constGet the shape as a vector of sizes (one per axis).
function totalElements
inline std::size_t totalElements() constGet the total number of elements (product of shape).
function isContiguous
inline bool isContiguous() constWhether the underlying data is contiguous in memory.
When true, flatData() and tryGetCache() are available. When false, element access goes through getValueAt() or getColumn().
function getStorageType
inline TensorStorageType getStorageType() constGet the storage type identifier.
function tryGetCache
inline TensorStorageCache tryGetCache() constTry to get cached pointers for fast-path access.
Return: TensorStorageCache with valid=true if contiguous, invalid otherwise
Protected Functions Documentation
function ~TensorStorageBase
~TensorStorageBase() =defaultUpdated on 2026-11-13 at 20:25:39 +0000