protected final Decision doTraverse(Node<K, V> top, Cursor<? super K, ? super V> cursor) { if (top.isInternal()) { Decision d = doTraverse(top.left(ctx()), cursor); switch (d) { case REMOVE_AND_EXIT: // fall through case EXIT: return Decision.EXIT; case REMOVE: // fall through case CONTINUE: default: return doTraverse(top.right(ctx()), cursor); } } else { Map.Entry<K, V> e = AbstractCritBitTree.<Map.Entry<K, V>>cast(top); return cursor.select(e); } }
@Override public Entry<K, V> next() { return AbstractCritBitTree.<Entry<K, V>>cast(base.next()); }
public V remove(Object k) { if (root == null) { return null; } final K key = AbstractCritBitTree.<K>cast(k); if (!root.isInternal()) { if (ctx().chk.bitIndex(key, root.getKey()) < 0) { V out = root.getValue(); root = null; size--; return out; } else { return null; } } Node<K, V> grandparent = null; Node<K, V> parent = null; Node<K, V> cur = root; for (; ; ) { switch (cur.next(key, ctx())) { case LEFT: if (cur.hasExternalLeft()) { Node<K, V> leftNode = cur.left(ctx()); if (ctx().chk.bitIndex(key, leftNode.getKey()) < 0) { if (grandparent == null) { root = root.remove(key, ctx(), true); } else { grandparent.remove(key, ctx(), true); } size--; return leftNode.getValue(); } else { return null; } } grandparent = parent; parent = cur; cur = cur.left(ctx()); break; case RIGHT: if (cur.hasExternalRight()) { Node<K, V> rightNode = cur.right(ctx()); if (ctx().chk.bitIndex(key, rightNode.getKey()) < 0) { if (grandparent == null) { root = root.remove(key, ctx(), true); } else { grandparent.remove(key, ctx(), true); } size--; return rightNode.getValue(); } else { return null; } } grandparent = parent; parent = cur; cur = cur.right(ctx()); break; } } }