+// FEATURE: private void intersection() { }
+// FEATURE: private void union() { }
+// FEATURE: private void subset() { }
+// FEATURE: grow if we run out of slots
+
+/** a weight-balanced tree with fake leaves */
+public class BalancedTree {
+
+
+ // Instance Variables ///////////////////////////////////////////////////////////////////
+
+ private int root = 0; ///< the slot of the root element
+
+ private int cached_index = -1;
+ private int cached_slot = -1;
+
+ // Public API //////////////////////////////////////////////////////////////////////////
+
+ /** the number of elements in the tree */
+ public final int treeSize() { return root == 0 ? 0 : size[root]; }
+
+ /** clamps index to [0..treeSize()] and inserts object o *before* the specified index */
+ public final synchronized void insertNode(int index, Object o) {
+ if(o == null) throw new Error("can't insert nulls in the balanced tree");
+ cached_slot = cached_index = -1;
+ if (index < 0) index = 0;
+ if (index > treeSize()) index = treeSize();
+ int arg = allocateSlot(o);
+ if (root != 0) {
+ insert(index, arg, root, 0, false, false);
+ } else {
+ root = arg;
+ left[arg] = right[arg] = parent[arg] = 0;
+ size[arg] = 1;
+ }
+ }
+
+ /** clamps index to [0..treeSize()-1] and replaces the object at that index with object o */
+ public final synchronized void replaceNode(int index, Object o) {
+ if(o == null) throw new Error("can't insert nulls in the balanced tree");
+ cached_slot = cached_index = -1;
+ if(root == 0) throw new Error("called replaceNode() on an empty tree");
+ if (index < 0) index = 0;
+ if (index >= treeSize()) index = treeSize() - 1;
+ int arg = allocateSlot(o);
+ insert(index, arg, root, 0, true, false);
+ }
+
+ /** returns the index of o; runs in O((log n)^2) time unless cache hit */
+ public final synchronized int indexNode(Object o) {
+ if(o == null) return -1;
+ if (cached_slot != -1 && objects[cached_slot] == o) return cached_index;
+
+ int slot = getSlot(o);
+ if(slot == -1) return -1;
+
+ int index = 0;
+ while(true) {
+ // everything to the left is before us so add that to the index
+ index += sizeof(left[slot]);
+ // we are before anything on the right
+ while(left[parent[slot]] == slot) slot = parent[slot];
+ // we end of the first node who isn't on the left, go to the node that has as its child
+ slot = parent[slot];
+ // if we just processed the root we're done
+ if(slot == 0) break;
+ // count the node we're currently on towards the index
+ index++;
+ }
+ return index;
+ }
+
+ /** returns the object at index; runs in O(log n) time unless cache hit */
+ public final synchronized Object getNode(int index) {
+ if (index == cached_index) return objects[cached_slot];
+
+ if (cached_index != -1) {
+ int distance = Math.abs(index - cached_index);
+ // if the in-order distance between the cached node and the
+ // target node is less than log(n), it's probably faster to
+ // search directly.
+ if ((distance < 16) && ((2 << distance) < treeSize())) {
+ while(cached_index > index) { cached_slot = prev(cached_slot); cached_index--; }
+ while(cached_index < index) { cached_slot = next(cached_slot); cached_index++; }
+ return objects[cached_slot];
+ }
+ }
+ /*
+ cached_index = index;
+ cached_slot = get(index, root);
+ return objects[cached_slot];
+ */
+ return objects[get(index, root)];
+ }