From 819bcadee4ed866e3d87c4ad3513e7bf41ff421c Mon Sep 17 00:00:00 2001 From: llocram Date: Fri, 28 Aug 2020 14:18:41 +0200 Subject: [PATCH] Replaced hand-crafted find-algorithm with std::find_if. Removed a logical error (pos > points.size() never returns true). --- src/libs/vlayout/vlayoutpiece.cpp | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/libs/vlayout/vlayoutpiece.cpp b/src/libs/vlayout/vlayoutpiece.cpp index c6d79c4da..e4652fb52 100644 --- a/src/libs/vlayout/vlayoutpiece.cpp +++ b/src/libs/vlayout/vlayoutpiece.cpp @@ -1399,30 +1399,20 @@ QLineF VLayoutPiece::Edge(const QVector &path, int i) const } //--------------------------------------------------------------------------------------------------------------------- +// NOTE: Once C++17 is made mandatory, this method can further be refactored with std::optional int VLayoutPiece::EdgeByPoint(const QVector &path, const QPointF &p1) const { - if (p1.isNull()) + if (p1.isNull() || path.count() < 3) { return 0; } - if (path.count() < 3) + const auto points = Map(path); + const auto posIter = std::find_if(points.cbegin(), points.cend(), + [&p1](const auto &point){ return VFuzzyComparePoints(point, p1); }); + if (posIter != points.cend()) { - return 0; - } - - const QVector points = Map(path); - for (int i=0; i < points.size(); i++) - { - if (VFuzzyComparePoints(points.at(i), p1)) - { - int pos = i+1; - if (pos > points.size()) - { - pos = 1; - } - return pos; - } + return posIter - points.cbegin() + 1; } return 0; // Did not find edge }