Changeset 9231 in josm for trunk/src/org/openstreetmap/josm/tools/Diff.java
- Timestamp:
- 2016-01-01T02:35:34+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/Diff.java
r8976 r9231 152 152 * the worst this can do is cause suboptimal diff output. 153 153 * It cannot cause incorrect diff output. 154 * @param xoff xoff 155 * @param xlim xlim 156 * @param yoff yoff 157 * @param ylim ylim 158 * @return midpoint of the shortest edit script 154 159 */ 155 160 private int diag(int xoff, int xlim, int yoff, int ylim) { … … 164 169 int fmin = fmid, fmax = fmid; // Limits of top-down search. 165 170 int bmin = bmid, bmax = bmid; // Limits of bottom-up search. 166 /* True if southeast corner is on an odd 167 diagonal with respect to the northwest. */ 171 // True if southeast corner is on an odd diagonal with respect to the northwest. 168 172 final boolean odd = (fmid - bmid & 1) != 0; 169 173 … … 315 319 } 316 320 317 /** Compare in detail contiguous subsequences of the two files 318 which are known, as a whole, to match each other. 319 320 The results are recorded in the vectors filevec[N].changed_flag, by 321 storing a 1 in the element for each line that is an insertion or deletion. 322 323 The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1. 324 325 Note that XLIM, YLIM are exclusive bounds. 326 All line numbers are origin-0 and discarded lines are not counted. */ 327 321 /** 322 * Compare in detail contiguous subsequences of the two files 323 * which are known, as a whole, to match each other. 324 * 325 * The results are recorded in the vectors filevec[N].changed_flag, by 326 * storing a 1 in the element for each line that is an insertion or deletion. 327 * 328 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1. 329 * 330 * Note that XLIM, YLIM are exclusive bounds. 331 * All line numbers are origin-0 and discarded lines are not counted. 332 * @param xoff xoff 333 * @param xlim xlim 334 * @param yoff yoff 335 * @param ylim ylim 336 */ 328 337 private void compareseq(int xoff, int xlim, int yoff, int ylim) { 329 338 /* Slide down the bottom initial diagonal. */ … … 379 388 private boolean inhibit; 380 389 381 /** Adjust inserts/deletes of blank lines to join changes382 390 /** 391 * Adjust inserts/deletes of blank lines to join changes as much as possible. 383 392 */ 384 393 private void shift_boundaries() { … … 389 398 } 390 399 400 /** 401 * Script builder. 402 */ 391 403 public interface ScriptBuilder { 392 /** Scan the tables of which lines are inserted and deleted,393 394 395 396 397 398 404 /** 405 * Scan the tables of which lines are inserted and deleted, producing an edit script. 406 * @param changed0 true for lines in first file which do not match 2nd 407 * @param len0 number of lines in first file 408 * @param changed1 true for lines in 2nd file which do not match 1st 409 * @param len1 number of lines in 2nd file 410 * @return a linked list of changes - or null 399 411 */ 400 412 Change build_script( … … 472 484 } 473 485 474 /** Standard ScriptBuilders. */ 475 public static final ScriptBuilder 476 forwardScript = new ForwardScript(), 477 reverseScript = new ReverseScript(); 478 479 /** Report the differences of two files. DEPTH is the current directory depth. */ 486 /** Standard Forward ScriptBuilder. */ 487 public static final ScriptBuilder forwardScript = new ForwardScript(); 488 /** Standard Reverse ScriptBuilder. */ 489 public static final ScriptBuilder reverseScript = new ReverseScript(); 490 491 /** 492 * Report the differences of two files. DEPTH is the current directory depth. 493 * @param reverse if {@code true} use {@link #reverseScript} else use {@link #forwardScript} 494 * @return the differences of two files 495 */ 480 496 public final Change diff_2(final boolean reverse) { 481 497 return diff(reverse ? reverseScript : forwardScript); 482 498 } 483 499 484 /** Get the results of comparison as an edit script. The script 485 is described by a list of changes. The standard ScriptBuilder 486 implementations provide for forward and reverse edit scripts. 487 Alternate implementations could, for instance, list common elements 488 instead of differences. 489 @param bld an object to build the script from change flags 490 @return the head of a list of changes 500 /** 501 * Get the results of comparison as an edit script. The script 502 * is described by a list of changes. The standard ScriptBuilder 503 * implementations provide for forward and reverse edit scripts. 504 * Alternate implementations could, for instance, list common elements 505 * instead of differences. 506 * @param bld an object to build the script from change flags 507 * @return the head of a list of changes 491 508 */ 492 509 public Change diff(final ScriptBuilder bld) { 493 510 494 /* Some lines are obviously insertions or deletions 495 because they don't match anything. Detect them now, 496 and avoid even thinking about them in the main comparison algorithm. */ 497 511 // Some lines are obviously insertions or deletions because they don't match anything. 512 // Detect them now, and avoid even thinking about them in the main comparison algorithm. 498 513 discard_confusing_lines(); 499 514 500 /* Now do the main comparison algorithm, considering just the 501 undiscarded lines. */ 502 515 // Now do the main comparison algorithm, considering just the undiscarded lines. 503 516 xvec = filevec[0].undiscarded; 504 517 yvec = filevec[1].undiscarded; 505 518 506 int diags = 507 filevec[0].nondiscardedLines + filevec[1].nondiscardedLines + 3; 519 int diags = filevec[0].nondiscardedLines + filevec[1].nondiscardedLines + 3; 508 520 fdiag = new int[diags]; 509 521 fdiagoff = filevec[1].nondiscardedLines + 1; … … 512 524 513 525 compareseq(0, filevec[0].nondiscardedLines, 514 0, filevec[1].nondiscardedLines);526 0, filevec[1].nondiscardedLines); 515 527 fdiag = null; 516 528 bdiag = null; 517 529 518 /* Modify the results slightly to make them prettier 519 in cases where that can validly be done. */ 520 530 // Modify the results slightly to make them prettier in cases where that can validly be done. 521 531 shift_boundaries(); 522 532 523 /* Get the results of comparison in the form of a chain 524 of `struct change's -- an edit script. */ 533 // Get the results of comparison in the form of a chain of `struct change's -- an edit script. 525 534 return bld.build_script( 526 535 filevec[0].changedFlag, … … 529 538 filevec[1].bufferedLines 530 539 ); 531 532 540 } 533 541 … … 555 563 public final int line1; 556 564 557 /** Cons an additional entry onto the front of an edit script OLD. 558 LINE0 and LINE1 are the first affected lines in the two files (origin 0). 559 DELETED is the number of lines deleted here from file 0. 560 INSERTED is the number of lines inserted here in file 1. 561 562 If DELETED is 0 then LINE0 is the number of the line before 563 which the insertion was done; vice versa for INSERTED and LINE1. */ 565 /** 566 * Cons an additional entry onto the front of an edit script OLD. 567 * LINE0 and LINE1 are the first affected lines in the two files (origin 0). 568 * DELETED is the number of lines deleted here from file 0. 569 * INSERTED is the number of lines inserted here in file 1. 570 * 571 * If DELETED is 0 then LINE0 is the number of the line before 572 * which the insertion was done; vice versa for INSERTED and LINE1. 573 * @param line0 first affected lines in the two files (origin 0) 574 * @param line1 first affected lines in the two files (origin 0) 575 * @param deleted the number of lines deleted here from file 0 576 * @param inserted the number of lines inserted here in file 1 577 * @param old edit script 578 */ 564 579 public Change(int line0, int line1, int deleted, int inserted, Change old) { 565 580 this.line0 = line0; … … 573 588 * Returns the number of insertions and deletions of this change as well as 574 589 * (recursively) the changes linked via {@link #link}. 590 * @return recursive number of insertions and deletions 575 591 */ 576 592 public int getTotalNumberOfChanges() { … … 585 601 } 586 602 587 /** Data on one input file being compared. 603 /** 604 * Data on one input file being compared. 588 605 */ 589 606 class FileData { … … 591 608 /** Allocate changed array for the results of comparison. */ 592 609 void clear() { 593 /* Allocate a flag for each line of each file, saying whether that line 594 is an insertion or deletion. 595 Allocate an extra element, always zero, at each end of each vector. 596 */ 610 // Allocate a flag for each line of each file, saying whether that line is an insertion or deletion. 611 // Allocate an extra element, always zero, at each end of each vector. 597 612 changedFlag = new boolean[bufferedLines + 2]; 598 613 } 599 614 600 /** Return equiv_count[I] as the number of lines in this file601 that fall in equivalence class I.602 @return the array of equivalence class counts.615 /** 616 * Return equiv_count[I] as the number of lines in this file that fall in equivalence class I. 617 * @return the array of equivalence class counts. 603 618 */ 604 619 int[] equivCount() { … … 610 625 } 611 626 612 /** Discard lines that have no matches in another file.613 614 A line which is discarded will not be considered by the actual615 comparison algorithm; it will be as if that line were not in the file.616 The file's `realindexes' table maps virtual line numbers617 (which don't count the discarded lines) into real line numbers;618 this is how the actual comparison algorithm produces results619 that are comprehensible when the discarded lines are counted.620 <p> 621 When we discard a line, we also mark it as a deletion or insertion622 so that it will be printed in the output.623 @param f the other file627 /** 628 * Discard lines that have no matches in another file. 629 * 630 * A line which is discarded will not be considered by the actual comparison algorithm; 631 * it will be as if that line were not in the file. 632 * The file's `realindexes' table maps virtual line numbers 633 * (which don't count the discarded lines) into real line numbers; 634 * this is how the actual comparison algorithm produces results 635 * that are comprehensible when the discarded lines are counted. 636 * <p> 637 * When we discard a line, we also mark it as a deletion or insertion so that it will be printed in the output. 638 * @param f the other file 624 639 */ 625 640 void discard_confusing_lines(FileData f) { 626 641 clear(); 627 / * Set up table of which lines are going to be discarded. */642 // Set up table of which lines are going to be discarded. 628 643 final byte[] discarded = discardable(f.equivCount()); 629 644 630 /* Don't really discard the provisional lines except when they occur 631 in a run of discardables, with nonprovisionals at the beginning 632 and end. */ 645 // Don't really discard the provisional lines except when they occur in a run of discardables, 646 // with nonprovisionals at the beginning and end. 633 647 filterDiscards(discarded); 634 648 635 / * Actually discard the lines. */649 // Actually discard the lines. 636 650 discard(discarded); 637 651 } … … 674 688 /** 675 689 * Don't really discard the provisional lines except when they occur 676 * in a run of discardables, with nonprovisionals at the beginning 677 * and end.690 * in a run of discardables, with nonprovisionals at the beginning and end. 691 * @param discards discards 678 692 */ 679 693 private void filterDiscards(final byte[] discards) {
Note:
See TracChangeset
for help on using the changeset viewer.