summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/data/TopDownTransformIterator.java
blob: 388626423ea914ce5c5cb2e9c93f4c093cb1b34b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package bjc.utils.data;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<ContainedType>> {
	private Function<ContainedType, TopDownTransformResult> picker;
	private BiFunction<ITree<ContainedType>, 
			Consumer<Iterator<ITree<ContainedType>>>,
			ITree<ContainedType>>                           transform;

	private ITree<ContainedType> preParent;
	private ITree<ContainedType> postParent;
	
	private Deque<ITree<ContainedType>> preChildren;
	private Deque<ITree<ContainedType>> postChildren;

	private TopDownTransformIterator<ContainedType> curChild;

	private boolean done;
	private boolean initial;

	private Deque<Iterator<ITree<ContainedType>>> toYield;
	private Iterator<ITree<ContainedType>>        curYield;

	public TopDownTransformIterator(Function<ContainedType, TopDownTransformResult>                          pickr,
			BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> transfrm,
			ITree<ContainedType> tree) {
		preParent = tree;

		preChildren  = new LinkedList<>();
		postChildren = new LinkedList<>();
		toYield      = new LinkedList<>();

		picker      = pickr;
		transform = transfrm;

		done    = false;
		initial = true;
	}

	public void addYield(Iterator<ITree<ContainedType>> src) {
		if(curYield != null) {
			toYield.push(curYield);
		}
		
		curYield = src;
	}

	public boolean hasNext() {
		return !done;
	}

	public ITree<ContainedType> next() {
		if(done) throw new NoSuchElementException();

		if(initial) {
			TopDownTransformResult res = picker.apply(preParent.getHead());

			switch(res) {
				case PASSTHROUGH:
					postParent = new Tree<>(preParent.getHead());

					if(preParent.getChildrenCount() != 0) {
						for(int i = 0; i < preParent.getChildrenCount(); i++) {
							preChildren.add(preParent.getChild(i));
						}
						
						// Return whatever the first child is
						break;
					} else {
						done = true;
						return postParent;
					}
				case SKIP:
					done = true;
					return preParent;
				case TRANSFORM:
					done = true;
					return transform.apply(preParent, this::addYield);
				case PUSHDOWN:
					if(preParent.getChildrenCount() != 0) {
						for(int i = 0; i < preParent.getChildrenCount(); i++) {
							preChildren.add(preParent.getChild(i));
						}
						
						// Return whatever the first child is
						break;
					} else {
						done = true;
						return transform.apply(new Tree<>(preParent.getHead()), this::addYield);
					}
				case PULLUP:
					ITree<ContainedType> intRes = transform.apply(preParent, this::addYield);
					
					postParent = new Tree<>(intRes.getHead());

					if(intRes.getChildrenCount() != 0) {
						for(int i = 0; i < intRes.getChildrenCount(); i++) {
							preChildren.add(intRes.getChild(i));
						}
						
						// Return whatever the first child is
						break;
					} else {
						done = true;
						return postParent;
					}
				default:
					throw new IllegalArgumentException("Unknown result type " + res);
			}

			initial = false;
		}

		if(curYield != null) {
			if(curYield.hasNext()) {
				return curYield.next();
			} else {
				while(toYield.size() != 0 && !curYield.hasNext()) {
					curYield = toYield.pop();
				}

				if(toYield.size() == 0 && !curYield.hasNext()) {
					curYield = null;
				}
			}
		}

		if(curChild == null || !curChild.hasNext()) {
			if(preChildren.size() != 0) {
				curChild = new TopDownTransformIterator<>(picker, transform, preChildren.pop());
				
				ITree<ContainedType> res = curChild.next();
				postChildren.add(res);

				return res;
			} else {
				ITree<ContainedType> res = null;

				if(postParent == null) {
					res = new Tree<>(preParent.getHead());

					for(ITree<ContainedType> child : postChildren) {
						res.addChild(child);
					}

					res = transform.apply(res, this::addYield);
				} else {
					res = postParent;

					for(ITree<ContainedType> child : postChildren) {
						res.addChild(child);
					}
				}

				done = true;
				return res;
			}
		} else {
			ITree<ContainedType> res = curChild.next();
			postChildren.add(res);

			return res;
		}
	}
}