Make LIB_TREE_NODE sort irreflexive

std::sort requires explicit ordering of all elements.  It does not allow
for equality in its output
This commit is contained in:
Seth Hillbrand 2023-02-16 12:56:19 -08:00
parent 1355561d6c
commit 5c1b15f8e3
2 changed files with 23 additions and 13 deletions

View File

@ -92,7 +92,7 @@ void LIB_TREE_NODE::SortNodes()
std::sort( m_Children.begin(), m_Children.end(), std::sort( m_Children.begin(), m_Children.end(),
[]( std::unique_ptr<LIB_TREE_NODE>& a, std::unique_ptr<LIB_TREE_NODE>& b ) []( 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 ) for( std::unique_ptr<LIB_TREE_NODE>& node: m_Children )
@ -100,27 +100,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 ) if( aNode1.m_Type != aNode2.m_Type )
return 0; return aNode1.m_Type < aNode2.m_Type;
// Recently used sorts at top // Recently used sorts at top
if( aNode1.m_Name.StartsWith( wxT( "-- " ) ) ) 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( "-- " ) ) ) else if( aNode2.m_Name.StartsWith( wxT( "-- " ) ) )
return 0; {
return false;
}
// Pinned nodes go next // Pinned nodes go next
if( aNode1.m_Pinned && !aNode2.m_Pinned ) if( aNode1.m_Pinned && !aNode2.m_Pinned )
return 1; return true;
else if( aNode2.m_Pinned && !aNode1.m_Pinned ) else if( aNode2.m_Pinned && !aNode1.m_Pinned )
return -1; return false;
if( aNode1.m_Parent != aNode2.m_Parent ) if( aNode1.m_IntrinsicRank != aNode2.m_IntrinsicRank )
return 0; 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(); void SortNodes();
/** /**
* Compare two nodes. Returns negative if aNode1 < aNode2, zero if aNode1 == * Compare two nodes. Returns true if aNode1 < aNode2.
* aNode2, or positive 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(); LIB_TREE_NODE();
virtual ~LIB_TREE_NODE() {} virtual ~LIB_TREE_NODE() {}