private BulkScorerAndDoc advance(int min) throws IOException { assert tail.size() == minShouldMatch - 1; final HeadPriorityQueue head = this.head; final TailPriorityQueue tail = this.tail; BulkScorerAndDoc headTop = head.top(); BulkScorerAndDoc tailTop = tail.top(); while (headTop.next < min) { if (tailTop == null || headTop.cost <= tailTop.cost) { headTop.advance(min); headTop = head.updateTop(); } else { // swap the top of head and tail final BulkScorerAndDoc previousHeadTop = headTop; tailTop.advance(min); headTop = head.updateTop(tailTop); tailTop = tail.updateTop(previousHeadTop); } } return headTop; }