From 129c61a742adea095e30478dd2c29d277e8e89de Mon Sep 17 00:00:00 2001 From: John Beard Date: Wed, 24 Apr 2024 16:02:52 +0800 Subject: [PATCH] SELECTION::GetItemsSortedByTypeAndXY - minor tweaks Simplify a little by keeping the positions of A and B in variables. Also remove const when returning by value - all that does is inhibit a (possible, NRVO) move if you assign the result to a non-const vector. --- common/tool/selection.cpp | 22 ++++++++++++---------- include/tool/selection.h | 8 ++++---- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/common/tool/selection.cpp b/common/tool/selection.cpp index 55dde50ee8..6200d88c42 100644 --- a/common/tool/selection.cpp +++ b/common/tool/selection.cpp @@ -219,8 +219,8 @@ bool SELECTION::OnlyContains( std::vector aList ) const } -const std::vector SELECTION::GetItemsSortedByTypeAndXY( bool leftBeforeRight, - bool topBeforeBottom ) const +std::vector SELECTION::GetItemsSortedByTypeAndXY( bool leftBeforeRight, + bool topBeforeBottom ) const { std::vector sorted_items = std::vector( m_items.begin(), m_items.end() ); @@ -229,24 +229,27 @@ const std::vector SELECTION::GetItemsSortedByTypeAndXY( bool leftBefo { if( a->Type() == b->Type() ) { - if( a->GetSortPosition().x == b->GetSortPosition().x ) + const VECTOR2I aPos = a->GetSortPosition(); + const VECTOR2I bPos = b->GetSortPosition(); + + if( aPos.x == bPos.x ) { // Ensure deterministic sort - if( a->GetSortPosition().y == b->GetSortPosition().y ) + if( aPos.y == bPos.y ) return a->m_Uuid < b->m_Uuid; if( topBeforeBottom ) - return a->GetSortPosition().y < b->GetSortPosition().y; + return aPos.y < bPos.y; else - return a->GetSortPosition().y > b->GetSortPosition().y; + return aPos.y > bPos.y; } else if( leftBeforeRight ) { - return a->GetSortPosition().x < b->GetSortPosition().x; + return aPos.x < bPos.x; } else { - return a->GetSortPosition().x > b->GetSortPosition().x; + return aPos.x > bPos.x; } } else @@ -254,12 +257,11 @@ const std::vector SELECTION::GetItemsSortedByTypeAndXY( bool leftBefo return a->Type() < b->Type(); } } ); - return sorted_items; } -const std::vector SELECTION::GetItemsSortedBySelectionOrder() const +std::vector SELECTION::GetItemsSortedBySelectionOrder() const { using pairedIterators = std::pair; diff --git a/include/tool/selection.h b/include/tool/selection.h index ba5ce5c95c..889657f70f 100644 --- a/include/tool/selection.h +++ b/include/tool/selection.h @@ -131,12 +131,12 @@ public: /** * Returns a copy of this selection of items sorted by their X then Y position. * - * @return Vector of sorted items + * @return std::vector of sorted items */ - const std::vector GetItemsSortedByTypeAndXY( bool leftBeforeRight = true, - bool topBeforeBottom = true ) const; + std::vector GetItemsSortedByTypeAndXY( bool leftBeforeRight = true, + bool topBeforeBottom = true ) const; - const std::vector GetItemsSortedBySelectionOrder() const; + std::vector GetItemsSortedBySelectionOrder() const; /// Returns the center point of the selection area bounding box. virtual VECTOR2I GetCenter() const;