Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 210688
Collapse All | Expand All

(-)compare/org/eclipse/compare/rangedifferencer/RangeComparatorLCS.java (+54 lines)
Lines 133-136 Link Here
133
		subMonitor.worked(work);	
133
		subMonitor.worked(work);	
134
	}
134
	}
135
135
136
	/**
137
	 * This method takes an LCS result interspersed with zeros (i.e. empty slots 
138
	 * from the LCS algorithm), compacts it and shifts the LCS chunks as far towards 
139
	 * the front as possible. This tends to produce good results most of the time.
140
	 * 
141
	 * @param lcsSide A subsequence of original, presumably it is the LCS of it and
142
	 *            some other collection of lines
143
	 * @param length The number of non-empty (i.e non-zero) entries in LCS
144
	 * @param comparator The comparator used to generate the LCS
145
	 */
146
	 private void compactAndShiftLCS(int[] lcsSide, int length,
147
			IRangeComparator comparator) {
148
		// If the LCS is empty, just return
149
		if (length == 0)
150
			return;
151
		// Skip any leading empty slots
152
		int j = 0;
153
		while (lcsSide[j] == 0) {
154
			j++;
155
		}
156
		// Put the first non-empty value in position 0
157
		lcsSide[0] = lcsSide[j];
158
		j++;
159
		// Push all non-empty values down into the first N slots (where N is the length)
160
		for (int i = 1; i < length; i++) {
161
			while (lcsSide[j] == 0) {
162
				j++;
163
			}
164
			// Push the difference down as far as possible by comparing the line at the 
165
			// start of the diff with the line and the end and adjusting if they are the same
166
			int nextLine = lcsSide[i - 1] + 1;
167
			if (nextLine != lcsSide[j] && comparator.rangesEqual(nextLine - 1, comparator, lcsSide[j] - 1)) {
168
				lcsSide[i] = nextLine;
169
			} else {
170
				lcsSide[i] = lcsSide[j];
171
			}
172
			j++;
173
		}
174
		// Zero all slots after the length
175
		for (int i = length; i < lcsSide.length; i++) {
176
			lcsSide[i] = 0;
177
		}
178
	}
179
	
180
	/* (non-Javadoc)
181
	 * @see org.eclipse.compare.internal.LCS#longestCommonSubsequence(org.eclipse.core.runtime.SubMonitor)
182
	 */
183
	public void longestCommonSubsequence(SubMonitor subMonitor) {
184
		super.longestCommonSubsequence(subMonitor);
185
		if (lcs != null) { // The LCS can be null if one of the sides is empty
186
			compactAndShiftLCS(lcs[0], getLength(), comparator1);
187
			compactAndShiftLCS(lcs[1], getLength(), comparator2);
188
		}
189
	}
136
}
190
}

Return to bug 210688