Sparse Vector

template<typename ElemType, internal::TypeLayout Layout>
class SparseVector : public mocca::MatrixBase<SparseVector<ElemType, Layout>>

One of the basic types of MOCCA. SparseVector represents a column or row vector in the sparse format, storing only the non-zero elements. For convenience, a row vector can be referenced as SparseRowVector. By default, a SparseVector is a column vector.

See Sparse Vector in Basic Datatypes for more information.

Template Parameters:

  • ElemType - Type of the nonzero elements

  • Layout - Either kRowMajor(row vector) or kColMajor(column vector).

Assignment

SparseVector &operator=(const SparseVector &other)

Copy Assignment Operator. Replaces the container’s content with the contents of other.

Complexity: Linear in other.size()

Parameters:

other[in] another SparseVector to use as data source

Returns:

*this

template<typename T, internal::TypeLayout L>
SparseVector &operator=(const SparseVector<T, L> &other)

Copy Assignment Operator. Replaces the container’s content with the contents of other.

Complexity: Linear in other.size()

Parameters:

other[in] another SparseVector to use as data source

Returns:

*this

SparseVector &operator=(SparseVector &&other) noexcept

Move Assignment Operator. Replaces the container’s content with the contents of other, following the move semantics (i.e., the data is moved from other to this container). other is valid but left in a unspecified state afterwards.

Complexity: Constant

Parameters:

other[in] another SparseVector to use as data source

Returns:

*this

template<typename E>
SparseVector &operator=(const MatrixBase<E> &expr)

Evaluates the vector expression and assign the result to the SparseVector. This routine automatically resizes the SparseVector to match the expression dimensions.

Complexity: Linear in expr.size()

Parameters:

expr[in] a vector expression

Throws:

std::length_error – if the dimensions of the operands in``expr`` do not match and cannot be broadcasted.

Returns:

*this

template<typename Mat>
SparseVector &operator=(const CSRow<Mat> &csr_row)

Copy the contents of a CSRow to this container

Complexity: Linear in expr.size()

Parameters:

csr_row[in] a CSRow to use as data source

Returns:

*this

Internal Buffers

Warning

These routines aims to provide interoperability to other libraries. Changing the properties of the internal buffers (e.g., its size) may result in undefined behaviour.

index_t *indexes_ptr() const noexcept

Complexity: Constant

Returns:

a raw pointer to the columns/rows buffer (i.e., buffer containing the column/row of each nonzero element in the SparseVector).

ElemType *nonzeros_ptr() const noexcept

Complexity: Constant

Returns:

a raw pointer to buffer with values of each nonzero element in the SparseVector.

const Array<index_t> &indexes() const noexcept

Complexity: Constant

Returns:

the internal buffer containing the column/row of each nonzero element in the SparseVector.

const Array<ElemType> &nonzeros() const noexcept

Complexity: Constant

Returns:

the internal buffer with values of each nonzero element in the SparseVector.

Iterator

iterator begin() noexcept

Complexity: Constant

Returns:

an iterator to the begin of the SparseVector

iterator end() noexcept

Complexity: Constant

Returns:

an iterator to past-the-end element of the SparseVector

const_iterator cbegin() const noexcept

Complexity: Constant

Returns:

a const iterator to the begin of the SparseVector

const_iterator cend() const noexcept

Complexity: Constant

Returns:

a const iterator to past-the-end element of the SparseVector

Element Access

ElemType &operator[](index_t nz_idx)

Accesses the nonzero element at (nz_idx) position. In this case, the indexing only includes nonzeros elements.

Complexity: Constant

Returns:

a reference to the value of the requested element

ElemType operator[](index_t nz_idx) const

Accesses the nonzero element at (nz_idx) position. In this case, the indexing only includes nonzeros elements.

Complexity: Constant

Returns:

the requested element

std::pair<index_t, ElemType> nonzero_at(index_t nz_idx) const

Accesses the nonzero element at (nz_idx). In this case, the indexing only includes nonzeros elements.

Complexity: Constant

Returns:

a std::pair containing the element value and index

index_t index_at(index_t pos) const

Accesses the index of the nonzero element at (pos) position. In this case, the indexing only includes nonzeros elements.

Complexity: Constant

Returns:

the index of requested element

ElemType operator()(index_t idx) const

Accesses the SparseVector element at (idx).

Complexity: O(log(size())) (binary search)

Warning

This routine do not create new elements. Modifying the value of a zero entry will have no effect on the container. To create a new nonzero entry, use either insert() or at_ref().

Parameters:

idx[in] index of the requested element

Returns:

the requested element

ElemType at(index_t idx) const

Accesses the SparseVector element at (idx).

Complexity: O(log(size())) (binary search)

Warning

This routine do not create new elements. Modifying the value of a zero entry will have no effect on the container. To create a new nonzero entry, use either insert() or at_ref().

Parameters:

idx[in] index of the requested element

Returns:

the requested element

ElemType &at_ref(index_t idx)

Accesses the SparseVector element at (idx).

Complexity: O(log(size())) (binary search), plus the cost of insert(), if the element do not exist.

Note

If the element do not exists in the container, this routine will insert a new element in (row) and then return a reference to the newly created entry.

Parameters:

idx[in] index of the requested element

Returns:

a reference to the value of the requested element

Capacity

constexpr index_t rows() const noexcept

Complexity: Constant

Returns:

the number of rows in the SparseVector

constexpr index_t cols() const noexcept

Complexity: Constant

Returns:

the number of columns in the SparseVector

constexpr index_t size() const noexcept

Complexity: Constant

Returns:

the number of nonzeros entries in the SparseVector

void reserve(index_t nonzeros)

Reserves space in memory for nonzeros elements, clearing the SparseVector content. If nonzeros is less than the allocated capacity, this routine only clears the SparseVector content.

Complexity: Constant

Parameters:

nonzeros[in] reserved size

Modifiers

void clear() noexcept

Clears all the nonzero entries of the SparseVector.

Complexity: Constant

void resize(index_t dim_size)

Resizes the SparseVector, clearing all the content.

Complexity: Linear in nrows.

Parameters:

dim_size[in] new dimension size

void insert(index_t idx, ElemType val)

Inserts a new nonzero element at (row, col). This element must not exist on the SparseVector.

Complexity: Constant if the elements are inserted in order and the container is properly allocated. Linear in size(), otherwise.

Parameters:
  • idx[in] index of the inserted element

  • val[in] value of the inserted element

void swap(SparseVector &other)

Swaps the SparseVector content with other, including any memory allocation.

Complexity: Constant

Parameters:

other – another SparseVector to exchange content with

Public Functions

SparseVector()

Default Constructor. Creates an empty SparseVector.

Complexity: Constant

SparseVector(index_t dim_size)

Creates a SparseVector and fill with zeros.

Complexity: Constant

Parameters:

dim_size[in] vector size

SparseVector(index_t dim_size, index_t nz)

Creates a SparseVector and reserves space in the container for nz entries.

Complexity: Constant

Note

This constructor only reserves memory space, but does not initialise the nonzero entries. Use either fill, insert or the at_ref routines to assign values to these entries.

Parameters:
  • dim_size[in] vector size

  • nz[in] reserved size for non-zeros elements

template<typename T, internal::TypeLayout L>
SparseVector(const SparseVector<T, L> &other)

Copy Constructor. Creates a SparseVector and then copy the content from other.

Complexity: Linear in other.size()

Parameters:

other[in] another SparseVector to use as data source

SparseVector(const SparseVector &other)

Copy Constructor. Creates a SparseVector and then copy the content from other.

Complexity: Linear in other.size()

Parameters:

other[in] another SparseVector to use as data source

SparseVector(SparseVector &&other) noexcept

Move Constructor. Creates a SparseVector and then move the content from other, following the move semantics (i.e., the data is moved from other to this container). other is valid but left in a unspecified state afterwards.

Complexity: Constant

Parameters:

other[in] another SparseVector to use as data source

template<typename E>
SparseVector(const MatrixBase<E> &expr)

Creates a SparseVector from a vector expression.

Complexity: Linear in this->size() (Typical)

Parameters:

expr[in] a vector expression

Throws:

std::length_error – if ``expr`` have more than one dimension

template<typename Mat>
SparseVector(const CSRow<Mat> &csr_row)

Creates a SparseVector from a CSRow;

Complexity: Linear in this->size() (Typical)|

Parameters:

csr_row[in] a CSRow to use as data source

virtual ~SparseVector() = default

Default destructor.

Friends

template<typename T, internal::TypeLayout L>
friend std::ostream &operator<<(std::ostream &stream, const SparseVector<T, L> &vec)

Stream Operator. Writes the content of the SparseVector on a given stream.

Complexity: Linear in this->size()

Parameters:
  • stream[in] output stream (e.g., std::cout)

  • expr[in] CSRMatrix

Returns:

stream