X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FBalancedTree.java;h=dd993a0dc5e8984003da3e256b66c6c45a36c145;hb=3f8aa5300e178e8975b0edc896a5a9d303e7bdf3;hp=a04b20f683df918251030e661df3666adb383502;hpb=2b3517021799c6d2f5ebbd3af4f399ddbfd2c4e8;p=org.ibex.core.git diff --git a/src/org/ibex/util/BalancedTree.java b/src/org/ibex/util/BalancedTree.java index a04b20f..dd993a0 100644 --- a/src/org/ibex/util/BalancedTree.java +++ b/src/org/ibex/util/BalancedTree.java @@ -16,8 +16,6 @@ package org.ibex.util; /** a weight-balanced tree with fake leaves */ public class BalancedTree { - private static final boolean DEBUG = true; - // Instance Variables /////////////////////////////////////////////////////////////////// private int root = 0; ///< the slot of the root element @@ -25,8 +23,6 @@ public class BalancedTree { private int cached_index = -1; private int cached_slot = -1; - private FinalizationHelper fh; - // Public API ////////////////////////////////////////////////////////////////////////// /** the number of elements in the tree */ @@ -47,12 +43,10 @@ public class BalancedTree { if (root != 0) { insert(index, arg, root, 0, false, false); } else { - if(fh == null) fh = new FinalizationHelper(this); root = arg; left[arg] = right[arg] = parent[arg] = 0; size[arg] = 1; } - if(DEBUG) check(); } } @@ -66,7 +60,6 @@ public class BalancedTree { if (index >= treeSize()) index = treeSize() - 1; int arg = allocateSlot(o); insert(index, arg, root, 0, true, false); - if(DEBUG) check(); } } @@ -130,7 +123,6 @@ public class BalancedTree { left[del] = right[del] = size[del] = parent[del] = 0; Object ret = objects[del]; objects[del] = null; - if(DEBUG) check(); return ret; } } @@ -147,7 +139,6 @@ public class BalancedTree { } while(i != 0); root = 0; } - if(DEBUG) check(); } // Node Data ///////////////////////////////////////////////////////////////////////// @@ -304,8 +295,6 @@ public class BalancedTree { if (size[arg] != 0) throw new Error("double insertion"); - if(DEBUG) check(); - // we are replacing an existing node if (replace) { if (diff != 0) throw new Error("this should never happen"); // since we already clamped the index @@ -321,7 +310,6 @@ public class BalancedTree { if(right[slot] != 0) parent[right[slot]] = arg; objects[slot] = null; left[slot] = right[slot] = size[slot] = parent[slot] = 0; - if(DEBUG) check(); // we become the child of a former leaf } else if (slot == 0) { @@ -336,7 +324,6 @@ public class BalancedTree { // we take the place of a preexisting node } else { - if(DEBUG) check(); left[arg] = left[slot]; // steal slot's left subtree left[slot] = 0; right[arg] = slot; // make slot our right subtree @@ -425,52 +412,9 @@ public class BalancedTree { return slot; } } - - static class FinalizationHelper { - private BalancedTree bt; - FinalizationHelper(BalancedTree bt) { this.bt = bt; } - protected void finalize() { bt.clear(); } - } - // Debugging /////////////////////////////////////////////////////////////////////////// - - public void check() { check(false); } - public void check(boolean expensive) { - if(expensive) System.err.println("--> Running expensive balanced tree checks"); - try { - if(expensive) - for(int i=0;i 0) { - if(parent[left[node]] != node) throw new Error("parent node mismatch on left child of " + node); - check(left[node]); - } - if(right[node] > 0) { - if(parent[right[node]] != node) throw new Error("parent node mismatch on right child of " + node); - check(right[node]); - } - } - + protected void finalize() { clear(); } + public void printTree() { if(root == 0) System.err.println("Tree is empty"); else printTree(root,0,false); @@ -486,17 +430,4 @@ public class BalancedTree { printTree(right[node],indent+1,false); } } - - /*public static void main(String[] args) { - BalancedTree t = new BalancedTree(); - for(int i=0;i