- // Ref //////////////////////////////////////////////////////////////////////////////
-
- /**
- * 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 Forest<T> implements Iterable<Forest.Body<T>> {
- private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
- public Ref() { }
- public void merge(Forest p) { if (p!=this) hp.add(p, true); }
- public <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
- for(Forest.Body<T> bod : this)
- ivbc.invoke(bod, b, c);
- }
- public Iterator<Body<T>> iterator() {
- final Iterator<Forest<T>> ift = hp==null ? null : hp.iterator();
- return new Iterator<Body<T>>() {
- Iterator<Body<T>> ibt = ift==null ? null : ift.hasNext() ? ift.next().iterator() : null;
- public void remove() { throw new RuntimeException("not supported"); }
- public boolean hasNext() {
- if (ibt==null) return false;
- if (ibt.hasNext()) return true;
- ibt = ift.hasNext() ? ift.next().iterator() : null;
- return hasNext();
- }
- public Body<T> next() {
- return ibt.next();
+ // Many //////////////////////////////////////////////////////////////////////////////
+
+ /** An "ambiguity node"; this is immutable once it has been "looked at" */
+ static class Many<NodeType> extends Forest<NodeType> {
+
+ private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
+ private boolean touched = false;
+
+ public Many() { }
+
+ public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
+
+ public Tree<NodeType> expand1() throws Ambiguous {
+ touched();
+ if (hp.size() > 1) {
+ HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
+ Iterator<Forest<NodeType>> ih = hp.iterator();
+ ih.next().gather(hf0);
+ for(Forest<NodeType> f : hp) {
+ HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
+ f.gather(hf1);
+ hf0.retainAll(hf1);