From 8fefea6d36b96d92e8e2b6eb6edf91baed1f4fd5 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Thu, 30 Jun 2016 15:24:15 -0400 Subject: [PATCH] Rewrite LabelTrack::SortLabels --- src/LabelTrack.cpp | 72 +++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/src/LabelTrack.cpp b/src/LabelTrack.cpp index f6d661238..c298c24e7 100644 --- a/src/LabelTrack.cpp +++ b/src/LabelTrack.cpp @@ -2805,48 +2805,42 @@ bool LabelTrack::IsGoodLabelEditKey(const wxKeyEvent & evt) /// sort (with a linear search) is a reasonable choice. void LabelTrack::SortLabels() { - int i,j; - LabelStruct * pTemp; - for (i = 1; i < (int)mLabels.Count(); i++) + const auto begin = mLabels.begin(); + const auto nn = (int)mLabels.size(); + int i = 1; + while (true) { - j=i-1; - while( (j>=0) && (mLabels[j]->getT0() > - mLabels[i]->getT0()) ) - { - j--; - } - j++; - if( jgetT0() <= mLabels[i]->getT0()) + ++i; + if (i >= nn) + break; - // Various indecese need to be updated with the moved items... - if( mMouseOverLabelLeft <=i ) - { - if( mMouseOverLabelLeft == i ) - mMouseOverLabelLeft=j; - else if( mMouseOverLabelLeft >= j) - mMouseOverLabelLeft++; + // Where must element i sink to? At most i - 1, maybe less + int j = i - 2; + while( (j >= 0) && (mLabels[j]->getT0() > mLabels[i]->getT0()) ) + --j; + ++j; + + // Now fix the disorder + std::rotate( + begin + j, + begin + i, + begin + i + 1 + ); + + // Various indices need to be updated with the moved items... + auto update = [=](int &index) { + if( index <= i ) { + if( index == i ) + index = j; + else if( index >= j) + ++index; } - if( mMouseOverLabelRight <=i ) - { - if( mMouseOverLabelRight == i ) - mMouseOverLabelRight=j; - else if( mMouseOverLabelRight >= j) - mMouseOverLabelRight++; - } - if( mSelIndex <=i ) - { - if( mSelIndex == i ) - mSelIndex=j; - else if( mSelIndex >= j) - mSelIndex++; - } - } + }; + update(mMouseOverLabelLeft); + update(mMouseOverLabelRight); + update(mSelIndex); } }