1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-07-12 14:47:43 +02:00

Rewrite LabelTrack::SortLabels

This commit is contained in:
Paul Licameli 2016-06-30 15:24:15 -04:00
parent 15bb2f0434
commit 8fefea6d36

@ -2805,48 +2805,42 @@ bool LabelTrack::IsGoodLabelEditKey(const wxKeyEvent & evt)
/// sort (with a linear search) is a reasonable choice. /// sort (with a linear search) is a reasonable choice.
void LabelTrack::SortLabels() void LabelTrack::SortLabels()
{ {
int i,j; const auto begin = mLabels.begin();
LabelStruct * pTemp; const auto nn = (int)mLabels.size();
for (i = 1; i < (int)mLabels.Count(); i++) int i = 1;
while (true)
{ {
j=i-1; // Find the next disorder
while( (j>=0) && (mLabels[j]->getT0() > while (i < nn && mLabels[i - 1]->getT0() <= mLabels[i]->getT0())
mLabels[i]->getT0()) ) ++i;
{ if (i >= nn)
j--; break;
}
j++;
if( j<i)
{
// Remove at i and insert at j.
// Don't use DeleteLabel() since just moving it.
pTemp = mLabels[i];
mLabels.RemoveAt( i );
mLabels.Insert(pTemp, j);
// Various indecese need to be updated with the moved items... // Where must element i sink to? At most i - 1, maybe less
if( mMouseOverLabelLeft <=i ) int j = i - 2;
{ while( (j >= 0) && (mLabels[j]->getT0() > mLabels[i]->getT0()) )
if( mMouseOverLabelLeft == i ) --j;
mMouseOverLabelLeft=j; ++j;
else if( mMouseOverLabelLeft >= j)
mMouseOverLabelLeft++; // Now fix the disorder
} std::rotate(
if( mMouseOverLabelRight <=i ) begin + j,
{ begin + i,
if( mMouseOverLabelRight == i ) begin + i + 1
mMouseOverLabelRight=j; );
else if( mMouseOverLabelRight >= j)
mMouseOverLabelRight++; // Various indices need to be updated with the moved items...
} auto update = [=](int &index) {
if( mSelIndex <=i ) if( index <= i ) {
{ if( index == i )
if( mSelIndex == i ) index = j;
mSelIndex=j; else if( index >= j)
else if( mSelIndex >= j) ++index;
mSelIndex++;
}
} }
};
update(mMouseOverLabelLeft);
update(mMouseOverLabelRight);
update(mSelIndex);
} }
} }