/** * Replace all values at the specified index with the specified new value. * * <p>Currently this uses a naive implementation of remove then add. If * it proves desirable, it may be worthwhile to optimize this implementation * with one that performs the remove and insert simultaneously, to save * on tree navigation. * * @return the element that was updated. This is non-null unless the size * parameter is 0, in which case the result is always <code>null</code>. */ public Element<T0> set(int index, /*[ COLORED_START ]*/ byte indexColors, byte color, /*[ COLORED_END ]*/ T0 value, int size) { remove(index, /*[ COLORED_START ]*/ indexColors, /*[ COLORED_END ]*/ size); return add(index, /*[ COLORED_START ]*/ indexColors, color, /*[ COLORED_END ]*/ value, size); }
assert(BciiTree.colorAsIndex(color) >= 0 && BciiTree.colorAsIndex(color) < 7); this.color = color;
this.node = (/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/)tree.get(currentIndex /*[ COLORED_START ]*/, nextIndexColors /*[ COLORED_END ]*/); count1 = tree.convertIndexColor(currentIndex, nextIndexColors, (byte)1) + (node.color == 1 ? 0 : 1); count2 = tree.convertIndexColor(currentIndex, nextIndexColors, (byte)2) + (node.color == 2 ? 0 : 1); count4 = tree.convertIndexColor(currentIndex, nextIndexColors, (byte)4) + (node.color == 4 ? 0 : 1); if(node.color == 1) this.index = count1 - tree.indexOfNode(this.node, (byte)1); if(node.color == 2) this.index = count2 - tree.indexOfNode(this.node, (byte)2); if(node.color == 4) this.index = count4 - tree.indexOfNode(this.node, (byte)4);
/** * Remove the specified element from the tree outright. */ public void remove(Element<T0> element) { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ node = (/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/)element; /*[ WIDE_NODES_START ]*/ assert(node.size > 0); /*[ WIDE_NODES_END ]*/ assert(root != null); // delete the node by adding to the zero queue fixCountsThruRoot(node, /*[ COLORED_START ]*/ node.color, /*[ COLORED_END ]*/ /*[ WIDE_NODES_START(-1) ]*/ -node.size /*[ WIDE_NODES_END ]*/); /*[ WIDE_NODES_START ]*/ node.size = 0; /*[ WIDE_NODES_END ]*/ zeroQueue.add(node); drainZeroQueue(); assert(valid()); }
/** * Remove size values at the specified index. Only values of the type * specified in indexColors will be removed. * * <p>Note that if the two nodes on either side of the removed node could * be merged, they probably will not be merged by this implementation. This * is to simplify the implementation, but it means that when iterating a * tree, sometimes multiple nodes of the same color and value will be * encountered in sequence. */ public void remove(int index, /*[ COLORED_START ]*/ byte indexColors, /*[ COLORED_END ]*/ int size) { if(size == 0) return; assert(index >= 0); assert(index + size <= size(/*[ COLORED_START ]*/ indexColors /*[ COLORED_END ]*/)); assert(root != null); // remove values from the tree removeFromSubtree(root, index, /*[ COLORED_START ]*/ indexColors, /*[ COLORED_END ]*/ size); // clean up any nodes that got deleted drainZeroQueue(); assert(valid()); }
/** * Print this tree as a list of colors, removing all hierarchy. */ /*[ COLORED_START ]*/ public String asSequenceOfColors() { if(root == null) return ""; // print it flattened, like a list of colors StringBuffer result = new StringBuffer(); for(BciiNode n = firstNode(); n != null; n = next(n)) { Object color = coder.getColors().get(colorAsIndex(n.color)); for(/*[ WIDE_NODES_START(true) ]*/ int i = 0; i < n.size; i++/*[ WIDE_NODES_END ]*/) { result.append(color); } } return result.toString(); } /*[ COLORED_END ]*/
for(/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ currentFollower = parent; true; currentFollower = next(currentFollower)) { fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size); return parent; /*[ 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); fixHeightPostChange(parent, false); return inserted; /*[ 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); fixHeightPostChange(parent, false); return inserted;
if(index >= parentLeftSize && index <= parentRightStartIndex) { /*[ WIDE_NODES_START ]*/ parent.size += size; /*[ WIDE_NODES_END ]*/ fixCountsThruRoot(parent, /*[ COLORED_START ]*/ color, /*[ COLORED_END ]*/ size); return parent; /*[ 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); fixHeightPostChange(parent, false); return inserted; int parentRightHalfSize = parentRightStartIndex - index; /*[ WIDE_NODES_START ]*/ parent.size -= parentRightHalfSize; /*[ WIDE_NODES_END ]*/ fixCountsThruRoot(parent, /*[ COLORED_START ]*/ parent.color, /*[ COLORED_END ]*/ -parentRightHalfSize); Element<T0> inserted = insertIntoSubtree(parent, index, /*[ COLORED_START ]*/ indexColors, parent.color, /*[ COLORED_END ]*/ null, parentRightHalfSize); inserted.set(parent.t0); /*[ 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); fixHeightPostChange(parent, false); return inserted;
fixCountsThruRoot(replacement, /*[ COLORED_START ]*/ replacement.color, /*[ COLORED_END ]*/ /*[ WIDE_NODES_START(-1) ]*/ -replacement.size /*[ WIDE_NODES_END ]*/); replaceChild(replacement, replacement.left); replacement.height = toReplace.height; /*[ REFRESH_COUNTS(replacement) ]*/ replacement.refreshCounts(); /*[ EXAMPLE_END ]*/ replaceChild(toReplace, replacement); fixCountsThruRoot(replacement.parent, /*[ COLORED_START ]*/ replacement.color, /*[ COLORED_END ]*/ /*[ WIDE_NODES_START(1) ]*/ replacement.size /*[ WIDE_NODES_END ]*/);
removeFromSubtree(nodeLeft, index, /*[ COLORED_START ]*/ indexColors, /*[ COLORED_END ]*/ toRemove); size -= toRemove; leftSize -= toRemove; size -= toRemove; rightStartIndex -= toRemove; fixCountsThruRoot(node, /*[ COLORED_START ]*/ node.color, /*[ COLORED_END ]*/ -toRemove); if(/*[ WIDE_NODES_START(true) ]*/ node.size == 0 /*[ WIDE_NODES_END ]*/) { zeroQueue.add(node);
/** {@inheritDoc} */ @Override public T0 get(int index) { return tree.get(index /*[ COLORED_START ]*/, colors /*[ COLORED_END ]*/).get(); }
/** * 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 ]*/
/** {@inheritDoc} */ @Override public void add(int index, T0 element) { tree.add(index, /*[ COLORED_START ]*/ colors, color, /*[ COLORED_END ]*/ element, 1); }
/** * Replace the specified node with the specified replacement. This does the * replacement, then walks up the tree to ensure heights are correct, so * the replacement node should have its height set first before this method * is called. */ private void replaceChild(/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ node, /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ replacement) { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ nodeParent = node.parent; // replace the root if(nodeParent == null) { assert(node == root); root = replacement; // replace on the left } else if(nodeParent.left == node) { nodeParent.left = replacement; // replace on the right } else if(nodeParent.right == node) { nodeParent.right = replacement; } // update the replacement's parent if(replacement != null) { replacement.parent = nodeParent; } // the height has changed, update that up the tree fixHeightPostChange(nodeParent, true); } /**
/** * Remove size values at the specified index. Only values of the type * specified in indexColors will be removed. * * <p>Note that if the two nodes on either side of the removed node could * be merged, they probably will not be merged by this implementation. This * is to simplify the implementation, but it means that when iterating a * tree, sometimes multiple nodes of the same color and value will be * encountered in sequence. */ public void remove(int index, /*[ COLORED_START ]*/ byte indexColors, /*[ COLORED_END ]*/ int size) { if(size == 0) return; assert(index >= 0); assert(index + size <= size(/*[ COLORED_START ]*/ indexColors /*[ COLORED_END ]*/)); assert(root != null); // remove values from the tree removeFromSubtree(root, index, /*[ COLORED_START ]*/ indexColors, /*[ COLORED_END ]*/ size); // clean up any nodes that got deleted drainZeroQueue(); assert(valid()); }
/** * Remove the specified element from the tree outright. */ public void remove(Element<T0> element) { /*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/ node = (/*[ NODENAME_START ]*/ BciiNode<T0,T1> /*[ NODENAME_END ]*/)element; /*[ WIDE_NODES_START ]*/ assert(node.size > 0); /*[ WIDE_NODES_END ]*/ assert(root != null); // delete the node by adding to the zero queue fixCountsThruRoot(node, /*[ COLORED_START ]*/ node.color, /*[ COLORED_END ]*/ /*[ WIDE_NODES_START(-1) ]*/ -node.size /*[ WIDE_NODES_END ]*/); /*[ WIDE_NODES_START ]*/ node.size = 0; /*[ WIDE_NODES_END ]*/ zeroQueue.add(node); drainZeroQueue(); assert(valid()); }
/** * Print this tree as a list of colors, removing all hierarchy. */ /*[ COLORED_START ]*/ public String asSequenceOfColors() { if(root == null) return ""; // print it flattened, like a list of colors StringBuffer result = new StringBuffer(); for(BciiNode n = firstNode(); n != null; n = next(n)) { Object color = coder.getColors().get(colorAsIndex(n.color)); for(/*[ WIDE_NODES_START(true) ]*/ int i = 0; i < n.size; i++/*[ WIDE_NODES_END ]*/) { result.append(color); } } return result.toString(); } /*[ COLORED_END ]*/