Skip to content

Commit aa2b710

Browse files
committed
Change SloppyPhraseMatcher to use LessThan
1 parent 3a849db commit aa2b710

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

lucene/core/src/java/org/apache/lucene/search/SloppyPhraseMatcher.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,21 @@ public SloppyPhraseMatcher(
9494
this.numPostings = postings.length;
9595
this.captureLeadMatch = captureLeadMatch;
9696
pq =
97-
PriorityQueue.usingComparator(
97+
PriorityQueue.usingLessThan(
9898
postings.length,
99-
Comparator.<PhrasePositions>comparingInt(pp -> pp.position)
99+
(pp1, pp2) -> {
100+
if (pp1.position == pp2.position)
100101
// same doc and pp.position, so decide by actual term positions.
101102
// rely on: pp.position == tp.position - offset.
102-
.thenComparingInt(pp -> pp.offset)
103-
.thenComparingInt(pp -> pp.ord));
103+
if (pp1.offset == pp2.offset) {
104+
return pp1.ord < pp2.ord;
105+
} else {
106+
return pp1.offset < pp2.offset;
107+
}
108+
else {
109+
return pp1.position < pp2.position;
110+
}
111+
});
104112
phrasePositions = new PhrasePositions[postings.length];
105113
for (int i = 0; i < postings.length; ++i) {
106114
phrasePositions[i] =

lucene/core/src/java/org/apache/lucene/util/FloatComparator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.util.Comparator;
2020

2121
/** A specialization of {@link Comparator} that compares {@code float} values */
22+
@FunctionalInterface
2223
public interface FloatComparator {
2324

25+
@FunctionalInterface
2426
interface ToFloatFunction<T> {
2527
float applyAsFloat(T obj);
2628
}

lucene/core/src/java/org/apache/lucene/util/PriorityQueue.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@
3737
*/
3838
public abstract class PriorityQueue<T> implements Iterable<T> {
3939

40+
@FunctionalInterface
41+
public interface LessThan<T> {
42+
boolean lessThan(T a, T b);
43+
}
44+
45+
/** Create a {@code PriorityQueue} that orders elements using the specified {@code lessThan} */
46+
public static <T> PriorityQueue<T> usingLessThan(int maxSize, LessThan<? super T> lessThan) {
47+
return new PriorityQueue<>(maxSize) {
48+
@Override
49+
protected boolean lessThan(T a, T b) {
50+
return lessThan.lessThan(a, b);
51+
}
52+
};
53+
}
54+
55+
/** Create a {@code PriorityQueue} that orders elements using the specified {@code lessThan} */
56+
public static <T> PriorityQueue<T> usingLessThan(
57+
int maxSize, Supplier<T> sentinelObjectSupplier, LessThan<? super T> lessThan) {
58+
return new PriorityQueue<>(maxSize, sentinelObjectSupplier) {
59+
@Override
60+
protected boolean lessThan(T a, T b) {
61+
return lessThan.lessThan(a, b);
62+
}
63+
};
64+
}
65+
4066
/** Create a {@code PriorityQueue} that orders elements using the specified {@code comparator} */
4167
public static <T> PriorityQueue<T> usingComparator(
4268
int maxSize, Comparator<? super T> comparator) {

0 commit comments

Comments
 (0)