1 | // A represents the return type of the backtrackable main computation |
2 | sclass BStack_v2b<A> extends VStack is IBStack<A> { |
3 | static int idCounter; |
4 | int stackID = ++idCounter; // for debugging |
5 | |
6 | // Last backtracking point where we will go next when backtracking |
7 | // this stack. |
8 | Branch branch; |
9 | |
10 | sclass Branch { |
11 | // stack that the branch starts with |
12 | BStack_v2b<A> stack; |
13 | |
14 | // remaining options to execute on top of stack |
15 | Iterator<IVF1> options; |
16 | |
17 | // what to undo before branching |
18 | L<AutoCloseable> undos; |
19 | |
20 | void addUndo(AutoCloseable undo) { |
21 | if (undos == null) undos = new L; |
22 | undos.add(undo); |
23 | } |
24 | } |
25 | |
26 | *() {} |
27 | *(Computable<A> computation) { push(computation); } |
28 | *(Iterable<Computable> l) { pushAll(l); } |
29 | |
30 | sclass NoOptionsException extends RuntimeException {} |
31 | |
32 | BStack_v2b<A> newBranch() { |
33 | new BStack_v2b<A> b; |
34 | b.branchsnapshot = snapshot; |
35 | s.options = options; |
36 | s.undos = undos; |
37 | undos = null; |
38 | s.stack = shallowCloneElements(stack); |
39 | ret s; |
40 | } |
41 | |
42 | public void addUndo(AutoCloseable undo) { |
43 | if (undo == null || branch == null) ret; |
44 | branch.addUndo(undo); |
45 | } |
46 | |
47 | public <B extends VStack.Computable> void options(B function, IVF1<B>... options) { |
48 | options(function, arrayIterator(options)); |
49 | } |
50 | |
51 | public <B extends VStack.Computable> void options(B function, Iterable<IVF1<B>> options) { |
52 | Iterator<IVF1<B>> iterator = nonNullIterator(iterator(options)); |
53 | |
54 | if (!iterator.hasNext()) |
55 | throw new NoOptionsException; |
56 | |
57 | IVF1<B> firstOption = iterator.next(); |
58 | |
59 | // All options except first are stored as alternatives |
60 | // in cloned stacks. |
61 | |
62 | setOptions(iterator); |
63 | |
64 | // Execute the first option |
65 | firstOption.get(function); |
66 | } |
67 | |
68 | srecord noeq ExecuteOption(IVF1 option) is VStack.Computable { |
69 | public void step(VStack stack, O subComputationResult) { |
70 | O target = stack.caller(); |
71 | stack.return(); |
72 | option.get(target); |
73 | } |
74 | } |
75 | |
76 | <B> void setOptions(Iterator<IVF1<B>> options) { |
77 | branch = new Branch; |
78 | snapshot = cloneStack(); |
79 | this.options = (Iterator) options; |
80 | } |
81 | |
82 | // Try to backtrack one step and return a new stack |
83 | // Returns null if no backtracking possible |
84 | public BStack_v2b<A> backtrack() ctex { |
85 | if (branch == null) null; |
86 | ret branch.backtrack(); |
87 | |
88 | // Apply undos in reverse order |
89 | for (int i = l(undos)-1; i >= 0; i--) |
90 | undos.get(i).close(); |
91 | |
92 | // no more options? done |
93 | if (options == null || !options.hasNext()) null; |
94 | |
95 | // get snapshot and option to execute |
96 | BStack_v2b<A> nextStack = snapshot; |
97 | var option = options.next(); |
98 | |
99 | // If there are more options in the list, make another clone |
100 | // of the snapshot |
101 | bool moreOptions = options.hasNext(); |
102 | if (moreOptions) { |
103 | nextStack = new BStack_v2; |
104 | nextStack.stack = shallowCloneElements(snapshot.stack); |
105 | nextStack.snapshot = snapshot; |
106 | nextStack.options = options; |
107 | } |
108 | |
109 | nextStack.push(new ExecuteOption(option)); |
110 | ret nextStack; |
111 | } |
112 | |
113 | public A nextResult() { |
114 | stepAll(this); |
115 | return (A) latestResult; |
116 | } |
117 | |
118 | // calculate next result and print stack at every step |
119 | public A nextResultWithPrintStruct(long maxSteps) { |
120 | stepMaxWithPrintIndentedStruct(this, maxSteps); |
121 | return (A) latestResult; |
122 | } |
123 | } |
Began life as a copy of #1035394
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1035418 |
Snippet name: | BStack_v2b - introducing a new sub-object, "Branch" [dev.] |
Eternal ID of this version: | #1035418/1 |
Text MD5: | f5a8ae4e24a8d83c17b0bf09e222c42f |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-05-06 23:06:08 |
Source code size: | 3481 bytes / 123 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 100 / 117 |
Referenced in: | [show references] |