@@ -77,7 +77,7 @@ class BlockRecognizer<RAW extends Trackable, BASE extends Trackable> { | |||
} | |||
// Check if remaining number of lines exceeds threshold. It avoids processing too many combinations. | |||
if (basesByLine.keySet().size() * rawsByLine.keySet().size() >= 250_000) { | |||
if (isOverLimit(basesByLine.keySet().size(), rawsByLine.keySet().size())) { | |||
return; | |||
} | |||
@@ -97,6 +97,10 @@ class BlockRecognizer<RAW extends Trackable, BASE extends Trackable> { | |||
} | |||
} | |||
static boolean isOverLimit(long a, long b) { | |||
return Math.multiplyExact(a, b) >= 250_000; | |||
} | |||
/** | |||
* @param startLineA number of line from first version of text (numbering starts from 1) | |||
* @param startLineB number of line from second version of text (numbering starts from 1) |
@@ -41,6 +41,15 @@ public class BlockRecognizerTest { | |||
assertThat(compute(seq("bcde"), seq("abcde"), 3, 4)).isEqualTo(4); | |||
} | |||
@Test | |||
public void isOverLimit() { | |||
assertThat(BlockRecognizer.isOverLimit(20, 40)).isFalse(); | |||
assertThat(BlockRecognizer.isOverLimit(3, 100_000)).isTrue(); | |||
// multiplication of these two ints is higher than Integer.MAX_VALUE | |||
assertThat(BlockRecognizer.isOverLimit(50_000, 60_000)).isTrue(); | |||
} | |||
private int compute(LineHashSequence seqA, LineHashSequence seqB, int ai, int bi) { | |||
return BlockRecognizer.lengthOfMaximalBlock(seqA, ai, seqB, bi); | |||
} |