checkpoint
authoradam <adam@megacz.com>
Sun, 22 Jan 2006 09:26:24 +0000 (04:26 -0500)
committeradam <adam@megacz.com>
Sun, 22 Jan 2006 09:26:24 +0000 (04:26 -0500)
darcs-hash:20060122092624-5007d-421f2cd5bf5315cf84138b8c9f30bc98f916132a.gz

src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/util/FastSet.java

index 933515b..4f4baac 100644 (file)
@@ -7,23 +7,7 @@ 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.Body<T>>*/ implements Visitable<Forest.Body<T>> {
-
-    public abstract <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
-    private static class TreeMaker<T> extends HashSet<Tree<T>> implements Invokable<Forest.Body<T>,Boolean,Integer> {
-        public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
-        public void invoke(Forest.Body<T> bod, Boolean toss, Integer i) {
-            if (i==null) {
-                ArrayList<Tree<T>> toks = this.toks;
-                this.toks = new ArrayList<Tree<T>>();
-                //addAll(bod.expand(toss, 0, new TreeMaker<T>()));
-                bod.expand(toss, 0, this);
-                this.toks = toks;
-            } else {
-                bod.expand(toss, i, this);
-            }
-        }
-    }
+public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>> {
 
     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
@@ -126,6 +110,20 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/ implem
         public Forest resolve() { return this; }
     }
 
+    public abstract <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
+    private static class TreeMaker<T> extends HashSet<Tree<T>> implements Invokable<Forest.Body<T>,Boolean,Integer> {
+        public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
+        public void invoke(Forest.Body<T> bod, Boolean toss, Integer i) {
+            if (i==null) {
+                ArrayList<Tree<T>> toks = this.toks;
+                this.toks = new ArrayList<Tree<T>>();
+                bod.expand(toss, 0, this);
+                this.toks = toks;
+            } else {
+                bod.expand(toss, i, this);
+            }
+        }
+    }
 
     // Statics //////////////////////////////////////////////////////////////////////////////
 
index b94613e..1e6c6d5 100644 (file)
@@ -1,7 +1,7 @@
 package edu.berkeley.sbp.util;
 import java.util.*;
 
-public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T> {
+public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T>, Visitable<T> {
 
     public static final int INITIAL_SIZE = 8;
 
@@ -28,6 +28,12 @@ public /*final*/ class FastSet<T> implements Iterator<T>, Iterable<T> {
         for(T t : s) array[size++] = t;
     }
 
+    public <B,C> void invoke(Invokable<T,B,C> ivbc, B b, C c) {
+        if (only!=null) ivbc.invoke(only, b, c);
+        else for(int j=0; j<size; j++)
+            ivbc.invoke((T)array[j], b, c);
+    }
+
     public int size() { return only==null ? size : 1; }
     private void grow() {
         Object[] array2 = array==null ? new Object[INITIAL_SIZE] : new Object[array.length * 2];