Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2006, 2007 IBM Corporation and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: IBM Corporation - initial API and implementation /
 
 package org.eclipse.compare.rangedifferencer;
 
 import java.util.List;
 
 
 /* package */class RangeComparatorLCS extends LCS {
 
     private final IRangeComparator comparator1comparator2;
 
     private int[][] lcs;
 
     public static RangeDifference[] findDifferences(
             LCSSettings settingsIRangeComparator leftIRangeComparator right) {
         RangeComparatorLCS lcs = new RangeComparatorLCS(leftright);
         lcs.longestCommonSubsequencesettings);
         return lcs.getDifferences();
     }
 
     public RangeComparatorLCS(IRangeComparator comparator1,
             IRangeComparator comparator2) {
         this. = comparator1;
         this. = comparator2;
     }
 
     @Override
     protected int getLength1() {
         return .getRangeCount();
     }
 
     @Override
     protected int getLength2() {
         return .getRangeCount();
     }
 
     @Override
     protected void initializeLcs(int lcsLength) {
          = new int[2][lcsLength];
     }
 
     @Override
     protected boolean isRangeEqual(int i1int i2) {
         return .rangesEqual(i1i2);
     }
 
     @Override
     protected void setLcs(int sl1int sl2) {
         // Add one to the values so that 0 can mean that the slot is empty
         [0][sl1] = sl1 + 1;
         [1][sl1] = sl2 + 1;
     }
 
     public RangeDifference[] getDifferences() {
     	
         List differences = new ArrayList();
         int length = getLength();
         if (length == 0) {
             differences.add(new RangeDifference(., 0,
                     .getRangeCount(), 0, 
                             .getRangeCount()));
         } else {
             int index1index2;
             index1 = index2 = 0;
             int l1l2;
             int s1 = -1;
             int s2 = -1;
             while (index1 < [0].length && index2 < [1].length) {
                 // Move both LCS lists to the next occupied slot
                 while ((l1 = [0][index1]) == 0) {
                     index1++;
                     if (index1 >= [0].length)
                         break;
                 }
                 if (index1 >= [0].length)
                     break;
                 while ((l2 = [1][index2]) == 0) {
                     index2++;
                     if (index2 >= [1].length)
                         break;
                 }
                 if (index2 >= [1].length)
                     break;
                 // Convert the entry to an array index (see setLcs(int,
                 // int))
                 int end1 = l1 - 1;
                 int end2 = l2 - 1;
                if (s1 == -1 && (end1 != 0 || end2 != 0)) {
                    // There is a diff at the beginning
                    // TODO: We need to conform that this is the proper
                    // order
                    differences.add(new RangeDifference(
                            ., 0, end2, 0, end1));
                } else if (end1 != s1 + 1 || end2 != s2 + 1) {
                    // A diff was found on one of the sides
                    int leftStart = s1 + 1;
                    int leftLength = end1 - leftStart;
                    int rightStart = s2 + 1;
                    int rightLength = end2 - rightStart;
                    // TODO: We need to conform that this is the proper
                    // order
                    differences.add(new RangeDifference(
                            .rightStart,
                            rightLengthleftStartleftLength));
                }
                s1 = end1;
                s2 = end2;
                index1++;
                index2++;
            }
            if (s1 != -1
                    && (s1 + 1 < .getRangeCount() || s2 + 1 < 
                            .getRangeCount())) {
                // TODO: we need to find the proper way of representing an
                // append
                int leftStart = s1 < .getRangeCount() ? s1 + 1
                        : s1;
                int rightStart = s2 < .getRangeCount() ? s2 + 1
                        : s2;
                // TODO: We need to conform that this is the proper order
                differences.add(new RangeDifference(.,
                        rightStart.getRangeCount() - (s2 + 1),
                        leftStart.getRangeCount() - (s1 + 1)));
            }
        }
        return (RangeDifference[]) differences
                .toArray(new RangeDifference[differences.size()]);
    }
New to GrepCode? Check out our FAQ X