/** * 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); /*[ REFRESH_COUNTS(subtreeRoot) ]*/ subtreeRoot.refreshCounts(); /*[ EXAMPLE_END ]*/ byte newSubtreeRootRightHeight = newSubtreeRoot.right != null ? newSubtreeRoot.right.height : 0; newSubtreeRoot.height = (byte)(Math.max(newSubtreeRootLeftHeight, newSubtreeRootRightHeight) + 1); /*[ REFRESH_COUNTS(newSubtreeRoot) ]*/ newSubtreeRoot.refreshCounts(); /*[ EXAMPLE_END ]*/
/** * 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) { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ node = (/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/)element; // count all elements left of this node int index = node.left != null ? node.left./*[ COLORED_START(count1) ]*/ size(colorsOut) /*[ COLORED_END ]*/ : 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./*[ COLORED_START(count1) ]*/ size(colorsOut) /*[ COLORED_END ]*/ : 0; index += /*[ NODE_SIZE(node.parent, colorsOut) EXAMPLE_START ]*/ node.parent.nodeSize(colorsOut) /*[ EXAMPLE_END ]*/; } } return index; }
int parentLeftSize = parentLeft != null ? parentLeft./*[ COLORED_START(count1) ]*/ size(indexColors) /*[ COLORED_END ]*/ : 0; int parentRightStartIndex = parentLeftSize + /*[ NODE_SIZE(parent, indexColors) EXAMPLE_START ]*/ parent.nodeSize(indexColors) /*[ EXAMPLE_END ]*/; /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = new /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, parent); parent.left = inserted; fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size); parentRightStartIndex = parentLeftSize + /*[ NODE_SIZE(parent, indexColors) EXAMPLE_START ]*/ parent.nodeSize(indexColors) /*[ EXAMPLE_END ]*/; int parentSize = parent./*[ COLORED_START(count1) ]*/ size(indexColors) /*[ COLORED_END ]*/; assert(index <= parentSize); /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ parentRight = parent.right; /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = new /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, parent); parent.right = inserted; fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ 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 /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, null); assert(valid()); return this.root; } else { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = insertIntoSubtreeInSortedOrder(root, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ 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(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) { if(root == null) return 0; else return root./*[ COLORED_START(count1) ]*/ size(colors) /*[ COLORED_END ]*/; }
/** * Change the color of the specified element. */ public final void setColor(Element<T0> element, byte color) { BciiNode node = (BciiNode)element; byte oldColor = node.getColor(); if(oldColor == color) return; fixCountsThruRoot(node, oldColor, -node.size); node.color = color; fixCountsThruRoot(node, color, node.size); } /*[ COLORED_END ]*/
public T0 value() { if(node == null) throw new IllegalStateException(); return node.get(); } public Element<T0> node() {
/** * 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) { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ node = (/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/)element; // count all elements left of this node int index = node.left != null ? node.left./*[ COLORED_START(count1) ]*/ size(colorsOut) /*[ COLORED_END ]*/ : 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./*[ COLORED_START(count1) ]*/ size(colorsOut) /*[ COLORED_END ]*/ : 0; index += /*[ NODE_SIZE(node.parent, colorsOut) EXAMPLE_START ]*/ node.parent.nodeSize(colorsOut) /*[ EXAMPLE_END ]*/; } } return index; }
int parentLeftSize = parentLeft != null ? parentLeft./*[ COLORED_START(count1) ]*/ size(indexColors) /*[ COLORED_END ]*/ : 0; int parentRightStartIndex = parentLeftSize + /*[ NODE_SIZE(parent, indexColors) EXAMPLE_START ]*/ parent.nodeSize(indexColors) /*[ EXAMPLE_END ]*/; /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = new /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, parent); parent.left = inserted; fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size); parentRightStartIndex = parentLeftSize + /*[ NODE_SIZE(parent, indexColors) EXAMPLE_START ]*/ parent.nodeSize(indexColors) /*[ EXAMPLE_END ]*/; int parentSize = parent./*[ COLORED_START(count1) ]*/ size(indexColors) /*[ COLORED_END ]*/; assert(index <= parentSize); /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ parentRight = parent.right; /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = new /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, parent); parent.right = inserted; fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ 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 /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, null); assert(valid()); return this.root; } else { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = insertIntoSubtreeInSortedOrder(root, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ 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(/*[ COLORED_START ]*/ byte colors /*[ COLORED_END ]*/) { if(root == null) return 0; else return root./*[ COLORED_START(count1) ]*/ size(colors) /*[ COLORED_END ]*/; }
/** * Change the color of the specified element. */ public final void setColor(Element<T0> element, byte color) { BciiNode node = (BciiNode)element; byte oldColor = node.getColor(); if(oldColor == color) return; fixCountsThruRoot(node, oldColor, -node.size); node.color = color; fixCountsThruRoot(node, color, node.size); } /*[ COLORED_END ]*/
public T0 value() { if(node == null) throw new IllegalStateException(); return node.get(); } public Element<T0> node() {
/** * 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) { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ node = (/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/)element; // count all elements left of this node int index = node.left != null ? node.left./*[ COLORED_START(count1) ]*/ size(colorsOut) /*[ COLORED_END ]*/ : 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./*[ COLORED_START(count1) ]*/ size(colorsOut) /*[ COLORED_END ]*/ : 0; index += /*[ NODE_SIZE(node.parent, colorsOut) EXAMPLE_START ]*/ node.parent.nodeSize(colorsOut) /*[ EXAMPLE_END ]*/; } } return index; }
int parentLeftSize = parentLeft != null ? parentLeft./*[ COLORED_START(count1) ]*/ size(indexColors) /*[ COLORED_END ]*/ : 0; int parentRightStartIndex = parentLeftSize + /*[ NODE_SIZE(parent, indexColors) EXAMPLE_START ]*/ parent.nodeSize(indexColors) /*[ EXAMPLE_END ]*/; /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = new /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, parent); parent.left = inserted; fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size); parentRightStartIndex = parentLeftSize + /*[ NODE_SIZE(parent, indexColors) EXAMPLE_START ]*/ parent.nodeSize(indexColors) /*[ EXAMPLE_END ]*/; int parentSize = parent./*[ COLORED_START(count1) ]*/ size(indexColors) /*[ COLORED_END ]*/; assert(index <= parentSize); /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ parentRight = parent.right; /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ inserted = new /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/(/*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size, value, parent); parent.right = inserted; fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size);