public boolean stealFromRight(Leaf right) { if (right.size() > capacity / 2) { append(right.firstKey(), right.firstValue()); right.removeFirst(); return true; } return false; }
private void compactLeaves(Leaf node, int index, int nodeOffset) { if (index + 1 <= size) { Leaf left = node; Leaf right = (Leaf) getChild(nodeOffset + 2); if (left.stealFromRight(right)) { setChild(nodeOffset + 1, right.firstKey()); } else { left.mergeFrom(right); removeMergedNode(nodeOffset); } } else { Leaf left = (Leaf) getChild(nodeOffset - 2); Leaf right = node; if (right.stealFromLeft(left)) { setChild(nodeOffset - 1, right.firstKey()); } else { left.mergeFrom(right); removeMergedNode(nodeOffset); } } }
public Object put(Comparator comparator, Object key, Object val) { Object oldVal; int search = binarySearch(this, 0, size, key, comparator); if (search > -1) { Entry entry = get(search); oldVal = entry.getValue(); entry.setValue(val); } else if (size < capacity) { oldVal = null; search = -(search + 1); insert(search, key, val); } else { Leaf next = Leaf.newInstance(capacity); int halfSize = size / 2; shallowCopy(this, halfSize, next, 0, halfSize); clear(halfSize, size); size = halfSize; next.size = halfSize; if (compare(comparator, key, next.firstKey()) < 0) { put(comparator, key, val); } else { next.put(comparator, key, val); } next.next = this.next; this.next = next; oldVal = Node.Sentinal.SPLIT; } return oldVal; }