Sparse Vector

template<Number ElemType, DataLocation Location, template<typename, typename> typename Storage>
class SparseVector : public HighFM::MatrixBase<SparseVector<ElemType, Location, Storage>>

An object representing a sparse row vector in Linear Algebra. Depending on the Storage parameter, this object will be referred either as SparseVectorMap (when Storage is set to DataMap) or as just SparseVector (when Storage is set to Array).

An instance of this class is returned when calling the mat(r, All) method, and usually, this is the only way it is used.

Template Parameters:
  • ElemType – Data types of the entries. Must be either a double or float.

  • Location – Location of the data

  • Storage – The container to store the data

Constructor

SparseVector()

Creates an empty SparseVector.

template<DataLocation OtherLoc, template<typename, typename> typename OtherStorage>
SparseVector(const SparseVector<ElemType, OtherLoc, OtherStorage> &other)

Creates a SparseVector from an other SparseVector.

Parameters:

other[in] another SparseVector to use as the data source.

SparseVector(SparseVectorData<ElemType> data)

Maps the SparseVector to an external container when using DataMap as Storage. Otherwise, creates a new SparseVector and then copies the data from the external container.

Parameters:

data[in] struct containing the information about the container

SparseVector(const SparseVector &other)

Creates a SparseVector from an other SparseVector. When using DataMap as Storage, this will just duplicate the map without copying the data.

Parameters:

other[in] another SparseVector to use as the data source.

template<ResizableContainer S = storage_value>
SparseVector(SparseVector &&other) noexcept

Creates a new SparseVector and then moves the content from an other SparseVector.

Warning

This constructor is only available when using Array as Storage.

Parameters:

other[in] another SparseVector to use as the data source.

Destructor

virtual ~SparseVector() = default

Default destructor.

Assignment

SparseVector &operator=(const SparseVector &other)

Copies the content from an other SparseVector.

Parameters:

other[in] another SparseVector to use as the data source.

Returns:

*this.

template<DataLocation OtherLoc, template<typename, typename> typename OtherStorage>
SparseVector &operator=(const SparseVector<ElemType, OtherLoc, OtherStorage> &other)

Copies the content from an other SparseVector.

Parameters:

other[in] another SparseVector to use as the data source.

Returns:

*this.

template<ResizableContainer S = storage_value>
SparseVector &operator=(SparseVector &&other) noexcept

Moves the content from an other SparseVector to this container.

Warning

This constructor is only available when using Array as Storage.

Parameters:

other[in] another SparseVector to use as the data source.

Returns:

*this.

Element Access

value_type &operator[](index_t idx)

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

Parameters:

idx[in] index of the nonzero element to return

Returns:

Reference to requested element

value_type operator[](index_t idx) const

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

Parameters:

idx[in] position of the nonzero element to return

Returns:

The requested element

SparseVectorEntry<value_type> nonzero_at(index_t idx) const

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

Parameters:

idx[in] position of the nonzero element to return

Returns:

a SparseVectorEntry object containing the column and the value of the nonzero element.

index_t index_at(index_t idx) const

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

Returns:

the index of requested element

Capacity

index_t rows() const noexcept
Returns:

The number of rows in the SparseVector.

index_t cols() const noexcept
Returns:

The number of columns in the SparseVector

index_t size() const noexcept
Returns:

the number of nonzeros entries in the SparseVector

template<ResizableContainer S = storage_value>
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 content from SparseVector.

Note

This routine is only available when using Array as Storage.

Parameters:

nonzeros[in] reserved size.

template<ResizableContainer S = storage_value>
void clear() noexcept

Clears all the nonzero entries of the SparseVector. This routine does not change the size of the vector.

Note

This routine is only available when using Array as Storage.

template<ResizableContainer S = storage_value>
void resize(index_t new_size)

Resizes the SparseVector, changing its size to new_size. This routine will clear the vector content.

Note

This routine is only available when using Array as Storage.

Parameters:

new_size[in] new size of the container

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.

storage_index &indexes() noexcept
Returns:

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

const storage_index &indexes() const noexcept
Returns:

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

storage_value &values() noexcept
Returns:

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

const storage_value &values() const noexcept
Returns:

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

Modifiers

void swap(SparseVector &other) noexcept

Swaps the SparseVector content with other, including any memory allocation (if applicable).

Parameters:

other[inout] another SparseVector to exchange content with.

template<RemappableContainer S = storage_value>
void map_to(SparseVectorData<ElemType> data)

Replaces the managed container.

Note

This routine is only available when using DataMap as Storage.

Parameters:

data[in] struct containing the information about the container

template<template<typename, typename> typename OtherStorage, RemappableContainer S = storage_value>
void map_to(const SparseVector<ElemType, Location, OtherStorage> &other)

Replaces the managed container.

Note

This routine is only available when using DataMap as Storage.

Parameters:

other[in] struct containing the information about the container

SparseVectorData<ElemType> mat_data() const noexcept
Returns:

a simple struct with a pointer to the container and its dimensions.

template<typename T>
struct SparseVectorData

A simple struct storing the data of a dense vector.

Public Members

index_t size

The size of the vector.

index_t nnz

The number of nonzeros in the vector.

index_t *nz_indexes

An array with the index of each nonzero entry in the vector.

T *nz_values

An array with the values of each nonzero entry in the vector.