#ifndef _FRAME_ON_DISK_H
#define _FRAME_ON_DISK_H 1
+#include <algorithm>
#include <mutex>
#include <string>
#include <vector>
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)
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;
}
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;
}
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;
}
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;
}
}
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;
}