cueAt method
The cue showing at position, or null.
A binary search, because this runs on every position report and a feature film carries a couple of thousand cues.
Implementation
DVCaptionCue? cueAt(Duration position) {
final List<DVCaptionCue> all = cues;
int low = 0;
int high = all.length - 1;
int found = -1;
while (low <= high) {
final int mid = (low + high) >> 1;
if (all[mid].start <= position) {
found = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
// Overlapping cues: the latest one to start that still covers.
for (int i = found; i >= 0 && i > found - 4; i--) {
if (all[i].covers(position)) return all[i];
}
return null;
}