checkpoint
[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.Region    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.Region    getRegion() { return location; }
26
27     public Tree(Input.Region loc, T head)                   { this(loc, head, null); }
28     public Tree(Input.Region 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       // FIXME
40         if (head==null) return null;
41         if (head instanceof ToJava) {
42             StringBuffer sb = new StringBuffer();
43             ((ToJava)head).toJava(sb);
44             return sb.toString();
45         }
46         return (head==null?"null":("\""+StringUtil.toJavaString(head.toString())+"\""));
47     }
48     protected String left()   { return "{"; }
49     protected String right()  { return "}"; }
50     protected boolean ignoreSingleton() { return false; }
51
52     public GraphViz.Node toGraphViz(GraphViz gv) {
53         if (gv.hasNode(this)) return gv.createNode(this);
54         GraphViz.Node n = gv.createNode(this);
55         n.label = head()==null ? "" : head().toString();
56         //n.color = "red";
57         for(Tree t : this) n.edge(t, null);
58         return n;
59     }
60     public boolean isTransparent() { return false; }
61     public boolean isHidden() { return false; }
62
63     public static interface TreeFunctor<T,R> extends Functor<Iterable<Tree<T>>, R> {
64     }
65
66     public static class ArrayBuildingTreeFunctor<T> implements TreeFunctor<T,T[]>, ToJava {
67
68         public void toJava(StringBuffer sb) { sb.append("new Tree.ArrayBuildingTreeFunctor()"); }
69         public String toString() { return ""; }
70         
71         public T[] invoke(Iterable<Tree<T>> t) {
72             ArrayList ret = new ArrayList();
73             for(Tree tc : t) {
74                 if (tc.head() != null && tc.head() instanceof Functor)
75                     ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc.children()));
76                 else if (tc.numChildren() == 0)
77                     ret.add(tc.head());
78                 else {
79                     System.err.println("FIXME: don't know what to do about " + tc);
80                     ret.add(null);
81                 }
82             }
83             return (T[])ret.toArray(new Object[0]);
84         }
85     }
86
87 }