From 81966fa1a68293a5dfa37408da993bc95ac8402d Mon Sep 17 00:00:00 2001 From: Oskar Wallgren Date: Fri, 13 Oct 2017 20:46:55 +0200 Subject: [PATCH 1/2] Sort note patterns after both position and key --- include/Note.h | 12 ++++++++++-- src/tracks/Pattern.cpp | 23 +---------------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/include/Note.h b/include/Note.h index 2d448c6d4..c14f9d931 100644 --- a/include/Note.h +++ b/include/Note.h @@ -113,11 +113,19 @@ public: void quantizeLength( const int qGrid ); void quantizePos( const int qGrid ); - static inline bool lessThan( Note * &lhs, Note * &rhs ) + static inline bool lessThan( const Note * lhs, const Note * rhs ) { // function to compare two notes - must be called explictly when // using qSort - return (bool) ((int) ( *lhs ).pos() < (int) ( *rhs ).pos()); + if( (int)( *lhs ).pos() < (int)( *rhs ).pos() ) + { + return true; + } + else if( (int)( *lhs ).pos() > (int)( *rhs ).pos() ) + { + return false; + } + return ( (int)( *lhs ).key() > (int)( *rhs ).key() ); } inline bool selected() const diff --git a/src/tracks/Pattern.cpp b/src/tracks/Pattern.cpp index 3fa01462b..cf02d6476 100644 --- a/src/tracks/Pattern.cpp +++ b/src/tracks/Pattern.cpp @@ -213,28 +213,7 @@ Note * Pattern::addNote( const Note & _new_note, const bool _quant_pos ) } instrumentTrack()->lock(); - if( m_notes.size() == 0 || m_notes.back()->pos() <= new_note->pos() ) - { - m_notes.push_back( new_note ); - } - else - { - // simple algorithm for inserting the note between two - // notes with smaller and greater position - // maybe it could be optimized by starting in the middle and - // going forward or backward but note-inserting isn't that - // time-critical since it is usually not done while playing... - long new_note_abs_time = new_note->pos(); - NoteVector::Iterator it = m_notes.begin(); - - while( it != m_notes.end() && - ( *it )->pos() < new_note_abs_time ) - { - ++it; - } - - m_notes.insert( it, new_note ); - } + m_notes.insert(std::upper_bound(m_notes.begin(), m_notes.end(), new_note, Note::lessThan), new_note); instrumentTrack()->unlock(); checkType(); From 9341acd1291c41abbfbe48a678a57ada741f4bf0 Mon Sep 17 00:00:00 2001 From: Oskar Wallgren Date: Thu, 19 Oct 2017 03:35:55 +0200 Subject: [PATCH 2/2] Remove obsolete function - qSort() --- src/tracks/Pattern.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tracks/Pattern.cpp b/src/tracks/Pattern.cpp index cf02d6476..e726ca2ee 100644 --- a/src/tracks/Pattern.cpp +++ b/src/tracks/Pattern.cpp @@ -271,7 +271,7 @@ Note * Pattern::noteAtStep( int _step ) void Pattern::rearrangeAllNotes() { // sort notes by start time - qSort(m_notes.begin(), m_notes.end(), Note::lessThan ); + std::sort(m_notes.begin(), m_notes.end(), Note::lessThan); }