use of com.intellij.diff.util.Range in project intellij-community by JetBrains.
the class DiffIterableUtil method verifyFullCover.
private static void verifyFullCover(@NotNull DiffIterable iterable) {
int last1 = 0;
int last2 = 0;
Boolean lastEquals = null;
for (Pair<Range, Boolean> pair : iterateAll(iterable)) {
Range range = pair.first;
Boolean equal = pair.second;
assert last1 == range.start1;
assert last2 == range.start2;
assert !Comparing.equal(lastEquals, equal);
last1 = range.end1;
last2 = range.end2;
lastEquals = equal;
}
assert last1 == iterable.getLength1();
assert last2 == iterable.getLength2();
}
use of com.intellij.diff.util.Range in project intellij-community by JetBrains.
the class DiffIterableUtil method iterateAll.
//
// Misc
//
@NotNull
public static Iterable<Pair<Range, Boolean>> iterateAll(@NotNull final DiffIterable iterable) {
return () -> new Iterator<Pair<Range, Boolean>>() {
@NotNull
private final Iterator<Range> myChanges = iterable.changes();
@NotNull
private final Iterator<Range> myUnchanged = iterable.unchanged();
@Nullable
private Range lastChanged = myChanges.hasNext() ? myChanges.next() : null;
@Nullable
private Range lastUnchanged = myUnchanged.hasNext() ? myUnchanged.next() : null;
@Override
public boolean hasNext() {
return lastChanged != null || lastUnchanged != null;
}
@Override
public Pair<Range, Boolean> next() {
boolean equals;
if (lastChanged == null) {
equals = true;
} else if (lastUnchanged == null) {
equals = false;
} else {
equals = lastUnchanged.start1 < lastChanged.start1 || lastUnchanged.start2 < lastChanged.start2;
}
if (equals) {
Range range = lastUnchanged;
lastUnchanged = myUnchanged.hasNext() ? myUnchanged.next() : null;
//noinspection ConstantConditions
return Pair.create(range, true);
} else {
Range range = lastChanged;
lastChanged = myChanges.hasNext() ? myChanges.next() : null;
return Pair.create(range, false);
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
use of com.intellij.diff.util.Range in project intellij-community by JetBrains.
the class ChunkOptimizer method build.
@NotNull
public FairDiffIterable build() {
for (Range range : myIterable.iterateUnchanged()) {
myRanges.add(range);
processLastRanges();
}
return fair(createUnchanged(myRanges, myData1.size(), myData2.size()));
}
use of com.intellij.diff.util.Range in project intellij-community by JetBrains.
the class ChunkOptimizer method processLastRanges.
private void processLastRanges() {
// nothing to do
if (myRanges.size() < 2)
return;
Range range1 = myRanges.get(myRanges.size() - 2);
Range range2 = myRanges.get(myRanges.size() - 1);
if (range1.end1 != range2.start1 && range1.end2 != range2.start2) {
// it means that given DiffIterable is not LCS (because we can build a smaller one). This should not happen.
return;
}
int count1 = range1.end1 - range1.start1;
int count2 = range2.end1 - range2.start1;
int equalForward = expandForward(myData1, myData2, range1.end1, range1.end2, range1.end1 + count2, range1.end2 + count2);
int equalBackward = expandBackward(myData1, myData2, range2.start1 - count1, range2.start2 - count1, range2.start1, range2.start2);
// nothing to do
if (equalForward == 0 && equalBackward == 0)
return;
// merge chunks left [A]B[B] -> [AB]B
if (equalForward == count2) {
myRanges.remove(myRanges.size() - 1);
myRanges.remove(myRanges.size() - 1);
myRanges.add(new Range(range1.start1, range1.end1 + count2, range1.start2, range1.end2 + count2));
processLastRanges();
return;
}
// merge chunks right [A]A[B] -> A[AB]
if (equalBackward == count1) {
myRanges.remove(myRanges.size() - 1);
myRanges.remove(myRanges.size() - 1);
myRanges.add(new Range(range2.start1 - count1, range2.end1, range2.start2 - count1, range2.end2));
processLastRanges();
return;
}
Side touchSide = Side.fromLeft(range1.end1 == range2.start1);
int shift = getShift(touchSide, equalForward, equalBackward, range1, range2);
if (shift != 0) {
myRanges.remove(myRanges.size() - 1);
myRanges.remove(myRanges.size() - 1);
myRanges.add(new Range(range1.start1, range1.end1 + shift, range1.start2, range1.end2 + shift));
myRanges.add(new Range(range2.start1 + shift, range2.end1, range2.start2 + shift, range2.end2));
}
}
use of com.intellij.diff.util.Range in project intellij-community by JetBrains.
the class ComparisonManagerImpl method correctIgnoredRangesSecondStep.
@NotNull
private static FairDiffIterable correctIgnoredRangesSecondStep(@NotNull FairDiffIterable iterable, @NotNull List<Line> lines1, @NotNull List<Line> lines2, @NotNull BitSet ignored1, @NotNull BitSet ignored2) {
DiffIterableUtil.ChangeBuilder builder = new DiffIterableUtil.ChangeBuilder(lines1.size(), lines2.size());
for (Range range : iterable.iterateUnchanged()) {
int count = range.end1 - range.start1;
for (int i = 0; i < count; i++) {
int index1 = range.start1 + i;
int index2 = range.start2 + i;
if (areIgnoredEqualLines(lines1.get(index1), lines2.get(index2), ignored1, ignored2)) {
builder.markEqual(index1, index2);
}
}
}
return fair(builder.finish());
}
Aggregations