Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

109
LINES

< > BotCompany Repo | #1035394 // BStack_v2 - a virtual stack with backtracking capability (version 2, more efficient, OK)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (22926L/140K).

1  
// A represents the return type of the backtrackable main computation
2  
sclass BStack_v2<A> extends VStack is IBStack<A> {
3  
  static int idCounter;
4  
  int stackID = ++idCounter; // for debugging
5  
  
6  
  // cloned version made at last branch point
7  
  BStack_v2<A> snapshot;
8  
  
9  
  // remaining options at last branch point
10  
  Iterator<IVF1> options;
11  
  
12  
  // steps to undo before switching to the alternative
13  
  L<AutoCloseable> undos;
14  
  
15  
  *() {}
16  
  *(Computable<A> computation) { push(computation); }
17  
  *(Iterable<Computable> l) { pushAll(l); }
18  
  
19  
  sclass NoOptionsException extends RuntimeException {}
20  
  
21  
  BStack_v2<A> cloneStack() {
22  
    new BStack_v2<A> s;
23  
    s.snapshot = snapshot;
24  
    s.options = options;
25  
    s.undos = undos;
26  
    undos = null;
27  
    s.stack = shallowCloneElements(stack);
28  
    ret s;
29  
  }
30  
  
31  
  public void addUndo(AutoCloseable undo) {
32  
    if (undo == null || snapshot == null) ret;
33  
    if (undos == null) undos = new L;
34  
    undos.add(undo);
35  
  }
36  
  
37  
  public <B extends VStack.Computable> void options(B function, IVF1<B>... options) {
38  
    options(function, arrayIterator(options));
39  
  }
40  
    
41  
  public <B extends VStack.Computable> void options(B function, Iterable<IVF1<B>> options) {
42  
    Iterator<IVF1<B>> iterator = nonNullIterator(iterator(options));
43  
    
44  
    if (!iterator.hasNext())
45  
      throw new NoOptionsException;
46  
      
47  
    IVF1<B> firstOption = iterator.next();
48  
    
49  
    // All options except first are stored as alternatives
50  
    // in cloned stacks.
51  
    
52  
    setOptions(iterator);
53  
54  
    // Execute the first option
55  
    firstOption.get(function);
56  
  }
57  
  
58  
  srecord noeq ExecuteOption(IVF1 option) is VStack.Computable {
59  
    public void step(VStack stack, O subComputationResult) {
60  
      O target = stack.caller();
61  
      stack.return();
62  
      option.get(target);
63  
    }
64  
  }
65  
  
66  
  <B> void setOptions(Iterator<IVF1<B>> options) {
67  
    snapshot = cloneStack();
68  
    this.options = (Iterator) options;
69  
  }
70  
  
71  
  // Try to backtrack one step and return a new stack
72  
  // Returns null if no backtracking possible
73  
  public BStack_v2<A> backtrack() ctex {
74  
    // Apply undos in reverse order
75  
    for (int i = l(undos)-1; i >= 0; i--)
76  
      undos.get(i).close();
77  
        
78  
    // no more options? done
79  
    if (options == null || !options.hasNext()) null;
80  
    
81  
    // get snapshot and option to execute
82  
    BStack_v2<A> nextStack = snapshot;
83  
    var option = options.next();
84  
    
85  
    // If there are more options in the list, make another clone
86  
    // of the snapshot
87  
    bool moreOptions = options.hasNext();
88  
    if (moreOptions) {
89  
      nextStack = new BStack_v2;
90  
      nextStack.stack = shallowCloneElements(snapshot.stack);
91  
      nextStack.snapshot = snapshot;
92  
      nextStack.options = options;
93  
    }
94  
    
95  
    nextStack.push(new ExecuteOption(option));
96  
    ret nextStack;
97  
  }
98  
  
99  
  public A nextResult() {
100  
    stepAll(this);
101  
    return (A) latestResult;
102  
  }
103  
  
104  
  // calculate next result and print stack at every step
105  
  public A nextResultWithPrintStruct(long maxSteps) {
106  
    stepMaxWithPrintIndentedStruct(this, maxSteps);
107  
    return (A) latestResult;
108  
  }
109  
}

Author comment

Began life as a copy of #1035381

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035394
Snippet name: BStack_v2 - a virtual stack with backtracking capability (version 2, more efficient, OK)
Eternal ID of this version: #1035394/19
Text MD5: 46e0827a4bfa7bf9ba1ecb7534f331aa
Transpilation MD5: d6e6c9adb8504945c6badccf03dfab3c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-06 01:33:59
Source code size: 3170 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 113 / 227
Version history: 18 change(s)
Referenced in: [show references]