Skip to content

Commit

Permalink
update binary search to ignore end ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
Irev-Dev committed Feb 24, 2025
1 parent 1f0ab8d commit d82a541
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/lib/selections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,11 @@ function findOverlappingArtifactsFromIndex(
const selectionRange = selection.codeRef.range
const results: ArtifactEntry[] = []

// Binary search to find the first range that could overlap
// Look for the first range that ends after our selection starts
// Binary search to find the last range where range[0] < selectionRange[0]
// This search does not take into consideration the end range, so it's possible
// the index it finds dose not have any overlap (depending on the end range)
// but it's main purpose is to act as a starting point for the linear part of the search
// so a tiny loss in efficiency is acceptable to keep the code simple
let left = 0
let right = index.length - 1
let startIndex = 0
Expand All @@ -517,12 +520,12 @@ function findOverlappingArtifactsFromIndex(
const mid = left + Math.floor((right - left) / 2)
const midRange = index[mid].range

if (midRange[1] < selectionRange[0]) {
// This range ends before our selection starts, look in right half
if (midRange[0] < selectionRange[0]) {
// This range starts before our selection, look in right half for later ones
startIndex = mid
left = mid + 1
} else {
// This range might overlap, check left half for earlier overlaps
startIndex = mid
// This range starts at or after our selection, look in left half
right = mid - 1
}
}
Expand Down

0 comments on commit d82a541

Please sign in to comment.