Vector

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

One of the basic types within MOCCA. The Vector class represents either a column vector (i.e., the elements are arranged as one column) or a row vector (i.e., the elements are arranged as one row) in Linear Algebra.

Storage-wise, both row and column vector are stored as a plain contiguous Array. For convenience, the row vector can be referred as RowVector and column vector, as just Vector.

See Vector in Basic Datatypes for more information.

Template Parameters:

  • ElemType - Numeric type of the elements in the vector (i.e., any integral, floating-point or std::complex types).

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

Constructors

Vector()

Default Constructor. Creates an empty Vector.

Complexity: Constant

Vector(index_t size)

Creates a Vector and fill_sorted_by_col with zeros.

Complexity: Constant

Parameters:

size[in] vector size

Vector(index_t size, ElemType val)

Creates a Vector and then fill it with val.

Complexity: Linear in size

Parameters:
  • size[in] vector size

  • val[in] value to initialize the vector with

Vector(std::initializer_list<ElemType> ilist)

Creates a Vector from an initializer list.

Complexity: Linear in ilist size

Parameters:

ilist[in] initializer list to use as data source

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

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

Complexity: Linear in other.size()

Parameters:

other[in] another Vector to use as data source

Vector(const Vector &other)

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

Complexity: Linear in other.size()

Parameters:

other[in] another Vector to use as data source

Vector(Vector &&other) noexcept

Move Constructor. Creates a Vector and then move the content from other Vector, 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 Vector to use as data source

template<typename InIter, internal::require_input_iterator<InIter> = true>
Vector(InIter first, InIter last)

Creates a Vector from range [first, last[.

Complexity: Linear in distance between first and last

Parameters:
  • first[in] iterator to the first element in the range

  • last[in] iterator to the “past-the-end” element in the range

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

Creates a Vector from a vector expression.

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

Parameters:

expr[in] a vector expressionW

Throws:

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

Destructor

virtual ~Vector() = default

Default destructor.

Assignment

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

Copy Assignment Operator. Replaces Vector’s content with a copy of the contents of other Vector. This routine will automatically reallocate the container if needed.

Complexity: Linear in other.size()

Parameters:

other[in] another Vector to use as data source

Returns:

*this

Vector &operator=(const Vector &other)

Copy Assignment Operator. Replaces Vector’s content with a copy of the contents of other Vector. This routine will automatically reallocate the container if needed.

Complexity: Linear in other.size()

Parameters:

other[in] another Vector to use as data source

Returns:

*this

Vector &operator=(Vector &&other) noexcept

Move Assignment Operator. Replaces Vector’s content with a the contents of other Vector, 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 vector to use as data source

Returns:

*this

Vector &operator=(std::initializer_list<ElemType> ilist)

Copy Assignment Operator. Replaces Vector’s content with the values defined in the initializer list. This routine automatically resizes the Vector to match the ilist.size().

Complexity: Linear in ilist size

Parameters:

ilist[in] initializer list to use as data source

Returns:

*this

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

Evaluates an expression and assign the results to the Vector. This routine automatically resizes the Vector to match the expression size. The resizing can be disabled by setting MOCCA_AUTO_RESIZE to 0.

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

Parameters:

expr[in] a vector expression

Returns:

*this

Element Access

ElemType &operator()(index_t idx)

Accesses element at idx position.

Complexity: Constant

Parameters:

idx[in] position of the element to return

Returns:

a reference to requested element

ElemType operator()(index_t idx) const

Accesses element at idx position.

Complexity: Constant

Parameters:

idx[in] position of the element to return

Returns:

the requested element

Capacity and Modifiers

constexpr index_t rows() const noexcept

Complexity: Constant

Returns:

the number of rows in the Vector

constexpr index_t cols() const noexcept

Complexity: Constant

Returns:

the number of columns in the Vector

constexpr index_t size() const noexcept

Complexity: Constant

Returns:

the number of entries in the Vector

void resize(index_t new_size)

Resizes the container to new_size. This routine will clear the previous content from the vector and reallocate the container if needed.

Replaces Array::resize().

Complexity: Constant

Parameters:

new_size[in] new size of the container

Throws:

std::length_error – if `new_size < 0`

void resize(index_t new_size, ElemType val)

Resizes the container to new_size and then set all elements to val. This routine will clear the previous content from the vector and reallocate the container if needed.

Replaces Array::resize().

Complexity: Constant

Parameters:

new_size[in] new size of the container

Throws:

std::length_error – if `new_size < 0`

void safe_resize(index_t new_size)

Resizes the container to new_size. This routines preserves the original content of the Vector.

Replaces Array::resize().

Complexity: Linear in size()

Parameters:

new_size[in] new size of the container

Throws:

std::length_error – if `new_size < 0`

void safe_resize(index_t new_size, ElemType val)

Resizes the container to new_size and then set all new elements to val. This routines preserves the original content of the Vector.

Replaces Array::resize().

Complexity: Linear in size()

Parameters:

new_size[in] new size of the container

Throws:

std::length_error – if `new_size < 0`

void reshape(index_t size)

Reshape the Vector without changing its content.

Warning

The new shape of the vector must not exceed the allocated space.

Parameters:

size[in] new size

Throws:

std::length_error – if the ``size`` is less than 1 or ``size > capacity()``. Complexity: Constant

void swap(Vector &other) noexcept

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

Complexity: Constant

Parameters:

other[inout] another Vector to exchange content with

Friends

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

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

Complexity: Linear in this->size()

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

  • expr[in] CSRMatrix

Returns:

stream