mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-24 05:18:58 +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) {
|
public int minDistance(IntList[] positions) {
|
||||||
IntIterator[] sequences = new IntIterator[present.cardinality()];
|
IntList[] sequences = new IntList[present.cardinality()];
|
||||||
int[] iterOffsets = new int[sequences.length];
|
int[] iterOffsets = new int[sequences.length];
|
||||||
|
|
||||||
for (int oi = 0, si = 0; oi < offsets.length; oi++) {
|
for (int oi = 0, si = 0; oi < offsets.length; oi++) {
|
||||||
@ -162,7 +162,7 @@ public class PhraseConstraintGroupList {
|
|||||||
if (posForTerm == null) {
|
if (posForTerm == null) {
|
||||||
return Integer.MAX_VALUE;
|
return Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
sequences[si++] = posForTerm.iterator();
|
sequences[si++] = posForTerm;
|
||||||
iterOffsets[si - 1] = -oi;
|
iterOffsets[si - 1] = -oi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,19 +148,19 @@ public class SequenceOperations {
|
|||||||
return minDistance;
|
return minDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int minDistance(IntIterator[] iterators) {
|
public static int minDistance(IntList[] positions) {
|
||||||
return minDistance(iterators, new int[iterators.length]);
|
return minDistance(positions, new int[positions.length]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int minDistance(IntIterator[] iterators, int[] iterOffsets) {
|
public static int minDistance(IntList[] positions, int[] offsets) {
|
||||||
if (iterators.length <= 1)
|
if (positions.length <= 1)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int[] values = new int[iterators.length];
|
int[] values = new int[positions.length];
|
||||||
|
int[] indexes = new int[positions.length];
|
||||||
for (int i = 0; i < iterators.length; i++) {
|
for (int i = 0; i < positions.length; i++) {
|
||||||
if (iterators[i].hasNext())
|
if (indexes[i] < positions[i].size())
|
||||||
values[i] = iterators[i].nextInt() + iterOffsets[i];
|
values[i] = positions[i].getInt(indexes[i]++) + offsets[i];
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -177,13 +177,14 @@ public class SequenceOperations {
|
|||||||
|
|
||||||
minDist = Math.min(minDist, maxVal - minVal);
|
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 (values[i] == minVal) {
|
||||||
if (!iterators[i].hasNext()) {
|
if (indexes[i] < positions[i].size()) {
|
||||||
|
values[i] = positions[i].getInt(indexes[i]++) + offsets[i];
|
||||||
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
values[i] = iterators[i].nextInt() + iterOffsets[i];
|
|
||||||
|
|
||||||
if (values[i] > maxVal) {
|
if (values[i] > maxVal) {
|
||||||
maxVal = values[i];
|
maxVal = values[i];
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package nu.marginalia.sequence;
|
package nu.marginalia.sequence;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.IntIterator;
|
|
||||||
import it.unimi.dsi.fastutil.ints.IntList;
|
import it.unimi.dsi.fastutil.ints.IntList;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@ -91,6 +90,6 @@ class SequenceOperationsTest {
|
|||||||
GammaCodedSequence seq2 = GammaCodedSequence.generate(wa, 20, 50, 100);
|
GammaCodedSequence seq2 = GammaCodedSequence.generate(wa, 20, 50, 100);
|
||||||
GammaCodedSequence seq3 = GammaCodedSequence.generate(wa, 30, 60, 90);
|
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