ec3da782bf7a8378c944092accb9ce59cb239262
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.util.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
10 public abstract class Forest<T> extends PrintableTree<Forest.Body<T>> implements Iterable<Forest.Body<T>> {
11
12     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
14         Iterator<Tree<T>> it = expand(true).iterator();
15         if (!it.hasNext()) throw new ParseFailed();
16         return it.next();
17     }
18
19     /** expand this forest into a set of trees */
20     //public abstract HashSet<Tree<T>>  expand(boolean toss);
21         public HashSet<Tree<T>> expand(boolean toss) {
22             HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
23             for(Body<T> b : this)
24                 ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
25             if (toss && ret.size() > 1) throw new Ambiguous(this);
26             return ret;
27         }
28
29     static        <T> Forest<T> singleton(Input.Location loc)                       { return create(loc, null, new Forest[] { }, false, true); }
30     static        <T> Forest<T> singleton(Input.Location loc, Forest<T> body)       { return create(loc, null, new Forest[] { body },  false, true); }
31     static        <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
32     public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
33         return new MultiForest<T>(new Body<T>(loc, tag, tokens, unwrap, singleton));
34     }
35
36     // Body //////////////////////////////////////////////////////////////////////////////
37
38     protected static class Body<T> extends PrintableTree<Forest<T>> implements Iterable<Forest<T>> {
39
40         private final Input.Location    location;
41         private final T                 tag;
42         private final Forest<T>[]       tokens;
43         private final boolean           unwrap;
44         private final boolean           singleton;
45
46         private Body(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
47             this.location = loc;
48             this.tag = tag;
49             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
50             if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
51             if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
52             this.unwrap = unwrap;
53             this.singleton = singleton;
54         }
55
56         private HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h) {
57             if (singleton) {
58                 for(Body<T> b : tokens[0]) b.expand(toss, toks, i, h);
59
60             } else if (i==tokens.length) {
61                 h.add(new Tree<T>(null, tag, toks.toArray(tree_hint)));
62
63             } else if (unwrap && i==tokens.length-1) {
64                 if (tokens[i] != null)
65                     for(Body b : tokens[i])
66                         b.expand(toss, toks, 0, h);
67
68             } else {
69                 boolean hit = false;
70                 for(Tree<T> r : tokens[i].expand(toss)) {
71                     hit = true;
72                     int old = toks.size();
73                     toks.add(r);
74                     expand(toss, toks, i+1, h);
75                     while(toks.size() > old) toks.remove(toks.size()-1);
76                 }
77                 //if (!hit) throw new Error();
78             }
79             return h;
80         }
81
82         void addTo(FastSet<Body<T>> h) {
83             /*if (!singleton)*/ h.add(this, true);
84             //else for(Body b : tokens[0]) b.addTo(h);
85         }
86
87         protected String  headToString()         { return null; }
88         protected String  headToJava()           { return null; }
89         protected String  left()                 { return "{"; }
90         protected String  right()                { return "}"; }
91         protected boolean ignoreSingleton()      { return false; }
92         public    Iterator<Forest<T>> iterator() { return new ArrayIterator<Forest<T>>(tokens); }
93     }
94
95
96     // Ref //////////////////////////////////////////////////////////////////////////////
97
98     /**
99      *  This class represents a partially complete collection of
100      *  forests to be viewed as a forest at some later date; once
101      *  viewed, it becomes immutable
102      */
103     static class Ref<T> extends Forest<T> {
104         private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
105         public Ref() { }
106         public void merge(Forest p) { if (p!=this) hp.add(p, true); }
107         public Iterator<Body<T>> iterator() {
108             final Iterator<Forest<T>> ift = hp==null ? null : hp.iterator();
109             return new Iterator<Body<T>>() {
110                 Iterator<Body<T>> ibt = ift==null ? null : ift.hasNext() ? ift.next().iterator() : null;
111                 public void remove() { throw new RuntimeException("not supported"); }
112                 public boolean hasNext() {
113                     if (ibt==null) return false;
114                     if (ibt.hasNext()) return true;
115                     ibt = ift.hasNext() ? ift.next().iterator() : null;
116                     return hasNext();
117                 }
118                 public Body<T> next() {
119                     return ibt.next();
120                 }
121             };
122
123         }
124         public Forest resolve() { return this; }
125     }
126
127     private static class MultiForest<T> extends Forest<T> {
128         private final FastSet<Body<T>> results = new FastSet<Body<T>>();
129         public MultiForest(Body<T> b) { results.add(b); }
130         public Iterator<Body<T>> iterator() { return results.iterator(); }
131     }
132
133     // Statics //////////////////////////////////////////////////////////////////////////////
134
135     private static Tree[] tree_hint = new Tree[0];
136     private static final Forest[] emptyForestArray = new Forest[0];
137
138     protected String  headToString()    { return null; }
139     protected String  headToJava()      { return null; }
140     protected String  left()            { return "<?"; }
141     protected String  right()           { return "?>"; }
142     protected boolean ignoreSingleton() { return true; }
143 }