1b9ddfa668f8bcee675892b4c216cc7408456fac
[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         this.children = ArrayUtil.clone(children, Tree.class);
52     }
53
54
55     // PrintableTree /////////////////////////////////////////////////////////////////////////////
56
57     protected String headToString() { return head()==null?null:head().toString(); }
58     protected String headToJava()   {
59       // FIXME
60         if (head()==null) return null;
61         if (head() instanceof ToJava) {
62             StringBuffer sb = new StringBuffer();
63             ((ToJava)head()).toJava(sb);
64             return sb.toString();
65         }
66         return (head()==null?"null":("\""+StringUtil.toJavaString(head().toString())+"\""));
67     }
68     protected String left()   { return "{"; }
69     protected String right()  { return "}"; }
70     protected boolean ignoreSingleton() { return false; }
71
72
73     // ToGraphViz /////////////////////////////////////////////////////////////////////////////
74
75     public GraphViz.Node toGraphViz(GraphViz gv) {
76         if (gv.hasNode(this)) return gv.createNode(this);
77         GraphViz.Node n = gv.createNode(this);
78         n.label = head()==null ? "" : head().toString();
79         //n.color = "red";
80         for(Tree t : this) n.edge(t, null);
81         return n;
82     }
83     public boolean isTransparent() { return false; }
84     public boolean isHidden() { return false; }
85
86
87     // TreeFunctor /////////////////////////////////////////////////////////////////////////////
88
89     public static interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> { }
90     
91 }