/** * 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; }
/** * Move all the least spans in the queue that are positioned on the same document, node and position to their * next position and adjust the heap. * * @return If the least spans has no more positions, returns false. */ public final boolean nextPositionAndAdjust() throws IOException { // Count number of spans, starting from the top, having the same document, node and position. // Add 1 to count the top. int count = this.countPositionEqualToTop(1) + 1; // Move the spans to the next position for (int i = 0; i < count; i++) { this.top().nextPosition(); this.updateTop(); } // if the top's position is the sentinel value, it means that the positions are exhausted return (this.top().start() == PositionsIterator.NO_MORE_POS) ? false : true; }
/** * Move all the least spans in the queue that are positioned on the document and node to their * next node and adjust the heap. * * @return If the least spans has no more nodes, returns false. */ public final boolean nextNodeAndAdjust() throws IOException { // Count number of spans, starting from the top, having the same document and node. // Add 1 to count the top. int count = this.countNodeEqualToTop(1) + 1; // Move the spans to the next node for (int i = 0; i < count; i++) { this.top().nextNode(); this.updateTop(); } // if the top's position is the sentinel value, it means that the nodes are exhausted return (NodeUtils.compare(this.top().node(), DocsAndNodesIterator.NO_MORE_NOD) == 0) ? false : true; }
private Spans top() { return queue.top(); }
public float scoreInNode() throws IOException { // Sum the score of spans, starting from the top, having the same document and node. // Add the score of the top. return this.sumScoreInNode(1) + this.top().scoreInNode(); }