11using FineCodeCoverage . Engine . Cobertura ;
2- using FineCodeCoverage . Engine . Model ;
3- using Newtonsoft . Json . Linq ;
42using System ;
53using System . Collections . Generic ;
64using System . Linq ;
7- using System . Text ;
8- using System . Windows . Documents ;
9- using System . Windows . Media ;
105
11- namespace SharedProject . Core . Model
6+ namespace FineCodeCoverage . Engine . Model
127{
138 // FileLineCoverage maps from a filename to the list of lines in the file
149 internal class FileLineCoverage
@@ -18,29 +13,48 @@ internal class FileLineCoverage
1813
1914 public void Add ( string filename , IEnumerable < Line > lines )
2015 {
21- if ( ! m_coverageLines . TryGetValue ( filename , out var classCoverageLines ) )
16+ if ( ! m_coverageLines . TryGetValue ( filename , out var fileCoverageLines ) )
2217 {
23- classCoverageLines = new List < Line > ( ) ;
24- m_coverageLines . Add ( filename , classCoverageLines ) ;
18+ fileCoverageLines = new List < Line > ( ) ;
19+ m_coverageLines . Add ( filename , fileCoverageLines ) ;
2520 }
2621
27- classCoverageLines . AddRange ( lines ) ;
28- classCoverageLines . Sort ( ( a , b ) => a . Number - b . Number ) ;
22+ fileCoverageLines . AddRange ( lines ) ;
23+ }
24+
25+ internal void Completed ( )
26+ {
27+ foreach ( var lines in m_coverageLines . Values )
28+ lines . Sort ( ( a , b ) => a . Number - b . Number ) ;
2929 }
3030
3131 public IEnumerable < Line > GetLines ( string filePath , int startLineNumber , int endLineNumber )
3232 {
3333 if ( ! m_coverageLines . TryGetValue ( filePath , out var lines ) )
34- yield break ;
34+ return Enumerable . Empty < Line > ( ) ;
3535
36+ int first = lines . LowerBound ( line => line . Number < startLineNumber ) ;
37+ int last = first ;
38+ while ( last < lines . Count && lines [ last ] . Number <= endLineNumber )
39+ ++ last ;
40+
41+ return lines . GetRange ( first , last ) ;
42+ }
43+ }
44+
45+ public static class ListExtensions
46+ {
47+ // Returns the index of the first element in a sorted list where the comparison function is false
48+ public static int LowerBound < T > ( this IList < T > list , Func < T , bool > compare )
49+ {
3650 // binary search to find the first line
3751 int first = 0 ;
38- int count = lines . Count ;
52+ int count = list . Count ;
3953 while ( count > 0 )
4054 {
4155 int step = count / 2 ;
4256 int it = first + step ;
43- if ( lines [ it ] . Number < startLineNumber )
57+ if ( compare ( list [ it ] ) )
4458 {
4559 first = ++ it ;
4660 count -= step + 1 ;
@@ -51,8 +65,7 @@ public IEnumerable<Line> GetLines(string filePath, int startLineNumber, int endL
5165 }
5266 }
5367
54- for ( int it = first ; it < lines . Count && lines [ it ] . Number <= endLineNumber ; ++ it )
55- yield return lines [ it ] ;
68+ return first ;
5669 }
5770 }
5871}
0 commit comments