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.

HistogramDiffIndex.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. import org.eclipse.jgit.JGitText;
  45. /**
  46. * Support {@link HistogramDiff} by computing occurrence counts of elements.
  47. *
  48. * Each element in the range being considered is put into a hash table, tracking
  49. * the number of times that distinct element appears in the sequence. Once all
  50. * elements have been inserted from sequence A, each element of sequence B is
  51. * probed in the hash table and the longest common subsequence with the lowest
  52. * occurrence count in A is used as the result.
  53. *
  54. * @param <S>
  55. * type of the base sequence.
  56. */
  57. final class HistogramDiffIndex<S extends Sequence> {
  58. private static final int REC_NEXT_SHIFT = 28 + 8;
  59. private static final int REC_PTR_SHIFT = 8;
  60. private static final int REC_PTR_MASK = (1 << 28) - 1;
  61. private static final int REC_CNT_MASK = (1 << 8) - 1;
  62. private static final int MAX_PTR = REC_PTR_MASK;
  63. private static final int MAX_CNT = (1 << 8) - 1;
  64. private final int maxChainLength;
  65. private final HashedSequenceComparator<S> cmp;
  66. private final HashedSequence<S> a;
  67. private final HashedSequence<S> b;
  68. private final Edit region;
  69. /** Keyed by {@link #hash(HashedSequence, int)} for {@link #recs} index. */
  70. private final int[] table;
  71. /** Number of low bits to discard from a key to index {@link #table}. */
  72. private final int keyShift;
  73. /**
  74. * Describes a unique element in sequence A.
  75. *
  76. * The records in this table are actually 3-tuples of:
  77. * <ul>
  78. * <li>index of next record in this table that has same hash code</li>
  79. * <li>index of first element in this occurrence chain</li>
  80. * <li>occurrence count for this element (length of locs list)</li>
  81. * </ul>
  82. *
  83. * The occurrence count is capped at {@link #MAX_CNT}, as the field is only
  84. * a few bits wide. Elements that occur more frequently will have their
  85. * count capped.
  86. */
  87. private long[] recs;
  88. /** Number of elements in {@link #recs}; also is the unique element count. */
  89. private int recCnt;
  90. /**
  91. * For {@code ptr}, {@code next[ptr - nextShift]} has subsequent index.
  92. *
  93. * For the sequence element {@code ptr}, the value stored at location
  94. * {@code next[ptr - nextShift]} is the next occurrence of the exact same
  95. * element in the sequence.
  96. *
  97. * Chains always run from the lowest index to the largest index. Therefore
  98. * the array will store {@code next[1] = 2}, but never {@code next[2] = 1}.
  99. * This allows a chain to terminate with {@code 0}, as {@code 0} would never
  100. * be a valid next element.
  101. *
  102. * The array is sized to be {@code region.getLenghtA()} and element indexes
  103. * are converted to array indexes by subtracting {@link #nextShift}, which
  104. * is just a cached version of {@code region.beginA}.
  105. */
  106. private int[] next;
  107. /** Value to subtract from element indexes to key {@link #next} array. */
  108. private int nextShift;
  109. private Edit lcs;
  110. private int cnt;
  111. private boolean hasCommon;
  112. HistogramDiffIndex(int maxChainLength, HashedSequenceComparator<S> cmp,
  113. HashedSequence<S> a, HashedSequence<S> b, Edit r) {
  114. this.maxChainLength = maxChainLength;
  115. this.cmp = cmp;
  116. this.a = a;
  117. this.b = b;
  118. this.region = r;
  119. if (region.endA >= MAX_PTR)
  120. throw new IllegalArgumentException(
  121. JGitText.get().sequenceTooLargeForDiffAlgorithm);
  122. final int sz = r.getLengthA();
  123. final int tableBits = tableBits(sz);
  124. table = new int[1 << tableBits];
  125. keyShift = 32 - tableBits;
  126. nextShift = r.beginA;
  127. recs = new long[Math.max(4, sz >>> 3)];
  128. next = new int[sz];
  129. }
  130. Edit findLongestCommonSequence() {
  131. if (!scanA())
  132. return null;
  133. lcs = new Edit(0, 0);
  134. cnt = maxChainLength + 1;
  135. for (int bPtr = region.beginB; bPtr < region.endB;)
  136. bPtr = tryLongestCommonSequence(bPtr);
  137. return hasCommon && maxChainLength < cnt ? null : lcs;
  138. }
  139. private boolean scanA() {
  140. // Scan the elements backwards, inserting them into the hash table
  141. // as we go. Going in reverse places the earliest occurrence of any
  142. // element at the start of the chain, so we consider earlier matches
  143. // before later matches.
  144. //
  145. SCAN: for (int ptr = region.endA - 1; region.beginA <= ptr; ptr--) {
  146. final int tIdx = hash(a, ptr);
  147. int chainLen = 0;
  148. for (int rIdx = table[tIdx]; rIdx != 0;) {
  149. final long rec = recs[rIdx];
  150. if (cmp.equals(a, recPtr(rec), a, ptr)) {
  151. // ptr is identical to another element. Insert it onto
  152. // the front of the existing element chain.
  153. //
  154. int newCnt = recCnt(rec) + 1;
  155. if (MAX_CNT < newCnt)
  156. newCnt = MAX_CNT;
  157. recs[rIdx] = recCreate(recNext(rec), ptr, newCnt);
  158. next[ptr - nextShift] = recPtr(rec);
  159. continue SCAN;
  160. }
  161. rIdx = recNext(rec);
  162. chainLen++;
  163. }
  164. if (chainLen == maxChainLength)
  165. return false;
  166. // This is the first time we have ever seen this particular
  167. // element in the sequence. Construct a new chain for it.
  168. //
  169. final int rIdx = ++recCnt;
  170. if (rIdx == recs.length) {
  171. int sz = Math.min(recs.length << 1, 1 + region.getLengthA());
  172. long[] n = new long[sz];
  173. System.arraycopy(recs, 0, n, 0, recs.length);
  174. recs = n;
  175. }
  176. recs[rIdx] = recCreate(table[tIdx], ptr, 1);
  177. table[tIdx] = rIdx;
  178. }
  179. return true;
  180. }
  181. private int tryLongestCommonSequence(final int bPtr) {
  182. int bNext = bPtr + 1;
  183. int rIdx = table[hash(b, bPtr)];
  184. for (long rec; rIdx != 0; rIdx = recNext(rec)) {
  185. rec = recs[rIdx];
  186. // If there are more occurrences in A, don't use this chain.
  187. if (recCnt(rec) > cnt) {
  188. if (!hasCommon)
  189. hasCommon = cmp.equals(a, recPtr(rec), b, bPtr);
  190. continue;
  191. }
  192. int as = recPtr(rec);
  193. if (!cmp.equals(a, as, b, bPtr))
  194. continue;
  195. hasCommon = true;
  196. TRY_LOCATIONS: for (;;) {
  197. int np = next[as - nextShift];
  198. int bs = bPtr;
  199. int ae = as + 1;
  200. int be = bs + 1;
  201. while (region.beginA < as && region.beginB < bs
  202. && cmp.equals(a, as - 1, b, bs - 1)) {
  203. as--;
  204. bs--;
  205. }
  206. while (ae < region.endA && be < region.endB
  207. && cmp.equals(a, ae, b, be)) {
  208. ae++;
  209. be++;
  210. }
  211. if (bNext < be)
  212. bNext = be;
  213. if (lcs.getLengthA() < ae - as || recCnt(rec) < cnt) {
  214. // If this region is the longest, or there are less
  215. // occurrences of it in A, its now our LCS.
  216. //
  217. lcs.beginA = as;
  218. lcs.beginB = bs;
  219. lcs.endA = ae;
  220. lcs.endB = be;
  221. cnt = recCnt(rec);
  222. }
  223. // Because we added elements in reverse order index 0
  224. // cannot possibly be the next position. Its the first
  225. // element of the sequence and thus would have been the
  226. // value of as at the start of the TRY_LOCATIONS loop.
  227. //
  228. if (np == 0)
  229. break TRY_LOCATIONS;
  230. while (np < ae) {
  231. // The next location to consider was actually within
  232. // the LCS we examined above. Don't reconsider it.
  233. //
  234. np = next[np - nextShift];
  235. if (np == 0)
  236. break TRY_LOCATIONS;
  237. }
  238. as = np;
  239. }
  240. }
  241. return bNext;
  242. }
  243. private int hash(HashedSequence<S> s, int idx) {
  244. return (cmp.hash(s, idx) * 0x9e370001 /* mix bits */) >>> keyShift;
  245. }
  246. private static long recCreate(int next, int ptr, int cnt) {
  247. return ((long) next << REC_NEXT_SHIFT) //
  248. | ((long) ptr << REC_PTR_SHIFT) //
  249. | cnt;
  250. }
  251. private static int recNext(long rec) {
  252. return (int) (rec >>> REC_NEXT_SHIFT);
  253. }
  254. private static int recPtr(long rec) {
  255. return ((int) (rec >>> REC_PTR_SHIFT)) & REC_PTR_MASK;
  256. }
  257. private static int recCnt(long rec) {
  258. return ((int) rec) & REC_CNT_MASK;
  259. }
  260. private static int tableBits(final int sz) {
  261. int bits = 31 - Integer.numberOfLeadingZeros(sz);
  262. if (bits == 0)
  263. bits = 1;
  264. if (1 << bits < sz)
  265. bits++;
  266. return bits;
  267. }
  268. }