Skip to main content
summaryrefslogtreecommitdiffstats
blob: 480e3c08e3f4781545e86dc4bcc3a15d6715850a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*******************************************************************************
 * Copyright (c) 2000, 2014 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal.core;

import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;


/* Used to determine the change set responsible for each line */
public abstract class LCS {
	public static final double TOO_LONG = 100000000.0;	// 10^8, the value of N*M when
														// to start binding the
														// run time

	private static final double POW_LIMIT = 1.5; // limit the time to
													// D^POW_LIMIT

	private int max_differences; // the maximum number of differences from
									// each end to consider

	private int length;

	/**
	 * Myers' algorithm for longest common subsequence. O((M + N)D) worst case
	 * time, O(M + N + D^2) expected time, O(M + N) space
	 * (http://citeseer.ist.psu.edu/myers86ond.html)
	 *
	 * Note: Beyond implementing the algorithm as described in the paper I have
	 * added diagonal range compression which helps when finding the LCS of a
	 * very long and a very short sequence, also bound the running time to (N +
	 * M)^1.5 when both sequences are very long.
	 *
	 * After this method is called, the longest common subsequence is available
	 * by calling getResult() where result[0] is composed of
	 * entries from l1 and result[1] is composed of entries from l2
	 * @param subMonitor
	 */
	public void longestCommonSubsequence(SubMonitor subMonitor) {
		int length1 = getLength1();
		int length2 = getLength2();
		if (length1 == 0 || length2 == 0) {
			this.length = 0;
			return;
		}

		this.max_differences = (length1 + length2 + 1) / 2; // ceil((N+M)/2)
		if (!isCappingDisabled() && (double) length1 * (double) length2 > TOO_LONG) {
			// limit complexity to D^POW_LIMIT for long sequences
			this.max_differences = (int) Math.pow(this.max_differences, POW_LIMIT - 1.0);
		}

		initializeLcs(length1);

		subMonitor.beginTask(null, length1);

		/*
		 * The common prefixes and suffixes are always part of some LCS, include
		 * them now to reduce our search space
		 */
		int forwardBound;
		int max = Math.min(length1, length2);
		for (forwardBound = 0; forwardBound < max
				&& isRangeEqual(forwardBound, forwardBound); forwardBound++) {
			setLcs(forwardBound, forwardBound);
			worked(subMonitor, 1);
		}

		int backBoundL1 = length1 - 1;
		int backBoundL2 = length2 - 1;

		while (backBoundL1 >= forwardBound && backBoundL2 >= forwardBound
				&& isRangeEqual(backBoundL1, backBoundL2)) {
			setLcs(backBoundL1, backBoundL2);
			backBoundL1--;
			backBoundL2--;
			worked(subMonitor, 1);
		}

		this.length = forwardBound
				+ length1
				- backBoundL1
				- 1
				+ lcs_rec(forwardBound, backBoundL1, forwardBound,
						backBoundL2, new int[2][length1 + length2 + 1],
						new int[3], subMonitor);

	}

	private boolean isCappingDisabled() {
		return ComparePlugin.getDefault().isCappingDisabled();
	}

	/**
	 * The recursive helper function for Myers' LCS. Computes the LCS of
	 * l1[bottoml1 .. topl1] and l2[bottoml2 .. topl2] fills in the appropriate
	 * location in lcs and returns the length
	 *
	 * @param l1 The 1st sequence
	 * @param bottoml1 Index in the 1st sequence to start from (inclusive)
	 * @param topl1 Index in the 1st sequence to end on (inclusive)
	 * @param l2 The 2nd sequence
	 * @param bottoml2 Index in the 2nd sequence to start from (inclusive)
	 * @param topl2 Index in the 2nd sequence to end on (inclusive)
	 * @param V should be allocated as int[2][l1.length + l2.length + 1], used
	 *            to store furthest reaching D-paths
	 * @param snake should be allocated as int[3], used to store the beginning
	 *            x, y coordinates and the length of the latest snake traversed
	 * @param subMonitor
	 * @param lcs should be allocated as TextLine[2][l1.length], used to store
	 *            the common points found to be part of the LCS where lcs[0]
	 *            references lines of l1 and lcs[1] references lines of l2.
	 *
	 * @return the length of the LCS
	 */
	private int lcs_rec(
			int bottoml1, int topl1,
			int bottoml2, int topl2,
			int[][] V, int[] snake, SubMonitor subMonitor) {

		// check that both sequences are non-empty
		if (bottoml1 > topl1 || bottoml2 > topl2) {
			return 0;
		}

		int d = find_middle_snake(bottoml1, topl1, bottoml2, topl2, V, snake, subMonitor);
		// System.out.println(snake[0] + " " + snake[1] + " " + snake[2]);

		// need to store these so we don't lose them when they're overwritten by
		// the recursion
		int len = snake[2];
		int startx = snake[0];
		int starty = snake[1];

		// the middle snake is part of the LCS, store it
		for (int i = 0; i < len; i++) {
			setLcs(startx + i, starty + i);
			worked(subMonitor, 1);
		}

		if (d > 1) {
			return len
					+ lcs_rec(bottoml1, startx - 1, bottoml2, starty - 1, V, snake, subMonitor)
					+ lcs_rec(startx + len, topl1, starty + len, topl2, V, snake, subMonitor);
		} else if (d == 1) {
			/*
			 * In this case the sequences differ by exactly 1 line. We have
			 * already saved all the lines after the difference in the for loop
			 * above, now we need to save all the lines before the difference.
			 */
			int max = Math.min(startx - bottoml1, starty - bottoml2);
			for (int i = 0; i < max; i++) {
				setLcs(bottoml1 + i, bottoml2 + i);
				worked(subMonitor, 1);
			}
			return max + len;
		}

		return len;
	}

	private void worked(SubMonitor subMonitor, int work) {
		if (subMonitor.isCanceled())
			throw new OperationCanceledException();
		subMonitor.worked(work);
	}

	/**
	 * Helper function for Myers' LCS algorithm to find the middle snake for
	 * l1[bottoml1..topl1] and l2[bottoml2..topl2] The x, y coodrdinates of the
	 * start of the middle snake are saved in snake[0], snake[1] respectively
	 * and the length of the snake is saved in s[2].
	 *
	 * @param l1 The 1st sequence
	 * @param bottoml1 Index in the 1st sequence to start from (inclusive)
	 * @param topl1 Index in the 1st sequence to end on (inclusive)
	 * @param l2 The 2nd sequence
	 * @param bottoml2 Index in the 2nd sequence to start from (inclusive)
	 * @param topl2 Index in the 2nd sequence to end on (inclusive)
	 * @param V should be allocated as int[2][l1.length + l2.length + 1], used
	 *            to store furthest reaching D-paths
	 * @param snake should be allocated as int[3], used to store the beginning
	 *            x, y coordinates and the length of the middle snake
	 * @subMonitor subMonitor
	 *
	 * @return The number of differences (SES) between l1[bottoml1..topl1] and
	 *         l2[bottoml2..topl2]
	 */
	private int find_middle_snake(
			int bottoml1, int topl1,
			int bottoml2, int topl2,
			int[][] V, int[] snake,
			SubMonitor subMonitor) {
		int N = topl1 - bottoml1 + 1;
		int M = topl2 - bottoml2 + 1;
		// System.out.println("N: " + N + " M: " + M + " bottom: " + bottoml1 +
		// ", " +
		// bottoml2 + " top: " + topl1 + ", " + topl2);
		int delta = N - M;
		boolean isEven;
		if ((delta & 1) == 1) {
			isEven = false;
		} else {
			isEven = true;
		}

		int limit = Math.min(this.max_differences, (N + M + 1) / 2); // ceil((N+M)/2)

		int value_to_add_forward; // a 0 or 1 that we add to the start offset
									// to make it odd/even
		if ((M & 1) == 1) {
			value_to_add_forward = 1;
		} else {
			value_to_add_forward = 0;
		}

		int value_to_add_backward;
		if ((N & 1) == 1) {
			value_to_add_backward = 1;
		} else {
			value_to_add_backward = 0;
		}

		int start_forward = -M;
		int end_forward = N;
		int start_backward = -N;
		int end_backward = M;

		V[0][limit + 1] = 0;
		V[1][limit - 1] = N;
		for (int d = 0; d <= limit; d++) {

			int start_diag = Math.max(value_to_add_forward + start_forward, -d);
			int end_diag = Math.min(end_forward, d);
			value_to_add_forward = 1 - value_to_add_forward;

			// compute forward furthest reaching paths
			for (int k = start_diag; k <= end_diag; k += 2) {
				int x;
				if (k == -d
						|| (k < d && V[0][limit + k - 1] < V[0][limit + k + 1])) {
					x = V[0][limit + k + 1];
				} else {
					x = V[0][limit + k - 1] + 1;
				}

				int y = x - k;

				snake[0] = x + bottoml1;
				snake[1] = y + bottoml2;
				snake[2] = 0;
				// System.out.println("1 x: " + x + " y: " + y + " k: " + k + "
				// d: " + d );
				while (x < N && y < M
						&& isRangeEqual(x + bottoml1, y + bottoml2)) {
					x++;
					y++;
					snake[2]++;
				}
				V[0][limit + k] = x;
				// System.out.println(x + " " + V[1][limit+k -delta] + " " + k +
				// " " + delta);
				if (!isEven && k >= delta - d + 1 && k <= delta + d - 1
						&& x >= V[1][limit + k - delta]) {
					// System.out.println("Returning: " + (2*d-1));
					return 2 * d - 1;
				}

				// check to see if we can cut down the diagonal range
				if (x >= N && end_forward > k - 1) {
					end_forward = k - 1;
				} else if (y >= M) {
					start_forward = k + 1;
					value_to_add_forward = 0;
				}
			}

			start_diag = Math.max(value_to_add_backward + start_backward, -d);
			end_diag = Math.min(end_backward, d);
			value_to_add_backward = 1 - value_to_add_backward;

			// compute backward furthest reaching paths
			for (int k = start_diag; k <= end_diag; k += 2) {
				int x;
				if (k == d
						|| (k != -d && V[1][limit + k - 1] < V[1][limit + k + 1])) {
					x = V[1][limit + k - 1];
				} else {
					x = V[1][limit + k + 1] - 1;
				}

				int y = x - k - delta;

				snake[2] = 0;
				// System.out.println("2 x: " + x + " y: " + y + " k: " + k + "
				// d: " + d);
				while (x > 0 && y > 0
						&& isRangeEqual(x - 1 + bottoml1, y - 1 + bottoml2)) {
					x--;
					y--;
					snake[2]++;
				}
				V[1][limit + k] = x;

				if (isEven && k >= -delta - d && k <= d - delta
						&& x <= V[0][limit + k + delta]) {
					// System.out.println("Returning: " + 2*d);
					snake[0] = bottoml1 + x;
					snake[1] = bottoml2 + y;

					return 2 * d;
				}

				// check to see if we can cut down our diagonal range
				if (x <= 0) {
					start_backward = k + 1;
					value_to_add_backward = 0;
				} else if (y <= 0 && end_backward > k - 1) {
					end_backward = k - 1;
				}
			}
			worked(subMonitor, 1);
		}

		/*
		 * computing the true LCS is too expensive, instead find the diagonal
		 * with the most progress and pretend a midle snake of length 0 occurs
		 * there.
		 */

		int[] most_progress = findMostProgress(M, N, limit, V);

		snake[0] = bottoml1 + most_progress[0];
		snake[1] = bottoml2 + most_progress[1];
		snake[2] = 0;
		return 5; /*
					 * HACK: since we didn't really finish the LCS computation
					 * we don't really know the length of the SES. We don't do
					 * anything with the result anyway, unless it's <=1. We know
					 * for a fact SES > 1 so 5 is as good a number as any to
					 * return here
					 */
	}

	/**
	 * Takes the array with furthest reaching D-paths from an LCS computation
	 * and returns the x,y coordinates and progress made in the middle diagonal
	 * among those with maximum progress, both from the front and from the back.
	 *
	 * @param M the length of the 1st sequence for which LCS is being computed
	 * @param N the length of the 2nd sequence for which LCS is being computed
	 * @param limit the number of steps made in an attempt to find the LCS from
	 *            the front and back
	 * @param V the array storing the furthest reaching D-paths for the LCS
	 *            computation
	 * @return The result as an array of 3 integers where result[0] is the x
	 *         coordinate of the current location in the diagonal with the most
	 *         progress, result[1] is the y coordinate of the current location
	 *         in the diagonal with the most progress and result[2] is the
	 *         amount of progress made in that diagonal
	 */
	private static int[] findMostProgress(int M, int N, int limit, int[][] V) {
		int delta = N - M;

		int forward_start_diag;
		if ((M & 1) == (limit & 1)) {
			forward_start_diag = Math.max(-M, -limit);
		} else {
			forward_start_diag = Math.max(1 - M, -limit);
		}

		int forward_end_diag = Math.min(N, limit);

		int backward_start_diag;
		if ((N & 1) == (limit & 1)) {
			backward_start_diag = Math.max(-N, -limit);
		} else {
			backward_start_diag = Math.max(1 - N, -limit);
		}

		int backward_end_diag = Math.min(M, limit);

		int[][] max_progress = new int[Math.max(forward_end_diag
				- forward_start_diag, backward_end_diag - backward_start_diag) / 2 + 1][3];
		int num_progress = 0; // the 1st entry is current, it is initialized
								// with 0s

		// first search the forward diagonals
		for (int k = forward_start_diag; k <= forward_end_diag; k += 2) {
			int x = V[0][limit + k];
			int y = x - k;
			if (x > N || y > M) {
				continue;
			}

			int progress = x + y;
			if (progress > max_progress[0][2]) {
				num_progress = 0;
				max_progress[0][0] = x;
				max_progress[0][1] = y;
				max_progress[0][2] = progress;
			} else if (progress == max_progress[0][2]) {
				num_progress++;
				max_progress[num_progress][0] = x;
				max_progress[num_progress][1] = y;
				max_progress[num_progress][2] = progress;
			}
		}

		boolean max_progress_forward = true; // initially the maximum
												// progress is in the forward
												// direction

		// now search the backward diagonals
		for (int k = backward_start_diag; k <= backward_end_diag; k += 2) {
			int x = V[1][limit + k];
			int y = x - k - delta;
			if (x < 0 || y < 0) {
				continue;
			}

			int progress = N - x + M - y;
			if (progress > max_progress[0][2]) {
				num_progress = 0;
				max_progress_forward = false;
				max_progress[0][0] = x;
				max_progress[0][1] = y;
				max_progress[0][2] = progress;
			} else if (progress == max_progress[0][2] && !max_progress_forward) {
				num_progress++;
				max_progress[num_progress][0] = x;
				max_progress[num_progress][1] = y;
				max_progress[num_progress][2] = progress;
			}
		}

		// return the middle diagonal with maximal progress.
		return max_progress[num_progress / 2];
	}

	protected abstract int getLength2();

	protected abstract int getLength1();

	protected abstract boolean isRangeEqual(int i1, int i2);

	protected abstract void setLcs(int sl1, int sl2);

	protected abstract void initializeLcs(int lcsLength);

	public int getLength() {
		return this.length;
	}
}

Back to the top