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.
This commit is contained in:
John Beard 2024-04-24 16:02:52 +08:00
parent 2f3196c18c
commit 129c61a742
2 changed files with 16 additions and 14 deletions

View File

@ -219,8 +219,8 @@ bool SELECTION::OnlyContains( std::vector<KICAD_T> aList ) const
} }
const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedByTypeAndXY( bool leftBeforeRight, std::vector<EDA_ITEM*> SELECTION::GetItemsSortedByTypeAndXY( bool leftBeforeRight,
bool topBeforeBottom ) const bool topBeforeBottom ) const
{ {
std::vector<EDA_ITEM*> sorted_items = std::vector<EDA_ITEM*>( m_items.begin(), m_items.end() ); std::vector<EDA_ITEM*> sorted_items = std::vector<EDA_ITEM*>( m_items.begin(), m_items.end() );
@ -229,24 +229,27 @@ const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedByTypeAndXY( bool leftBefo
{ {
if( a->Type() == b->Type() ) 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 // Ensure deterministic sort
if( a->GetSortPosition().y == b->GetSortPosition().y ) if( aPos.y == bPos.y )
return a->m_Uuid < b->m_Uuid; return a->m_Uuid < b->m_Uuid;
if( topBeforeBottom ) if( topBeforeBottom )
return a->GetSortPosition().y < b->GetSortPosition().y; return aPos.y < bPos.y;
else else
return a->GetSortPosition().y > b->GetSortPosition().y; return aPos.y > bPos.y;
} }
else if( leftBeforeRight ) else if( leftBeforeRight )
{ {
return a->GetSortPosition().x < b->GetSortPosition().x; return aPos.x < bPos.x;
} }
else else
{ {
return a->GetSortPosition().x > b->GetSortPosition().x; return aPos.x > bPos.x;
} }
} }
else else
@ -254,12 +257,11 @@ const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedByTypeAndXY( bool leftBefo
return a->Type() < b->Type(); return a->Type() < b->Type();
} }
} ); } );
return sorted_items; return sorted_items;
} }
const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedBySelectionOrder() const std::vector<EDA_ITEM*> SELECTION::GetItemsSortedBySelectionOrder() const
{ {
using pairedIterators = std::pair<decltype( m_items.begin() ), using pairedIterators = std::pair<decltype( m_items.begin() ),
decltype( m_itemsOrders.begin() )>; decltype( m_itemsOrders.begin() )>;

View File

@ -131,12 +131,12 @@ public:
/** /**
* Returns a copy of this selection of items sorted by their X then Y position. * 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<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true, std::vector<EDA_ITEM*> GetItemsSortedByTypeAndXY( bool leftBeforeRight = true,
bool topBeforeBottom = true ) const; bool topBeforeBottom = true ) const;
const std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const; std::vector<EDA_ITEM*> GetItemsSortedBySelectionOrder() const;
/// Returns the center point of the selection area bounding box. /// Returns the center point of the selection area bounding box.
virtual VECTOR2I GetCenter() const; virtual VECTOR2I GetCenter() const;