Use wxArrayInt methods instead of duplicating them.

Just use wxArrayInt::Index() instead of doing a linear search in the array
manually. Also use RemoveAt() + Insert() instead of manually iterating over
the items.

See #16110.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76454 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2014-05-04 22:13:02 +00:00
parent c03557f0be
commit 34d3e680c8
2 changed files with 11 additions and 38 deletions

View File

@@ -1448,15 +1448,10 @@ public:
if ( m_colAt.IsEmpty() ) if ( m_colAt.IsEmpty() )
return idx; return idx;
for ( int i = 0; i < m_numCols; i++ ) int pos = m_colAt.Index(idx);
{ wxASSERT_MSG( pos != wxNOT_FOUND, "invalid column index" );
if ( m_colAt[i] == idx )
return i;
}
wxFAIL_MSG( "invalid column index" ); return pos;
return wxNOT_FOUND;
} }
// reset the columns positions to the default order // reset the columns positions to the default order

View File

@@ -218,15 +218,10 @@ unsigned int wxHeaderCtrlBase::GetColumnPos(unsigned int idx) const
wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" ); wxCHECK_MSG( idx < count, wxNO_COLUMN, "invalid index" );
const wxArrayInt order = GetColumnsOrder(); const wxArrayInt order = GetColumnsOrder();
for ( unsigned n = 0; n < count; n++ ) int pos = order.Index(idx);
{ wxCHECK_MSG( pos != wxNOT_FOUND, wxNO_COLUMN, "column unexpectedly not displayed at all" );
if ( (unsigned)order[n] == idx )
return n;
}
wxFAIL_MSG( "column unexpectedly not displayed at all" ); return (unsigned int)pos;
return wxNO_COLUMN;
} }
/* static */ /* static */
@@ -234,31 +229,14 @@ void wxHeaderCtrlBase::MoveColumnInOrderArray(wxArrayInt& order,
unsigned int idx, unsigned int idx,
unsigned int pos) unsigned int pos)
{ {
const unsigned count = order.size(); int posOld = order.Index(idx);
wxASSERT_MSG( posOld != wxNOT_FOUND, "invalid index" );
wxArrayInt orderNew; if ( pos != (unsigned int)posOld )
orderNew.reserve(count);
for ( unsigned n = 0; ; n++ )
{ {
// NB: order of checks is important for this to work when the new order.RemoveAt(posOld);
// column position is the same as the old one order.Insert(idx, pos);
// insert the column at its new position
if ( orderNew.size() == pos )
orderNew.push_back(idx);
if ( n == count )
break;
// delete the column from its old position
const unsigned idxOld = order[n];
if ( idxOld == idx )
continue;
orderNew.push_back(idxOld);
} }
order.swap(orderNew);
} }
void void