refactored Topology to make it a value (immutable) class
[sbp.git] / src / edu / berkeley / sbp / Walk.java
index 8b6eae6..4f133a4 100644 (file)
@@ -52,7 +52,7 @@ abstract class Walk<T> {
         public HashSet<Element> bottom(Element e)     { return acc; }
         public HashSet<Element> sequence(Sequence seq) { return bottom(seq); }
         public HashSet<Element> walkAtom(Atom r) {
-            c.atoms.put(e, c.atoms.get(e)==null ? r.dup() : c.atoms.get(e).union(r.dup()));
+            c.atoms.put(e, c.atoms.get(e)==null ? r : c.atoms.get(e).union(r));
             return super.walkAtom(r);
         }
     }
@@ -83,7 +83,7 @@ abstract class Walk<T> {
         public WalkTokenSet(Topology<Tok> cs)          { this.cs = cs; }
         public WalkTokenSet(Topology<Tok> cs, Cache c) { super(c); this.cs = cs; }
         public Topology<Tok> bottom(Element e)         { return cs; }
-        public Topology<Tok> walkAtom(Atom r)          { cs.add(r.dup()); return cs; }
+        public Topology<Tok> walkAtom(Atom r)          { cs = cs.union(r); return cs; }
     }
 
     class First<Tok extends Token> extends WalkTokenSet<Tok> {
@@ -127,7 +127,7 @@ abstract class Walk<T> {
             if (c != null) {
                 Topology<Tok> cached = (Topology<Tok>)c.follow.get(e);
                 if (cached != null) {
-                    cs.add(cached);
+                    cs = cs.union(cached);
                     eof |= c.eof.get(e);
                     return cs;
                 }
@@ -136,7 +136,7 @@ abstract class Walk<T> {
             Topology<Tok> cso = cs;
             boolean eofo = eof;
             eof = false;
-            cs = cso.fresh();
+            cs = cso.empty();
 
             if (e instanceof Parser.Table.Top) eof = true;
             for(Element x : all) {
@@ -146,13 +146,13 @@ abstract class Walk<T> {
                 Sequence a = (Sequence)x;
                 Position mp = null;
                 for(Position pos = a.firstp(); pos != null && !pos.isLast(); pos = pos.next()) {
-                    if (matched) cs.add(new First<Tok>(cs.fresh(), c).walk(pos.element()));
+                    if (matched) cs = cs.union(new First<Tok>(cs.empty(), c).walk(pos.element()));
                     if (pos.isLast()) { matched = (matched && pos.element().possiblyEpsilon(c)); continue; }
                     boolean good = false;
                     if (e instanceof Atom) {
                         Topology top = c.atoms.get(pos.element());
                         if (top==null) continue;
-                        if (!(top.containsAll(((Atom)e).dup()))) continue;
+                        if (!(top.containsAll(((Atom)e)))) continue;
                     } else {
                         if (c.ys.get(pos.element()).contains(e)) good = true;
                     }
@@ -166,15 +166,15 @@ abstract class Walk<T> {
 
             if (e instanceof Sequence) {
                 Sequence s = (Sequence)e;
-                if (s.noFollow() != null) cs.remove(s.noFollow().dup());
+                if (s.noFollow() != null) cs = cs.minus(s.noFollow());
             }
 
             if (c != null && e==me) {
-                c.follow.put(e, cs.dup());
+                c.follow.put(e, cs);
                 c.eof.put(e, eof);
             }
 
-            cso.add(cs);
+            cso = cso.union(cs);
             cs = cso;
             eofo |= eof;
             eof = eofo;