checkpoint
authoradam <adam@megacz.com>
Sat, 15 Jul 2006 06:04:29 +0000 (02:04 -0400)
committeradam <adam@megacz.com>
Sat, 15 Jul 2006 06:04:29 +0000 (02:04 -0400)
darcs-hash:20060715060429-5007d-ed9c121c3508c433b6d7be8f2c748b98d3d90b32.gz

src/edu/berkeley/sbp/Forest.java
src/edu/berkeley/sbp/Tree.java
src/edu/berkeley/sbp/util/ArrayIterator.java
src/edu/berkeley/sbp/util/ConcatenateIterator.java [new file with mode: 0644]

index e7c0be4..afe0a85 100644 (file)
@@ -90,6 +90,11 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
     public boolean ambiguous() { return false; }
     public /*protected*/ static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
 
+        private final Input.Region      location;
+        private final T                 tag;
+        private final Forest<T>[]       tokens;
+        private final boolean           unwrap;
+
         public boolean isTransparent() { return false; }
         public boolean isHidden() { return false; }
         public GraphViz.Node toGraphViz(GraphViz gv) {
@@ -118,11 +123,6 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
             ivbc.invoke(this, b, c);
         }
 
-        private final Input.Region      location;
-        private final T                 tag;
-        private final Forest<T>[]       tokens;
-        private final boolean           unwrap;
-
         private MyBody(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap) {
             this.location = loc;
             this.tag = tag;
index da0c56c..c525ffa 100644 (file)
@@ -12,38 +12,57 @@ public class Tree<T>
     implements Iterable<Tree<T>>,
                GraphViz.ToGraphViz {
 
-    final T           head;
-          Tree<T>[]   children;
-    final Input.Region    location;
+    private final T            head;
+    private final Tree<T>[]    children;
+    private final Input.Region location;
+    private final boolean      lift;
 
     public T                 head()        { return head; }
-    public int               numChildren() { return children.length; }
-    public Iterable<Tree<T>> children()    { return new ArrayIterator(children); }
-    public Iterator<Tree<T>> iterator()    { return new ArrayIterator(children); }
-    public Tree<T>           child(int i)  { return children[i]; }
+
+    private Tree<T> lifted() { return children[children.length-1]; }
+
+    public int               numChildren() {
+        if (lift && children.length > 0)
+            return (children.length-1)+lifted().numChildren();
+        return children.length;
+    }
+    public Iterable<Tree<T>> children()    { return this; }
+    public Iterator<Tree<T>> iterator()    {
+        if (lift)
+            return new ConcatenateIterator(new ArrayIterator(children, 0, children.length-1),
+                                           children[children.length-1].iterator());
+        return new ArrayIterator(children);
+    }
+    public Tree<T>           child(int i)  {
+        if (lift && i >= children.length-1)
+            return children[children.length-1].child(i-(children.length-1));
+        return children[i];
+    }
 
     public Input.Region    getRegion() { return location; }
 
     public Tree(Input.Region loc, T head)                   { this(loc, head, null); }
-    public Tree(Input.Region loc, T head, Tree<T>[] children) {
+    public Tree(Input.Region loc, T head, Tree<T>[] children) { this(loc, head, children, false); }
+    public Tree(Input.Region loc, T head, Tree<T>[] children, boolean lift) {
         this.location = loc;
         this.head = head;
+        this.lift = lift;
 
         Tree<T>[] children2 = children==null ? new Tree[0] : new Tree[children.length];
         if (children != null) System.arraycopy(children, 0, children2, 0, children.length);
         this.children = children2;
     }
 
-    protected String headToString() { return head==null?null:head.toString(); }
+    protected String headToString() { return head()==null?null:head().toString(); }
     protected String headToJava()   {
       // FIXME
-        if (head==null) return null;
-        if (head instanceof ToJava) {
+        if (head()==null) return null;
+        if (head() instanceof ToJava) {
             StringBuffer sb = new StringBuffer();
-            ((ToJava)head).toJava(sb);
+            ((ToJava)head()).toJava(sb);
             return sb.toString();
         }
-        return (head==null?"null":("\""+StringUtil.toJavaString(head.toString())+"\""));
+        return (head()==null?"null":("\""+StringUtil.toJavaString(head().toString())+"\""));
     }
     protected String left()   { return "{"; }
     protected String right()  { return "}"; }
index 61bbcfb..df541e5 100644 (file)
@@ -4,12 +4,20 @@ import java.util.*;
 public final class ArrayIterator<T> implements Iterator<T>, Iterable<T> {
 
     private final T[] array;
-    private       int i     = 0;
+    private int start;
+    private int len;
+    private int i = 0;
 
-    public ArrayIterator(T[] array) { this.array = array; }
+    public ArrayIterator(T[] array) { this(array, 0, array.length); }
+    public ArrayIterator(T[] array, int start, int len) {
+        this.start = start;
+        this.len = len;
+        this.array = array;
+        this.i = start;
+    }
 
     public void    remove()       { throw new Error(); }
-    public boolean hasNext()      { return i<array.length; }
+    public boolean hasNext()      { return i<start+len; }
     public T       next()         { return array[i++]; }
     public Iterator<T> iterator() { return this; }
 }
diff --git a/src/edu/berkeley/sbp/util/ConcatenateIterator.java b/src/edu/berkeley/sbp/util/ConcatenateIterator.java
new file mode 100644 (file)
index 0000000..d936189
--- /dev/null
@@ -0,0 +1,18 @@
+package edu.berkeley.sbp.util;
+import java.util.*;
+
+public final class ConcatenateIterator<T> implements Iterator<T>, Iterable<T> {
+
+    private Iterator<T> i1;
+    private Iterator<T> i2;
+
+    public ConcatenateIterator(Iterator<T> i1, Iterator<T> i2) {
+        this.i1 = i1;
+        this.i2 = i2;
+    }
+
+    public void    remove()       { throw new Error(); }
+    public boolean hasNext()      { return i1.hasNext() || i2.hasNext(); }
+    public T       next()         { return i1.hasNext() ? i1.next() : i2.next(); }
+    public Iterator<T> iterator() { return this; }
+}