|
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 |
| package edu.rice.cs.plt.recur; |
|
36 |
| |
|
37 |
| import java.util.LinkedList; |
|
38 |
| import edu.rice.cs.plt.collect.Multiset; |
|
39 |
| import edu.rice.cs.plt.collect.HashMultiset; |
|
40 |
| import edu.rice.cs.plt.tuple.Quad; |
|
41 |
| import edu.rice.cs.plt.tuple.IdentityQuad; |
|
42 |
| import edu.rice.cs.plt.lambda.Runnable4; |
|
43 |
| import edu.rice.cs.plt.lambda.Thunk; |
|
44 |
| import edu.rice.cs.plt.lambda.Lambda4; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| public class RecursionStack4<T1, T2, T3, T4> { |
|
66 |
| |
|
67 |
| private final Lambda4<? super T1, ? super T2, ? super T3, ? super T4, |
|
68 |
| ? extends Quad<T1, T2, T3, T4>> _quadFactory; |
|
69 |
| private final Multiset<Quad<T1, T2, T3, T4>> _previous; |
|
70 |
| private final LinkedList<Quad<T1, T2, T3, T4>> _stack; |
|
71 |
| |
|
72 |
| |
|
73 |
0
| public RecursionStack4() { this(IdentityQuad.<T1, T2, T3, T4>factory()); }
|
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
0
| public RecursionStack4(Lambda4<? super T1, ? super T2, ? super T3, ? super T4,
|
|
82 |
| ? extends Quad<T1, T2, T3, T4>> quadFactory) { |
|
83 |
0
| _quadFactory = quadFactory;
|
|
84 |
0
| _previous = new HashMultiset<Quad<T1, T2, T3, T4>>();
|
|
85 |
0
| _stack = new LinkedList<Quad<T1, T2, T3, T4>>();
|
|
86 |
| } |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
0
| public boolean contains(T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
|
|
93 |
0
| return _previous.contains(_quadFactory.value(arg1, arg2, arg3, arg4));
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
0
| public boolean contains(T1 arg1, T2 arg2, T3 arg3, T4 arg4, int threshold) {
|
|
101 |
0
| return _previous.count(_quadFactory.value(arg1, arg2, arg3, arg4)) >= threshold;
|
|
102 |
| } |
|
103 |
| |
|
104 |
| |
|
105 |
0
| public void push(T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
|
|
106 |
0
| Quad<T1, T2, T3, T4> wrapped = _quadFactory.value(arg1, arg2, arg3, arg4);
|
|
107 |
0
| _stack.addLast(wrapped);
|
|
108 |
0
| _previous.add(wrapped);
|
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
0
| public void pop(T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
|
|
116 |
0
| Quad<T1, T2, T3, T4> wrapped = _quadFactory.value(arg1, arg2, arg3, arg4);
|
|
117 |
0
| if (_stack.isEmpty() || !_stack.getLast().equals(wrapped)) {
|
|
118 |
0
| throw new IllegalArgumentException("given args are not on top of the stack");
|
|
119 |
| } |
|
120 |
0
| _stack.removeLast();
|
|
121 |
0
| _previous.remove(wrapped);
|
|
122 |
| } |
|
123 |
| |
|
124 |
| |
|
125 |
0
| public int size() { return _stack.size(); }
|
|
126 |
| |
|
127 |
| |
|
128 |
0
| public boolean isEmpty() { return _stack.isEmpty(); }
|
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
0
| public void run(Runnable r, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
|
|
135 |
0
| if (!contains(arg1, arg2, arg3, arg4)) {
|
|
136 |
0
| push(arg1, arg2, arg3, arg4);
|
|
137 |
0
| try { r.run(); }
|
|
138 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
139 |
| } |
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
0
| public void run(Runnable r, T1 arg1, T2 arg2, T3 arg3, T4 arg4, int threshold) {
|
|
147 |
0
| if (!contains(arg1, arg2, arg3, arg4, threshold)) {
|
|
148 |
0
| push(arg1, arg2, arg3, arg4);
|
|
149 |
0
| try { r.run(); }
|
|
150 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
151 |
| } |
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
0
| public void run(Runnable r, Runnable infiniteCase, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
|
|
160 |
0
| Runnable toRun = (contains(arg1, arg2, arg3, arg4) ? infiniteCase : r);
|
|
161 |
0
| push(arg1, arg2, arg3, arg4);
|
|
162 |
0
| try { toRun.run(); }
|
|
163 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
164 |
| } |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
0
| public void run(Runnable r, Runnable infiniteCase, T1 arg1, T2 arg2, T3 arg3, T4 arg4, int threshold) {
|
|
172 |
0
| Runnable toRun = (contains(arg1, arg2, arg3, arg4, threshold) ? infiniteCase : r);
|
|
173 |
0
| push(arg1, arg2, arg3, arg4);
|
|
174 |
0
| try { toRun.run(); }
|
|
175 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4>
|
|
183 |
| void run(Runnable4<? super V1, ? super V2, ? super V3, ? super V4> r, V1 arg1, V2 arg2, |
|
184 |
| V3 arg3, V4 arg4) { |
|
185 |
0
| if (!contains(arg1, arg2, arg3, arg4)) {
|
|
186 |
0
| push(arg1, arg2, arg3, arg4);
|
|
187 |
0
| try { r.run(arg1, arg2, arg3, arg4); }
|
|
188 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
189 |
| } |
|
190 |
| } |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4>
|
|
198 |
| void run(Runnable4<? super V1, ? super V2, ? super V3, ? super V4> r, V1 arg1, V2 arg2, |
|
199 |
| V3 arg3, V4 arg4, int threshold) { |
|
200 |
0
| if (!contains(arg1, arg2, arg3, arg4, threshold)) {
|
|
201 |
0
| push(arg1, arg2, arg3, arg4);
|
|
202 |
0
| try { r.run(arg1, arg2, arg3, arg4); }
|
|
203 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
204 |
| } |
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4>
|
|
213 |
| void run(Runnable4<? super V1, ? super V2, ? super V3, ? super V4> r, |
|
214 |
| Runnable4<? super V1, ? super V2, ? super V3, ? super V4> infiniteCase, |
|
215 |
| V1 arg1, V2 arg2, V3 arg3, V4 arg4) { |
|
216 |
| |
|
217 |
0
| @SuppressWarnings("unchecked") Runnable4<? super V1, ? super V2, ? super V3, ? super V4> toRun =
|
|
218 |
| (Runnable4<? super V1, ? super V2, ? super V3, ? super V4>) |
|
219 |
0
| (contains(arg1, arg2, arg3, arg4) ? infiniteCase : r);
|
|
220 |
0
| push(arg1, arg2, arg3, arg4);
|
|
221 |
0
| try { toRun.run(arg1, arg2, arg3, arg4); }
|
|
222 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
223 |
| } |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4>
|
|
231 |
| void run(Runnable4<? super V1, ? super V2, ? super V3, ? super V4> r, |
|
232 |
| Runnable4<? super V1, ? super V2, ? super V3, ? super V4> infiniteCase, |
|
233 |
| V1 arg1, V2 arg2, V3 arg3, V4 arg4, int threshold) { |
|
234 |
| |
|
235 |
0
| @SuppressWarnings("unchecked") Runnable4<? super V1, ? super V2, ? super V3, ? super V4> toRun =
|
|
236 |
| (Runnable4<? super V1, ? super V2, ? super V3, ? super V4>) |
|
237 |
0
| (contains(arg1, arg2, arg3, arg4, threshold) ? infiniteCase : r);
|
|
238 |
0
| push(arg1, arg2, arg3, arg4);
|
|
239 |
0
| try { toRun.run(arg1, arg2, arg3, arg4); }
|
|
240 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
0
| public <R> R apply(Thunk<? extends R> thunk, R infiniteCase, T1 arg1, T2 arg2, T3 arg3, T4 arg4) {
|
|
250 |
0
| if (!contains(arg1, arg2, arg3, arg4)) {
|
|
251 |
0
| push(arg1, arg2, arg3, arg4);
|
|
252 |
0
| try { return thunk.value(); }
|
|
253 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
254 |
| } |
|
255 |
0
| else { return infiniteCase; }
|
|
256 |
| } |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
0
| public <R> R apply(Thunk<? extends R> thunk, R infiniteCase, T1 arg1, T2 arg2, T3 arg3, T4 arg4,
|
|
265 |
| int threshold) { |
|
266 |
0
| if (!contains(arg1, arg2, arg3, arg4, threshold)) {
|
|
267 |
0
| push(arg1, arg2, arg3, arg4);
|
|
268 |
0
| try { return thunk.value(); }
|
|
269 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
270 |
| } |
|
271 |
0
| else { return infiniteCase; }
|
|
272 |
| } |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
0
| public <R> R apply(Thunk<? extends R> thunk, Thunk<? extends R> infiniteCase, T1 arg1,
|
|
282 |
| T2 arg2, T3 arg3, T4 arg4) { |
|
283 |
0
| Thunk<? extends R> toApply = (contains(arg1, arg2, arg3, arg4) ? infiniteCase : thunk);
|
|
284 |
0
| push(arg1, arg2, arg3, arg4);
|
|
285 |
0
| try { return toApply.value(); }
|
|
286 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
287 |
| } |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
0
| public <R> R apply(Thunk<? extends R> thunk, Thunk<? extends R> infiniteCase, T1 arg1,
|
|
297 |
| T2 arg2, T3 arg3, T4 arg4, int threshold) { |
|
298 |
0
| Thunk<? extends R> toApply = (contains(arg1, arg2, arg3, arg4, threshold) ? infiniteCase : thunk);
|
|
299 |
0
| push(arg1, arg2, arg3, arg4);
|
|
300 |
0
| try { return toApply.value(); }
|
|
301 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
302 |
| } |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4, R>
|
|
311 |
| R apply(Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> lambda, |
|
312 |
| R infiniteCase, V1 arg1, V2 arg2, V3 arg3, V4 arg4) { |
|
313 |
0
| if (!contains(arg1, arg2, arg3, arg4)) {
|
|
314 |
0
| push(arg1, arg2, arg3, arg4);
|
|
315 |
0
| try { return lambda.value(arg1, arg2, arg3, arg4); }
|
|
316 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
317 |
| } |
|
318 |
0
| else { return infiniteCase; }
|
|
319 |
| } |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4, R>
|
|
329 |
| R apply(Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> lambda, |
|
330 |
| R infiniteCase, V1 arg1, V2 arg2, V3 arg3, V4 arg4, int threshold) { |
|
331 |
0
| if (!contains(arg1, arg2, arg3, arg4, threshold)) {
|
|
332 |
0
| push(arg1, arg2, arg3, arg4);
|
|
333 |
0
| try { return lambda.value(arg1, arg2, arg3, arg4); }
|
|
334 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
335 |
| } |
|
336 |
0
| else { return infiniteCase; }
|
|
337 |
| } |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4, R>
|
|
347 |
| R apply(Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> lambda, |
|
348 |
| Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> infiniteCase, |
|
349 |
| V1 arg1, V2 arg2, V3 arg3, V4 arg4) { |
|
350 |
| |
|
351 |
0
| @SuppressWarnings("unchecked") Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> toApply =
|
|
352 |
| (Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R>) |
|
353 |
0
| (contains(arg1, arg2, arg3, arg4) ? infiniteCase : lambda);
|
|
354 |
0
| push(arg1, arg2, arg3, arg4);
|
|
355 |
0
| try { return toApply.value(arg1, arg2, arg3, arg4); }
|
|
356 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
357 |
| } |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
0
| public <V1 extends T1, V2 extends T2, V3 extends T3, V4 extends T4, R>
|
|
367 |
| R apply(Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> lambda, |
|
368 |
| Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> infiniteCase, |
|
369 |
| V1 arg1, V2 arg2, V3 arg3, V4 arg4, int threshold) { |
|
370 |
| |
|
371 |
0
| @SuppressWarnings("unchecked") Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R> toApply =
|
|
372 |
| (Lambda4<? super V1, ? super V2, ? super V3, ? super V4, ? extends R>) |
|
373 |
0
| (contains(arg1, arg2, arg3, arg4, threshold) ? infiniteCase : lambda);
|
|
374 |
0
| push(arg1, arg2, arg3, arg4);
|
|
375 |
0
| try { return toApply.value(arg1, arg2, arg3, arg4); }
|
|
376 |
0
| finally { pop(arg1, arg2, arg3, arg4); }
|
|
377 |
| } |
|
378 |
| |
|
379 |
| |
|
380 |
0
| public static <T1, T2, T3, T4> RecursionStack4<T1, T2, T3, T4> make() {
|
|
381 |
0
| return new RecursionStack4<T1, T2, T3, T4>();
|
|
382 |
| } |
|
383 |
| |
|
384 |
| |
|
385 |
0
| public static <T1, T2, T3, T4> RecursionStack4<T1, T2, T3, T4>
|
|
386 |
| make(Lambda4<? super T1, ? super T2, ? super T3, ? super T4, ? extends Quad<T1, T2, T3, T4>> quadFactory) { |
|
387 |
0
| return new RecursionStack4<T1, T2, T3, T4>(quadFactory);
|
|
388 |
| } |
|
389 |
| |
|
390 |
| } |