Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::DynMatrix< T > Class Template Reference

Dynamic matrix with sparse storage. More...

#include <tpl_dynMat.H>

Inheritance diagram for Aleph::DynMatrix< T >:
[legend]
Collaboration diagram for Aleph::DynMatrix< T >:
[legend]

Classes

class  Iterator
 Iterator for traversing matrix elements. More...
 

Public Types

using Set_Type = DynMatrix< T >
 The container type (self-reference for generic programming).
 
using Item_Type = T
 The type of elements stored in the matrix.
 
using Key_Type = T
 Alias for Item_Type, required by functional mixins.
 

Public Member Functions

void set_default_initial_value (const T &value)
 Set the default value for unwritten entries.
 
const T & get_default_value () const noexcept
 Get the default value for unwritten entries.
 
void swap (DynMatrix &mat) noexcept(std::is_nothrow_swappable_v< T >)
 Swap the contents of this matrix with another.
 
void set_dimension (const size_t num_rows, const size_t num_cols)
 Change the matrix dimensions.
 
void allocate ()
 Pre-allocate memory for the entire matrix.
 
 DynMatrix (const size_t num_rows, const size_t num_cols, const T &zero=T())
 Construct a matrix with specified dimensions.
 
 DynMatrix () noexcept(std::is_nothrow_default_constructible_v< T >)=default
 Default constructor - creates an invalid matrix.
 
 ~DynMatrix ()
 Destructor - releases all allocated memory.
 
 DynMatrix (const DynMatrix< T > &mat)
 Copy constructor.
 
 DynMatrix (DynMatrix< T > &&mat) noexcept(std::is_nothrow_default_constructible_v< T > and std::is_nothrow_swappable_v< T >)
 Move constructor.
 
DynMatrix< T > & operator= (const DynMatrix< T > &mat)
 Copy assignment operator.
 
DynMatrix< T > & operator= (DynMatrix< T > &&mat) noexcept(std::is_nothrow_default_constructible_v< T > and std::is_nothrow_swappable_v< T >)
 Move assignment operator.
 
bool operator== (const DynMatrix< T > &mat) const
 Equality comparison.
 
bool operator!= (const DynMatrix< T > &mat) const
 Inequality comparison.
 
constexpr size_t rows () const noexcept
 Get the number of rows.
 
constexpr size_t cols () const noexcept
 Get the number of columns.
 
constexpr size_t size () const noexcept
 Get the total number of entries.
 
constexpr bool is_square () const noexcept
 Check if the matrix is square.
 
constexpr bool is_empty () const noexcept
 Check if the matrix is empty (uninitialized).
 
const T & read (const size_t i, const size_t j) const
 Read the entry at position (i, j).
 
const T & read_ne (const size_t i, const size_t j) const noexcept
 Read entry without bounds checking.
 
T & write (const size_t i, const size_t j, const T &data)
 Write a value to position (i, j).
 
T & write (const size_t i, const size_t j, T &&data)
 See write(const size_t i, const size_t j, const T & data).
 
T & access (const size_t i, const size_t j)
 Direct access to an allocated entry.
 
const T & access (const size_t i, const size_t j) const
 Direct const access to an allocated entry.
 
T & operator() (const size_t i, const size_t j)
 Subscript operator for allocated entries.
 
const T & operator() (const size_t i, const size_t j) const
 Const subscript operator for allocated entries.
 
void fill (const T &value)
 Fill all entries with a value.
 
DynMatrix< T > transpose () const
 Create a transposed copy of the matrix.
 
Iterator get_it () const
 Get an iterator positioned at the first element.
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool traverse (Operation &operation) const
 Traverse all elements in row-major order.
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool traverse (Operation &operation)
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool traverse (Operation &&operation) const
 See traverse(Operation & operation).
 
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool traverse (Operation &&operation)
 See traverse(Operation & operation).
 
template<class Operation >
bool traverse_allocated (Operation &&operation)
 Traverse entries in allocated memory blocks.
 
template<class Operation >
bool traverse_allocated (Operation &&operation) const
 See traverse_allocated(Operation && operation).
 
- Public Member Functions inherited from LocateFunctions< DynMatrix< T >, T >
auto get_it ()
 Return a properly initialized iterator positioned at the first item on the container.
 
auto get_it () const
 Read-only iterator positioned at the first item.
 
auto get_it (const size_t pos)
 Return a properly initialized iterator positioned at the pos item on the container.
 
auto get_it (const size_t pos) const
 Read-only iterator positioned at the pos item.
 
auto get_itor ()
 Alias of get_it().
 
auto get_itor () const
 See get_itor().
 
T & nth_ne (const size_t n) noexcept(Aleph::dry_detail::mutable_traversal_is_noexcept< C, T >)
 Return the n‑th element without bounds checking.
 
const T & nth_ne (const size_t n) const noexcept(Aleph::dry_detail::const_traversal_is_noexcept< DynMatrix< T >, T >)
 Constant overload of nth_ne(size_t).
 
T & nth (const size_t n)
 Return the n-th item of the container.
 
const T & nth (const size_t n) const
 Const overload of nth(size_t).
 
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< DynMatrix< T >, Operation >::type, T > T * find_ptr (Operation &operation) noexcept(mutable_search_is_noexcept< Operation >())
 Find a pointer to an item in the container according to a searching criterion.
 
const T * find_ptr (Operation &operation) const noexcept(const_search_is_noexcept< Operation >())
 Const overload of find_ptr(Operation&).
 
const T * find_ptr (Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
 Overload of find_ptr() const that accepts rvalues.
 
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< DynMatrix< T >, Operation >::type, T > T * find_ptr (Operation &&operation) noexcept(mutable_search_is_noexcept< Operation >())
 Overload of find_ptr() that accepts rvalues.
 
size_t find_index (Operation &operation) const noexcept(const_search_is_noexcept< Operation >())
 Find the position of an item in the container according to a searching criterion.
 
size_t find_index (Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
 Overload of find_index() that accepts rvalues.
 
and std::default_initializable< T > and std::copy_constructible< T > std::tuple< bool, T > find_item (Operation &operation) const noexcept(find_item_is_noexcept< Operation >())
 Safe sequential searching of an item matching a criterion.
 
and std::default_initializable< T > and std::copy_constructible< T > std::tuple< bool, T > find_item (Operation &&operation) const noexcept(find_item_is_noexcept< Operation >())
 Overload of find_item() that accepts rvalues.
 
and std::copy_constructible< T > std::optional< T > find_opt (Operation &operation) const
 Copy of the first item satisfying a criterion, if any.
 
and std::copy_constructible< T > std::optional< T > find_opt (Operation &&operation) const
 Overload of find_opt() that accepts rvalues.
 
bool contains_if (Operation &&operation) const noexcept(const_search_is_noexcept< Operation >())
 Test if an item satisfying a criterion is present in the container.
 
bool contains (const T &item) const
 Test if an item is present in the container using equality.
 
- Public Member Functions inherited from FunctionalMethods< DynMatrix< T >, T >
void emplace (Args &&... args)
 Appends a new element into the container by constructing it from the given args.
 
void emplace_end (Args &&... args)
 See emplace(Args && ... args).
 
void emplace_ins (Args &&... args)
 
size_t ninsert (Args &&... args)
 Insert n variadic items.
 
size_t nappend (Args &&... args)
 Append n variadic items.
 
void for_each (Operation &operation) const
 Traverse all the container and performs an operation on each element.
 
void for_each (Operation &&operation) const
 Overload of for_each() that accepts rvalues.
 
void each (Operation &operation) const
 Alias of for_each().
 
void each (Operation &&operation) const
 Alias of for_each() that accepts rvalues.
 
void each (const size_t pos, const size_t slice, Operation &operation) const
 Traverse the container starting at pos taking one item every slice, performing an operation on each visited element.
 
void each (const size_t pos, const size_t slice, Operation &&operation) const
 See each(size_t pos, size_t slice, Operation & operation) const.
 
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< DynMatrix< T >, Operation >::type, T > void each (const size_t pos, const size_t slice, Operation &operation)
 Traverse the container starting at pos taking one item every slice, performing an operation that may modify each visited item.
 
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< DynMatrix< T >, Operation >::type, T > void each (const size_t pos, const size_t slice, Operation &&operation)
 See each(size_t pos, size_t slice, Operation & operation).
 
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< DynMatrix< T >, Operation >::type, T > void mutable_for_each (Operation &operation)
 Apply a mutable operation to each element of the container.
 
and Aleph::dry_detail::MutablyTraversable< typename Aleph::concepts_detail::defer< DynMatrix< T >, Operation >::type, T > void mutable_for_each (Operation &&operation)
 See mutable_for_each(Operation & operation).
 
bool all (Operation &operation) const
 Check if all the elements of the container satisfy a condition.
 
bool all (Operation &&operation) const
 Overload of all() that accepts rvalues.
 
bool exists (Operation &op) const
 Test for existence in the container of an element satisfying a criterion.
 
bool exists (Operation &&op) const
 Overload of exists() that accepts rvalues.
 
bool none (Operation &op) const
 Test that no element satisfies a criterion.
 
bool none (Operation &&op) const
 Overload of none() that accepts rvalues.
 
size_t count_if (Operation &op) const
 Count the elements that satisfy a criterion.
 
size_t count_if (Operation &&op) const
 Overload of count_if() that accepts rvalues.
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps (Operation &op) const
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps (Operation &&op) const
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps_if (Prop prop, Operation &op) const
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > maps_if (Prop prop, Operation &&op) const
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map (Operation &op) const
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map (Operation &&op) const
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map_if (Prop prop, Operation &op) const
 
Aleph::DynList< Aleph::dry_detail::map_result_t< R, Operation &, T > > map_if (Prop prop, Operation &&op) const
 
auto flat_map (Operation &op) const
 Map each element to a container and concatenate the results.
 
auto flat_map (Operation &&op) const
 Overload of flat_map() that accepts rvalues.
 
Aleph::DynList< T > to_dynlist () const
 Convert container to DynList.
 
std::vector< T > to_vector () const
 Convert container to std::vector.
 
Acc foldl (const Acc &init, Op &op) const
 Fold the elements of the container to a specific result.
 
Acc foldl (const Acc &init, Op &&op) const
 Overload of foldl() that accepts rvalues.
 
Acc fold_left (const Acc &init, Op &op) const
 Alias for foldl().
 
Acc fold_left (const Acc &init, Op &&op) const
 Overload of fold_left() that accepts rvalues.
 
T fold (const T &init, Operation &operation) const
 Simplified version of foldl() where the folded type is the same type of elements stored in the container.
 
T fold (const T &init, Operation &&operation) const
 Overload of fold() that accepts rvalues.
 
Acc foldr (const Acc &init, Op &op) const
 Fold the elements from the last to the first.
 
Acc foldr (const Acc &init, Op &&op) const
 Overload of foldr() that accepts rvalues.
 
and std::copy_constructible< U > std::optional< T > reduce (Op &op) const
 Fold the elements with no initial value.
 
and std::copy_constructible< U > std::optional< T > reduce (Op &&op) const
 Overload of reduce() that accepts rvalues.
 
Aleph::DynList< T > filter (Operation &operation) const
 Filter the elements of a container according to a matching criterion.
 
Aleph::DynList< T > filter (Operation &&operation) const
 Overload of filter() that accepts rvalues.
 
Aleph::DynList< const T * > ptr_filter (Operation &operation) const
 Filter the elements of a container according to a matching criterion and return a pointer to the matched items in the container.
 
Aleph::DynList< const T * > ptr_filter (Operation &&operation) const
 Overload of ptr_filter() that accepts rvalues.
 
Aleph::DynList< std::tuple< T, size_t > > pfilter (Operation &operation) const
 Filter the elements of a container according to a matching criterion and determine its positions respect to the traversal of container.
 
Aleph::DynList< std::tuple< T, size_t > > pfilter (Operation &&operation) const
 Overload of pfilter() that accepts rvalues.
 
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition (Operation &op) const
 Exclusive partition of container according to a filter criterion.
 
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition (Operation &&op) const
 Overload of partition() that accepts rvalues.
 
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > partition (size_t n) const
 Exclusive partition of container in the nth item.
 
std::pair< Aleph::DynList< T >, Aleph::DynList< T > > split_half () const
 Split the container into two halves by alternating elements.
 
std::tuple< Aleph::DynList< T >, Aleph::DynList< T > > tpartition (Operation &op) const
 Exclusive partition of container according to a filter criterion.
 
std::tuple< Aleph::DynList< T >, Aleph::DynList< T > > tpartition (Operation &&op) const
 Overload of tpartition() that accepts rvalues.
 
size_t length () const noexcept(Aleph::dry_detail::const_traversal_is_noexcept< DynMatrix< T >, T >)
 Count the number of elements of a container.
 
Aleph::DynList< T > rev () const
 Return a list with the elements of container in reverse order respect to its traversal order.
 
Aleph::DynList< T > take (const size_t n) const
 Return a list with the first n elements seen in the container during its traversal.
 
Aleph::DynList< T > take (size_t i, const size_t j, const size_t step=1) const
 Return a list with the elements at positions i, i + step, i + 2 step, ... that are not greater than j, respect to the traversal order.
 
Aleph::DynList< T > drop (const size_t n) const
 Drop the first n elements seen in the container during its traversal.
 
Aleph::DynList< T > take_while (Operation &op) const
 The longest prefix of elements that satisfy a criterion.
 
Aleph::DynList< T > take_while (Operation &&op) const
 Overload of take_while() that accepts rvalues.
 
Aleph::DynList< T > drop_while (Operation &op) const
 The elements after the longest prefix that satisfies a criterion.
 
Aleph::DynList< T > drop_while (Operation &&op) const
 Overload of drop_while() that accepts rvalues.
 
void mutable_drop (const size_t n)
 Drop the first n elements seen from container.
 

Private Member Functions

const T & read_array_entry (const size_t i) const noexcept
 
T & write_array_entry (const size_t i, const T &data)
 
T & write_array_entry (const size_t i, T &&data)
 
constexpr size_t get_index (const size_t i, const size_t j) const noexcept
 
void check_indices (const size_t i, const size_t j) const
 

Private Attributes

size_t n = 0
 Number of rows.
 
size_t m = 0
 Number of columns.
 
DynArray< T > * array_ptr = nullptr
 
T default_value = T()
 

Additional Inherited Members

- Static Public Member Functions inherited from FunctionalMethods< DynMatrix< T >, T >
static DynMatrix< T > build (Items &&... items)
 Build a container that holds the given items.
 
static D build_as (Items &&... items)
 Build a D, a class derived from the container, holding the given items.
 
- Public Attributes inherited from FunctionalMethods< DynMatrix< T >, T >
 and
 Insert a new element into the container by constructing it from the given args.
 
and Aleph::InvocableWith< Operation &, const T & > and
 Conditional mapping of the elements of the container.
 

Detailed Description

template<typename T>
class Aleph::DynMatrix< T >

Dynamic matrix with sparse storage.

DynMatrix<T> implements a two-dimensional matrix of generic type T with dynamic, sparse storage. Only entries that are explicitly written consume memory; unwritten entries return the default "zero" value.

Features

  • Sparse storage: Only written entries consume memory
  • Dynamic dimensions: Can be resized at runtime
  • Row-major order: Elements are stored in row-major order for cache efficiency
  • Functional interface: Supports traverse, maps, filter, foldl, etc.
  • Customizable zero value: Default value for unwritten entries

Example Usage

// Create a 3x4 matrix with default value 0.0
DynMatrix<double> mat(3, 4, 0.0);
// Write some entries
mat.write(0, 0, 1.0);
mat.write(1, 2, 5.0);
// Read entries (unwritten returns 0.0)
double a = mat.read(0, 0); // 1.0
double b = mat.read(0, 1); // 0.0 (default)
// Using operator() for allocated entries
mat.allocate(); // Allocate all entries
mat(2, 3) = 7.0;
// Traverse all written entries
mat.traverse([](double& val) {
std::cout << val << " ";
return true;
});
Dynamic matrix with sparse storage.
Definition tpl_dynMat.H:122

Memory Model

The matrix uses a DynArray<T> internally, which provides:

  • O(1) access to any entry (amortized)
  • Memory allocation only for written entries
  • Efficient traversal of all written entries

Thread Safety

This class is not thread-safe. External synchronization is required for concurrent access from multiple threads.

Template Parameters
TType of elements stored in the matrix. Must be default-constructible.
See also
DynArray The underlying storage container
Examples
hungarian_example.cc.

Definition at line 119 of file tpl_dynMat.H.

Member Typedef Documentation

◆ Item_Type

template<typename T >
using Aleph::DynMatrix< T >::Item_Type = T

The type of elements stored in the matrix.

Definition at line 168 of file tpl_dynMat.H.

◆ Key_Type

template<typename T >
using Aleph::DynMatrix< T >::Key_Type = T

Alias for Item_Type, required by functional mixins.

Definition at line 171 of file tpl_dynMat.H.

◆ Set_Type

template<typename T >
using Aleph::DynMatrix< T >::Set_Type = DynMatrix<T>

The container type (self-reference for generic programming).

Definition at line 165 of file tpl_dynMat.H.

Constructor & Destructor Documentation

◆ DynMatrix() [1/4]

template<typename T >
Aleph::DynMatrix< T >::DynMatrix ( const size_t  num_rows,
const size_t  num_cols,
const T &  zero = T() 
)
inline

Construct a matrix with specified dimensions.

Creates a matrix with num_rows rows and num_cols columns. All entries are initialized to zero (default: T()).

Parameters
[in]num_rowsNumber of rows (must be > 0).
[in]num_colsNumber of columns (must be > 0).
[in]zeroDefault value for unwritten entries.
Exceptions
std::bad_allocIf memory allocation fails.
std::invalid_argumentIf num_rows or num_cols is zero.

Definition at line 288 of file tpl_dynMat.H.

References ah_invalid_argument_if, Aleph::DynMatrix< T >::m, Aleph::DynMatrix< T >::n, and Aleph::DynMatrix< T >::set_dimension().

◆ DynMatrix() [2/4]

template<typename T >
Aleph::DynMatrix< T >::DynMatrix ( ) const
defaultnoexcept

Default constructor - creates an invalid matrix.

Creates a matrix with zero dimensions. The matrix must be initialized with set_dimension() before use.

Note
This constructor is provided primarily for container compatibility. Most operations will fail on an uninitialized matrix.
Exceptions
Whatthe default construction of T throws, for the default value of the matrix; nothing when it is noexcept.

◆ ~DynMatrix()

template<typename T >
Aleph::DynMatrix< T >::~DynMatrix ( )
inline

Destructor - releases all allocated memory.

Definition at line 311 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr.

◆ DynMatrix() [3/4]

template<typename T >
Aleph::DynMatrix< T >::DynMatrix ( const DynMatrix< T > &  mat)
inline

Copy constructor.

Creates a deep copy of mat.

Parameters
[in]matThe matrix to copy.
Exceptions
std::bad_allocIf memory allocation fails.

Definition at line 324 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::m, Aleph::DynMatrix< T >::n, and Aleph::DynMatrix< T >::set_dimension().

◆ DynMatrix() [4/4]

template<typename T >
Aleph::DynMatrix< T >::DynMatrix ( DynMatrix< T > &&  mat)
inlinenoexcept

Move constructor.

Takes the dimensions, the entries and the default value of mat, which is left as a matrix built by default: without dimensions, and with a T built by default as its default value.

Parameters
[in,out]matThe matrix to move from.
Exceptions
Whatthe default construction of T throws, for the default value that mat is left with, and then mat is as it was; or what the swap of the default values throws. Nothing when both are noexcept.

Definition at line 347 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::swap().

Member Function Documentation

◆ access() [1/2]

template<typename T >
T & Aleph::DynMatrix< T >::access ( const size_t  i,
const size_t  j 
)
inline

Direct access to an allocated entry.

Returns a reference to the entry at position (i, j). The entry must have been previously written, or the matrix must have been allocated with allocate().

Time complexity: O(1).

Parameters
[in]iRow index (0-based).
[in]jColumn index (0-based).
Returns
A modifiable reference to the entry.
Warning
Behavior is undefined if the entry has not been allocated. Call allocate() first or use write() for safe access.

Definition at line 542 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, and Aleph::DynMatrix< T >::get_index().

Referenced by Aleph::DynMatrix< T >::operator()(), Aleph::DynMatrix< T >::operator()(), and Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::print().

◆ access() [2/2]

template<typename T >
const T & Aleph::DynMatrix< T >::access ( const size_t  i,
const size_t  j 
) const
inline

Direct const access to an allocated entry.

Parameters
[in]iRow index.
[in]jColumn index.
Returns
A const reference to the entry.

Definition at line 553 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, and Aleph::DynMatrix< T >::get_index().

◆ allocate()

template<typename T >
void Aleph::DynMatrix< T >::allocate ( )
inline

Pre-allocate memory for the entire matrix.

Allocates memory for all n*m entries, allowing O(1) access via operator() without triggering additional allocations.

Exceptions
std::bad_allocIf there is not enough memory.
Note
After calling allocate(), operator() can be used safely for any valid index without checking for allocation.

Definition at line 271 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::m, and Aleph::DynMatrix< T >::n.

Referenced by Aleph::Floyd_All_Shortest_Paths< GT, Distance, SA >::Floyd_All_Shortest_Paths(), Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::DynMatrix< T >::fill(), Aleph::hungarian_max_assignment(), make_matrix(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TYPED_TEST().

◆ check_indices()

template<typename T >
void Aleph::DynMatrix< T >::check_indices ( const size_t  i,
const size_t  j 
) const
inlineprivate

◆ cols()

template<typename T >
constexpr size_t Aleph::DynMatrix< T >::cols ( ) const
inlineconstexprnoexcept

Get the number of columns.

Returns
The number of columns in the matrix.

Definition at line 447 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::m.

Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), Aleph::hungarian_max_assignment(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), and TEST_F().

◆ fill()

template<typename T >
void Aleph::DynMatrix< T >::fill ( const T &  value)
inline

Fill all entries with a value.

Sets all entries to value. This allocates memory for all entries.

Parameters
[in]valueThe value to fill with.
Exceptions
std::bad_allocIf memory allocation fails.

Definition at line 590 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::allocate(), Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::m, Aleph::DynMatrix< T >::n, and value.

Referenced by TEST().

◆ get_default_value()

template<typename T >
const T & Aleph::DynMatrix< T >::get_default_value ( ) const
inlinenoexcept

Get the default value for unwritten entries.

Returns
The current default value.

Definition at line 194 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::default_value.

Referenced by TEST(), TEST(), and TEST().

◆ get_index()

template<typename T >
constexpr size_t Aleph::DynMatrix< T >::get_index ( const size_t  i,
const size_t  j 
) const
inlineconstexprprivatenoexcept

◆ get_it()

template<typename T >
Iterator Aleph::DynMatrix< T >::get_it ( ) const
inline

Get an iterator positioned at the first element.

Returns
An iterator at position (0, 0).

Definition at line 715 of file tpl_dynMat.H.

◆ is_empty()

template<typename T >
constexpr bool Aleph::DynMatrix< T >::is_empty ( ) const
inlineconstexprnoexcept

Check if the matrix is empty (uninitialized).

Returns
true if dimensions are zero, false otherwise.

Definition at line 465 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::m, and Aleph::DynMatrix< T >::n.

Referenced by TEST(), TEST(), TEST(), TEST(), and TEST().

◆ is_square()

template<typename T >
constexpr bool Aleph::DynMatrix< T >::is_square ( ) const
inlineconstexprnoexcept

Check if the matrix is square.

Returns
true if rows() == cols(), false otherwise.

Definition at line 459 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::m, and Aleph::DynMatrix< T >::n.

Referenced by TEST(), TEST(), TEST(), TEST(), and TEST().

◆ operator!=()

template<typename T >
bool Aleph::DynMatrix< T >::operator!= ( const DynMatrix< T > &  mat) const
inline

Inequality comparison.

Parameters
[in]matThe matrix to compare with.
Returns
true if matrices are not equal, false otherwise.

Definition at line 432 of file tpl_dynMat.H.

◆ operator()() [1/2]

template<typename T >
T & Aleph::DynMatrix< T >::operator() ( const size_t  i,
const size_t  j 
)
inline

Subscript operator for allocated entries.

Equivalent to access(i, j).

Parameters
[in]iRow index.
[in]jColumn index.
Returns
A modifiable reference to the entry.

Definition at line 566 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::access().

◆ operator()() [2/2]

template<typename T >
const T & Aleph::DynMatrix< T >::operator() ( const size_t  i,
const size_t  j 
) const
inline

Const subscript operator for allocated entries.

Parameters
[in]iRow index.
[in]jColumn index.
Returns
A const reference to the entry.

Definition at line 577 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::access().

◆ operator=() [1/2]

template<typename T >
DynMatrix< T > & Aleph::DynMatrix< T >::operator= ( const DynMatrix< T > &  mat)
inline

Copy assignment operator.

Replaces the contents of this matrix with a copy of mat.

Parameters
[in]matThe matrix to copy.
Returns
Reference to this matrix.
Exceptions
std::bad_allocIf memory allocation fails.

Definition at line 364 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::default_value, Aleph::DynMatrix< T >::m, Aleph::DynMatrix< T >::n, and Aleph::DynMatrix< T >::set_dimension().

◆ operator=() [2/2]

template<typename T >
DynMatrix< T > & Aleph::DynMatrix< T >::operator= ( DynMatrix< T > &&  mat)
inlinenoexcept

Move assignment operator.

Transfers ownership of resources from mat to this matrix.

Parameters
[in,out]matThe matrix to move from.
Returns
Reference to this matrix.
Exceptions
Whatthe default construction of T throws, for the default value that mat is left with, and then both matrices are as they were; or what the swap of the default values throws. Nothing when both are noexcept.

Definition at line 392 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::swap().

◆ operator==()

template<typename T >
bool Aleph::DynMatrix< T >::operator== ( const DynMatrix< T > &  mat) const
inline

Equality comparison.

Returns true if this matrix has the same dimensions as mat and all corresponding entries are equal (using T::operator==).

Parameters
[in]matThe matrix to compare with.
Returns
true if matrices are equal, false otherwise.

Definition at line 414 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::m, N, Aleph::DynMatrix< T >::n, and Aleph::DynMatrix< T >::read_array_entry().

◆ read()

template<typename T >
const T & Aleph::DynMatrix< T >::read ( const size_t  i,
const size_t  j 
) const
inline

Read the entry at position (i, j).

Returns the value at row i and column j. If the entry has not been written, returns the default value.

Time complexity: O(1) amortized.

Parameters
[in]iRow index (0-based).
[in]jColumn index (0-based).
Returns
The value at position (i, j).
Exceptions
std::out_of_rangeIf i >= rows() or j >= cols().

Definition at line 480 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::check_indices(), Aleph::DynMatrix< T >::get_index(), and Aleph::DynMatrix< T >::read_array_entry().

Referenced by Aleph::Hungarian_Assignment< Cost_Type >::Hungarian_Assignment(), compute_assignment_cost(), Aleph::hungarian_max_assignment(), main(), Aleph::Hungarian_Assignment< Cost_Type >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), and TEST_F().

◆ read_array_entry()

◆ read_ne()

template<typename T >
const T & Aleph::DynMatrix< T >::read_ne ( const size_t  i,
const size_t  j 
) const
inlinenoexcept

Read entry without bounds checking.

Parameters
[in]iRow index.
[in]jColumn index.
Returns
The value at position (i, j).
Warning
Behavior is undefined if indices are out of bounds.

Definition at line 494 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::get_index(), and Aleph::DynMatrix< T >::read_array_entry().

◆ rows()

◆ set_default_initial_value()

template<typename T >
void Aleph::DynMatrix< T >::set_default_initial_value ( const T &  value)
inline

Set the default value for unwritten entries.

Changes the value returned for entries that have not been explicitly written. This also affects new entries created by touch().

Parameters
[in]valueThe new default value.
Note
This does not modify existing entries; it only affects reads of unwritten positions and newly allocated entries.

Definition at line 183 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::default_value, and value.

Referenced by TEST().

◆ set_dimension()

template<typename T >
void Aleph::DynMatrix< T >::set_dimension ( const size_t  num_rows,
const size_t  num_cols 
)
inline

Change the matrix dimensions.

Resizes the matrix to have num_rows rows and num_cols columns. All existing data is discarded.

Parameters
[in]num_rowsThe new number of rows.
[in]num_colsThe new number of columns.
Exceptions
std::overflow_errorIf num_rows * num_cols does not fit in size_t.
std::length_errorIf the number of entries exceeds what a DynArray can hold.
std::bad_allocIf memory allocation fails.
Note
Strong guarantee: the new storage is built before the old one is released, so if anything throws the matrix is unchanged.
Warning
This operation discards all existing data. If you need to preserve data, copy it before resizing.

Definition at line 243 of file tpl_dynMat.H.

References ah_overflow_error_if, FunctionalMethods< DynMatrix< T >, T >::and, Aleph::DynMatrix< T >::array_ptr, Aleph::DynArray< T >::compute_sizes(), Aleph::DynMatrix< T >::default_value, Aleph::DynMatrix< T >::m, and Aleph::DynMatrix< T >::n.

Referenced by Aleph::DynMatrix< T >::DynMatrix(), Aleph::DynMatrix< T >::DynMatrix(), and Aleph::DynMatrix< T >::operator=().

◆ size()

template<typename T >
constexpr size_t Aleph::DynMatrix< T >::size ( ) const
inlineconstexprnoexcept

Get the total number of entries.

Returns
rows() * cols()

Definition at line 453 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::m, and Aleph::DynMatrix< T >::n.

Referenced by Aleph::DynMatrix< T >::Iterator::has_curr(), TEST(), TEST(), and TEST().

◆ swap()

template<typename T >
void Aleph::DynMatrix< T >::swap ( DynMatrix< T > &  mat)
inlinenoexcept

Swap the contents of this matrix with another.

Exchanges the contents of this matrix with those of mat in constant time O(1). No elements are copied or moved individually.

Parameters
[in,out]matThe matrix to swap with.
Exceptions
Whatthe swap of the default values throws; nothing when it is noexcept. It goes first, so that if it throws, the dimensions and the entries stay where they were. It is the swap of T found by its arguments, if it has one, and std::swap() otherwise.

Definition at line 212 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::default_value, Aleph::DynMatrix< T >::m, Aleph::DynMatrix< T >::n, and Aleph::DynMatrix< T >::swap().

Referenced by Aleph::DynMatrix< T >::DynMatrix(), Aleph::DynMatrix< T >::operator=(), Aleph::DynMatrix< T >::swap(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ transpose()

template<typename T >
DynMatrix< T > Aleph::DynMatrix< T >::transpose ( ) const
inline

Create a transposed copy of the matrix.

Returns a new matrix where entry (i,j) contains this->read(j,i).

Returns
The transposed matrix.
Exceptions
std::bad_allocIf memory allocation fails.

Definition at line 605 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::default_value, Aleph::DynMatrix< T >::get_index(), Aleph::DynMatrix< T >::m, Aleph::DynMatrix< T >::n, Aleph::DynMatrix< T >::read_array_entry(), and Aleph::DynMatrix< T >::write().

Referenced by TEST().

◆ traverse() [1/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool Aleph::DynMatrix< T >::traverse ( Operation &&  operation)
inline

See traverse(Operation & operation).

Definition at line 755 of file tpl_dynMat.H.

◆ traverse() [2/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool Aleph::DynMatrix< T >::traverse ( Operation &&  operation) const
inline

See traverse(Operation & operation).

Definition at line 747 of file tpl_dynMat.H.

◆ traverse() [3/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
Aleph::DynMatrix< T >::traverse ( Operation &  operation)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 739 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::traverse().

◆ traverse() [4/4]

template<typename T >
template<class Operation >
requires Aleph::PredicateWith<Operation &, const T &>
bool Aleph::DynMatrix< T >::traverse ( Operation &  operation) const
inline

Traverse all elements in row-major order.

Executes operation on each element. The traversal stops early if operation returns false.

Template Parameters
OperationA callable with signature bool(const T&).
Parameters
[in]operationThe operation to perform.
Returns
true if all elements were visited, false if stopped early.

Definition at line 728 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::m, Aleph::DynMatrix< T >::n, and Aleph::DynMatrix< T >::read_array_entry().

Referenced by Aleph::DynMatrix< T >::traverse().

◆ traverse_allocated() [1/2]

template<typename T >
template<class Operation >
bool Aleph::DynMatrix< T >::traverse_allocated ( Operation &&  operation)
inline

Traverse entries in allocated memory blocks.

Visits all entries within allocated memory blocks. This is more efficient than traverse() when the matrix is very sparse, but note that it visits all entries in each allocated block, not just those that were explicitly written.

Template Parameters
OperationA callable with signature bool(T&).
Parameters
[in]operationThe operation to perform.
Returns
true if all entries were visited, false if stopped early.
Note
Due to the block-based allocation of DynArray, this function may visit entries that contain the default value but happen to be in the same block as a written entry.

Definition at line 776 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::m, and Aleph::DynMatrix< T >::n.

◆ traverse_allocated() [2/2]

template<typename T >
template<class Operation >
bool Aleph::DynMatrix< T >::traverse_allocated ( Operation &&  operation) const
inline

See traverse_allocated(Operation && operation).

Definition at line 794 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr, Aleph::DynMatrix< T >::m, and Aleph::DynMatrix< T >::n.

◆ write() [1/2]

template<typename T >
T & Aleph::DynMatrix< T >::write ( const size_t  i,
const size_t  j,
const T &  data 
)
inline

Write a value to position (i, j).

Sets the value at row i and column j. Allocates memory for the entry if not already allocated.

Time complexity: O(1) amortized.

Parameters
[in]iRow index (0-based).
[in]jColumn index (0-based).
[in]dataThe value to write.
Returns
A modifiable reference to the written value.
Exceptions
std::out_of_rangeIf i >= rows() or j >= cols().
std::bad_allocIf memory allocation fails.

Definition at line 514 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::check_indices(), Aleph::DynMatrix< T >::get_index(), and Aleph::DynMatrix< T >::write_array_entry().

Referenced by FilledMatrix::FilledMatrix(), SquareMatrix::SquareMatrix(), demo_modular_linalg(), main(), Moved_From_Case< Hungarian_Assignment< long > >::make(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), and Aleph::DynMatrix< T >::transpose().

◆ write() [2/2]

template<typename T >
T & Aleph::DynMatrix< T >::write ( const size_t  i,
const size_t  j,
T &&  data 
)
inline

See write(const size_t i, const size_t j, const T & data).

Definition at line 521 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::check_indices(), Aleph::DynMatrix< T >::get_index(), and Aleph::DynMatrix< T >::write_array_entry().

◆ write_array_entry() [1/2]

template<typename T >
T & Aleph::DynMatrix< T >::write_array_entry ( const size_t  i,
const T &  data 
)
inlineprivate

◆ write_array_entry() [2/2]

template<typename T >
T & Aleph::DynMatrix< T >::write_array_entry ( const size_t  i,
T &&  data 
)
inlineprivate

Definition at line 142 of file tpl_dynMat.H.

References Aleph::DynMatrix< T >::array_ptr.

Member Data Documentation

◆ array_ptr

◆ default_value

◆ m

◆ n


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