@@ -8,82 +8,23 @@ public class TranslationMapping
88    { 
99        private  bool  constructed ; 
1010
11-         private  readonly  List < int >  originalIndexes  =  new ( ) ; 
12-         private  readonly  List < int >  translatedIndexes  =  new ( ) ; 
11+         // Asssuming one original item maps to multi translated items 
12+         // list[i] is the last translated index + 1 of original index i 
13+         private  readonly  List < int >  originalToTranslated  =  [ ] ; 
1314
14-         private  int  translatedLength  =  0 ; 
15- 
16-         public  void  AddNewIndex ( int  originalIndex ,  int  translatedIndex ,  int  length ) 
15+         public  void  AddNewIndex ( int  translatedIndex ,  int  length ) 
1716        { 
1817            if  ( constructed ) 
1918                throw  new  InvalidOperationException ( "Mapping shouldn't be changed after constructed" ) ; 
2019
21-             originalIndexes . Add ( originalIndex ) ; 
22-             translatedIndexes . Add ( translatedIndex ) ; 
23-             translatedIndexes . Add ( translatedIndex  +  length ) ; 
24-             translatedLength  +=  length  -  1 ; 
20+             originalToTranslated . Add ( translatedIndex  +  length ) ; 
2521        } 
2622
2723        public  int  MapToOriginalIndex ( int  translatedIndex ) 
2824        { 
29-             if  ( translatedIndex  >  translatedIndexes . Last ( ) ) 
30-                 return  translatedIndex  -  translatedLength  -  1 ; 
31- 
32-             int  lowerBound  =  0 ; 
33-             int  upperBound  =  originalIndexes . Count  -  1 ; 
34- 
35-             int  count  =  0 ; 
36- 
37-             // Corner case handle 
38-             if  ( translatedIndex  <  translatedIndexes [ 0 ] ) 
39-                 return  translatedIndex ; 
40- 
41-             if  ( translatedIndex  >  translatedIndexes . Last ( ) ) 
42-             { 
43-                 int  indexDef  =  0 ; 
44-                 for  ( int  k  =  0 ;  k  <  originalIndexes . Count ;  k ++ ) 
45-                 { 
46-                     indexDef  +=  translatedIndexes [ k  *  2  +  1 ]  -  translatedIndexes [ k  *  2 ] ; 
47-                 } 
48- 
49-                 return  translatedIndex  -  indexDef  -  1 ; 
50-             } 
51- 
52-             // Binary Search with Range 
53-             for  ( int  i  =  originalIndexes . Count  /  2 ; ;  count ++ ) 
54-             { 
55-                 if  ( translatedIndex  <  translatedIndexes [ i  *  2 ] ) 
56-                 { 
57-                     // move to lower middle 
58-                     upperBound  =  i ; 
59-                     i  =  ( i  +  lowerBound )  /  2 ; 
60-                 } 
61-                 else  if  ( translatedIndex  >  translatedIndexes [ i  *  2  +  1 ]  -  1 ) 
62-                 { 
63-                     lowerBound  =  i ; 
64-                     // move to upper middle 
65-                     // due to floor of integer division, move one up on corner case 
66-                     i  =  ( i  +  upperBound  +  1 )  /  2 ; 
67-                 } 
68-                 else 
69-                 { 
70-                     return  originalIndexes [ i ] ; 
71-                 } 
72- 
73-                 if  ( upperBound  -  lowerBound  <=  1  && 
74-                     translatedIndex  >  translatedIndexes [ lowerBound  *  2  +  1 ]  && 
75-                     translatedIndex  <  translatedIndexes [ upperBound  *  2 ] ) 
76-                 { 
77-                     int  indexDef  =  0 ; 
78- 
79-                     for  ( int  j  =  0 ;  j  <  upperBound ;  j ++ ) 
80-                     { 
81-                         indexDef  +=  translatedIndexes [ j  *  2  +  1 ]  -  translatedIndexes [ j  *  2 ] ; 
82-                     } 
25+             int  loc  =  originalToTranslated . BinarySearch ( translatedIndex ) ; 
8326
84-                     return  translatedIndex  -  indexDef  -  1 ; 
85-                 } 
86-             } 
27+             return  loc  >  0  ?  loc  :  ~ loc ; 
8728        } 
8829
8930        public  void  endConstruct ( ) 
0 commit comments