From 0126918fb523d05ed8a142d8dd67300a9bb5931b Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 9 May 2017 12:13:08 -0400 Subject: [PATCH] Simplify InsertOrReplaceRelative, always trim when to domain... ... Comment in it says that it does not check for discontinuities if it replaces! This will not matter in the uses of it internal to the Envelope class, which will in fact always be proper insertions, having checked first for the presence of a point. --- src/Envelope.cpp | 37 +++++++++++-------------------------- src/Envelope.h | 1 + 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/Envelope.cpp b/src/Envelope.cpp index b2144e764..a4777bd4c 100644 --- a/src/Envelope.cpp +++ b/src/Envelope.cpp @@ -1107,35 +1107,20 @@ int Envelope::InsertOrReplaceRelative(double when, double value) #endif int len = mEnv.size(); + when = std::max( 0.0, std::min( mTrackLen, when ) ); - if (len && when < 0.0) - return 0; - if ((len > 1) && when > mTrackLen) - return len - 1; + auto range = EqualRange( when, 0 ); + int index = range.first; - if (when < 0.0) - when = 0.0; - if ((len>1) && when > mTrackLen) - when = mTrackLen; - - int i = 0; - - while (i < len && when > mEnv[i].GetT()) - i++; - - if(i < len && when == mEnv[i].GetT()) - // modify existing - mEnv[i].SetVal( this, value ); - else { + if ( index < range.second ) + // modify existing + // In case of a discontinuity, ALWAYS CHANGING LEFT LIMIT ONLY! + mEnv[ index ].SetVal( this, value ); + else // Add NEW - EnvPoint e{ when, value }; - if (i < len) { - Insert(i, e); - } else { - mEnv.push_back(e); - } - } - return i; + Insert( index, EnvPoint { when, value } ); + + return index; } std::pair Envelope::EqualRange( double when, double sampleDur ) const diff --git a/src/Envelope.h b/src/Envelope.h index eb358ec0f..37e2d4598 100644 --- a/src/Envelope.h +++ b/src/Envelope.h @@ -196,6 +196,7 @@ public: private: int InsertOrReplaceRelative(double when, double value); + friend class EnvelopeEditor; /** \brief Accessor for points */ const EnvPoint &operator[] (int index) const