unrolling forests without recursion
[sbp.git] / src / edu / berkeley / sbp / Atom.java
index 1b9c254..6c7a25e 100644 (file)
@@ -8,38 +8,37 @@ import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.*;
 
 /** an element which matches exactly one input token */
-public abstract class Atom<T extends Token> extends Element implements Topology<T> {
+public abstract class Atom<T> extends Element implements Topology<T> {
 
-    private final Topology<T> rt;
-
-    public Atom(Topology<T> rt) { this.rt = rt; }
-
-    void reachable(HashSet<Sequence.Position> h) { /* do-nothing */ }
-
-    /** equality is based on the underlying <tt>Topology</tt> */
-    public int hashCode() { return rt.hashCode(); }
-    
-    /** equality is based on the underlying <tt>Topology</tt> */
-    public boolean equals(Object o) { return o != null && o instanceof Atom && ((Atom)o).rt.equals(rt); }
-
-    /** declared abstract to force subclasses to override it in a useful manner */
-    public abstract String toString();
+    protected abstract Topology<T> top();
+    public    abstract String       toString();
+    public             StringBuffer toString(StringBuffer sb) { sb.append(this); return sb; }
 
     // Topology Thunks //////////////////////////////////////////////////////////////////////////////
 
-    public void              add(Topology<T> t)         { rt.add(t); }
-    public void              add(T t)                   { rt.add(t); }
-    public void              remove(Topology<T> t)      { rt.remove(t); }
-    public void              remove(T t)                { rt.remove(t); }
-    public Topology<T>       dup()                      { return rt.dup(); }
-    public boolean           contains(T v)              { return rt.contains(v); }
-    public Topology<T>       fresh()                    { return rt.fresh(); }
-    public Topology<T>       intersect(Topology<T> t)   { return rt.intersect(t); }
-    public Topology<T>       minus(Topology<T> t)       { return rt.minus(t); }
-    public Topology<T>       union(Topology<T> t)       { return rt.union(t); }
-    public Topology<T>       complement()               { return rt.complement(); }
-    public boolean           disjoint(Topology<T> t)    { return rt.disjoint(t); }
-    public boolean           containsAll(Topology<T> t) { return rt.containsAll(t); }
-    
+    public Topology<T>       unwrap()                   { return top().unwrap(); }
+    public Topology<T>       empty()                    { return top().empty(); }
+    public boolean           contains(T v)              { return top().contains(v); }
+    public Topology<T>       intersect(Topology<T> t)   { return top().intersect(t); }
+    public Topology<T>       minus(Topology<T> t)       { return top().minus(t); }
+    public Topology<T>       union(Topology<T> t)       { return top().union(t); }
+    public Topology<T>       complement()               { return top().complement(); }
+    public boolean           disjoint(Topology<T> t)    { return top().disjoint(t); }
+    public boolean           containsAll(Topology<T> t) { return top().containsAll(t); }
+    public int               hashCode()                 { return top().hashCode(); }
+    public boolean           equals(Object o)           { return o != null && o instanceof Atom && ((Atom)o).top().equals(top()); }
+
+    /** if all expressions matching <tt>e</tt> are exactly one token
+     *  long, <b>attempt to</b> return an Atom representing that token
+     *  (undecidable in general; only works in trivial cases)
+     */
+    public static Topology toAtom(Element e) {
+        if (e instanceof Atom) return (Atom)e;
+        if (e instanceof Sequence) return ((Sequence)e).toAtom();
+        Topology ret = null;
+        for(Sequence s : (Union)e)
+            ret = ret==null ? toAtom(s) : ret.union(s.toAtom());
+        return ret;
+    }
 }