MTL5 -- Matrix Template Library C++ API
mtl:: -- C++20 header-only linear algebra for mixed-precision algorithm design
Loading...
Searching...
No Matches
mtl::sparse_vector< Value, Parameters > Class Template Reference

Sparse vector stored as dual sorted arrays (indices + values). More...

#include <sparse_vector.hpp>

Classes

class  const_iterator

Public Types

using param_type = Parameters
using orientation = typename Parameters::orientation
using size_type = typename Parameters::size_type
using value_type = Value
using reference = Value&
using const_reference = const Value&

Public Member Functions

 sparse_vector ()
 sparse_vector (size_type n)
size_type size () const
size_type nnz () const
bool empty () const
size_type num_rows () const
 Orientation-aware dimensions (same pattern as dense_vector).
size_type num_cols () const
value_type operator() (size_type i) const
reference operator[] (size_type i)
bool exists (size_type i) const
void insert (size_type i, const Value &v)
void clear ()
void reserve (size_type nnz)
 Reserve storage for nnz entries, so that many inserts don't reallocate.
void crop (magnitude_t< Value > threshold)
 Drops the entries whose magnitude is below threshold, in place.
const std::vector< size_type > & indices () const
const std::vector< Value > & values () const
const_iterator begin () const
const_iterator end () const

Detailed Description

template<typename Value, typename Parameters = parameters<>>
class mtl::sparse_vector< Value, Parameters >

Sparse vector stored as dual sorted arrays (indices + values).

Indices are maintained in sorted order for O(log n) lookup.

Index checks (#830), by the "check once" policy of #825/#826:

  • insert() and operator[], which store an index, check it in every build and throw std::out_of_range (through detail::throw_or_abort, so a -fno-exceptions build aborts). There is no finalize step to check once at, so each insertion checks: one comparison next to the O(log nnz) search.
  • operator(), a read, checks only when bounds_checking is on, as the other containers' element access does.
  • exists() returns false for an index outside the vector. A signed size_type rejects negative indices too.

Member Typedef Documentation

◆ const_reference

template<typename Value, typename Parameters = parameters<>>
using mtl::vec::sparse_vector< Value, Parameters >::const_reference = const Value&

◆ orientation

template<typename Value, typename Parameters = parameters<>>
using mtl::vec::sparse_vector< Value, Parameters >::orientation = typename Parameters::orientation

◆ param_type

template<typename Value, typename Parameters = parameters<>>
using mtl::vec::sparse_vector< Value, Parameters >::param_type = Parameters

◆ reference

template<typename Value, typename Parameters = parameters<>>
using mtl::vec::sparse_vector< Value, Parameters >::reference = Value&

◆ size_type

template<typename Value, typename Parameters = parameters<>>
using mtl::vec::sparse_vector< Value, Parameters >::size_type = typename Parameters::size_type

◆ value_type

template<typename Value, typename Parameters = parameters<>>
using mtl::vec::sparse_vector< Value, Parameters >::value_type = Value

Constructor & Destructor Documentation

◆ sparse_vector() [1/2]

template<typename Value, typename Parameters = parameters<>>
mtl::vec::sparse_vector< Value, Parameters >::sparse_vector ( )
inline

◆ sparse_vector() [2/2]

template<typename Value, typename Parameters = parameters<>>
mtl::vec::sparse_vector< Value, Parameters >::sparse_vector ( size_type n)
inlineexplicit

Member Function Documentation

◆ begin()

template<typename Value, typename Parameters = parameters<>>
const_iterator mtl::vec::sparse_vector< Value, Parameters >::begin ( ) const
inline

◆ clear()

template<typename Value, typename Parameters = parameters<>>
void mtl::vec::sparse_vector< Value, Parameters >::clear ( )
inline

◆ crop()

template<typename Value, typename Parameters = parameters<>>
void mtl::vec::sparse_vector< Value, Parameters >::crop ( magnitude_t< Value > threshold)
inline

Drops the entries whose magnitude is below threshold, in place.

The threshold is a magnitude (the real type, for a complex Value). A NaN entry is kept: it is not below any threshold, and dropping it would hide it (#830). Taken by value: a reference could alias an entry, as in v.crop(v[1]), which the compaction overwrites.

◆ empty()

template<typename Value, typename Parameters = parameters<>>
bool mtl::vec::sparse_vector< Value, Parameters >::empty ( ) const
inline

◆ end()

template<typename Value, typename Parameters = parameters<>>
const_iterator mtl::vec::sparse_vector< Value, Parameters >::end ( ) const
inline

◆ exists()

template<typename Value, typename Parameters = parameters<>>
bool mtl::vec::sparse_vector< Value, Parameters >::exists ( size_type i) const
inline

◆ indices()

template<typename Value, typename Parameters = parameters<>>
const std::vector< size_type > & mtl::vec::sparse_vector< Value, Parameters >::indices ( ) const
inline

◆ insert()

template<typename Value, typename Parameters = parameters<>>
void mtl::vec::sparse_vector< Value, Parameters >::insert ( size_type i,
const Value & v )
inline

◆ nnz()

template<typename Value, typename Parameters = parameters<>>
size_type mtl::vec::sparse_vector< Value, Parameters >::nnz ( ) const
inline

◆ num_cols()

template<typename Value, typename Parameters = parameters<>>
size_type mtl::vec::sparse_vector< Value, Parameters >::num_cols ( ) const
inline

◆ num_rows()

template<typename Value, typename Parameters = parameters<>>
size_type mtl::vec::sparse_vector< Value, Parameters >::num_rows ( ) const
inline

Orientation-aware dimensions (same pattern as dense_vector).

◆ operator()()

template<typename Value, typename Parameters = parameters<>>
value_type mtl::vec::sparse_vector< Value, Parameters >::operator() ( size_type i) const
inline

◆ operator[]()

template<typename Value, typename Parameters = parameters<>>
reference mtl::vec::sparse_vector< Value, Parameters >::operator[] ( size_type i)
inline

◆ reserve()

template<typename Value, typename Parameters = parameters<>>
void mtl::vec::sparse_vector< Value, Parameters >::reserve ( size_type nnz)
inline

Reserve storage for nnz entries, so that many inserts don't reallocate.

Out-of-order insertion still moves the later entries.

◆ size()

template<typename Value, typename Parameters = parameters<>>
size_type mtl::vec::sparse_vector< Value, Parameters >::size ( ) const
inline

◆ values()

template<typename Value, typename Parameters = parameters<>>
const std::vector< Value > & mtl::vec::sparse_vector< Value, Parameters >::values ( ) const
inline

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