/** * ensures a leaf node we have seeked in, is not positioned outside of its bounds, * by moving us into its parents (if any); if it is the root, we're permitted to be out-of-bounds * as this indicates exhaustion */ private NodeCursor<K> ensureValidLocation(boolean forwards, NodeCursor<K> cur) { assert cur.isLeaf(); int position = cur.position; // if we're out of bounds of the leaf, move once in direction of travel if ((position < 0) | (position >= getLeafKeyEnd(cur.node))) cur = moveOutOfLeaf(forwards, cur, root()); return cur; }
if ((index < 0) | (index >= BTree.size(rootNode()))) if ((index < -1) | (index > BTree.size(rootNode()))) throw new IndexOutOfBoundsException(index + " not in range [0.." + BTree.size(rootNode()) + ")"); reset(index == -1); return; NodeCursor<K> cur = root(); assert cur.nodeOffset == 0; while (true)
if ((!tryOne & cur.isLeaf()) && !(tryOne = (cur.advanceLeafNode(forwards) || (cur = moveOutOfLeaf(forwards, cur, null)) != null))) this.cur = root(); return false; while (cur != root()) cur = ensureValidLocation(forwards, cur);
/** * Move the cursor to either the first or last item in the btree * @param start true if should move to the first item; false moves to the last */ void reset(boolean start) { cur = root(); root().inChild = false; // this is a corrupt position, but we ensure we never use it except to start our search from root().position = start ? -1 : getKeyEnd(root().node); }
if ((index < 0) | (index >= BTree.size(rootNode()))) if ((index < -1) | (index > BTree.size(rootNode()))) throw new IndexOutOfBoundsException(index + " not in range [0.." + BTree.size(rootNode()) + ")"); reset(index == -1); return; NodeCursor<K> cur = root(); assert cur.nodeOffset == 0; while (true)
if ((!tryOne & cur.isLeaf()) && !(tryOne = (cur.advanceLeafNode(forwards) || (cur = moveOutOfLeaf(forwards, cur, null)) != null))) this.cur = root(); return false; while (cur != root()) cur = ensureValidLocation(forwards, cur);
/** * Move the cursor to either the first or last item in the btree * @param start true if should move to the first item; false moves to the last */ void reset(boolean start) { cur = root(); root().inChild = false; // this is a corrupt position, but we ensure we never use it except to start our search from root().position = start ? -1 : getKeyEnd(root().node); }
if ((index < 0) | (index >= BTree.size(rootNode()))) if ((index < -1) | (index > BTree.size(rootNode()))) throw new IndexOutOfBoundsException(index + " not in range [0.." + BTree.size(rootNode()) + ")"); reset(index == -1); return; NodeCursor<K> cur = root(); assert cur.nodeOffset == 0; while (true)
if ((!tryOne & cur.isLeaf()) && !(tryOne = (cur.advanceLeafNode(forwards) || (cur = moveOutOfLeaf(forwards, cur, null)) != null))) this.cur = root(); return false; while (cur != root()) cur = ensureValidLocation(forwards, cur);
/** * ensures a leaf node we have seeked in, is not positioned outside of its bounds, * by moving us into its parents (if any); if it is the root, we're permitted to be out-of-bounds * as this indicates exhaustion */ private NodeCursor<K> ensureValidLocation(boolean forwards, NodeCursor<K> cur) { assert cur.isLeaf(); int position = cur.position; // if we're out of bounds of the leaf, move once in direction of travel if ((position < 0) | (position >= getLeafKeyEnd(cur.node))) cur = moveOutOfLeaf(forwards, cur, root()); return cur; }
/** * Move the cursor to either the first or last item in the btree * @param start true if should move to the first item; false moves to the last */ void reset(boolean start) { cur = root(); root().inChild = false; // this is a corrupt position, but we ensure we never use it except to start our search from root().position = start ? -1 : getKeyEnd(root().node); }
if ((index < 0) | (index >= BTree.size(rootNode()))) if ((index < -1) | (index > BTree.size(rootNode()))) throw new IndexOutOfBoundsException(index + " not in range [0.." + BTree.size(rootNode()) + ")"); reset(index == -1); return; NodeCursor<K> cur = root(); assert cur.nodeOffset == 0; while (true)
if ((!tryOne & cur.isLeaf()) && !(tryOne = (cur.advanceLeafNode(forwards) || (cur = moveOutOfLeaf(forwards, cur, null)) != null))) this.cur = root(); return false; while (cur != root()) cur = ensureValidLocation(forwards, cur);
/** * ensures a leaf node we have seeked in, is not positioned outside of its bounds, * by moving us into its parents (if any); if it is the root, we're permitted to be out-of-bounds * as this indicates exhaustion */ private NodeCursor<K> ensureValidLocation(boolean forwards, NodeCursor<K> cur) { assert cur.isLeaf(); int position = cur.position; // if we're out of bounds of the leaf, move once in direction of travel if ((position < 0) | (position >= getLeafKeyEnd(cur.node))) cur = moveOutOfLeaf(forwards, cur, root()); return cur; }
/** * Move the cursor to either the first or last item in the btree * @param start true if should move to the first item; false moves to the last */ void reset(boolean start) { cur = root(); root().inChild = false; // this is a corrupt position, but we ensure we never use it except to start our search from root().position = start ? -1 : getKeyEnd(root().node); }
/** * ensures a leaf node we have seeked in, is not positioned outside of its bounds, * by moving us into its parents (if any); if it is the root, we're permitted to be out-of-bounds * as this indicates exhaustion */ private NodeCursor<K> ensureValidLocation(boolean forwards, NodeCursor<K> cur) { assert cur.isLeaf(); int position = cur.position; // if we're out of bounds of the leaf, move once in direction of travel if ((position < 0) | (position >= getLeafKeyEnd(cur.node))) cur = moveOutOfLeaf(forwards, cur, root()); return cur; }
/** * 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; }
/** * 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(); }
/** * 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; }
/** * 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(); }