@ -99,7 +99,7 @@ package org.eclipse.jgit.diff;
* by the prior step 2 or 5 . < / li >
* by the prior step 2 or 5 . < / li >
* < / ol >
* < / ol >
* /
* /
public class PatienceDiff implement s DiffAlgorithm {
public class PatienceDiff extend s DiffAlgorithm {
/** Algorithm we use when there are no common unique lines in a region. */
/** Algorithm we use when there are no common unique lines in a region. */
private DiffAlgorithm fallback ;
private DiffAlgorithm fallback ;
@ -114,38 +114,10 @@ public class PatienceDiff implements DiffAlgorithm {
fallback = alg ;
fallback = alg ;
}
}
public < S extends Sequence , C extends SequenceComparator < ? super S > > EditList diff (
public < S extends Sequence > EditList diffNonCommon (
C cmp , S a , S b ) {
SequenceComparator < ? super S > cmp , S a , S b ) {
Edit region = new Edit ( 0 , a . size ( ) , 0 , b . size ( ) ) ;
region = cmp . reduceCommonStartEnd ( a , b , region ) ;
switch ( region . getType ( ) ) {
case INSERT :
case DELETE : {
EditList r = new EditList ( ) ;
r . add ( region ) ;
return r ;
}
case REPLACE : {
SubsequenceComparator < S > cs = new SubsequenceComparator < S > ( cmp ) ;
Subsequence < S > as = Subsequence . a ( a , region ) ;
Subsequence < S > bs = Subsequence . b ( b , region ) ;
return Subsequence . toBase ( diffImpl ( cs , as , bs ) , as , bs ) ;
}
case EMPTY :
return new EditList ( ) ;
default :
throw new IllegalStateException ( ) ;
}
}
private < S extends Sequence , C extends SequenceComparator < ? super S > > EditList diffImpl (
C cmp , S a , S b ) {
State < S > s = new State < S > ( new HashedSequencePair < S > ( cmp , a , b ) ) ;
State < S > s = new State < S > ( new HashedSequencePair < S > ( cmp , a , b ) ) ;
s . diff ( new Edit ( 0 , s . a . size ( ) , 0 , s . b . size ( ) ) , null , 0 , 0 ) ;
s . diffReplace ( new Edit ( 0 , s . a . size ( ) , 0 , s . b . size ( ) ) , null , 0 , 0 ) ;
return s . edits ;
return s . edits ;
}
}
@ -166,25 +138,12 @@ public class PatienceDiff implements DiffAlgorithm {
this . edits = new EditList ( ) ;
this . edits = new EditList ( ) ;
}
}
private void diff ( Edit r , long [ ] pCommon , int pIdx , int pEnd ) {
void diffReplace ( Edit r , long [ ] pCommon , int pIdx , int pEnd ) {
switch ( r . getType ( ) ) {
case INSERT :
case DELETE :
edits . add ( r ) ;
return ;
case REPLACE :
break ;
case EMPTY :
default :
throw new IllegalStateException ( ) ;
}
PatienceDiffIndex < S > p ;
PatienceDiffIndex < S > p ;
Edit lcs ;
p = new PatienceDiffIndex < S > ( cmp , a , b , r , pCommon , pIdx , pEnd ) ;
p = new PatienceDiffIndex < S > ( cmp , a , b , r , pCommon , pIdx , pEnd ) ;
Edit lcs = p . findLongestCommonSequence ( ) ;
lcs = p . findLongestCommonSequence ( ) ;
if ( lcs ! = null ) {
if ( lcs ! = null ) {
pCommon = p . nCommon ;
pCommon = p . nCommon ;
@ -196,20 +155,40 @@ public class PatienceDiff implements DiffAlgorithm {
diff ( r . after ( lcs ) , pCommon , pIdx + 1 , pEnd ) ;
diff ( r . after ( lcs ) , pCommon , pIdx + 1 , pEnd ) ;
} else if ( fallback ! = null ) {
} else if ( fallback ! = null ) {
p = null ;
pCommon = null ;
pCommon = null ;
p = null ;
SubsequenceComparator < HashedSequence < S > > cs ;
SubsequenceComparator < HashedSequence < S > > cs = subcmp ( ) ;
cs = new SubsequenceComparator < HashedSequence < S > > ( cmp ) ;
Subsequence < HashedSequence < S > > as = Subsequence . a ( a , r ) ;
Subsequence < HashedSequence < S > > as = Subsequence . a ( a , r ) ;
Subsequence < HashedSequence < S > > bs = Subsequence . b ( b , r ) ;
Subsequence < HashedSequence < S > > bs = Subsequence . b ( b , r ) ;
EditList res = fallback . diff ( cs , as , bs ) ;
EditList res = fallback . diffNonCommon ( cs , as , bs ) ;
edits . addAll ( Subsequence . toBase ( res , as , bs ) ) ;
edits . addAll ( Subsequence . toBase ( res , as , bs ) ) ;
} else {
} else {
edits . add ( r ) ;
edits . add ( r ) ;
}
}
}
}
private void diff ( Edit r , long [ ] pCommon , int pIdx , int pEnd ) {
switch ( r . getType ( ) ) {
case INSERT :
case DELETE :
edits . add ( r ) ;
break ;
case REPLACE :
diffReplace ( r , pCommon , pIdx , pEnd ) ;
break ;
case EMPTY :
default :
throw new IllegalStateException ( ) ;
}
}
private SubsequenceComparator < HashedSequence < S > > subcmp ( ) {
return new SubsequenceComparator < HashedSequence < S > > ( cmp ) ;
}
}
}
}
}