Make LIB_TREE_NODE sort irreflexive

std::sort requires explicit ordering of all elements.  It does not allow
for equality in its output

(cherry picked from commit 5c1b15f8e3)
This commit is contained in:
Seth Hillbrand 2023-02-16 12:56:19 -08:00
parent 6b43bb8fe3
commit d3a8cb84a6
2 changed files with 23 additions and 13 deletions

View File

@ -90,7 +90,7 @@ void LIB_TREE_NODE::SortNodes()
std::sort( m_Children.begin(), m_Children.end(),
[]( std::unique_ptr<LIB_TREE_NODE>& a, std::unique_ptr<LIB_TREE_NODE>& b )
{
return Compare( *a, *b ) > 0;
return Compare( *a, *b );
} );
for( std::unique_ptr<LIB_TREE_NODE>& node: m_Children )
@ -98,27 +98,38 @@ void LIB_TREE_NODE::SortNodes()
}
int LIB_TREE_NODE::Compare( LIB_TREE_NODE const& aNode1, LIB_TREE_NODE const& aNode2 )
bool LIB_TREE_NODE::Compare( LIB_TREE_NODE const& aNode1, LIB_TREE_NODE const& aNode2 )
{
if( aNode1.m_Type != aNode2.m_Type )
return 0;
return aNode1.m_Type < aNode2.m_Type;
// Recently used sorts at top
if( aNode1.m_Name.StartsWith( wxT( "-- " ) ) )
return 1;
{
if( aNode2.m_Name.StartsWith( wxT( "-- " ) ) )
{
return aNode1.m_IntrinsicRank > aNode2.m_IntrinsicRank;
}
else
{
return true;
}
}
else if( aNode2.m_Name.StartsWith( wxT( "-- " ) ) )
return 0;
{
return false;
}
// Pinned nodes go next
if( aNode1.m_Pinned && !aNode2.m_Pinned )
return 1;
return true;
else if( aNode2.m_Pinned && !aNode1.m_Pinned )
return -1;
return false;
if( aNode1.m_Parent != aNode2.m_Parent )
return 0;
if( aNode1.m_IntrinsicRank != aNode2.m_IntrinsicRank )
return aNode1.m_IntrinsicRank > aNode2.m_IntrinsicRank;
return aNode1.m_IntrinsicRank - aNode2.m_IntrinsicRank;
return reinterpret_cast<const void*>( &aNode1 ) < reinterpret_cast<const void*>( &aNode2 );
}

View File

@ -101,10 +101,9 @@ public:
void SortNodes();
/**
* Compare two nodes. Returns negative if aNode1 < aNode2, zero if aNode1 ==
* aNode2, or positive if aNode1 > aNode2.
* Compare two nodes. Returns true if aNode1 < aNode2.
*/
static int Compare( LIB_TREE_NODE const& aNode1, LIB_TREE_NODE const& aNode2 );
static bool Compare( LIB_TREE_NODE const& aNode1, LIB_TREE_NODE const& aNode2 );
LIB_TREE_NODE();
virtual ~LIB_TREE_NODE() {}