@Override boolean nextPhrase() throws IOException { int first = 0; NodePhrasePosition lastPosition = phrasePositions[phrasePositions.length - 1]; NodePhrasePosition firstPosition = phrasePositions[first]; // scan forward in last if (lastPosition.pos == PositionsIterator.NO_MORE_POS || !lastPosition.nextPosition()) { return false; } while (firstPosition.pos < lastPosition.pos) { do { if (!firstPosition.nextPosition()) { // scan forward in first return false; } } while (firstPosition.pos < lastPosition.pos); lastPosition = firstPosition; first = (first == (phrasePositions.length - 1)) ? 0 : first + 1; firstPosition = phrasePositions[first]; } // all equal: a match return true; }