Stratax 0.2.0
Loading...
Searching...
No Matches
stratax::container::Matrix< T > Class Template Reference

Stores a rank-2 Stratax array in row-major order. More...

#include <Matrix.hpp>

Public Types

using value_type = T
 Element type stored by the matrix.
using iterator = typename core::Buffer<T>::iterator
 Mutable iterator over matrix elements.
using const_iterator = typename core::Buffer<T>::const_iterator
 Const iterator over matrix elements.
using reverse_iterator = typename core::Buffer<T>::reverse_iterator
 Mutable reverse iterator over matrix elements.
using const_reverse_iterator = typename core::Buffer<T>::const_reverse_iterator
 Const reverse iterator over matrix elements.
template<typename U>
using rebind = Matrix<U>
 Rebinds the matrix container to another element type.

Public Member Functions

 Matrix ()
 Creates a default rank-2 empty matrix.
 Matrix (std::size_t rows, std::size_t cols)
 Creates a rank-2 matrix with the given number of rows and columns.
 Matrix (const core::Shape &shape)
 Creates a matrix from a validated rank-2 shape.
 Matrix (std::size_t rows, std::size_t cols, const T &value)
 Creates a matrix and fills it with a value.
 Matrix (std::initializer_list< std::initializer_list< T > > list)
 Creates a matrix from a nested initializer list.
 Matrix (const Matrix &)=default
 Creates a copy of another matrix.
 Matrix (Matrix &&) noexcept=default
 Transfers ownership from another matrix.
Matrixoperator= (const Matrix &)=default
 Replaces this matrix with a copy of another matrix.
Matrixoperator= (Matrix &&) noexcept=default
 Replaces this matrix by taking ownership from another matrix.
 ~Matrix ()=default
 Destroys the matrix.
std::size_t size () const noexcept
 Returns the total number of elements in the matrix.
bool empty () const noexcept
 Returns whether the matrix contains no elements.
std::size_t rows () const noexcept
 Returns the number of rows.
std::size_t cols () const noexcept
 Returns the number of columns.
const stratax::core::Shapeshape () const noexcept
 Returns the matrix shape.
const stratax::core::Stridesstrides () const noexcept
 Returns the matrix strides.
std::size_t rank () const noexcept
 Returns the matrix rank.
T & operator() (std::size_t row, std::size_t col)
 Returns an element by row and column with bounds checking.
const T & operator() (std::size_t row, std::size_t col) const
 Returns an element by row and column with bounds checking.
T & operator[] (std::size_t index) noexcept
 Returns a flat element without bounds checking.
const T & operator[] (std::size_t index) const noexcept
 Returns a flat element without bounds checking.
T & at (std::ptrdiff_t row, std::ptrdiff_t col)
 Returns an element by row and column.
const T & at (std::ptrdiff_t row, std::ptrdiff_t col) const
 Returns an element by row and column.
T & front ()
 Returns the first element.
const T & front () const
 Returns the first element as a const reference.
T & back ()
 Returns the last element.
const T & back () const
 Returns the last element as a const reference.
T * data () noexcept
 Returns the raw data pointer.
const T * data () const noexcept
 Returns the raw data pointer as a const pointer.
iterator begin () noexcept
 Returns an iterator to the first element.
const_iterator begin () const noexcept
 Returns a const iterator to the first element.
const_iterator cbegin () const noexcept
 Returns a const iterator to the first element.
iterator end () noexcept
 Returns an iterator one past the last element.
const_iterator end () const noexcept
 Returns a const iterator one past the last element.
const_iterator cend () const noexcept
 Returns a const iterator one past the last element.
reverse_iterator rbegin () noexcept
 Returns a reverse iterator to the last element.
const_reverse_iterator rbegin () const noexcept
 Returns a const reverse iterator to the last element.
const_reverse_iterator crbegin () const noexcept
 Returns a const reverse iterator to the last element.
reverse_iterator rend () noexcept
 Returns a reverse iterator before the first element.
const_reverse_iterator rend () const noexcept
 Returns a const reverse iterator before the first element.
const_reverse_iterator crend () const noexcept
 Returns a const reverse iterator before the first element.
void fill (const T &value)
 Fills every element with the same value.
void swap (Matrix &other) noexcept
 Swaps the contents of two matrices.

Detailed Description

template<typename T>
requires Numeric<T>
class stratax::container::Matrix< T >

Stores a rank-2 Stratax array in row-major order.

Matrix owns contiguous storage together with shape and stride metadata, making it suitable for generic array operations and two-dimensional indexing.

Template Parameters
TNumeric element type.

Definition at line 28 of file Matrix.hpp.

Member Typedef Documentation

◆ const_iterator

template<typename T>
using stratax::container::Matrix< T >::const_iterator = typename core::Buffer<T>::const_iterator

Const iterator over matrix elements.

Definition at line 43 of file Matrix.hpp.

◆ const_reverse_iterator

template<typename T>
using stratax::container::Matrix< T >::const_reverse_iterator = typename core::Buffer<T>::const_reverse_iterator

Const reverse iterator over matrix elements.

Definition at line 49 of file Matrix.hpp.

◆ iterator

template<typename T>
using stratax::container::Matrix< T >::iterator = typename core::Buffer<T>::iterator

Mutable iterator over matrix elements.

Definition at line 40 of file Matrix.hpp.

◆ rebind

template<typename T>
template<typename U>
using stratax::container::Matrix< T >::rebind = Matrix<U>

Rebinds the matrix container to another element type.

Definition at line 53 of file Matrix.hpp.

◆ reverse_iterator

template<typename T>
using stratax::container::Matrix< T >::reverse_iterator = typename core::Buffer<T>::reverse_iterator

Mutable reverse iterator over matrix elements.

Definition at line 46 of file Matrix.hpp.

◆ value_type

template<typename T>
using stratax::container::Matrix< T >::value_type = T

Element type stored by the matrix.

Definition at line 37 of file Matrix.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/5]

template<typename T>
stratax::container::Matrix< T >::Matrix ( )
inline

Creates a default rank-2 empty matrix.

The default matrix has shape (0, 0) and owns no element storage.

Definition at line 60 of file Matrix.hpp.

◆ Matrix() [2/5]

template<typename T>
stratax::container::Matrix< T >::Matrix ( std::size_t rows,
std::size_t cols )
inline

Creates a rank-2 matrix with the given number of rows and columns.

Parameters
rowsNumber of rows.
colsNumber of columns.

Definition at line 68 of file Matrix.hpp.

◆ Matrix() [3/5]

template<typename T>
stratax::container::Matrix< T >::Matrix ( const core::Shape & shape)
inlineexplicit

Creates a matrix from a validated rank-2 shape.

Parameters
shapeSource shape.
Exceptions
Exceptions::DimensionErrorIf the shape is not rank 2.

Definition at line 82 of file Matrix.hpp.

◆ Matrix() [4/5]

template<typename T>
stratax::container::Matrix< T >::Matrix ( std::size_t rows,
std::size_t cols,
const T & value )
inline

Creates a matrix and fills it with a value.

Parameters
rowsNumber of rows.
colsNumber of columns.
valueValue to copy into each element.

Definition at line 99 of file Matrix.hpp.

◆ Matrix() [5/5]

template<typename T>
stratax::container::Matrix< T >::Matrix ( std::initializer_list< std::initializer_list< T > > list)
inline

Creates a matrix from a nested initializer list.

Rows are interpreted in row-major order and must all have the same length.

Parameters
listRow-major source values.
Exceptions
Exceptions::StrataxErrorIf the rows are not rectangular.

Definition at line 115 of file Matrix.hpp.

Member Function Documentation

◆ at() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::at ( std::ptrdiff_t row,
std::ptrdiff_t col )
inline

Returns an element by row and column.

Parameters
rowRow index.
colColumn index.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 307 of file Matrix.hpp.

◆ at() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::at ( std::ptrdiff_t row,
std::ptrdiff_t col ) const
inline

Returns an element by row and column.

Parameters
rowRow index.
colColumn index.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 325 of file Matrix.hpp.

◆ back() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::back ( )
inline

Returns the last element.

Returns
Mutable reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 372 of file Matrix.hpp.

◆ back() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::back ( ) const
inline

Returns the last element as a const reference.

Returns
Const reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 388 of file Matrix.hpp.

◆ begin() [1/2]

template<typename T>
const_iterator stratax::container::Matrix< T >::begin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the matrix.

Definition at line 433 of file Matrix.hpp.

◆ begin() [2/2]

template<typename T>
iterator stratax::container::Matrix< T >::begin ( )
inlinenodiscardnoexcept

Returns an iterator to the first element.

Returns
Iterator to the beginning of the matrix.

Definition at line 423 of file Matrix.hpp.

◆ cbegin()

template<typename T>
const_iterator stratax::container::Matrix< T >::cbegin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the matrix.

Definition at line 443 of file Matrix.hpp.

◆ cend()

template<typename T>
const_iterator stratax::container::Matrix< T >::cend ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the matrix.

Definition at line 473 of file Matrix.hpp.

◆ cols()

template<typename T>
std::size_t stratax::container::Matrix< T >::cols ( ) const
inlinenodiscardnoexcept

Returns the number of columns.

Returns
Column count.

Definition at line 205 of file Matrix.hpp.

◆ crbegin()

template<typename T>
const_reverse_iterator stratax::container::Matrix< T >::crbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 503 of file Matrix.hpp.

◆ crend()

template<typename T>
const_reverse_iterator stratax::container::Matrix< T >::crend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 533 of file Matrix.hpp.

◆ data() [1/2]

template<typename T>
const T * stratax::container::Matrix< T >::data ( ) const
inlinenodiscardnoexcept

Returns the raw data pointer as a const pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 413 of file Matrix.hpp.

◆ data() [2/2]

template<typename T>
T * stratax::container::Matrix< T >::data ( )
inlinenodiscardnoexcept

Returns the raw data pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 403 of file Matrix.hpp.

◆ empty()

template<typename T>
bool stratax::container::Matrix< T >::empty ( ) const
inlinenodiscardnoexcept

Returns whether the matrix contains no elements.

Returns
true when the matrix size is zero.

Definition at line 185 of file Matrix.hpp.

◆ end() [1/2]

template<typename T>
const_iterator stratax::container::Matrix< T >::end ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the matrix.

Definition at line 463 of file Matrix.hpp.

◆ end() [2/2]

template<typename T>
iterator stratax::container::Matrix< T >::end ( )
inlinenodiscardnoexcept

Returns an iterator one past the last element.

Returns
Iterator to the end of the matrix.

Definition at line 453 of file Matrix.hpp.

◆ fill()

template<typename T>
void stratax::container::Matrix< T >::fill ( const T & value)
inline

Fills every element with the same value.

Parameters
valueValue to assign to each element.

Definition at line 543 of file Matrix.hpp.

◆ front() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::front ( )
inline

Returns the first element.

Returns
Mutable reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 340 of file Matrix.hpp.

◆ front() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::front ( ) const
inline

Returns the first element as a const reference.

Returns
Const reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 356 of file Matrix.hpp.

◆ operator()() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::operator() ( std::size_t row,
std::size_t col )
inline

Returns an element by row and column with bounds checking.

Parameters
rowRow index.
colColumn index.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 249 of file Matrix.hpp.

◆ operator()() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::operator() ( std::size_t row,
std::size_t col ) const
inline

Returns an element by row and column with bounds checking.

Parameters
rowRow index.
colColumn index.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 265 of file Matrix.hpp.

◆ operator[]() [1/2]

template<typename T>
const T & stratax::container::Matrix< T >::operator[] ( std::size_t index) const
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 293 of file Matrix.hpp.

◆ operator[]() [2/2]

template<typename T>
T & stratax::container::Matrix< T >::operator[] ( std::size_t index)
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 280 of file Matrix.hpp.

◆ rank()

template<typename T>
std::size_t stratax::container::Matrix< T >::rank ( ) const
inlinenodiscardnoexcept

Returns the matrix rank.

Returns
The number of dimensions, always 2 for valid matrices.

Definition at line 235 of file Matrix.hpp.

◆ rbegin() [1/2]

template<typename T>
const_reverse_iterator stratax::container::Matrix< T >::rbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 493 of file Matrix.hpp.

◆ rbegin() [2/2]

template<typename T>
reverse_iterator stratax::container::Matrix< T >::rbegin ( )
inlinenodiscardnoexcept

Returns a reverse iterator to the last element.

Returns
Reverse iterator starting at the last element.

Definition at line 483 of file Matrix.hpp.

◆ rend() [1/2]

template<typename T>
const_reverse_iterator stratax::container::Matrix< T >::rend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 523 of file Matrix.hpp.

◆ rend() [2/2]

template<typename T>
reverse_iterator stratax::container::Matrix< T >::rend ( )
inlinenodiscardnoexcept

Returns a reverse iterator before the first element.

Returns
Reverse iterator representing the end sentinel.

Definition at line 513 of file Matrix.hpp.

◆ rows()

template<typename T>
std::size_t stratax::container::Matrix< T >::rows ( ) const
inlinenodiscardnoexcept

Returns the number of rows.

Returns
Row count.

Definition at line 195 of file Matrix.hpp.

◆ shape()

template<typename T>
const stratax::core::Shape & stratax::container::Matrix< T >::shape ( ) const
inlinenoexcept

Returns the matrix shape.

Returns
Shape describing the matrix.

Definition at line 215 of file Matrix.hpp.

◆ size()

template<typename T>
std::size_t stratax::container::Matrix< T >::size ( ) const
inlinenodiscardnoexcept

Returns the total number of elements in the matrix.

Returns
Number of stored elements.

Definition at line 175 of file Matrix.hpp.

◆ strides()

template<typename T>
const stratax::core::Strides & stratax::container::Matrix< T >::strides ( ) const
inlinenoexcept

Returns the matrix strides.

Returns
Strides describing the matrix layout.

Definition at line 225 of file Matrix.hpp.

◆ swap()

template<typename T>
void stratax::container::Matrix< T >::swap ( Matrix< T > & other)
inlinenoexcept

Swaps the contents of two matrices.

Parameters
otherMatrix to exchange state with.

Definition at line 553 of file Matrix.hpp.


The documentation for this class was generated from the following files: