From 6cd70b668a23a33679f11c3c484bd09c8c130616 Mon Sep 17 00:00:00 2001 From: Maximilian Reininghaus <maximilian.reininghaus@kit.edu> Date: Fri, 2 Dec 2022 14:59:29 +0100 Subject: [PATCH] const-correctness of Path --- corsika/detail/framework/geometry/Path.inl | 36 +++++++++++++--------- corsika/framework/geometry/Path.hpp | 20 ++++++++---- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/corsika/detail/framework/geometry/Path.inl b/corsika/detail/framework/geometry/Path.inl index c36753188..b0078840b 100644 --- a/corsika/detail/framework/geometry/Path.inl +++ b/corsika/detail/framework/geometry/Path.inl @@ -9,7 +9,9 @@ #pragma once #include <deque> + #include <corsika/framework/geometry/Point.hpp> +#include <corsika/framework/core/PhysicalUnits.hpp> namespace corsika { @@ -37,30 +39,36 @@ namespace corsika { } inline void Path::removeFromEnd() { - auto lastpoint_ = points_.back(); - points_.pop_back(); - int dequesize_ = points_.size(); - if (dequesize_ == 0 || dequesize_ == 1) { + int const dequesize = points_.size(); + if (dequesize == 0) { length_ = LengthType::zero(); - } else if (dequesize_ == 2) { - length_ = (points_.back() - points_.front()).getNorm(); - } else { - length_ -= (lastpoint_ - points_.back()).getNorm(); + return; } + if (dequesize == 1) { + length_ = LengthType::zero(); + return; + } + + length_ -= distance(points_.back(), points_[dequesize - 2]); + points_.pop_back(); } inline LengthType Path::getLength() const { return length_; } - inline Point Path::getStart() const { return points_.front(); } + inline Point const& Path::getStart() const { return points_.front(); } - inline Point Path::getEnd() const { return points_.back(); } + inline Point const& Path::getEnd() const { return points_.back(); } - inline Point Path::getPoint(std::size_t const index) const { return points_.at(index); } + inline Point const& Path::getPoint(std::size_t const index) const { + return points_.at(index); + } - inline auto Path::begin() { return points_.begin(); } + inline Path::iterator Path::begin() { return points_.begin(); } + inline Path::const_iterator Path::begin() const { return points_.cbegin(); } - inline auto Path::end() { return points_.end(); } + inline Path::iterator Path::end() { return points_.end(); } + inline Path::const_iterator Path::end() const { return points_.cend(); } inline int Path::getNSegments() const { return points_.size() - 1; } -} // namespace corsika \ No newline at end of file +} // namespace corsika diff --git a/corsika/framework/geometry/Path.hpp b/corsika/framework/geometry/Path.hpp index 620cbeeab..03bc6a15f 100644 --- a/corsika/framework/geometry/Path.hpp +++ b/corsika/framework/geometry/Path.hpp @@ -9,6 +9,8 @@ #pragma once #include <deque> + +#include <corsika/framework/core/PhysicalUnits.hpp> #include <corsika/framework/geometry/Point.hpp> namespace corsika { @@ -20,6 +22,10 @@ namespace corsika { class Path { std::deque<Point> points_; ///< The points that make up this path. LengthType length_ = LengthType::zero(); ///< The length of the path. + + using iterator = std::deque<Point>::iterator; + using const_iterator = std::deque<Point>::const_iterator; + public: /** * Create a Path with a given starting Point. @@ -49,27 +55,29 @@ namespace corsika { /** * Get the starting point of the path. */ - inline Point getStart() const; + inline Point const& getStart() const; /** * Get the end point of the path. */ - inline Point getEnd() const; + inline Point const& getEnd() const; /** * Get a specific point of the path. */ - inline Point getPoint(std::size_t const index) const; + inline Point const& getPoint(std::size_t index) const; /** * Return an iterator to the start of the Path. */ - inline auto begin(); + inline const_iterator begin() const; + inline iterator begin(); /** * Return an iterator to the end of the Path. */ - inline auto end(); + inline const_iterator end() const; + inline iterator end(); /** * Get the number of steps in the path. @@ -82,4 +90,4 @@ namespace corsika { } // namespace corsika -#include <corsika/detail/framework/geometry/Path.inl> \ No newline at end of file +#include <corsika/detail/framework/geometry/Path.inl> -- GitLab