/** * Perform a traversal of the heap binary tree using recursion. Given a node, * visit its children and check if their spans is equivalent to the least * spans. If the spans is equivalent, it increments the count and recursively visit its * two children. */ private final int countPositionEqualToTop(final int root) throws IOException { int count = 0; final int i1 = (root << 1); // index of first child node final int i2 = i1 + 1; // index of second child node Spans top = this.top(); if (i1 <= this.size()) { final Spans child1 = (Spans) this.getHeapArray()[i1]; if (positionEqual(top, child1)) { count++; count += this.countPositionEqualToTop(i1); } } if (i2 <= this.size()) { final Spans child2 = (Spans) this.getHeapArray()[i2]; if (positionEqual(top, child2)) { count++; count += this.countPositionEqualToTop(i2); } } return count; }
/** * Perform a traversal of the heap binary tree using recursion. Given a node, * visit its children and check if their spans is equivalent to the least * spans. If the spans is equivalent, it increments the count and recursively visit its * two children. */ private final int countNodeEqualToTop(final int root) throws IOException { int count = 0; final int i1 = (root << 1); // index of first child node final int i2 = i1 + 1; // index of second child node Spans top = this.top(); if (i1 <= this.size()) { final Spans child1 = (Spans) this.getHeapArray()[i1]; if (nodeEqual(top, child1)) { count++; count += this.countNodeEqualToTop(i1); } } if (i2 <= this.size()) { final Spans child2 = (Spans) this.getHeapArray()[i2]; if (nodeEqual(top, child2)) { count++; count += this.countNodeEqualToTop(i2); } } return count; }
/** * Perform a traversal of the heap binary tree using recursion. Given a node, * visit its children and check if their spans is equivalent to the least * spans. If the spans is equivalent, it sums its score for the current node and recursively visit its * two children. */ private final float sumScoreInNode(final int root) throws IOException { float score = 0; final int i1 = (root << 1); // index of first child node final int i2 = i1 + 1; // index of second child node Spans top = this.top(); if (i1 <= this.size()) { final Spans child1 = (Spans) this.getHeapArray()[i1]; if (nodeEqual(top, child1)) { score += child1.scoreInNode(); score += this.sumScoreInNode(i1); } } if (i2 <= this.size()) { final Spans child2 = (Spans) this.getHeapArray()[i2]; if (nodeEqual(top, child2)) { score += child2.scoreInNode(); score += this.sumScoreInNode(i2); } } return score; }
@Override public boolean skipToCandidate(int target) throws IOException { if (!qInitialized) { qInitialized = true; return initSpanQueue(target); } boolean skipCalled = false; while (queue.size() != 0 && top().doc() < target) { if (top().skipToCandidate(target)) { queue.updateTop(); } else { queue.pop(); } skipCalled = true; } if (skipCalled) { return queue.size() != 0; } return nextCandidateDocument(); }
@Override public boolean nextCandidateDocument() throws IOException { if (!qInitialized) { qInitialized = true; return initSpanQueue(-1); } if (queue.size() == 0) { // all done return false; } if (top().nextCandidateDocument()) { // move to next queue.updateTop(); return true; } queue.pop(); // exhausted a clause return queue.size() != 0; }
private boolean initSpanQueue(final int target) throws IOException { for (Spans subSpan : subSpans) { if (((target == -1) && subSpan.nextCandidateDocument()) || ((target != -1) && subSpan.skipToCandidate(target))) { queue.add(subSpan); } } return queue.size() != 0; }