10 #ifndef INCLUDE_SVECTOR_BASEVECTOR_HPP_
11 #define INCLUDE_SVECTOR_BASEVECTOR_HPP_
18 #include <initializer_list>
20 #include <type_traits>
34 template <std::
size_t D,
typename T =
double>
class Vector {
37 static_assert(std::is_arithmetic<T>::value,
"Vector type must be numeric");
39 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
40 template <std::
size_t D1, std::
size_t D2,
typename T1,
typename T2>
43 template <std::
size_t D1, std::
size_t D2,
typename T1,
typename T2>
46 template <std::
size_t D1, std::
size_t D2,
typename T1,
typename T2>
49 template <std::
size_t D1, std::
size_t D2,
typename T1,
typename T2>
55 typedef typename std::array<T, D>::const_iterator
57 typedef typename std::array<T, D>::reverse_iterator
59 typedef typename std::array<T, D>::const_reverse_iterator
81 Vector(
const std::initializer_list<T> args) {
85 std::size_t counter = 0;
86 for (
const auto &num : args) {
102 for (std::size_t i = 0; i < D; i++) {
121 if (
this == &other) {
125 for (std::size_t i = 0; i < D; i++) {
154 std::string str =
"<";
155 for (std::size_t i = 0; i < D - 1; i++) {
166 #ifdef SVECTOR_USE_CLASS_OPERATORS
185 for (std::size_t i = 0; i < D; i++) {
208 Vector<D, T>
operator-(
const Vector<D, T> &other)
const {
210 for (std::size_t i = 0; i < D; i++) {
230 Vector<D, T>
operator*(
const T other)
const {
232 for (std::size_t i = 0; i < D; i++) {
252 Vector<D, T>
operator/(
const T other)
const {
254 for (std::size_t i = 0; i < D; i++) {
274 bool operator==(
const Vector<D, T> &other)
const {
275 for (std::size_t i = 0; i < D; i++) {
297 bool operator!=(
const Vector<D, T> &other)
const {
298 return !((*this) == other);
313 for (std::size_t i = 0; i < D; i++) {
330 for (std::size_t i = 0; i < D; i++) {
345 for (std::size_t i = 0; i < D; i++) {
360 for (std::size_t i = 0; i < D; i++) {
375 for (std::size_t i = 0; i < D; i++) {
390 for (std::size_t i = 0; i < D; i++) {
412 for (std::size_t i = 0; i < D; i++) {
427 T sum_of_squares = 0;
430 sum_of_squares += i * i;
433 return std::sqrt(sum_of_squares);
497 const T &
at(
const std::size_t index)
const {
632 #ifdef SVECTOR_EXPERIMENTAL_COMPARE
654 template <std::
size_t D2,
typename T2>
656 std::size_t min_dim = std::min(D, D2);
657 std::size_t counter = 0;
661 if constexpr (D != D2) {
666 return D < D2 ? -1 : 1;
670 for (std::size_t i = 0; i < min_dim; i++) {
671 if (this->m_components[i] == other[i]) {
673 }
else if (this->m_components[i] < other[i]) {
680 if (counter != D || counter != D2) {
bool operator==(const EmbVec2D &lhs, const EmbVec2D &rhs)
Compares equality of two vectors.
Definition: embed.h:470
bool operator!=(const EmbVec2D &lhs, const EmbVec2D &rhs)
Compares inequality of two vectors.
Definition: embed.h:482
EmbVec2D operator/(const EmbVec2D &lhs, const float rhs)
Scalar division.
Definition: embed.h:458
EmbVec2D operator*(const EmbVec2D &lhs, const float rhs)
Scalar multiplication.
Definition: embed.h:443
A base vector representation.
Definition: vector.hpp:34
std::array< T, D >::const_reverse_iterator const_reverse_iterator
An std::array::const_reverse_iterator.
Definition: vector.hpp:60
const T & at(const std::size_t index) const
Value of a certain component of a vector.
Definition: vector.hpp:497
Vector()
No-argument constructor.
Definition: vector.hpp:67
std::array< T, D >::const_iterator const_iterator
An std::array::const_iterator.
Definition: vector.hpp:56
Vector< D, T > operator+() const
Positive of a vector.
Definition: vector.hpp:328
reverse_iterator rend() noexcept
Reverse iterator to first element - 1.
Definition: vector.hpp:608
Vector(Vector< D, T > &&) noexcept=default
Move constructor.
reverse_iterator rbegin() noexcept
Reverse iterator to last element.
Definition: vector.hpp:577
T magn() const
Magnitude.
Definition: vector.hpp:426
Vector< D, T > & operator+=(const Vector< D, T > &other)
In-place addition.
Definition: vector.hpp:344
const_reverse_iterator rbegin() const noexcept
Const reverse iterator to last element.
Definition: vector.hpp:590
iterator begin() noexcept
Iterator of first element.
Definition: vector.hpp:522
std::array< T, D >::iterator iterator
An std::array::iterator.
Definition: vector.hpp:37
const_iterator end() const noexcept
Const interator of last element + 1.
Definition: vector.hpp:562
Vector< D, T > normalize() const
Normalizes a vector.
Definition: vector.hpp:446
Vector< D, T > & operator-=(const Vector< D, T > &other)
In-place subtraction.
Definition: vector.hpp:359
const_reverse_iterator rend() const noexcept
Const reverse iterator to first element - 1.
Definition: vector.hpp:625
virtual std::string toString() const
Returns string form of vector.
Definition: vector.hpp:153
Vector< D, T > operator-() const
Negative of a vector.
Definition: vector.hpp:311
Vector(const std::initializer_list< T > args)
Initializes a vector given initializer list.
Definition: vector.hpp:81
Vector< D, T > & operator=(Vector< D, T > &&) noexcept=default
Move assignment operator.
iterator end() noexcept
Interator of last element + 1.
Definition: vector.hpp:549
Vector(const Vector< D, T > &other)
Copy constructor.
Definition: vector.hpp:101
std::array< T, D > m_components
An array of components for the vector.
Definition: vector.hpp:630
T dot(const Vector< D, T > &other) const
Dot product.
Definition: vector.hpp:409
Vector< D, T > & operator/=(const T other)
In-place scalar division.
Definition: vector.hpp:389
Vector< D, T > & operator*=(const T other)
In-place scalar multiplication.
Definition: vector.hpp:374
std::array< T, D >::reverse_iterator reverse_iterator
An std::array::reverse_iterator.
Definition: vector.hpp:58
T & operator[](const std::size_t index)
Sets value of a certain component.
Definition: vector.hpp:483
bool isZero() const
Determines whether the current vector is a zero vector.
Definition: vector.hpp:460
const_iterator begin() const noexcept
Const interator of first element.
Definition: vector.hpp:532
T & at(const std::size_t index)
Sets value of a certain component.
Definition: vector.hpp:510
constexpr std::size_t numDimensions() const
Gets the number of dimensions.
Definition: vector.hpp:453
const T & operator[](const std::size_t index) const
Value of a certain component of a vector.
Definition: vector.hpp:472