fixed the ugly comma bug; still unsure of the ultimate solution
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index e74d81e..98e8e4b 100644 (file)
@@ -7,7 +7,11 @@ import java.util.*;
 import java.lang.reflect.*;
 
 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
-public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>> {
+public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>>, IntegerMappable {
+
+    private static int master_idx = 0;
+    private final int idx = master_idx++;
+    public int toInt() { return idx; }
 
     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
@@ -125,6 +129,10 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ impl
     static class Ref<T> extends Forest<T> {
         private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
         public Ref() { }
+        public int toInt() {
+            if (hp.size()==1) return hp.iterator().next().toInt();
+            return super.toInt();
+        }
         public void merge(Forest p) { if (p!=this) hp.add(p, true); }
         public <B,C> void visit(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
             if (hp==null) return;