+public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/
+ implements Visitable<Forest.Body<T>>,
+ IntegerMappable,
+ GraphViz.ToGraphViz {
+
+ private static int master_idx = 0;
+ private final int idx = master_idx++;
+ public int toInt() { return idx; }
+
+ public abstract void expand(TaskList tl, HashSet<Tree<T>> ht);
+ public abstract void gather(TaskList tl, HashSet<Tree<T>>[] ht, HashSet<Tree<T>> target);
+
+ public static class TaskList extends ArrayList<TaskList.Task> {
+ public interface Task {
+ public void perform();
+ }
+ public class ExpandTask implements Task {
+ private Forest f;
+ private HashSet hs;
+ public ExpandTask(Forest f, HashSet hs) { this.f = f; this.hs = hs; }
+ public void perform() { f.expand(TaskList.this, hs); }
+ }
+ public class GatherTask implements Task {
+ private Forest f;
+ private HashSet[] ht;
+ private HashSet hs;
+ public GatherTask(Forest f, HashSet<Tree<?>>[] ht, HashSet<Tree<?>> hs) { this.f = f; this.hs = hs; this.ht = ht;}
+ public void perform() { f.gather(TaskList.this, ht, hs); }
+ }
+ public void expand(Forest f, HashSet hs) {
+ add(new ExpandTask(f, hs));
+ }
+ public void gather(Forest f, HashSet[] ht, HashSet hs) {
+ add(new GatherTask(f, ht, hs));
+ }
+ public void run() {
+ while(true) {
+ if (isEmpty()) return;
+ Task task = get(size()-1);
+ remove(size()-1);
+ task.perform();
+ }
+ }
+ }