diff base/EventSeries.cpp @ 1653:eaad70939848 single-point

Add nearest-event-matching search
author Chris Cannam
date Tue, 19 Mar 2019 14:24:05 +0000
parents 513192aa9b03
children 26aa42fd60e9
line wrap: on
line diff
--- a/base/EventSeries.cpp	Tue Mar 19 13:05:56 2019 +0000
+++ b/base/EventSeries.cpp	Tue Mar 19 14:24:05 2019 +0000
@@ -401,6 +401,43 @@
     return true;
 }
 
+bool
+EventSeries::getNearestEventMatching(sv_frame_t startSearchAt,
+                                     std::function<bool(const Event &)> predicate,
+                                     Direction direction,
+                                     Event &found) const
+{
+    auto pitr = lower_bound(m_events.begin(), m_events.end(),
+                            Event(startSearchAt));
+
+    while (true) {
+
+        if (direction == Backward) {
+            if (pitr == m_events.begin()) {
+                break;
+            } else {
+                --pitr;
+            }
+        } else {
+            if (pitr == m_events.end()) {
+                break;
+            }
+        }
+
+        const Event &e = *pitr;
+        if (predicate(e)) {
+            found = e;
+            return true;
+        }
+
+        if (direction == Forward) {
+            ++pitr;
+        }
+    }
+
+    return false;
+}
+
 Event
 EventSeries::getEventByIndex(int index) const
 {