mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-23 13:09:00 +00:00
(index) Optimize SequenceOperations.minDistance
This commit is contained in:
parent
d94373f4b1
commit
efd56efc63
@ -142,7 +142,7 @@ public class PhraseConstraintGroupList {
|
||||
}
|
||||
|
||||
public int minDistance(IntList[] positions) {
|
||||
IntIterator[] sequences = new IntIterator[present.cardinality()];
|
||||
IntList[] sequences = new IntList[present.cardinality()];
|
||||
int[] iterOffsets = new int[sequences.length];
|
||||
|
||||
for (int oi = 0, si = 0; oi < offsets.length; oi++) {
|
||||
@ -162,7 +162,7 @@ public class PhraseConstraintGroupList {
|
||||
if (posForTerm == null) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
sequences[si++] = posForTerm.iterator();
|
||||
sequences[si++] = posForTerm;
|
||||
iterOffsets[si - 1] = -oi;
|
||||
}
|
||||
|
||||
|
@ -148,19 +148,19 @@ public class SequenceOperations {
|
||||
return minDistance;
|
||||
}
|
||||
|
||||
public static int minDistance(IntIterator[] iterators) {
|
||||
return minDistance(iterators, new int[iterators.length]);
|
||||
public static int minDistance(IntList[] positions) {
|
||||
return minDistance(positions, new int[positions.length]);
|
||||
}
|
||||
|
||||
public static int minDistance(IntIterator[] iterators, int[] iterOffsets) {
|
||||
if (iterators.length <= 1)
|
||||
public static int minDistance(IntList[] positions, int[] offsets) {
|
||||
if (positions.length <= 1)
|
||||
return 0;
|
||||
|
||||
int[] values = new int[iterators.length];
|
||||
|
||||
for (int i = 0; i < iterators.length; i++) {
|
||||
if (iterators[i].hasNext())
|
||||
values[i] = iterators[i].nextInt() + iterOffsets[i];
|
||||
int[] values = new int[positions.length];
|
||||
int[] indexes = new int[positions.length];
|
||||
for (int i = 0; i < positions.length; i++) {
|
||||
if (indexes[i] < positions[i].size())
|
||||
values[i] = positions[i].getInt(indexes[i]++) + offsets[i];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
@ -177,13 +177,14 @@ public class SequenceOperations {
|
||||
|
||||
minDist = Math.min(minDist, maxVal - minVal);
|
||||
|
||||
for (int i = 0;; i = (i + 1) % iterators.length)
|
||||
for (int i = 0;; i = (i + 1) % positions.length)
|
||||
{
|
||||
if (values[i] == minVal) {
|
||||
if (!iterators[i].hasNext()) {
|
||||
if (indexes[i] < positions[i].size()) {
|
||||
values[i] = positions[i].getInt(indexes[i]++) + offsets[i];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
values[i] = iterators[i].nextInt() + iterOffsets[i];
|
||||
|
||||
if (values[i] > maxVal) {
|
||||
maxVal = values[i];
|
||||
|
@ -1,6 +1,5 @@
|
||||
package nu.marginalia.sequence;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntIterator;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -91,6 +90,6 @@ class SequenceOperationsTest {
|
||||
GammaCodedSequence seq2 = GammaCodedSequence.generate(wa, 20, 50, 100);
|
||||
GammaCodedSequence seq3 = GammaCodedSequence.generate(wa, 30, 60, 90);
|
||||
|
||||
assertEquals(19, SequenceOperations.minDistance(new IntIterator[]{seq1.iterator(), seq2.iterator(), seq3.iterator()}));
|
||||
assertEquals(19, SequenceOperations.minDistance(new IntList[]{seq1.values(), seq2.values(), seq3.values()}));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user