-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
295 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Finds overlaps in arrays of byte ranges, using ratcheting pointers | ||
* instead of nested loops for O(n) runtime instead of O(n^2) | ||
*/ | ||
export function findCoveredBytes(coveredRanges, moduleRanges): { [module: string]: number } { | ||
const sortedCoverageRanges = coveredRanges.sort((a, b) => a.start - b.start); | ||
const sortedModuleRanges = moduleRanges.sort((a, b) => a.start - b.start); | ||
|
||
let i = 0; | ||
let j = 0; | ||
const sizes = {}; | ||
|
||
while (i < moduleRanges.length && j < coveredRanges.length) { | ||
const moduleRange = sortedModuleRanges[i]; | ||
const coverageRange = sortedCoverageRanges[j]; | ||
|
||
if (moduleRange.start <= coverageRange.end && moduleRange.end >= coverageRange.start) { | ||
// overlaps, calculate amount, move to next coverage range | ||
const end = Math.min(coverageRange.end, moduleRange.end); | ||
const start = Math.max(moduleRange.start, coverageRange.start); | ||
if (sizes[moduleRange.module] === undefined) { | ||
sizes[moduleRange.module] = 0; | ||
} | ||
sizes[moduleRange.module] += end - start + 1; | ||
|
||
if ( | ||
sortedModuleRanges[i + 1] !== undefined && | ||
sortedModuleRanges[i + 1].start <= coverageRange.end && | ||
sortedModuleRanges[i + 1].end >= coverageRange.start | ||
) { | ||
// next module also overlaps current coverage range, advance to next module instead of advancing coverage | ||
i++; | ||
} else { | ||
// check next coverage range, it may also overlap this module range | ||
j++; | ||
} | ||
} else if (moduleRange.end < coverageRange.start) { | ||
// module comes entirely before coverageRange, check next module range | ||
i++; | ||
} | ||
if (coverageRange.end < moduleRange.start) { | ||
// module range comes entirely after coverage range, check next coverage range | ||
j++; | ||
} | ||
} | ||
|
||
return sizes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.