changeset 1624:dcd510bd89db single-point

Try out Qt containers
author Chris Cannam
date Mon, 11 Mar 2019 11:17:30 +0000
parents 676e32fa403e
children 7dbb2a7b592e
files base/EventSeries.h
diffstat 1 files changed, 50 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/base/EventSeries.h	Mon Mar 11 11:17:18 2019 +0000
+++ b/base/EventSeries.h	Mon Mar 11 11:17:30 2019 +0000
@@ -19,6 +19,9 @@
 
 #include <set>
 
+#include <QHash>
+#include <QMap>
+
 //#define DEBUG_EVENT_SERIES 1
 
 /**
@@ -78,7 +81,7 @@
                            << endl;
                     break;
                 }
-                i->second.insert(p);
+                i.value().insert(p);
             }
         }
 
@@ -100,7 +103,7 @@
         if (pitr == m_events.end()) {
             return; // we don't know this event
         } else {
-            if (--(pitr->second) == 0) {
+            if (--(pitr.value()) == 0) {
                 isUnique = true;
                 m_events.erase(pitr);
             }
@@ -132,7 +135,7 @@
                     throw std::logic_error("unexpectedly reached end of map");
                 }
                 
-                i->second.erase(p);
+                i.value().remove(p);
             }
 
             // Tidy up by removing any entries that are now identical
@@ -147,8 +150,8 @@
             }
 
             for (auto i = i0; i != m_seams.end(); ++i) {
-                if (pitr != m_seams.end() && i->second == pitr->second) {
-                    redundant.push_back(i->first);
+                if (pitr != m_seams.end() && i.value() == pitr.value()) {
+                    redundant.push_back(i.key());
                 }
                 pitr = i;
                 if (i == i1) {
@@ -157,7 +160,7 @@
             }
 
             for (sv_frame_t f: redundant) {
-                m_seams.erase(f);
+                m_seams.remove(f);
             }
         }
 
@@ -212,11 +215,11 @@
         
         // first find any zero-duration events
         
-        auto pitr = m_events.lower_bound(Event(start, QString()));
-        while (pitr != m_events.end() && pitr->first.getFrame() < end) {
-            if (!pitr->first.hasDuration()) {
-                for (int i = 0; i < pitr->second; ++i) {
-                    span.push_back(pitr->first);
+        auto pitr = m_events.lowerBound(Event(start, QString()));
+        while (pitr != m_events.end() && pitr.key().getFrame() < end) {
+            if (!pitr.key().hasDuration()) {
+                for (int i = 0; i < pitr.value(); ++i) {
+                    span.push_back(pitr.key());
                 }
             }
             ++pitr;
@@ -225,20 +228,20 @@
         // now any non-zero-duration ones from the seam map
 
         std::set<Event> found;
-        auto sitr = m_seams.lower_bound(start);
-        if (sitr == m_seams.end() || sitr->first > start) {
+        auto sitr = m_seams.lowerBound(start);
+        if (sitr == m_seams.end() || sitr.key() > start) {
             if (sitr != m_seams.begin()) {
                 --sitr;
             }                
         }
-        while (sitr != m_seams.end() && sitr->first < end) {
-            for (const auto &p: sitr->second) {
+        while (sitr != m_seams.end() && sitr.key() < end) {
+            for (const auto &p: sitr.value()) {
                 found.insert(p);
             }
             ++sitr;
         }
         for (const auto &p: found) {
-            int n = m_events.at(p);
+            int n = m_events.value(p);
             if (n < 1) {
                 throw std::logic_error("event is in seams but not events");
             }
@@ -262,33 +265,39 @@
 
         // first find any zero-duration events
         
-        auto pitr = m_events.lower_bound(Event(frame, QString()));
-        while (pitr != m_events.end() && pitr->first.getFrame() == frame) {
-            if (!pitr->first.hasDuration()) {
-                for (int i = 0; i < pitr->second; ++i) {
-                    cover.push_back(pitr->first);
+        auto pitr = m_events.lowerBound(Event(frame, QString()));
+        while (pitr != m_events.end() && pitr.key().getFrame() == frame) {
+            if (!pitr.key().hasDuration()) {
+                for (int i = 0; i < pitr.value(); ++i) {
+                    cover.push_back(pitr.key());
                 }
             }
             ++pitr;
         }
 
-        // now any non-zero-duration ones from the seam map
+        // now any non-zero-duration ones from the seam map. We insert
+        // these into a std::set to recover the ordering, lost by QSet
         
-        auto sitr = m_seams.lower_bound(frame);
-        if (sitr == m_seams.end() || sitr->first > frame) {
+        std::set<Event> found;
+        auto sitr = m_seams.lowerBound(frame);
+        if (sitr == m_seams.end() || sitr.key() > frame) {
             if (sitr != m_seams.begin()) {
                 --sitr;
             }                
         }
-        if (sitr != m_seams.end() && sitr->first <= frame) {
-            for (const auto &p: sitr->second) {
-                int n = m_events.at(p);
-                if (n < 1) {
-                    throw std::logic_error("event is in seams but not events");
-                }
-                for (int i = 0; i < n; ++i) {
-                    cover.push_back(p);
-                }
+        if (sitr != m_seams.end() && sitr.key() <= frame) {
+            for (const auto &p: sitr.value()) {
+                found.insert(p);
+            }
+            ++sitr;
+        }
+        for (const auto &p: found) {
+            int n = m_events.value(p);
+            if (n < 1) {
+                throw std::logic_error("event is in seams but not events");
+            }
+            for (int i = 0; i < n; ++i) {
+                cover.push_back(p);
             }
         }
         
@@ -301,7 +310,7 @@
      * m_events.size() if the series contains duplicate events.
      */
     int m_count;
-
+    
     /**
      * The (ordered) Events map acts as a list of all the events in
      * the series. For reasons of backward compatibility, we have to
@@ -316,7 +325,7 @@
      * different "key" in this map - we only add or delete them or
      * change their counts.
      */
-    typedef std::map<Event, int> Events;
+    typedef QMap<Event, int> Events;
     Events m_events;
 
     /**
@@ -331,7 +340,7 @@
      * Only events with duration appear in this map; point events
      * appear only in m_events.
      */
-    typedef std::map<sv_frame_t, std::set<Event>> FrameEventMap;
+    typedef QMap<sv_frame_t, QSet<Event>> FrameEventMap;
     FrameEventMap m_seams;
 
     /** Create a seam at the given frame, copying from the prior seam
@@ -339,17 +348,17 @@
      *  leave it untouched.
      */
     void createSeam(sv_frame_t frame) {
-        auto itr = m_seams.lower_bound(frame);
-        if (itr == m_seams.end() || itr->first > frame) {
+        auto itr = m_seams.lowerBound(frame);
+        if (itr == m_seams.end() || itr.key() > frame) {
             if (itr != m_seams.begin()) {
                 --itr;
             }
         }
         if (itr == m_seams.end()) {
             m_seams[frame] = {};
-        } else if (itr->first < frame) {
-            m_seams[frame] = itr->second;
-        } else if (itr->first > frame) { // itr must be begin()
+        } else if (itr.key() < frame) {
+            m_seams[frame] = itr.value();
+        } else if (itr.key() > frame) { // itr must be begin()
             m_seams[frame] = {};
         }
     }