diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java index a89df974b66..c855d61d051 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java @@ -124,38 +124,54 @@ class OrderedIntervalsSource extends MinimizingConjunctionIntervalsSource { start = end = slop = IntervalIterator.NO_MORE_INTERVALS; int lastStart = Integer.MAX_VALUE; boolean minimizing = false; + final var subIterators = this.subIterators; + int currentIndex = i; while (true) { while (true) { - if (subIterators.get(i - 1).end() >= lastStart) { + var prev = subIterators.get(currentIndex - 1); + if (prev.end() >= lastStart) { + i = currentIndex; return start; } - if (i == subIterators.size() - || (minimizing && subIterators.get(i).start() > subIterators.get(i - 1).end())) { + if (currentIndex == subIterators.size()) { + break; + } + final IntervalIterator current = subIterators.get(currentIndex); + if (minimizing && (current.start() > prev.end())) { break; } do { - if (subIterators.get(i).end() >= lastStart - || subIterators.get(i).nextInterval() == IntervalIterator.NO_MORE_INTERVALS) { + if (current.end() >= lastStart + || current.nextInterval() == IntervalIterator.NO_MORE_INTERVALS) { + i = currentIndex; return start; } - } while (subIterators.get(i).start() <= subIterators.get(i - 1).end()); - i++; + } while (current.start() <= prev.end()); + currentIndex++; } - start = subIterators.get(0).start(); + var first = subIterators.getFirst(); + final int start = first.start(); + this.start = start; if (start == NO_MORE_INTERVALS) { + i = currentIndex; return end = NO_MORE_INTERVALS; } - end = subIterators.get(subIterators.size() - 1).end(); - slop = end - start + 1; + var last = subIterators.getLast(); + + final int end = last.end(); + this.end = end; + int slop = end - start + 1; for (IntervalIterator subIterator : subIterators) { slop -= subIterator.width(); } + this.slop = slop; onMatch.onMatch(); - lastStart = subIterators.get(subIterators.size() - 1).start(); - i = 1; - if (subIterators.get(0).nextInterval() == IntervalIterator.NO_MORE_INTERVALS) { + currentIndex = 1; + if (first.nextInterval() == IntervalIterator.NO_MORE_INTERVALS) { + i = currentIndex; return start; } + lastStart = last.start(); minimizing = true; } }