- /**
- * This class represents a partially complete collection of
- * forests to be viewed as a forest at some later date; once
- * viewed, it becomes immutable
- */
- static class Ref<T> extends IterableForest<T> {
- private FastSet<Forest> hp = new FastSet<Forest>();
- private Forest res = null;
- public Ref() { }
- public void merge(Forest p) {
- if (res != null) throw new Error("already resolved!");
- if (p==null) throw new Error();
- if (p!=this) hp.add(p, true);
- }
- public Iterator<Body<T>> iterator() { return ((IterableForest<T>)resolve()).iterator(); }
- public HashSet<Tree<T>> expand(boolean toss) { return resolve().expand(toss); }
- public String toString() { return resolve().toString(); }
- public Forest resolve() {
- if (hp==null) return res;
- FastSet<Body> nh = new FastSet<Body>();
- for(Forest<?> p : hp)
- for(Body<?> b : (IterableForest<?>)p)
- b.addTo(nh);
- res = new MultiForest(nh);
- hp = null;
- return res;
+ public Tree<T> expand1() throws Ambiguous {
+ touched();
+ if (hp.size() > 1) {
+ HashSet<Forest<T>> hf0 = new HashSet<Forest<T>>();
+ Iterator<Forest<T>> ih = hp.iterator();
+ ih.next().gather(hf0);
+ for(Forest<T> f : hp) {
+ HashSet<Forest<T>> hf1 = new HashSet<Forest<T>>();
+ f.gather(hf1);
+ hf0.retainAll(hf1);
+ }
+ HashSet<Tree<T>> ht = new HashSet<Tree<T>>();
+ expand(ht, hf0, new Tree(null, "*"));
+ throw new Ambiguous((Forest<?>)this,
+ (HashSet<Tree<?>>)(Object)ht);
+ }
+ return hp.iterator().next().expand1();
+ }
+
+ void gather(HashSet<Forest<T>> ht) {
+ touched();
+ ht.add(this);
+ for(Forest<T> f : hp) f.gather(ht);