track/audioplaybackcache: re-did binary search algorithms

Fixes #1617
Fixes #1615
This commit is contained in:
itsmattkc
2021-05-10 10:42:07 +10:00
parent febb5b0d25
commit 8383929476
2 changed files with 28 additions and 36 deletions
+11 -15
View File
@@ -508,27 +508,23 @@ int AudioPlaybackCache::Playlist::GetIndexOfPosition(qint64 pos)
return this->size() - 1;
}
int bottom = 0;
int top = this->size();
// Use a binary search to find the segment with the right offset
while (true) {
int mid = bottom + (top - bottom)/2;
int low = 0;
int high = this->size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
const Segment& mid_segment = this->at(mid);
if (mid_segment.offset() <= pos
&& mid_segment.offset() + mid_segment.size() > pos) {
if (mid_segment.offset() <= pos && mid_segment.offset() + mid_segment.size() > pos) {
return mid;
}
if (mid_segment.offset() > pos) {
// Segment we're looking for must be lower
top = mid;
} else if (mid_segment.offset() < pos) {
low = mid + 1;
} else {
// Segment we're looking for must be higher
bottom = mid;
high = mid - 1;
}
}
return -1;
}
qint64 AudioPlaybackCache::Playlist::GetLength() const