PlayRho  2.0.0
An interactive physics engine & library.
playrho::Vector< T, N > Struct Template Reference

Vector. More...

#include <playrho/Vector.hpp>

Inheritance diagram for playrho::Vector< T, N >:
[legend]

Public Types

using const_iterator = const value_type *
 Constant iterator type.
 
using const_pointer = const value_type *
 Constant pointer type.
 
using const_reference = const value_type &
 Constant reference type.
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 Constant reverse iterator type.
 
using difference_type = std::ptrdiff_t
 Difference type.
 
using iterator = value_type *
 Iterator type.
 
using pointer = value_type *
 Pointer type.
 
using reference = value_type &
 Reference type.
 
using reverse_iterator = std::reverse_iterator< iterator >
 Reverse iterator type.
 
using size_type = std::size_t
 Size type.
 
using value_type = T
 Value type.
 

Public Member Functions

constexpr Vector ()=default
 Default constructor. More...
 
template<typename... Tail>
constexpr Vector (std::enable_if_t< sizeof...(Tail)+1==N, T > head, Tail... tail) noexcept
 Initializing constructor.
 
constexpr reference at (size_type pos)
 Gets a reference to the requested element. More...
 
constexpr const_reference at (size_type pos) const
 Gets a constant reference to the requested element. More...
 
const_iterator begin () const noexcept
 Gets a "begin" iterator.
 
iterator begin () noexcept
 Gets a "begin" iterator.
 
const_iterator cbegin () const noexcept
 Gets a "begin" iterator.
 
const_iterator cend () const noexcept
 Gets an "end" iterator.
 
const_reverse_iterator crbegin () const noexcept
 Gets a reverse "begin" iterator.
 
const_reverse_iterator crend () const noexcept
 Gets a reverse "end" iterator.
 
constexpr const_pointer data () const noexcept
 Direct access to data.
 
constexpr pointer data () noexcept
 Direct access to data.
 
const_iterator end () const noexcept
 Gets an "end" iterator.
 
iterator end () noexcept
 Gets an "end" iterator.
 
constexpr const_reference operator[] (size_type pos) const noexcept
 Gets a constant reference to the requested element. More...
 
constexpr reference operator[] (size_type pos) noexcept
 Gets a reference to the requested element. More...
 
const_reverse_iterator rbegin () const noexcept
 Gets a reverse "begin" iterator.
 
reverse_iterator rbegin () noexcept
 Gets a reverse "begin" iterator.
 
const_reverse_iterator rend () const noexcept
 Gets a reverse "end" iterator.
 
reverse_iterator rend () noexcept
 Gets a reverse "end" iterator.
 

Static Public Member Functions

static constexpr bool empty () noexcept
 Whether empty. More...
 
static constexpr size_type max_size () noexcept
 Gets the max size.
 
static constexpr size_type size () noexcept
 Gets the size.
 

Public Attributes

value_type elements [N? N:1]
 Elements. More...
 

Related Functions

(Note that these are not member functions.)

template<typename T , std::size_t N>
constexpr auto abs (const Vector< T, N > &v) noexcept -> decltype(abs(T{}), Vector< T, N >{})
 Absolute value function for vectors.
 
template<std::size_t I, std::size_t N, typename T >
constexpr auto & get (Vector< T, N > &v) noexcept
 Gets the specified element of the given collection.
 
template<typename T , std::size_t N>
constexpr bool operator!= (const Vector< T, N > &lhs, const Vector< T, N > &rhs) noexcept
 Inequality operator.
 
template<std::size_t N, typename T1 , typename T2 , typename OT = decltype(T1{} * T2{})>
constexpr std::enable_if_t< IsMultipliableV< T1, T2 > &&!IsVectorV< T1 >, Vector< OT, N > > operator* (const T1 &s, const Vector< T2, N > &a) noexcept
 Multiplication operator for non-vector times vector. More...
 
template<std::size_t N, typename T1 , typename T2 , typename OT = decltype(T1{} * T2{})>
constexpr std::enable_if_t< IsMultipliableV< T1, T2 > &&!IsVectorV< T2 >, Vector< OT, N > > operator* (const Vector< T1, N > &a, const T2 &s) noexcept
 Multiplication operator for vector times non-vector. More...
 
template<typename T1 , typename T2 , std::size_t A, std::size_t B, std::size_t C, typename OT = decltype(T1{} * T2{})>
constexpr std::enable_if_t< IsMultipliableV< T1, T2 >, Vector< Vector< OT, C >, A > > operator* (const Vector< Vector< T1, B >, A > &lhs, const Vector< Vector< T2, C >, B > &rhs) noexcept
 Calculates the matrix product of the two given vector of vectors (matrices). More...
 
template<typename T1 , typename T2 , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T1, decltype(T1{} *T2{})>, Vector< T1, N > & > operator*= (Vector< T1, N > &lhs, const T2 rhs) noexcept
 Multiplication assignment operator.
 
template<typename T , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T, decltype(T{}+T{})>, Vector< T, N > > operator+ (Vector< T, N > lhs, const Vector< T, N > rhs) noexcept
 Adds two vectors component-wise.
 
template<typename T , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T, decltype(+T{})>, Vector< T, N > > operator+ (Vector< T, N > v) noexcept
 Unary plus operator.
 
template<typename T , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T, decltype(T{}+T{})>, Vector< T, N > & > operator+= (Vector< T, N > &lhs, const Vector< T, N > rhs) noexcept
 Increments the left hand side value by the right hand side value.
 
template<typename T , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T, decltype(T{} - T{})>, Vector< T, N > > operator- (Vector< T, N > lhs, const Vector< T, N > rhs) noexcept
 Subtracts two vectors component-wise.
 
template<typename T , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T, decltype(-T{})>, Vector< T, N > > operator- (Vector< T, N > v) noexcept
 Unary negation operator.
 
template<typename T , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T, decltype(T{} - T{})>, Vector< T, N > & > operator-= (Vector< T, N > &lhs, const Vector< T, N > rhs) noexcept
 Decrements the left hand side value by the right hand side value.
 
template<std::size_t N, typename T1 , typename T2 , typename OT = decltype(T1{} / T2{})>
constexpr std::enable_if_t< IsDivisableV< T1, T2 > &&!IsVectorV< T2 >, Vector< OT, N > > operator/ (const Vector< T1, N > &a, const T2 &s) noexcept
 Division operator.
 
template<typename T1 , typename T2 , std::size_t N>
constexpr std::enable_if_t< std::is_same_v< T1, decltype(T1{}/T2{})>, Vector< T1, N > & > operator/= (Vector< T1, N > &lhs, const T2 rhs) noexcept
 Division assignment operator.
 
template<std::size_t N0, class T0 , std::size_t N1, class T1 >
constexpr bool operator< (const Vector< T0, N0 > &lhs, const Vector< T1, N1 > &rhs) noexcept
 Less than operator. More...
 
template<typename T , std::size_t N>
std::ostream & operator<< (::std::ostream &os, const Vector< T, N > &value)
 Output stream operator.
 
template<typename T , std::size_t N>
constexpr bool operator== (const Vector< T, N > &lhs, const Vector< T, N > &rhs) noexcept
 Equality operator.
 

Detailed Description

template<typename T, std::size_t N>
struct playrho::Vector< T, N >

Vector.

A constexpr and constructor enhanced std::array-like template class for types supporting the + , - , * , and / arithmetic operators ("arithmetic types" as defined by the IsArithmetic type trait) that itself comes with non-member arithmetic operator support making Vector instances arithmetic types as well.

Note
This type is trivially default constructible - i.e. default construction performs no actions (no initialization).
See also
IsArithmetic, Vector Traits

Constructor & Destructor Documentation

◆ Vector()

template<typename T , std::size_t N>
constexpr playrho::Vector< T, N >::Vector ( )
constexprdefault

Default constructor.

Note
This constructor performs no action.

Member Function Documentation

◆ at() [1/2]

template<typename T , std::size_t N>
constexpr reference playrho::Vector< T, N >::at ( size_type  pos)
inlineconstexpr

Gets a reference to the requested element.

Exceptions
InvalidArgumentif given a position that's greater-than or equal-to size().

Referenced by playrho::d2::UnitVec::at().

◆ at() [2/2]

template<typename T , std::size_t N>
constexpr const_reference playrho::Vector< T, N >::at ( size_type  pos) const
inlineconstexpr

Gets a constant reference to the requested element.

Exceptions
InvalidArgumentif given a position that's greater-than or equal-to size().

◆ empty()

template<typename T , std::size_t N>
static constexpr bool playrho::Vector< T, N >::empty ( )
inlinestaticconstexprnoexcept

Whether empty.

Note
Always false for N > 0.

◆ operator[]() [1/2]

template<typename T , std::size_t N>
constexpr const_reference playrho::Vector< T, N >::operator[] ( size_type  pos) const
inlineconstexprnoexcept

Gets a constant reference to the requested element.

Note
No bounds checking is performed.
Parameters
posIndexing position into the vector to return reference for.
Precondition
pos is less-than size().

◆ operator[]() [2/2]

template<typename T , std::size_t N>
constexpr reference playrho::Vector< T, N >::operator[] ( size_type  pos)
inlineconstexprnoexcept

Gets a reference to the requested element.

Note
No bounds checking is performed.
Parameters
posIndexing position into the vector to return reference for.
Precondition
pos is less-than size().

Friends And Related Function Documentation

◆ operator*() [1/3]

template<std::size_t N, typename T1 , typename T2 , typename OT = decltype(T1{} * T2{})>
constexpr std::enable_if_t< IsMultipliableV< T1, T2 > &&!IsVectorV< T1 >, Vector< OT, N > > operator* ( const T1 &  s,
const Vector< T2, N > &  a 
)
related

Multiplication operator for non-vector times vector.

Note
Explicitly disabled for Vector * Vector to prevent this function from existing in that case and prevent errors like "use of overloaded operator '*' is ambiguous".

◆ operator*() [2/3]

template<std::size_t N, typename T1 , typename T2 , typename OT = decltype(T1{} * T2{})>
constexpr std::enable_if_t< IsMultipliableV< T1, T2 > &&!IsVectorV< T2 >, Vector< OT, N > > operator* ( const Vector< T1, N > &  a,
const T2 &  s 
)
related

Multiplication operator for vector times non-vector.

Note
Explicitly disabled for Vector * Vector to prevent this function from existing in that case and prevent errors like "use of overloaded operator '*' is ambiguous".

◆ operator*() [3/3]

template<typename T1 , typename T2 , std::size_t A, std::size_t B, std::size_t C, typename OT = decltype(T1{} * T2{})>
constexpr std::enable_if_t< IsMultipliableV< T1, T2 >, Vector< Vector< OT, C >, A > > operator* ( const Vector< Vector< T1, B >, A > &  lhs,
const Vector< Vector< T2, C >, B > &  rhs 
)
related

Calculates the matrix product of the two given vector of vectors (matrices).

Multiplies an A-by-B vector of vectors by a B-by-C vector of vectors returning an A-by-C vector of vectors.

Note
From Wikipedia:

Multiplication of two matrices is defined if and only if the number of columns of the left matrix is the same as the number of rows of the right matrix.

Matrix multiplication is not commutative.
Algorithmically speaking, this implementation is called the "naive" algorithm. For small matrices, like 3-by-3 or smaller matrices, its complexity shouldn't be an issue. The matrix dimensions are compile time constants anyway which can help compilers automatically identify loop unrolling and hardware level parallelism opportunities.
Parameters
lhsLeft-hand-side matrix.
rhsRight-hand-side matrix.
Returns
A-by-C matrix product of the left-hand-side matrix and the right-hand-side matrix.
See also
https://en.wikipedia.org/wiki/Matrix_multiplication
https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm
https://en.wikipedia.org/wiki/Commutative_property

◆ operator<()

template<std::size_t N0, class T0 , std::size_t N1, class T1 >
constexpr bool operator< ( const Vector< T0, N0 > &  lhs,
const Vector< T1, N1 > &  rhs 
)
related

Less than operator.

Note
Among other things, this also makes the Vector class usable with std::set.

Member Data Documentation

◆ elements

template<typename T , std::size_t N>
value_type playrho::Vector< T, N >::elements[N? N:1]

Elements.

Array of N elements unless N is 0 in which case this is an array of 1 element.

Warning
Don't access this directly!
Data is not initialized on default construction. This is intentional to avoid any performance overhead that default initialization might incur.

Referenced by playrho::Vector< T, N >::at(), playrho::Vector< T, N >::data(), and playrho::Vector< T, N >::operator[]().


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