X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FForest.java;h=dbcd3c06036df2727d53026031a1367beddb4380;hp=e675f635ff03468bece3501935cd1cfa24c409df;hb=e14f41fe193ceef8dfd8b4bc2f327e3550231a4c;hpb=526da96dd06e152d194ec92c9ef9df6085a1251b diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index e675f63..dbcd3c0 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -18,13 +18,12 @@ public abstract class Forest { /** expand this forest into a set of trees */ public abstract HashSet> expand(boolean toss); - public abstract boolean empty(); - static Forest singleton(Token.Location loc, Sequence creator) { return create(loc, null, new Forest[] { }, creator, false, true); } - static Forest singleton(Token.Location loc, Forest body, Sequence creator) { return create(loc, null, new Forest[] { body }, creator, false, true); } - static Forest leaf(Token.Location loc, T tag, Sequence creator) { return create(loc, tag, null, creator, false, false); } - public static Forest create(Token.Location loc, T tag, Forest[] tokens, Sequence creator, boolean unwrap, boolean singleton) { - return new MultiForest(loc, tag, tokens, creator, unwrap, singleton); + static Forest singleton(Token.Location loc) { return create(loc, null, new Forest[] { }, false, true); } + static Forest singleton(Token.Location loc, Forest body) { return create(loc, null, new Forest[] { body }, false, true); } + static Forest leaf(Token.Location loc, T tag) { return create(loc, tag, null, false, false); } + public static Forest create(Token.Location loc, T tag, Forest[] tokens, boolean unwrap, boolean singleton) { + return new MultiForest(loc, tag, tokens, unwrap, singleton); } // Body ////////////////////////////////////////////////////////////////////////////// @@ -34,17 +33,15 @@ public abstract class Forest { private final Token.Location location; private final T tag; private final Forest[] tokens; - private final Sequence creator; private final boolean unwrap; private final boolean singleton; - private Body(Token.Location loc, T tag, Forest[] tokens, Sequence creator, boolean unwrap, boolean singleton) { + private Body(Token.Location loc, T tag, Forest[] tokens, boolean unwrap, boolean singleton) { this.location = loc; this.tag = tag; this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length]; if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length); if (tokens != null) for(int i=0; i { } return h; } - void addTo(HashSet h) { - if (!singleton) h.add(this); - else for(Body b : (IterableForest)tokens[0]) b.addTo(h); - } + void addTo(FastSet h) { if (!singleton) h.add(this, true); else for(Body b : (IterableForest)tokens[0]) b.addTo(h); } - - private boolean rep = false; public String toString() { - if (rep) return "***"; - try { - rep = true; - StringBuffer ret = new StringBuffer(); - for(int i=0; i 0) { - ret.append(q); - ret.append(" "); - } + StringBuffer ret = new StringBuffer(); + for(int i=0; i 0) { + ret.append(q); + ret.append(" "); } - String tail = ret.toString().trim(); - String head = (tag!=null && !tag.toString().equals("")) ? (tail.length() > 0 ? tag+":" : tag+"") : ""; - if (tail.length() > 0) tail = "{" + tail + "}"; - return head + tail; - } finally { - rep = false; } + String tail = ret.toString().trim(); + String head = (tag!=null && !tag.toString().equals("")) ? (tail.length() > 0 ? tag+":" : tag+"") : ""; + if (tail.length() > 0) tail = "{" + tail + "}"; + return head + tail; } } // Ref ////////////////////////////////////////////////////////////////////////////// - static abstract class IterableForest extends Forest implements Iterable> { + private static abstract class IterableForest extends Forest implements Iterable> { public abstract Iterator> iterator(); } @@ -120,11 +106,6 @@ public abstract class Forest { * viewed, it becomes immutable */ static class Ref extends IterableForest { - public boolean empty() { - if (res!=null) return res.empty(); - for(Forest f : hp) if (!f.empty()) return false; - return true; - } private FastSet hp = new FastSet(); private Forest res = null; public Ref() { } @@ -151,11 +132,10 @@ public abstract class Forest { // Implementations ////////////////////////////////////////////////////////////////////////////// private static class MultiForest extends IterableForest { - public boolean empty() { return results.size()>0; } private final FastSet> results; private MultiForest(FastSet> results) { this.results = results; } - public MultiForest(Token.Location loc, T tag, Forest[] tokens, Sequence creator, boolean unwrap, boolean singleton) { - this.results = new FastSet>(new Body(loc, tag, tokens, creator, unwrap, singleton)); + public MultiForest(Token.Location loc, T tag, Forest[] tokens, boolean unwrap, boolean singleton) { + this.results = new FastSet>(new Body(loc, tag, tokens, unwrap, singleton)); } public Iterator> iterator() { return results.iterator(); } @@ -193,6 +173,5 @@ public abstract class Forest { // Statics ////////////////////////////////////////////////////////////////////////////// private static Tree[] tree_hint = new Tree[0]; - private static Body[] body_hint = new Body[0]; private static final Forest[] emptyForestArray = new Forest[0]; }