]> git.sesse.net Git - nageru/commitdiff
Add some binary search helpers.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 12 Dec 2018 23:36:03 +0000 (00:36 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 12 Dec 2018 23:36:03 +0000 (00:36 +0100)
futatabi/frame_on_disk.h
futatabi/mainwindow.cpp
futatabi/player.cpp

index 184385792528130df7e3ba16637385c0c14a35db..47fcb32d349d1b132835500374cb12d14dd98235 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef _FRAME_ON_DISK_H
 #define _FRAME_ON_DISK_H 1
 
+#include <algorithm>
 #include <mutex>
 #include <string>
 #include <vector>
@@ -34,4 +35,19 @@ private:
        int last_filename_idx = -1;
 };
 
+// Utility functions for dealing with binary search.
+inline std::vector<FrameOnDisk>::iterator
+find_last_frame_before(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
+{
+       return std::lower_bound(frames.begin(), frames.end(), pts_origin,
+               [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; });
+}
+
+inline std::vector<FrameOnDisk>::iterator
+find_first_frame_at_or_after(std::vector<FrameOnDisk> &frames, int64_t pts_origin)
+{
+       return std::upper_bound(frames.begin(), frames.end(), pts_origin - 1,
+               [](int64_t pts, const FrameOnDisk &frame) { return pts < frame.pts; });
+}
+
 #endif  // !defined(_FRAME_ON_DISK_H)
index 24a9d5b7e4e7db58e1d6bc894e060ba23548e485..57693ef91b2f47ade0a4a3b143af5dc68c3e73c0 100644 (file)
@@ -661,8 +661,7 @@ void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWind
                lock_guard<mutex> lock(frame_mu);
                if (frames[stream_idx].empty())
                        return;
-               auto it = lower_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts,
-                       [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; });
+               auto it = find_last_frame_before(frames[stream_idx], pts);
                if (it != frames[stream_idx].end()) {
                        pts = it->pts;
                }
@@ -671,8 +670,7 @@ void MainWindow::preview_single_frame(int64_t pts, unsigned stream_idx, MainWind
                lock_guard<mutex> lock(frame_mu);
                if (frames[stream_idx].empty())
                        return;
-               auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts - 1,
-                       [](int64_t pts, const FrameOnDisk &frame) { return pts < frame.pts; });
+               auto it = find_first_frame_at_or_after(frames[stream_idx], pts);
                if (it != frames[stream_idx].end()) {
                        pts = it->pts;
                }
index e20a43cc26bf84fe920ac9dff3b2892d96ee6644..a5d093ade2bba23422d092b382233f24f8cb9539 100644 (file)
@@ -103,10 +103,7 @@ got_clip:
                        lock_guard<mutex> lock(frame_mu);
 
                        // Find the first frame such that frame.pts <= in_pts.
-                       auto it = lower_bound(frames[stream_idx].begin(),
-                               frames[stream_idx].end(),
-                               in_pts_origin,
-                               [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; });
+                       auto it = find_last_frame_before(frames[stream_idx], in_pts_origin);
                        if (it != frames[stream_idx].end()) {
                                in_pts_origin = it->pts;
                        }
@@ -363,10 +360,7 @@ bool Player::find_surrounding_frames(int64_t pts, int stream_idx, FrameOnDisk *f
        lock_guard<mutex> lock(frame_mu);
 
        // Find the first frame such that frame.pts >= pts.
-       auto it = lower_bound(frames[stream_idx].begin(),
-               frames[stream_idx].end(),
-               pts,
-               [](const FrameOnDisk &frame, int64_t pts) { return frame.pts < pts; });
+       auto it = find_last_frame_before(frames[stream_idx], pts);
        if (it == frames[stream_idx].end()) {
                return false;
        }
@@ -451,8 +445,7 @@ void Player::override_angle(unsigned stream_idx)
        }
 
        lock_guard<mutex> lock(frame_mu);
-       auto it = upper_bound(frames[stream_idx].begin(), frames[stream_idx].end(), pts_out,
-               [](int64_t pts, const FrameOnDisk &frame) { return pts < frame.pts; });
+       auto it = find_first_frame_at_or_after(frames[stream_idx], pts_out);
        if (it == frames[stream_idx].end()) {
                return;
        }