X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;fp=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=eff94a47ecd7233f216a1adcfa7d3f10300a82f0;hp=79854c64b6b106ad2d4e972781a575ee552192ba;hb=93b9f1a57460257f71a4cef17419a723e294550d;hpb=d27d65cb994c6b9ae57a39f4bec99dd75c6bd6f7 diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 79854c6..eff94a4 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -115,8 +115,8 @@ public abstract class Sequence implements Iterable, SequenceOrElement { Iterable needs() { return needs; } Iterable hates() { return hates; } - Position firstp() { return firstp; } - Position lastp() { return firstp().last(); } + Pos firstp() { return firstp; } + Pos lastp() { return firstp().last(); } public Iterator iterator() { return new ArrayIterator(elements); } protected Sequence(Element[] elements) { @@ -124,18 +124,23 @@ public abstract class Sequence implements Iterable, SequenceOrElement { for(int i=0; i Forest postReduce(Input.Region loc, Forest[] args, Position p); + protected abstract Forest postReduce(Input.Region loc, Forest[] args, Pos p); // Position ////////////////////////////////////////////////////////////////////////////// static abstract class Pos implements IntegerMappable, Comparable, 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, 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 Forest 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, 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, 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 this.element()) */ public Position next() { return next; } @@ -217,7 +233,7 @@ public abstract class Sequence implements Iterable, 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());