Rename and simplify wxGridBlockCoords::ContainsBlock()

Change the return type of this function to a simple and clear bool
instead of 3-valued int requiring a special explanation. This is simpler
and not any less efficient as checking for whether one block contains
another or the other one contains this one are separate operations
anyhow.

Rename the function to a more grammatically correct name.

Also move it inline as it's now trivial enough for this to be worth it.
This commit is contained in:
Vadim Zeitlin
2020-04-05 01:37:56 +02:00
parent a5952ee087
commit 791a9e68ae
5 changed files with 23 additions and 42 deletions

View File

@@ -823,11 +823,12 @@ public:
m_leftCol <= cell.GetCol() && cell.GetCol() <= m_rightCol;
}
// Whether the blocks contain each other.
// returns 1, if this block contains the other,
// -1, if the other block contains this one,
// 0, otherwise
int ContainBlock(const wxGridBlockCoords& other) const;
// Return whether this blocks fully contains another one.
bool ContainsBlock(const wxGridBlockCoords& other) const
{
return m_topRow <= other.m_topRow && other.m_bottomRow <= m_bottomRow &&
m_leftCol <= other.m_leftCol && other.m_rightCol <= m_rightCol;
}
// Calculates the result blocks by subtracting the other block from this
// block. splitOrientation can be wxVERTICAL or wxHORIZONTAL.

View File

@@ -1924,14 +1924,12 @@ public:
bool ContainsCell(const wxGridCellCoords& cell) const;
/**
Whether the blocks contain each other.
Check whether this block contains another one.
@return
1, if this block contains the other,
-1, if the other block contains this one,
0, otherwise.
@true if @a other is entirely contained within this block.
*/
int ContainBlock(const wxGridBlockCoords& other) const;
int ContainsBlock(const wxGridBlockCoords& other) const;
/**
Calculates the result blocks by subtracting the other block from this

View File

@@ -1140,21 +1140,6 @@ const wxGridCornerHeaderRenderer& wxGridCellAttrProvider::GetCornerRenderer()
// wxGridBlockCoords
// ----------------------------------------------------------------------------
int wxGridBlockCoords::ContainBlock(const wxGridBlockCoords& other) const
{
// returns 1, if this block contains the other,
// -1, if the other block contains this one,
// 0, otherwise
if ( m_topRow <= other.m_topRow && other.m_bottomRow <= m_bottomRow &&
m_leftCol <= other.m_leftCol && other.m_rightCol <= m_rightCol )
return 1;
else if ( other.m_topRow <= m_topRow && m_bottomRow <= other.m_bottomRow &&
other.m_leftCol <= m_leftCol && m_rightCol <= other.m_rightCol )
return -1;
return 0;
}
wxGridBlockDiffResult
wxGridBlockCoords::Difference(const wxGridBlockCoords& other,
int splitOrientation) const

View File

@@ -741,19 +741,14 @@ void wxGridSelection::MergeOrAddBlock(wxVectorGridBlockCoords& blocks,
{
const wxGridBlockCoords& block = blocks[n];
switch ( block.ContainBlock(newBlock) )
{
case 1:
if ( block.ContainsBlock(newBlock) )
return;
case -1:
if ( newBlock.ContainsBlock(block) )
{
blocks.erase(blocks.begin() + n);
n--;
count--;
break;
default:
break;
}
}

View File

@@ -1337,17 +1337,19 @@ TEST_CASE("GridBlockCoords::ContainsCell", "[grid]")
CHECK(!wxGridBlockCoords(1, 1, 3, 3).ContainsCell(wxGridCellCoords(5, 5)));
}
TEST_CASE("GridBlockCoords::ContainBlock", "[grid]")
TEST_CASE("GridBlockCoords::ContainsBlock", "[grid]")
{
wxGridBlockCoords block1(1, 1, 5, 5);
wxGridBlockCoords block2(1, 1, 3, 3);
wxGridBlockCoords block3(2, 2, 7, 7);
wxGridBlockCoords block4(10, 10, 12, 12);
CHECK(block1.ContainBlock(block2) == 1);
CHECK(block2.ContainBlock(block1) == -1);
CHECK(block1.ContainBlock(block3) == 0);
CHECK(block1.ContainBlock(block4) == 0);
CHECK( block1.ContainsBlock(block2));
CHECK(!block2.ContainsBlock(block1));
CHECK(!block1.ContainsBlock(block3));
CHECK(!block1.ContainsBlock(block4));
CHECK(!block3.ContainsBlock(block1));
CHECK(!block4.ContainsBlock(block1));
}
TEST_CASE("GridBlockCoords::Difference", "[grid]")