7dbda47e2c258cf6749aecf81d2962aa157664f6
[sbp.git] / src / edu / berkeley / sbp / Tree.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.util.*;
4 import edu.berkeley.sbp.bind.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /** a tree (or node in a tree); see jargon.txt for details */
10 public class Tree<T>
11     extends PrintableTree<Tree<T>>
12     implements Iterable<Tree<T>>,
13                GraphViz.ToGraphViz {
14
15     private final Input.Region location;
16     private final T            head;
17     private final Tree<T>[]    children;
18     private final boolean      lift;
19
20     public T                 head()        { return head; }
21
22     private Tree<T> lifted() { return children[children.length-1]; }
23
24     public int               numChildren() {
25         return lift
26             ? (children.length-1)+lifted().numChildren()
27             : children.length;
28     }
29     public Iterable<Tree<T>> children()    { return this; }
30     public Iterator<Tree<T>> iterator()    {
31         return lift
32             ? new ConcatenateIterator(new ArrayIterator(children, 0, children.length-1),
33                                       children[children.length-1].iterator())
34             : new ArrayIterator(children);
35     }
36
37     public Tree<T> child(int i)  {
38         return lift && i >= children.length-1
39             ? children[children.length-1].child(i-(children.length-1))
40             : children[i];
41     }
42
43     public Input.Region    getRegion() { return location; }
44
45     public Tree(Input.Region loc, T head)                     { this(loc, head, null); }
46     public Tree(Input.Region loc, T head, Tree<T>[] children) { this(loc, head, children, false); }
47     public Tree(Input.Region loc, T head, Tree<T>[] children, boolean lift) {
48         this.location = loc;
49         this.head = head;
50         this.lift = lift && children != null && children.length > 0;
51
52         Tree<T>[] children2 = children==null ? new Tree[0] : new Tree[children.length];
53         if (children != null) System.arraycopy(children, 0, children2, 0, children.length);
54         this.children = children2;
55     }
56
57
58     // PrintableTree /////////////////////////////////////////////////////////////////////////////
59
60     protected String headToString() { return head()==null?null:head().toString(); }
61     protected String headToJava()   {
62       // FIXME
63         if (head()==null) return null;
64         if (head() instanceof ToJava) {
65             StringBuffer sb = new StringBuffer();
66             ((ToJava)head()).toJava(sb);
67             return sb.toString();
68         }
69         return (head()==null?"null":("\""+StringUtil.toJavaString(head().toString())+"\""));
70     }
71     protected String left()   { return "{"; }
72     protected String right()  { return "}"; }
73     protected boolean ignoreSingleton() { return false; }
74
75
76     // ToGraphViz /////////////////////////////////////////////////////////////////////////////
77
78     public GraphViz.Node toGraphViz(GraphViz gv) {
79         if (gv.hasNode(this)) return gv.createNode(this);
80         GraphViz.Node n = gv.createNode(this);
81         n.label = head()==null ? "" : head().toString();
82         //n.color = "red";
83         for(Tree t : this) n.edge(t, null);
84         return n;
85     }
86     public boolean isTransparent() { return false; }
87     public boolean isHidden() { return false; }
88
89     public static interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> { }
90     
91 }