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

87
LINES

< > BotCompany Repo | #1035381 // BStack - a virtual stack with backtracking capability (extension of VStack, OK)

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

Libraryless. Click here for Pure Java version (22806L/139K).

1  
// A represents the return type of the backtrackable main computation
2  
sclass BStack<A> extends VStack is IBStack<A> {
3  
  static int idCounter;
4  
  int stackID = ++idCounter; // for debugging
5  
  
6  
  // alternative to backtrack to
7  
  BStack<A> alternative;
8  
  
9  
  // steps to undo before switching to the alternative
10  
  L<AutoCloseable> undos;
11  
  
12  
  *() {}
13  
  *(Computable<A> computation) { push(computation); }
14  
  *(Iterable<Computable> l) { pushAll(l); }
15  
  
16  
  sclass NoOptionsException extends RuntimeException {}
17  
  
18  
  BStack<A> cloneStack() {
19  
    new BStack<A> s;
20  
    s.alternative = alternative;
21  
    s.undos = undos;
22  
    undos = null;
23  
    s.stack = shallowCloneElements(stack);
24  
    ret s;
25  
  }
26  
  
27  
  public void addUndo(AutoCloseable undo) {
28  
    if (undo == null) ret;
29  
    if (undos == null) undos = new L;
30  
    undos.add(undo);
31  
  }
32  
  
33  
  public <B extends VStack.Computable> void options(B function,
34  
    IVF1<B>... options) {
35  
    options(function, asList(options));
36  
  }
37  
    
38  
  public <B extends VStack.Computable> void options(B function, Iterable<IVF1<B>> options) {
39  
    L<IVF1<B>> optionsList = nonNulls(options);
40  
    
41  
    if (empty(optionsList))
42  
      throw new NoOptionsException;
43  
    
44  
    // All options except first are stored as alternatives
45  
    // in cloned stacks.
46  
    
47  
    for (int i = l(optionsList)-1; i > 0; i--)
48  
      setAlternative(optionsList.get(i));
49  
50  
    // Execute the first option
51  
    first(optionsList).get(function);
52  
  }
53  
  
54  
  srecord noeq ExecuteOption(IVF1 option) is VStack.Computable {
55  
    public void step(VStack stack, O subComputationResult) {
56  
      O target = stack.caller();
57  
      stack.return();
58  
      option.get(target);
59  
    }
60  
  }
61  
  
62  
  void setAlternative(IVF1 option) {
63  
    alternative = cloneStack();
64  
    alternative.push(new ExecuteOption(option));
65  
  }
66  
  
67  
  // Try to backtrack one step and return a new stack
68  
  // Returns null if no backtracking possible
69  
  public BStack<A> backtrack() ctex {
70  
    // Apply undos in reverse order
71  
    for (int i = l(undos)-1; i >= 0; i--)
72  
      undos.get(i).close();
73  
        
74  
    ret alternative;
75  
  }
76  
  
77  
  public A nextResult() {
78  
    stepAll(this);
79  
    return (A) latestResult;
80  
  }
81  
  
82  
  // calculate next result and print stack at every step
83  
  public A nextResultWithPrintStruct(long maxSteps) {
84  
    stepMaxWithPrintIndentedStruct(this, maxSteps);
85  
    return (A) latestResult;
86  
  }
87  
}

Author comment

Began life as a copy of #1030376

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035381
Snippet name: BStack - a virtual stack with backtracking capability (extension of VStack, OK)
Eternal ID of this version: #1035381/63
Text MD5: cfd2fef41fc8040476a874521ba24678
Transpilation MD5: 61f0f2467468a1ef17d058bfc12c3e6d
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:34:13
Source code size: 2423 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 243 / 595
Version history: 62 change(s)
Referenced in: [show references]