NodeCursor(Object[] node, NodeCursor<K> parent, Comparator<? super K> comparator) { this.node = node; this.parent = parent; this.comparator = comparator; // a well formed b-tree (text book, or ours) must be balanced, so by building a stack following the left-most branch // we have a stack capable of visiting any path in the tree this.child = BTree.isLeaf(node) ? null : new NodeCursor<>((Object[]) node[getChildStart(node)], this, comparator); }
/** * move out of a leaf node that is currently out of (its own) bounds * @return null if we're now out-of-bounds of the whole tree */ private <K> NodeCursor<K> moveOutOfLeaf(boolean forwards, NodeCursor<K> cur, NodeCursor<K> ifFail) { while (true) { cur = cur.parent; if (cur == null) { root().inChild = false; return ifFail; } if (cur.advanceIntoBranchFromChild(forwards)) break; } cur.inChild = false; return cur; }
if ((!tryOne & cur.isLeaf()) && !(tryOne = (cur.advanceLeafNode(forwards) || (cur = moveOutOfLeaf(forwards, cur, null)) != null))) K test = cur.value(); NodeCursor<K> bound = cur.boundIterator(forwards); if (bound == null) break; // we're all that's left int cmpbound = comparator.compare(bound.bound(forwards), key); // order of provision matters for asymmetric comparators if (forwards ? cmpbound > 0 : cmpbound < 0) break; // already in correct sub-tree cur.safeAdvanceIntoBranchFromChild(forwards); if (cmpbound == 0) // it was an exact match, so terminate here while (!(match = cur.seekInNode(key, forwards)) && !cur.isLeaf()) cur = cur.descend(); cur.position = forwards ? -1 : getKeyEnd(cur.node);
/** * move the Cursor one item, either forwards or backwards * @param forwards direction of travel * @return false iff the cursor is exhausted in the direction of travel */ int moveOne(boolean forwards) { NodeCursor<K> cur = this.cur; if (cur.isLeaf()) { // if we're a leaf, we try to step forwards inside ourselves if (cur.advanceLeafNode(forwards)) return cur.globalLeafIndex(); // if we fail, we just find our bounding parent this.cur = cur = moveOutOfLeaf(forwards, cur, root()); return cur.globalIndex(); } // otherwise we descend directly into our next child if (forwards) ++cur.position; cur = cur.descend(); // and go to its first item NodeCursor<K> next; while ( null != (next = cur.descendToFirstChild(forwards)) ) cur = next; this.cur = cur; return cur.globalLeafIndex(); }
int delta = isLeaf() & !forwards ? -1 : 0; this.position = delta -1 -find; return false;
public V next(K target) { if (!hasNext()) return null; int state = this.state; boolean found = seekTo(target, forwards, (state & (ON_ITEM | BEFORE_FIRST)) != 0); int index = cur.globalIndex(); V next = null; if (state == BEFORE_FIRST && compareToFirst(index) < 0) return null; int compareToLast = compareToLast(index); if ((compareToLast <= 0)) { state = compareToLast < 0 ? MIDDLE : LAST; if (found) { state |= ON_ITEM; next = (V) currentValue(); } } else state = END; this.state = (byte) state; this.index = index; return next; }
/** * move the Cursor one item, either forwards or backwards * @param forwards direction of travel * @return false iff the cursor is exhausted in the direction of travel */ int moveOne(boolean forwards) { NodeCursor<K> cur = this.cur; if (cur.isLeaf()) { // if we're a leaf, we try to step forwards inside ourselves if (cur.advanceLeafNode(forwards)) return cur.globalLeafIndex(); // if we fail, we just find our bounding parent this.cur = cur = moveOutOfLeaf(forwards, cur, root()); return cur.globalIndex(); } // otherwise we descend directly into our next child if (forwards) ++cur.position; cur = cur.descend(); // and go to its first item NodeCursor<K> next; while ( null != (next = cur.descendToFirstChild(forwards)) ) cur = next; this.cur = cur; return cur.globalLeafIndex(); }
int delta = isLeaf() & !forwards ? -1 : 0; this.position = delta -1 -find; return false;
public V next(K target) { if (!hasNext()) return null; int state = this.state; boolean found = seekTo(target, forwards, (state & (ON_ITEM | BEFORE_FIRST)) != 0); int index = cur.globalIndex(); V next = null; if (state == BEFORE_FIRST && compareToFirst(index) < 0) return null; int compareToLast = compareToLast(index); if ((compareToLast <= 0)) { state = compareToLast < 0 ? MIDDLE : LAST; if (found) { state |= ON_ITEM; next = (V) currentValue(); } } else state = END; this.state = (byte) state; this.index = index; return next; }
if ((!tryOne & cur.isLeaf()) && !(tryOne = (cur.advanceLeafNode(forwards) || (cur = moveOutOfLeaf(forwards, cur, null)) != null))) K test = cur.value(); NodeCursor<K> bound = cur.boundIterator(forwards); if (bound == null) break; // we're all that's left int cmpbound = comparator.compare(bound.bound(forwards), key); // order of provision matters for asymmetric comparators if (forwards ? cmpbound > 0 : cmpbound < 0) break; // already in correct sub-tree cur.safeAdvanceIntoBranchFromChild(forwards); if (cmpbound == 0) // it was an exact match, so terminate here while (!(match = cur.seekInNode(key, forwards)) && !cur.isLeaf()) cur = cur.descend(); cur.position = forwards ? -1 : getKeyEnd(cur.node);
/** * move the Cursor one item, either forwards or backwards * @param forwards direction of travel * @return false iff the cursor is exhausted in the direction of travel */ int moveOne(boolean forwards) { NodeCursor<K> cur = this.cur; if (cur.isLeaf()) { // if we're a leaf, we try to step forwards inside ourselves if (cur.advanceLeafNode(forwards)) return cur.globalLeafIndex(); // if we fail, we just find our bounding parent this.cur = cur = moveOutOfLeaf(forwards, cur, root()); return cur.globalIndex(); } // otherwise we descend directly into our next child if (forwards) ++cur.position; cur = cur.descend(); // and go to its first item NodeCursor<K> next; while ( null != (next = cur.descendToFirstChild(forwards)) ) cur = next; this.cur = cur; return cur.globalLeafIndex(); }
int delta = isLeaf() & !forwards ? -1 : 0; this.position = delta -1 -find; return false;
NodeCursor(Object[] node, NodeCursor<K> parent, Comparator<? super K> comparator) { this.node = node; this.parent = parent; this.comparator = comparator; // a well formed b-tree (text book, or ours) must be balanced, so by building a stack following the left-most branch // we have a stack capable of visiting any path in the tree this.child = BTree.isLeaf(node) ? null : new NodeCursor<>((Object[]) node[getChildStart(node)], this, comparator); }
/** * move out of a leaf node that is currently out of (its own) bounds * @return null if we're now out-of-bounds of the whole tree */ private <K> NodeCursor<K> moveOutOfLeaf(boolean forwards, NodeCursor<K> cur, NodeCursor<K> ifFail) { while (true) { cur = cur.parent; if (cur == null) { root().inChild = false; return ifFail; } if (cur.advanceIntoBranchFromChild(forwards)) break; } cur.inChild = false; return cur; }
public V next(K target) { if (!hasNext()) return null; int state = this.state; boolean found = seekTo(target, forwards, (state & (ON_ITEM | BEFORE_FIRST)) != 0); int index = cur.globalIndex(); V next = null; if (state == BEFORE_FIRST && compareToFirst(index) < 0) return null; int compareToLast = compareToLast(index); if ((compareToLast <= 0)) { state = compareToLast < 0 ? MIDDLE : LAST; if (found) { state |= ON_ITEM; next = (V) currentValue(); } } else state = END; this.state = (byte) state; this.index = index; return next; }
if ((!tryOne & cur.isLeaf()) && !(tryOne = (cur.advanceLeafNode(forwards) || (cur = moveOutOfLeaf(forwards, cur, null)) != null))) K test = cur.value(); NodeCursor<K> bound = cur.boundIterator(forwards); if (bound == null) break; // we're all that's left int cmpbound = comparator.compare(bound.bound(forwards), key); // order of provision matters for asymmetric comparators if (forwards ? cmpbound > 0 : cmpbound < 0) break; // already in correct sub-tree cur.safeAdvanceIntoBranchFromChild(forwards); if (cmpbound == 0) // it was an exact match, so terminate here while (!(match = cur.seekInNode(key, forwards)) && !cur.isLeaf()) cur = cur.descend(); cur.position = forwards ? -1 : getKeyEnd(cur.node);
/** * move the Cursor one item, either forwards or backwards * @param forwards direction of travel * @return false iff the cursor is exhausted in the direction of travel */ int moveOne(boolean forwards) { NodeCursor<K> cur = this.cur; if (cur.isLeaf()) { // if we're a leaf, we try to step forwards inside ourselves if (cur.advanceLeafNode(forwards)) return cur.globalLeafIndex(); // if we fail, we just find our bounding parent this.cur = cur = moveOutOfLeaf(forwards, cur, root()); return cur.globalIndex(); } // otherwise we descend directly into our next child if (forwards) ++cur.position; cur = cur.descend(); // and go to its first item NodeCursor<K> next; while ( null != (next = cur.descendToFirstChild(forwards)) ) cur = next; this.cur = cur; return cur.globalLeafIndex(); }