1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-04 14:19:30 +02:00

Rewrite TrackList::SwapNodes without GetLink(ed)

This commit is contained in:
Paul Licameli 2017-01-11 10:52:52 -05:00
parent b05c32e2ac
commit 7ec734b087

View File

@ -1157,11 +1157,10 @@ bool TrackList::CanMoveDown(Track * t) const
return GetNext(t, true) != NULL; return GetNext(t, true) != NULL;
} }
// This is used when you want to swap the track or pair of // This is used when you want to swap the channel group starting
// tracks in s1 with the track or pair of tracks in s2. // at s1 with that starting at s2.
// The complication is that the tracks are stored in a single // The complication is that the tracks are stored in a single
// linked list, and pairs of tracks are marked only by a flag // linked list.
// in one of the tracks.
void TrackList::SwapNodes(TrackNodePointer s1, TrackNodePointer s2) void TrackList::SwapNodes(TrackNodePointer s1, TrackNodePointer s2)
{ {
// if a null pointer is passed in, we want to know about it // if a null pointer is passed in, we want to know about it
@ -1169,60 +1168,52 @@ void TrackList::SwapNodes(TrackNodePointer s1, TrackNodePointer s2)
wxASSERT(!isNull(s2)); wxASSERT(!isNull(s2));
// Deal with first track in each team // Deal with first track in each team
Track *link; s1 = ( * FindLeader( s1.first->get() ) )->GetNode();
link = (*s1.first)->GetLink(); s2 = ( * FindLeader( s2.first->get() ) )->GetNode();
bool linked1 = link != nullptr;
if (linked1 && !(*s1.first)->GetLinked()) {
s1 = link->GetNode();
}
link = (*s2.first)->GetLink();
bool linked2 = link != nullptr;
if (linked2 && !(*s2.first)->GetLinked()) {
s2 = link->GetNode();
}
// Safety check... // Safety check...
if (s1 == s2) if (s1 == s2)
return; return;
// Be sure s1 is the earlier iterator // Be sure s1 is the earlier iterator
if ((*s1.first)->GetIndex() >= (*s2.first)->GetIndex()) { if ((*s1.first)->GetIndex() >= (*s2.first)->GetIndex())
std::swap(s1, s2); std::swap(s1, s2);
std::swap(linked1, linked2);
}
// Remove tracks // For saving the removed tracks
ListOfTracks::value_type save11 = std::move(*s1.first), save12{}; using Saved = std::vector< ListOfTracks::value_type >;
s1.first = erase(s1.first); Saved saved1, saved2;
if (linked1) {
wxASSERT(s1 != s2); auto doSave = [&] ( Saved &saved, TrackNodePointer &s ) {
save12 = std::move(*s1.first), s1.first = erase(s1.first); size_t nn = Channels( s.first->get() ).size();
} saved.resize( nn );
// Save them in backwards order
while( nn-- )
saved[nn] = std::move( *s.first ), s.first = erase(s.first);
};
doSave( saved1, s1 );
// The two ranges are assumed to be disjoint but might abut
const bool same = (s1 == s2); const bool same = (s1 == s2);
doSave( saved2, s2 );
ListOfTracks::value_type save21 = std::move(*s2.first), save22{};
s2.first = erase(s2.first);
if (linked2)
save22 = std::move(*s2.first), s2.first = erase(s2.first);
if (same) if (same)
// We invalidated s1! // Careful, we invalidated s1 in the second doSave!
s1 = s2; s1 = s2;
// Reinsert them // Reinsert them
Track *pTrack; auto doInsert = [&] ( Saved &saved, TrackNodePointer &s ) {
if (save22) Track *pTrack;
pTrack = save22.get(), for (auto & pointer : saved)
pTrack->SetOwner(mSelf, s1 = { insert(s1.first, std::move(save22)), this }); pTrack = pointer.get(),
pTrack = save21.get(), // Insert before s, and reassign s to point at the new node before
pTrack->SetOwner(mSelf, s1 = { insert(s1.first, std::move(save21)), this }); // old s; which is why we saved pointers in backwards order
pTrack->SetOwner(mSelf,
if (save12) s = { insert(s.first, std::move(pointer)), this } );
pTrack = save12.get(), };
pTrack->SetOwner(mSelf, s2 = { insert(s2.first, std::move(save12)), this }); // This does not invalidate s2 even when it equals s1:
pTrack = save11.get(), doInsert( saved2, s1 );
pTrack->SetOwner(mSelf, s2 = { insert(s2.first, std::move(save11)), this }); // Even if s2 was same as s1, this correctly inserts the saved1 range
// after the saved2 range, when done after:
doInsert( saved1, s2 );
// Now correct the Index in the tracks, and other things // Now correct the Index in the tracks, and other things
RecalcPositions(s1); RecalcPositions(s1);