You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiffAlgorithm.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.diff;
  44. /**
  45. * Compares two {@link Sequence}s to create an {@link EditList} of changes.
  46. *
  47. * An algorithm's {@code diff} method must be callable from concurrent threads
  48. * without data collisions. This permits some algorithms to use a singleton
  49. * pattern, with concurrent invocations using the same singleton. Other
  50. * algorithms may support parameterization, in which case the caller can create
  51. * a unique instance per thread.
  52. */
  53. public abstract class DiffAlgorithm {
  54. /**
  55. * Supported diff algorithm
  56. */
  57. public enum SupportedAlgorithm {
  58. /**
  59. * Myers diff algorithm
  60. */
  61. MYERS,
  62. /**
  63. * Histogram diff algorithm
  64. */
  65. HISTOGRAM
  66. }
  67. /**
  68. * @param alg
  69. * the diff algorithm for which an implementation should be
  70. * returned
  71. * @return an implementation of the specified diff algorithm
  72. */
  73. public static DiffAlgorithm getAlgorithm(SupportedAlgorithm alg) {
  74. switch (alg) {
  75. case MYERS:
  76. return MyersDiff.INSTANCE;
  77. case HISTOGRAM:
  78. return new HistogramDiff();
  79. default:
  80. throw new IllegalArgumentException();
  81. }
  82. }
  83. /**
  84. * Compare two sequences and identify a list of edits between them.
  85. *
  86. * @param <S>
  87. * type of sequence being compared.
  88. * @param cmp
  89. * the comparator supplying the element equivalence function.
  90. * @param a
  91. * the first (also known as old or pre-image) sequence. Edits
  92. * returned by this algorithm will reference indexes using the
  93. * 'A' side: {@link Edit#getBeginA()}, {@link Edit#getEndA()}.
  94. * @param b
  95. * the second (also known as new or post-image) sequence. Edits
  96. * returned by this algorithm will reference indexes using the
  97. * 'B' side: {@link Edit#getBeginB()}, {@link Edit#getEndB()}.
  98. * @return a modifiable edit list comparing the two sequences. If empty, the
  99. * sequences are identical according to {@code cmp}'s rules. The
  100. * result list is never null.
  101. */
  102. public <S extends Sequence> EditList diff(
  103. SequenceComparator<? super S> cmp, S a, S b) {
  104. Edit region = cmp.reduceCommonStartEnd(a, b, coverEdit(a, b));
  105. switch (region.getType()) {
  106. case INSERT:
  107. case DELETE:
  108. return EditList.singleton(region);
  109. case REPLACE: {
  110. SubsequenceComparator<S> cs = new SubsequenceComparator<S>(cmp);
  111. Subsequence<S> as = Subsequence.a(a, region);
  112. Subsequence<S> bs = Subsequence.b(b, region);
  113. EditList e = Subsequence.toBase(diffNonCommon(cs, as, bs), as, bs);
  114. // The last insertion may need to be shifted later if it
  115. // inserts elements that were previously reduced out as
  116. // common at the end.
  117. //
  118. Edit last = e.get(e.size() - 1);
  119. if (last.getType() == Edit.Type.INSERT) {
  120. while (last.endB < b.size()
  121. && cmp.equals(b, last.beginB, b, last.endB)) {
  122. last.beginA++;
  123. last.endA++;
  124. last.beginB++;
  125. last.endB++;
  126. }
  127. }
  128. return e;
  129. }
  130. case EMPTY:
  131. return new EditList(0);
  132. default:
  133. throw new IllegalStateException();
  134. }
  135. }
  136. private static <S extends Sequence> Edit coverEdit(S a, S b) {
  137. return new Edit(0, a.size(), 0, b.size());
  138. }
  139. /**
  140. * Compare two sequences and identify a list of edits between them.
  141. *
  142. * This method should be invoked only after the two sequences have been
  143. * proven to have no common starting or ending elements. The expected
  144. * elimination of common starting and ending elements is automatically
  145. * performed by the {@link #diff(SequenceComparator, Sequence, Sequence)}
  146. * method, which invokes this method using {@link Subsequence}s.
  147. *
  148. * @param <S>
  149. * type of sequence being compared.
  150. * @param cmp
  151. * the comparator supplying the element equivalence function.
  152. * @param a
  153. * the first (also known as old or pre-image) sequence. Edits
  154. * returned by this algorithm will reference indexes using the
  155. * 'A' side: {@link Edit#getBeginA()}, {@link Edit#getEndA()}.
  156. * @param b
  157. * the second (also known as new or post-image) sequence. Edits
  158. * returned by this algorithm will reference indexes using the
  159. * 'B' side: {@link Edit#getBeginB()}, {@link Edit#getEndB()}.
  160. * @return a modifiable edit list comparing the two sequences.
  161. */
  162. public abstract <S extends Sequence> EditList diffNonCommon(
  163. SequenceComparator<? super S> cmp, S a, S b);
  164. }