RTree: Fix iterator in single branch trees

In a single branch tree, we need to verify that all leaves do not match
the input search before returning.

Fixes #3764 | https://gitlab.com/kicad/code/kicad/issues/3764
This commit is contained in:
Seth Hillbrand 2020-01-14 00:30:15 -08:00
parent 1bdcb33f75
commit 6a47d0f507
2 changed files with 37 additions and 8 deletions

View File

@ -241,4 +241,34 @@ BOOST_AUTO_TEST_CASE( MixedElements )
BOOST_CHECK_EQUAL( count, 1 ); BOOST_CHECK_EQUAL( count, 1 );
} }
// This tests the case where the tree has no branches but we want to iterator over a subset
// where the first case may or may not match
BOOST_AUTO_TEST_CASE( SingleElementTree )
{
SCH_JUNCTION* junction = new SCH_JUNCTION( wxPoint( Mils2iu( 100 ), Mils2iu( 100 ) ) );
m_tree.insert( junction );
SCH_NO_CONNECT* nc = new SCH_NO_CONNECT( wxPoint( Mils2iu( 150 ), Mils2iu( 150 ) ) );
m_tree.insert( nc );
int count = 0;
for( auto item : m_tree.OfType( SCH_JUNCTION_T ) )
{
static_cast<void>( item );
count++;
}
BOOST_CHECK_EQUAL( count, 1 );
count = 0;
for( auto item : m_tree.OfType( SCH_NO_CONNECT_T ) )
{
static_cast<void>( item );
count++;
}
BOOST_CHECK_EQUAL( count, 1 );
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -404,18 +404,17 @@ public:
{ {
iterator retval( aRect ); iterator retval( aRect );
// Only a single element in the tree if( !m_root->m_count )
if( m_root->IsLeaf() )
{
if( m_root->m_count && Overlap( &aRect, &m_root->m_branch[0].m_rect ) )
retval.Push( m_root, 0 );
return retval; return retval;
}
retval.Push( m_root, 0 ); retval.Push( m_root, 0 );
++retval;
// If the first leaf matches, return the root pointer, otherwise,
// increment to the first match or empty if none.
if( m_root->IsLeaf() && Overlap( &aRect, &m_root->m_branch[0].m_rect ) )
return retval;
++retval;
return retval; return retval;
} }