From 867535c4a30b94aeef7902b88bde2d291b1c77c6 Mon Sep 17 00:00:00 2001 From: Gurkirat Singh Date: Mon, 24 Jul 2023 02:13:03 +0530 Subject: [PATCH] improve (src): reformat implementation of `Firefly::Vector` member functions --- src/vector/add.cpp | 11 ++++++++--- src/vector/angle_with.cpp | 3 ++- src/vector/area.cpp | 8 ++++++-- src/vector/elem_sum.cpp | 4 +++- src/vector/indexers.cpp | 5 +++-- src/vector/is_parallel.cpp | 6 +++++- src/vector/is_zero.cpp | 3 ++- src/vector/magnitude.cpp | 4 +++- src/vector/normalize.cpp | 4 +++- src/vector/scale.cpp | 9 ++++++--- src/vector/subtract.cpp | 16 ++++++++++++---- src/vector/vector.cpp | 7 ++++--- 12 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/vector/add.cpp b/src/vector/add.cpp index cabd3f9..0bf64ef 100644 --- a/src/vector/add.cpp +++ b/src/vector/add.cpp @@ -22,14 +22,19 @@ std::unique_ptr Vector::Add(Vector const &_fvec) const { std::unique_ptr Vector::Add(Real const &_scalar) const { std::unique_ptr sum = std::make_unique(*this); - std::transform(sum->m_vec->cbegin(), sum->m_vec->cend(), sum->m_vec->begin(), [&](Real const &_el) { return _el + _scalar; }); + std::transform(sum->m_vec->cbegin(), sum->m_vec->cend(), sum->m_vec->begin(), + [&](Real const &_el) { return _el + _scalar; }); return sum; } -std::unique_ptr Vector::operator+(Real const &_scalar) const { return this->Add(_scalar); } +std::unique_ptr Vector::operator+(Real const &_scalar) const { + return this->Add(_scalar); +} -std::unique_ptr Vector::operator+(Vector const &_fvec) const { return this->Add(_fvec); } +std::unique_ptr Vector::operator+(Vector const &_fvec) const { + return this->Add(_fvec); +} std::unique_ptr Vector::operator++() const { return this->Add(1); } } // namespace Firefly \ No newline at end of file diff --git a/src/vector/angle_with.cpp b/src/vector/angle_with.cpp index 45595b7..18ef508 100644 --- a/src/vector/angle_with.cpp +++ b/src/vector/angle_with.cpp @@ -5,7 +5,8 @@ namespace Firefly { Real Vector::AngleWith(Vector const &_fvec) const { - auto angle_radians = std::acos(this->Dot(_fvec) / (this->Magnitude() * _fvec.Magnitude())); + auto angle_radians = + std::acos(this->Dot(_fvec) / (this->Magnitude() * _fvec.Magnitude())); if (std::isnan(angle_radians)) { throw std::domain_error("Can not find angle with zero vector."); } diff --git a/src/vector/area.cpp b/src/vector/area.cpp index 01de62e..64edacf 100644 --- a/src/vector/area.cpp +++ b/src/vector/area.cpp @@ -1,7 +1,11 @@ #include "firefly/vector.hpp" namespace Firefly { -Real Vector::ParallelogramArea(Vector const &_fvec) const { return this->Cross(_fvec)->Magnitude(); } +Real Vector::ParallelogramArea(Vector const &_fvec) const { + return this->Cross(_fvec)->Magnitude(); +} -Real Vector::TriangleArea(Vector const &_fvec) const { return this->ParallelogramArea(_fvec) / 2; } +Real Vector::TriangleArea(Vector const &_fvec) const { + return this->ParallelogramArea(_fvec) / 2; +} } // namespace Firefly \ No newline at end of file diff --git a/src/vector/elem_sum.cpp b/src/vector/elem_sum.cpp index 6331b43..7e2f130 100644 --- a/src/vector/elem_sum.cpp +++ b/src/vector/elem_sum.cpp @@ -5,5 +5,7 @@ namespace Firefly { -Real Vector::ElemSum() const { return std::reduce(m_vec->cbegin(), m_vec->cend(), 0x0, std::plus()); } +Real Vector::ElemSum() const { + return std::reduce(m_vec->cbegin(), m_vec->cend(), 0x0, std::plus()); +} } // namespace Firefly \ No newline at end of file diff --git a/src/vector/indexers.cpp b/src/vector/indexers.cpp index d2f852e..3159a79 100644 --- a/src/vector/indexers.cpp +++ b/src/vector/indexers.cpp @@ -6,8 +6,9 @@ namespace Firefly { Real Vector::At(std::size_t idx) const { if (idx >= m_vec->size()) { - throw std::out_of_range("Index out of range. Requested index: " + std::to_string(idx) + - ", Vector size: " + std::to_string(m_vec->size())); + throw std::out_of_range( + "Index out of range. Requested index: " + std::to_string(idx) + + ", Vector size: " + std::to_string(m_vec->size())); } return m_vec->at(idx); diff --git a/src/vector/is_parallel.cpp b/src/vector/is_parallel.cpp index c637019..75bccb6 100644 --- a/src/vector/is_parallel.cpp +++ b/src/vector/is_parallel.cpp @@ -1,3 +1,5 @@ +#include + #include "firefly/vector.hpp" namespace Firefly { @@ -5,6 +7,8 @@ bool Vector::IsParallel(const Vector &_fvec) { if (this->Magnitude() == 0 || _fvec.Magnitude() == 0) { return true; } - return this->AngleWith(_fvec) == 0; + + auto angle_radians = this->AngleWith(_fvec); + return angle_radians == 0 || angle_radians == M_PI; } } // namespace Firefly \ No newline at end of file diff --git a/src/vector/is_zero.cpp b/src/vector/is_zero.cpp index 4c98148..806683c 100644 --- a/src/vector/is_zero.cpp +++ b/src/vector/is_zero.cpp @@ -5,6 +5,7 @@ namespace Firefly { bool Vector::IsZero() { - return std::all_of(m_vec->cbegin(), m_vec->cend(), [](Real const &_el) { return _el == 0; }); + return std::all_of(m_vec->cbegin(), m_vec->cend(), + [](Real const &_el) { return _el == 0; }); } } // namespace Firefly \ No newline at end of file diff --git a/src/vector/magnitude.cpp b/src/vector/magnitude.cpp index 771067a..d4a1ee6 100644 --- a/src/vector/magnitude.cpp +++ b/src/vector/magnitude.cpp @@ -3,5 +3,7 @@ #include "firefly/vector.hpp" namespace Firefly { -Real Vector::Magnitude() const { return static_cast(std::sqrt(this->Dot(*this))); } +Real Vector::Magnitude() const { + return static_cast(std::sqrt(this->Dot(*this))); +} } // namespace Firefly \ No newline at end of file diff --git a/src/vector/normalize.cpp b/src/vector/normalize.cpp index ae389e2..cc15b77 100644 --- a/src/vector/normalize.cpp +++ b/src/vector/normalize.cpp @@ -1,5 +1,7 @@ #include "firefly/vector.hpp" namespace Firefly { -std::unique_ptr Vector::Normalize() const { return this->Scale(1 / this->Magnitude()); } +std::unique_ptr Vector::Normalize() const { + return this->Scale(1 / this->Magnitude()); +} } // namespace Firefly \ No newline at end of file diff --git a/src/vector/scale.cpp b/src/vector/scale.cpp index 27d9c0f..abfb38d 100644 --- a/src/vector/scale.cpp +++ b/src/vector/scale.cpp @@ -6,11 +6,14 @@ namespace Firefly { std::unique_ptr Vector::Scale(Real const &_scalar) const { std::unique_ptr scaled = std::make_unique(*this); - std::transform(scaled->m_vec->cbegin(), scaled->m_vec->cend(), scaled->m_vec->begin(), - [&](Real const &_el) { return _el * _scalar; }); + std::transform(scaled->m_vec->cbegin(), scaled->m_vec->cend(), + scaled->m_vec->begin(), + [&](Real const &_el) { return _el * _scalar; }); return scaled; } -std::unique_ptr Vector::operator*(Real const &_scalar) const { return this->Scale(_scalar); } +std::unique_ptr Vector::operator*(Real const &_scalar) const { + return this->Scale(_scalar); +} } // namespace Firefly \ No newline at end of file diff --git a/src/vector/subtract.cpp b/src/vector/subtract.cpp index 79ee630..d42587b 100644 --- a/src/vector/subtract.cpp +++ b/src/vector/subtract.cpp @@ -3,11 +3,19 @@ #include "firefly/vector.hpp" namespace Firefly { -std::unique_ptr Vector::Subtract(Vector const &_fvec) const { return this->Add(*_fvec.Scale(-1)); } -std::unique_ptr Vector::Subtract(Real const &_scalar) const { return this->Add(-_scalar); } +std::unique_ptr Vector::Subtract(Vector const &_fvec) const { + return this->Add(*_fvec.Scale(-1)); +} +std::unique_ptr Vector::Subtract(Real const &_scalar) const { + return this->Add(-_scalar); +} -std::unique_ptr Vector::operator-(Vector const &_fvec) const { return this->Subtract(_fvec); } -std::unique_ptr Vector::operator-(Real const &_scalar) const { return this->Subtract(_scalar); } +std::unique_ptr Vector::operator-(Vector const &_fvec) const { + return this->Subtract(_fvec); +} +std::unique_ptr Vector::operator-(Real const &_scalar) const { + return this->Subtract(_scalar); +} std::unique_ptr Vector::operator--() const { return this->Subtract(1); } } // namespace Firefly \ No newline at end of file diff --git a/src/vector/vector.cpp b/src/vector/vector.cpp index 7b77e27..38e60d0 100644 --- a/src/vector/vector.cpp +++ b/src/vector/vector.cpp @@ -3,12 +3,13 @@ namespace Firefly { -Vector::Vector(std::size_t size) : m_vec{std::make_shared(size)} {} +Vector::Vector(std::size_t _size) + : m_vec{std::make_shared(_size)} {} Vector::Vector(VectorType const &_vec) : m_vec(std::make_shared(_vec)) {} -Vector::Vector(Vector const &_f) - : m_vec(std::make_shared(*_f.m_vec)) {} +Vector::Vector(Vector const &_fvec) + : m_vec(std::make_shared(*_fvec.m_vec)) {} } // namespace Firefly