From ef9c08962db99929febe84197e9162e6867f942d Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 22 Jan 2006 03:28:45 -0500 Subject: [PATCH] checkpoint darcs-hash:20060122082845-5007d-3690d5d49135c2f3175b9d6e48a9020ececfcf2c.gz --- src/edu/berkeley/sbp/Forest.java | 70 ++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index 0a20228..ec3da78 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -17,13 +17,20 @@ public abstract class Forest extends PrintableTree> implements } /** expand this forest into a set of trees */ - public abstract HashSet> expand(boolean toss); + //public abstract HashSet> expand(boolean toss); + public HashSet> expand(boolean toss) { + HashSet> ret = new HashSet>(); + for(Body b : this) + ret.addAll(b.expand(toss, new ArrayList>(), 0, new HashSet>())); + if (toss && ret.size() > 1) throw new Ambiguous(this); + return ret; + } static Forest singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); } static Forest singleton(Input.Location loc, Forest body) { return create(loc, null, new Forest[] { body }, false, true); } static Forest leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); } public static Forest create(Input.Location loc, T tag, Forest[] tokens, boolean unwrap, boolean singleton) { - return new MultiForest(loc, tag, tokens, unwrap, singleton); + return new MultiForest(new Body(loc, tag, tokens, unwrap, singleton)); } // Body ////////////////////////////////////////////////////////////////////////////// @@ -72,9 +79,9 @@ public abstract class Forest extends PrintableTree> implements return h; } - void addTo(FastSet h) { - if (!singleton) h.add(this, true); - else for(Body b : tokens[0]) b.addTo(h); + void addTo(FastSet> h) { + /*if (!singleton)*/ h.add(this, true); + //else for(Body b : tokens[0]) b.addTo(h); } protected String headToString() { return null; } @@ -94,44 +101,33 @@ public abstract class Forest extends PrintableTree> implements * viewed, it becomes immutable */ static class Ref extends Forest { - private FastSet hp = new FastSet(); - private Forest res = null; + private FastSet> hp = new FastSet>(); 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> iterator() { return ((Forest)resolve()).iterator(); } - public HashSet> expand(boolean toss) { return resolve().expand(toss); } - public Forest resolve() { - if (hp==null) return res; - FastSet nh = new FastSet(); - for(Forest p : hp) - for(Body b : (Forest)p) - b.addTo(nh); - res = new MultiForest(nh); - hp = null; - return res; + public void merge(Forest p) { if (p!=this) hp.add(p, true); } + public Iterator> iterator() { + final Iterator> ift = hp==null ? null : hp.iterator(); + return new Iterator>() { + Iterator> 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 next() { + return ibt.next(); + } + }; + } + public Forest resolve() { return this; } } - // Implementations ////////////////////////////////////////////////////////////////////////////// - private static class MultiForest extends Forest { - private final FastSet> results; - private MultiForest(FastSet> results) { this.results = results; } - public MultiForest(Input.Location loc, T tag, Forest[] tokens, boolean unwrap, boolean singleton) { - this.results = new FastSet>(new Body(loc, tag, tokens, unwrap, singleton)); - } + private final FastSet> results = new FastSet>(); + public MultiForest(Body b) { results.add(b); } public Iterator> iterator() { return results.iterator(); } - public HashSet> expand(boolean toss) { - HashSet> ret = new HashSet>(); - for(Body b : results) - ret.addAll(b.expand(toss, new ArrayList>(), 0, new HashSet>())); - if (toss && ret.size() > 1) throw new Ambiguous(this); - return ret; - } } // Statics ////////////////////////////////////////////////////////////////////////////// -- 1.7.10.4