From: adam Date: Sun, 22 Jan 2006 10:30:30 +0000 (-0500) Subject: checkpoint X-Git-Tag: tag_for_25-Mar~328 X-Git-Url: http://git.megacz.com/?p=sbp.git;a=commitdiff_plain;h=ccfd595c00c451e277958b98d94d4ab70de2f2a6 checkpoint darcs-hash:20060122103030-5007d-0eb08ef6167373dad28a87a904c3af79c212b681.gz --- diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index 420d655..475b300 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -18,15 +18,17 @@ public abstract class Forest /*extends PrintableTree>*/ impl /** expand this forest into a set of trees */ public HashSet> expand(boolean toss) { - final HashSet> hs = new HashSet>(); - TreeMaker ret = new TreeMaker(toss) { - public void start(T head, Input.Location loc) { } - public void finish(T head, Input.Location loc) { hs.add(new Tree(loc, head, toks.toArray(tree_hint))); } - public void child(Tree t) { toks.add(t); } - }; - visit(ret, null, null); - if (toss && hs.size() > 1) throw new Ambiguous(this); - return hs; + final HashSetTreeConsumer ret = new HashSetTreeConsumer(); + visit(new TreeMaker2(toss, ret), null, null); + if (toss && ret.size() > 1) throw new Ambiguous(this); + return ret; + } + + public static interface TreeConsumer { + public void addTree(Tree t); + } + public static class HashSetTreeConsumer extends HashSet> implements TreeConsumer { + public void addTree(Tree t) { super.add(t); } } static Forest singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); } @@ -120,6 +122,13 @@ public abstract class Forest /*extends PrintableTree>*/ impl } public abstract void visit(Invokable,B,C> ivbc, B b, C c); + private static class TreeMaker2 extends TreeMaker { + private TreeConsumer tc; + public TreeMaker2(boolean toss, TreeConsumer tc) { super(toss); this.tc = tc; } + public void finish(T head, Input.Location loc) { tc.addTree(new Tree(loc, head, toks.toArray(tree_hint)));; } + public void start(T head, Input.Location loc) { } + public void child(Tree t) { toks.add(t); } + } private static abstract class TreeMaker implements Invokable,Boolean,Integer> { public ArrayList> toks = new ArrayList>(); private boolean toss;