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