86a2ffa44fe0fe774da569e487c15b7461c8944a
[sbp.git] / src / edu / berkeley / sbp / Tree.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.util.*;
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     final T           head;
16           Tree<T>[]   children;
17     final Input.Location    location;
18
19     public T                 head()        { return head; }
20     public int               numChildren() { return children.length; }
21     public Iterable<Tree<T>> children()    { return new ArrayIterator(children); }
22     public Iterator<Tree<T>> iterator()    { return new ArrayIterator(children); }
23     public Tree<T>           child(int i)  { return children[i]; }
24
25     public Input.Location    getLocation() { return location; }
26
27     public Tree(Input.Location loc, T head)                   { this(loc, head, null); }
28     public Tree(Input.Location loc, T head, Tree<T>[] children) {
29         this.location = loc;
30         this.head = head;
31
32         Tree<T>[] children2 = children==null ? new Tree[0] : new Tree[children.length];
33         if (children != null) System.arraycopy(children, 0, children2, 0, children.length);
34         this.children = children2;
35     }
36
37     protected String headToString() { return head==null?null:head.toString(); }
38     protected String headToJava()   {
39         if (head==null) return null;
40         if (head instanceof ToJava) {
41             StringBuffer sb = new StringBuffer();
42             ((ToJava)head).toJava(sb);
43             return sb.toString();
44         }
45         return head==null?"null":("\""+StringUtil.toJavaString(head.toString())+"\"");
46     }
47     protected String left()   { return "{"; }
48     protected String right()  { return "}"; }
49     protected boolean ignoreSingleton() { return false; }
50
51     public GraphViz.Node toGraphViz(GraphViz gv) {
52         if (gv.hasNode(this)) return gv.createNode(this);
53         GraphViz.Node n = gv.createNode(this);
54         n.label = head()==null ? "" : head().toString();
55         //n.color = "red";
56         for(Tree t : this) n.edge(t, null);
57         return n;
58     }
59     public boolean isTransparent() { return false; }
60     public boolean isHidden() { return false; }
61
62     public static interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> {
63     }
64
65     public static class ArrayBuildingTreeFunctor<T> implements TreeFunctor<T,T[]>, ToJava {
66
67         public void toJava(StringBuffer sb) { sb.append("new Tree.ArrayBuildingTreeFunctor()"); }
68         public String toString() { return ""; }
69         
70         public T[] invoke(Iterable<Tree<T>> t) {
71             ArrayList ret = new ArrayList();
72             for(Tree tc : t) {
73                 if (tc.head() != null && tc.head() instanceof Functor)
74                     ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc.children()));
75                 else if (tc.numChildren() == 0)
76                     ret.add(tc.head());
77                 else {
78                     System.err.println("FIXME: don't know what to do about " + tc);
79                     ret.add(null);
80                 }
81             }
82             return (T[])ret.toArray(new Object[0]);
83         }
84     }
85
86 }