use Sequence.Pos rather than Sequence.Position wherever possible
[sbp.git] / src / edu / berkeley / sbp / Sequence.java
index 79854c6..eff94a4 100644 (file)
@@ -115,8 +115,8 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
     Iterable<Sequence> needs() { return needs; }
     Iterable<Sequence> hates() { return hates; }
 
-    Position firstp() { return firstp; }
-    Position lastp() { return firstp().last(); }
+    Pos firstp() { return firstp; }
+    Pos lastp() { return firstp().last(); }
 
     public Iterator<Element> iterator()    { return new ArrayIterator<Element>(elements); }
     protected Sequence(Element[] elements) {
@@ -124,18 +124,23 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
         for(int i=0; i<elements.length; i++)
             if (elements[i]==null)
                 throw new RuntimeException("cannot have nulls in a sequence: " + this);
-        this.firstp = new Position(0, null);
+        this.firstp = new Position(this, 0, null);
     }
 
     abstract Forest epsilonForm(Input.Region loc, Grammar cache);
 
-    protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Position p);
+    protected abstract <T> Forest<T> postReduce(Input.Region loc, Forest<T>[] args, Pos p);
 
 
     // Position //////////////////////////////////////////////////////////////////////////////
 
     static abstract class Pos implements IntegerMappable, Comparable<Pos>, Serializable {
+
+        public int ord = -1;
+        public int ord()     { return ord; }
+
         final Forest[] holder;
+
         Pos(int len) { this.holder = new Forest[len]; }
 
         public abstract int   provides();
@@ -143,12 +148,21 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
         public abstract int[] hates();
         public abstract boolean            owner_needed_or_hated();
 
+        public abstract boolean isFirst();
+        public abstract boolean isLast();
+        public abstract Pos last();
+        public abstract Pos prev();
+        public abstract Pos next();
+
+        abstract Sequence owner();
+        abstract Element  element();
+
         public abstract int numPops();
         public abstract <T> Forest<T> rewrite(Input.Region loc, Grammar cache);
     }
 
     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
-    class Position extends Pos implements IntegerMappable {
+    private static class Position extends Pos implements IntegerMappable {
         /*
         public Pos getPos() {
             return new DumbPos(elements.length, provides(), needs(), hates(), owner_needed_or_hated(), numPops(), 
@@ -165,19 +179,21 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
         public int ord()     { return ord; }
         public int numPops() { return pos; }
 
-                final int      pos;
-        private final Position next;
-        private final Position prev;
+                final     int      pos;
+        private final     Position next;
+        private final     Position prev;
+        private transient Sequence owner;
 
         public int     provides() { return owner().sernum; }
         public int[]   needs() { return owner().needs_int(); }
         public int[]   hates() { return owner().hates_int(); }
         public boolean owner_needed_or_hated() { return owner().needed_or_hated; }
         
-        private Position(int pos, Position prev) {
-            super(elements.length);
+        private Position(Sequence owner, int pos, Position prev) {
+            super(owner.elements.length);
+            this.owner    = owner;
             this.pos      = pos;
-            this.next     = pos==elements.length ? null : new Position(pos+1, this);
+            this.next     = pos==owner.elements.length ? null : new Position(owner, pos+1, this);
             this.prev     = prev;
         }
 
@@ -185,14 +201,14 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
             return ord - ((Position)p).ord;
         }
 
-        boolean isFirst() { return pos==0; }
+        public boolean isFirst() { return pos==0; }
         public int pos() { return pos; }
 
         /** the element immediately after this Position, or null if this is the last Position */
-        public Element  element() { return pos>=elements.length ? null : elements[pos]; }
+        public Element  element() { return pos>=owner().elements.length ? null : owner().elements[pos]; }
 
         /** the element which produces the sequence to which this Position belongs */
-        public Sequence owner() { return Sequence.this; }
+        public Sequence owner() { return owner; }
 
         /** the next Position (the Position after <tt>this.element()</tt>) */
         public Position next() { return next; }
@@ -217,7 +233,7 @@ public abstract class Sequence implements Iterable<Element>, SequenceOrElement {
         public String   toString() {
             StringBuffer ret = new StringBuffer();
             ret.append("<{");
-            for(Position p = Sequence.this.firstp(); p != null; p = p.next()) {
+            for(Position p = (Position)owner().firstp(); p != null; p = p.next()) {
                 ret.append(' ');
                 if (p==this) ret.append(" | ");
                 if (p.element()!=null) ret.append(p.element());