/** * Write this node out as a String, using the specified colors to write * each of the node values. */ String toString(List colors) { StringBuffer result = new StringBuffer(); asTree(0, result, colors); return result.toString(); } /**
byte subtreeRootRightHeight = subtreeRoot.right != null ? subtreeRoot.right.height : 0; subtreeRoot.height = (byte)(Math.max(subtreeRootLeftHeight, subtreeRootRightHeight) + 1); subtreeRoot.refreshCounts(); byte newSubtreeRootRightHeight = newSubtreeRoot.right != null ? newSubtreeRoot.right.height : 0; newSubtreeRoot.height = (byte)(Math.max(newSubtreeRootLeftHeight, newSubtreeRootRightHeight) + 1); newSubtreeRoot.refreshCounts();
/** * Get the index of the specified element, counting only the colors * specified. * * <p>This method is an hotspot, so its crucial that it run as efficiently * as possible. */ public int indexOfNode(Element<T0> element, byte colorsOut) { FourColorNode < T0> node = ( FourColorNode < T0> )element; // count all elements left of this node int index = node.left != null ? node.left. size(colorsOut) : 0; // add all elements on the left, all the way to the root for( ; node.parent != null; node = node.parent) { if(node.parent.right == node) { index += node.parent.left != null ? node.parent.left. size(colorsOut) : 0; index += node.parent.nodeSize(colorsOut) ; } } return index; }
int parentLeftSize = parentLeft != null ? parentLeft. size(indexColors) : 0; int parentRightStartIndex = parentLeftSize + parent.nodeSize(indexColors) ; FourColorNode < T0> inserted = new FourColorNode < T0> ( color, size, value, parent); parent.left = inserted; fixCountsThruRoot(parent, color, size); parentRightStartIndex = parentLeftSize + parent.nodeSize(indexColors) ; int parentSize = parent. size(indexColors) ; assert(index <= parentSize); FourColorNode < T0> parentRight = parent.right; FourColorNode < T0> inserted = new FourColorNode < T0> ( color, size, value, parent); parent.right = inserted; fixCountsThruRoot(parent, color, size);
/** * Add a tree node in sorted order. * * @param size the size of the node to insert. * @param value the node value. If non-<code>null</code>, the node may be * combined with other nodes of the same color and value. <code>null</code> * valued nodes will never be combined with each other. * @return the element the specified value was inserted into. This is non-null * unless the size parameter is 0, in which case the result is always * <code>null</code>. */ public Element<T0> addInSortedOrder(byte color, T0 value, int size) { assert(size >= 0); if(this.root == null) { this.root = new FourColorNode < T0> ( color, size, value, null); assert(valid()); return this.root; } else { FourColorNode < T0> inserted = insertIntoSubtreeInSortedOrder(root, color, value, size); assert(valid()); return inserted; } }
/** {@inheritDoc} */ @Override public String toString() { return toString(Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F", "G", "H"})); }
/** * The size of the tree for the specified colors. */ public int size( byte colors ) { if(root == null) return 0; else return root. size(colors) ; }
public T0 value() { if(node == null) throw new IllegalStateException(); return node.get(); } public Element<T0> node() {
/** * Change the color of the specified element. */ public final void setColor(Element<T0> element, byte color) { FourColorNode node = (FourColorNode)element; byte oldColor = node.getColor(); if(oldColor == color) return; fixCountsThruRoot(node, oldColor, -node.size); node.color = color; fixCountsThruRoot(node, color, node.size); }
/** * Get the index of the specified element, counting only the colors * specified. * * <p>This method is an hotspot, so its crucial that it run as efficiently * as possible. */ public int indexOfNode(Element<T0> element, byte colorsOut) { FourColorNode < T0> node = ( FourColorNode < T0> )element; // count all elements left of this node int index = node.left != null ? node.left. size(colorsOut) : 0; // add all elements on the left, all the way to the root for( ; node.parent != null; node = node.parent) { if(node.parent.right == node) { index += node.parent.left != null ? node.parent.left. size(colorsOut) : 0; index += node.parent.nodeSize(colorsOut) ; } } return index; }
int parentLeftSize = parentLeft != null ? parentLeft. size(indexColors) : 0; int parentRightStartIndex = parentLeftSize + parent.nodeSize(indexColors) ; FourColorNode < T0> inserted = new FourColorNode < T0> ( color, size, value, parent); parent.left = inserted; fixCountsThruRoot(parent, color, size); parentRightStartIndex = parentLeftSize + parent.nodeSize(indexColors) ; int parentSize = parent. size(indexColors) ; assert(index <= parentSize); FourColorNode < T0> parentRight = parent.right; FourColorNode < T0> inserted = new FourColorNode < T0> ( color, size, value, parent); parent.right = inserted; fixCountsThruRoot(parent, color, size);
/** * Add a tree node in sorted order. * * @param size the size of the node to insert. * @param value the node value. If non-<code>null</code>, the node may be * combined with other nodes of the same color and value. <code>null</code> * valued nodes will never be combined with each other. * @return the element the specified value was inserted into. This is non-null * unless the size parameter is 0, in which case the result is always * <code>null</code>. */ public Element<T0> addInSortedOrder(byte color, T0 value, int size) { assert(size >= 0); if(this.root == null) { this.root = new FourColorNode < T0> ( color, size, value, null); assert(valid()); return this.root; } else { FourColorNode < T0> inserted = insertIntoSubtreeInSortedOrder(root, color, value, size); assert(valid()); return inserted; } }
/** {@inheritDoc} */ @Override public String toString() { return toString(Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F", "G", "H"})); }
/** * The size of the tree for the specified colors. */ public int size( byte colors ) { if(root == null) return 0; else return root. size(colors) ; }
public T0 value() { if(node == null) throw new IllegalStateException(); return node.get(); } public Element<T0> node() {
/** * Change the color of the specified element. */ public final void setColor(Element<T0> element, byte color) { FourColorNode node = (FourColorNode)element; byte oldColor = node.getColor(); if(oldColor == color) return; fixCountsThruRoot(node, oldColor, -node.size); node.color = color; fixCountsThruRoot(node, color, node.size); }
/** * Get the index of the specified element, counting only the colors * specified. * * <p>This method is an hotspot, so its crucial that it run as efficiently * as possible. */ public int indexOfNode(Element<T0> element, byte colorsOut) { FourColorNode < T0> node = ( FourColorNode < T0> )element; // count all elements left of this node int index = node.left != null ? node.left. size(colorsOut) : 0; // add all elements on the left, all the way to the root for( ; node.parent != null; node = node.parent) { if(node.parent.right == node) { index += node.parent.left != null ? node.parent.left. size(colorsOut) : 0; index += node.parent.nodeSize(colorsOut) ; } } return index; }
int parentLeftSize = parentLeft != null ? parentLeft. size(indexColors) : 0; int parentRightStartIndex = parentLeftSize + parent.nodeSize(indexColors) ; FourColorNode < T0> inserted = new FourColorNode < T0> ( color, size, value, parent); parent.left = inserted; fixCountsThruRoot(parent, color, size); parentRightStartIndex = parentLeftSize + parent.nodeSize(indexColors) ; int parentSize = parent. size(indexColors) ; assert(index <= parentSize); FourColorNode < T0> parentRight = parent.right; FourColorNode < T0> inserted = new FourColorNode < T0> ( color, size, value, parent); parent.right = inserted; fixCountsThruRoot(parent, color, size);