simplevectors  0.3.9
Simple vector implementations in C++
Public Types | Public Member Functions | Protected Attributes | List of all members
svector::Vector< D, T > Class Template Reference

A base vector representation. More...

#include <vector.hpp>

Inheritance diagram for svector::Vector< D, T >:
Inheritance graph
[legend]

Public Types

typedef std::array< T, D >::iterator iterator
 An std::array::iterator.
 
typedef std::array< T, D >::const_iterator const_iterator
 An std::array::const_iterator.
 
typedef std::array< T, D >::reverse_iterator reverse_iterator
 An std::array::reverse_iterator.
 
typedef std::array< T, D >::const_reverse_iterator const_reverse_iterator
 An std::array::const_reverse_iterator.
 

Public Member Functions

 Vector ()
 No-argument constructor. More...
 
 Vector (const std::initializer_list< T > args)
 Initializes a vector given initializer list. More...
 
 Vector (const Vector< D, T > &other)
 Copy constructor. More...
 
 Vector (Vector< D, T > &&) noexcept=default
 Move constructor. More...
 
Vector< D, T > & operator= (const Vector< D, T > &other)
 Assignment operator. More...
 
Vector< D, T > & operator= (Vector< D, T > &&) noexcept=default
 Move assignment operator. More...
 
virtual ~Vector ()=default
 Destructor. More...
 
virtual std::string toString () const
 Returns string form of vector. More...
 
Vector< D, T > operator- () const
 Negative of a vector. More...
 
Vector< D, T > operator+ () const
 Positive of a vector. More...
 
Vector< D, T > & operator+= (const Vector< D, T > &other)
 In-place addition. More...
 
Vector< D, T > & operator-= (const Vector< D, T > &other)
 In-place subtraction. More...
 
Vector< D, T > & operator*= (const T other)
 In-place scalar multiplication. More...
 
Vector< D, T > & operator/= (const T other)
 In-place scalar division. More...
 
dot (const Vector< D, T > &other) const
 Dot product. More...
 
magn () const
 Magnitude. More...
 
Vector< D, T > normalize () const
 Normalizes a vector. More...
 
constexpr std::size_t numDimensions () const
 Gets the number of dimensions. More...
 
bool isZero () const
 Determines whether the current vector is a zero vector. More...
 
const T & operator[] (const std::size_t index) const
 Value of a certain component of a vector. More...
 
T & operator[] (const std::size_t index)
 Sets value of a certain component. More...
 
const T & at (const std::size_t index) const
 Value of a certain component of a vector. More...
 
T & at (const std::size_t index)
 Sets value of a certain component. More...
 
iterator begin () noexcept
 Iterator of first element. More...
 
const_iterator begin () const noexcept
 Const interator of first element. More...
 
iterator end () noexcept
 Interator of last element + 1. More...
 
const_iterator end () const noexcept
 Const interator of last element + 1. More...
 
reverse_iterator rbegin () noexcept
 Reverse iterator to last element. More...
 
const_reverse_iterator rbegin () const noexcept
 Const reverse iterator to last element. More...
 
reverse_iterator rend () noexcept
 Reverse iterator to first element - 1. More...
 
const_reverse_iterator rend () const noexcept
 Const reverse iterator to first element - 1. More...
 

Protected Attributes

std::array< T, D > m_components
 An array of components for the vector.
 

Detailed Description

template<std::size_t D, typename T = double>
class svector::Vector< D, T >

A base vector representation.

Note
The binary +, -, *, /, ==, and != operators are by default implemented in functions.hpp. To use the class implementation rather than the one in functions.hpp, define the variable SVECTOR_USE_CLASS_OPERATORS.
Template Parameters
DThe number of dimensions.
TVector type.

Constructor & Destructor Documentation

◆ Vector() [1/4]

template<std::size_t D, typename T = double>
svector::Vector< D, T >::Vector ( )
inline

No-argument constructor.

Initializes a zero vector (all components are 0).

◆ Vector() [2/4]

template<std::size_t D, typename T = double>
svector::Vector< D, T >::Vector ( const std::initializer_list< T >  args)
inline

Initializes a vector given initializer list.

The initializer list should represent the components of the vector in each dimension. If the size of the initializer list is greater than the number of dimensions given, then the vector only initializes with the first D elements in the list, where D is the number of dimensions. If the size of the initializer list is less than the number of dimensions given, then the vector fills the rest of the dimensions with the value 0.

Parameters
argsthe initializer list.

◆ Vector() [3/4]

template<std::size_t D, typename T = double>
svector::Vector< D, T >::Vector ( const Vector< D, T > &  other)
inline

Copy constructor.

Copies from another vector to an uninitialized vector.

◆ Vector() [4/4]

template<std::size_t D, typename T = double>
svector::Vector< D, T >::Vector ( Vector< D, T > &&  )
defaultnoexcept

Move constructor.

Uses C++ default move constructor.

◆ ~Vector()

template<std::size_t D, typename T = double>
virtual svector::Vector< D, T >::~Vector ( )
virtualdefault

Destructor.

Uses C++ default destructor.

Member Function Documentation

◆ at() [1/2]

template<std::size_t D, typename T = double>
T& svector::Vector< D, T >::at ( const std::size_t  index)
inline

Sets value of a certain component.

Sets a certain component of the vector given the dimension number.

Throws an out_of_range exception if the given number is out of bounds.

Parameters
indexThe dimension number.

◆ at() [2/2]

template<std::size_t D, typename T = double>
const T& svector::Vector< D, T >::at ( const std::size_t  index) const
inline

Value of a certain component of a vector.

Gets a reference to a specific component of the vector given the dimension number.

Throws an out_of_range exception if the given number is out of bounds.

Parameters
indexThe dimension number.
Returns
A constant reference to that dimension's component of the vector.

◆ begin() [1/2]

template<std::size_t D, typename T = double>
const_iterator svector::Vector< D, T >::begin ( ) const
inlinenoexcept

Const interator of first element.

Returns a constant iterator to the first dimension of the vector. This iterator will be equal to end() for a zero-dimension vector.

Returns
A constant iterator to the first dimension of the vector.

◆ begin() [2/2]

template<std::size_t D, typename T = double>
iterator svector::Vector< D, T >::begin ( )
inlinenoexcept

Iterator of first element.

Returns an iterator to the first dimension of the vector. This iterator will be equal to end() for a zero-dimension vector.

This can be used for looping through the dimensions of a vector.

Returns
An iterator to the first dimension of the vector.

◆ dot()

template<std::size_t D, typename T = double>
T svector::Vector< D, T >::dot ( const Vector< D, T > &  other) const
inline

Dot product.

Calculates the dot product of two vectors.

Note
The dimensions of the other vector must be the same as the current one.
Parameters
otherThe other vector to dot the current vector with.
Returns
A new vector representing the dot product of the two vectors.

◆ end() [1/2]

template<std::size_t D, typename T = double>
const_iterator svector::Vector< D, T >::end ( ) const
inlinenoexcept

Const interator of last element + 1.

Returns a constant iterator to the element following the last dimension of the vector.

This iterator is a placeholder and attempting to access it will result in undefined behavior.

Returns
A constant iterator to the element following the last dimension.

◆ end() [2/2]

template<std::size_t D, typename T = double>
iterator svector::Vector< D, T >::end ( )
inlinenoexcept

Interator of last element + 1.

Returns an iterator to the element following the last dimension of the vector.

This iterator is a placeholder and attempting to access it will result in undefined behavior.

This can be used for looping through the dimensions of a vector.

Returns
An iterator to the element following the last dimension.

◆ isZero()

template<std::size_t D, typename T = double>
bool svector::Vector< D, T >::isZero ( ) const
inline

Determines whether the current vector is a zero vector.

Returns
Whether the current vector is a zero vector.

◆ magn()

template<std::size_t D, typename T = double>
T svector::Vector< D, T >::magn ( ) const
inline

Magnitude.

Gets the magnitude of the vector.

Returns
The magnitude of the vector.

◆ normalize()

template<std::size_t D, typename T = double>
Vector<D, T> svector::Vector< D, T >::normalize ( ) const
inline

Normalizes a vector.

Finds the unit vector with the same direction angle as the current vector.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Returns
A new vector representing the normalized vector.

◆ numDimensions()

template<std::size_t D, typename T = double>
constexpr std::size_t svector::Vector< D, T >::numDimensions ( ) const
inlineconstexpr

Gets the number of dimensions.

Returns
Number of dimensions.

◆ operator*=()

template<std::size_t D, typename T = double>
Vector<D, T>& svector::Vector< D, T >::operator*= ( const T  other)
inline

In-place scalar multiplication.

Performs scalar multiplication on the current object.

Parameters
otherThe number to multiply by.

◆ operator+()

template<std::size_t D, typename T = double>
Vector<D, T> svector::Vector< D, T >::operator+ ( ) const
inline

Positive of a vector.

Creates new vector where the unary plus operator is applied to each component. In almost all cases, this returns the original vector.

Returns
The current vector.

◆ operator+=()

template<std::size_t D, typename T = double>
Vector<D, T>& svector::Vector< D, T >::operator+= ( const Vector< D, T > &  other)
inline

In-place addition.

Adds another vector object to the current object.

Parameters
otherThe other vector to add.

◆ operator-()

template<std::size_t D, typename T = double>
Vector<D, T> svector::Vector< D, T >::operator- ( ) const
inline

Negative of a vector.

Makes all components of the vector negative of what they currently are.

This can also be thought of flipping the direction of the vector.

Returns
A new vector representing the negative of the current vector.

◆ operator-=()

template<std::size_t D, typename T = double>
Vector<D, T>& svector::Vector< D, T >::operator-= ( const Vector< D, T > &  other)
inline

In-place subtraction.

Subtracts another vector object from the current object.

Parameters
otherThe other vector to subtract.

◆ operator/=()

template<std::size_t D, typename T = double>
Vector<D, T>& svector::Vector< D, T >::operator/= ( const T  other)
inline

In-place scalar division.

Performs scalar division on the current object.

Parameters
otherThe number to divide by.

◆ operator=() [1/2]

template<std::size_t D, typename T = double>
Vector<D, T>& svector::Vector< D, T >::operator= ( const Vector< D, T > &  other)
inline

Assignment operator.

Copies from another vector to a vector whose values already exist.

◆ operator=() [2/2]

template<std::size_t D, typename T = double>
Vector<D, T>& svector::Vector< D, T >::operator= ( Vector< D, T > &&  )
defaultnoexcept

Move assignment operator.

Uses C++ default move assignment operator.

◆ operator[]() [1/2]

template<std::size_t D, typename T = double>
T& svector::Vector< D, T >::operator[] ( const std::size_t  index)
inline

Sets value of a certain component.

Sets a certain component of the vector given the dimension number.

Parameters
indexThe dimension number.

◆ operator[]() [2/2]

template<std::size_t D, typename T = double>
const T& svector::Vector< D, T >::operator[] ( const std::size_t  index) const
inline

Value of a certain component of a vector.

Gets a reference to a specific component of the vector given the dimension number.

Parameters
indexThe dimension number.
Returns
A constant reference to that dimension's component of the vector.

◆ rbegin() [1/2]

template<std::size_t D, typename T = double>
const_reverse_iterator svector::Vector< D, T >::rbegin ( ) const
inlinenoexcept

Const reverse iterator to last element.

Returns a constant reverse iterator to the first dimension of the reversed vector. It corresponds to the last dimension of the original vector. The iterator will be equal to rend() for a zero-dimension vector.

Returns
A constant reverse iterator to the first dimension.

◆ rbegin() [2/2]

template<std::size_t D, typename T = double>
reverse_iterator svector::Vector< D, T >::rbegin ( )
inlinenoexcept

Reverse iterator to last element.

Returns a reverse iterator to the first dimension of the reversed vector. It corresponds to the last dimension of the original vector. The iterator will be equal to rend() for a zero-dimension vector.

This can be used for looping through the dimensions of a vector.

Returns
A reverse iterator to the first dimension.

◆ rend() [1/2]

template<std::size_t D, typename T = double>
const_reverse_iterator svector::Vector< D, T >::rend ( ) const
inlinenoexcept

Const reverse iterator to first element - 1.

Returns a constant reverse iterator to the element following the last dimension of the reversed vector. It corresponds to the element preceding the first dimension of the original vector.

This iterator is a placeholder and attempting to access it will result in undefined behavior.

Returns
A constant reverse iterator to the element following the last dimension.

◆ rend() [2/2]

template<std::size_t D, typename T = double>
reverse_iterator svector::Vector< D, T >::rend ( )
inlinenoexcept

Reverse iterator to first element - 1.

Returns a reverse iterator to the element following the last dimension of the reversed vector. It corresponds to the element preceding the first dimension of the original vector.

This iterator is a placeholder and attempting to access it will result in undefined behavior.

This can be used for looping through the dimensions of a vector.

Returns
A reverse iterator to the element following the last dimension.

◆ toString()

template<std::size_t D, typename T = double>
virtual std::string svector::Vector< D, T >::toString ( ) const
inlinevirtual

Returns string form of vector.

This string form can be used for printing.

Returns
The string form of the vector.

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