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).

// A represents the return type of the backtrackable main computation
sclass BStack<A> extends VStack is IBStack<A> {
  static int idCounter;
  int stackID = ++idCounter; // for debugging
  
  // alternative to backtrack to
  BStack<A> alternative;
  
  // steps to undo before switching to the alternative
  L<AutoCloseable> undos;
  
  *() {}
  *(Computable<A> computation) { push(computation); }
  *(Iterable<Computable> l) { pushAll(l); }
  
  sclass NoOptionsException extends RuntimeException {}
  
  BStack<A> cloneStack() {
    new BStack<A> s;
    s.alternative = alternative;
    s.undos = undos;
    undos = null;
    s.stack = shallowCloneElements(stack);
    ret s;
  }
  
  public void addUndo(AutoCloseable undo) {
    if (undo == null) ret;
    if (undos == null) undos = new L;
    undos.add(undo);
  }
  
  public <B extends VStack.Computable> void options(B function,
    IVF1<B>... options) {
    options(function, asList(options));
  }
    
  public <B extends VStack.Computable> void options(B function, Iterable<IVF1<B>> options) {
    L<IVF1<B>> optionsList = nonNulls(options);
    
    if (empty(optionsList))
      throw new NoOptionsException;
    
    // All options except first are stored as alternatives
    // in cloned stacks.
    
    for (int i = l(optionsList)-1; i > 0; i--)
      setAlternative(optionsList.get(i));

    // Execute the first option
    first(optionsList).get(function);
  }
  
  srecord noeq ExecuteOption(IVF1 option) is VStack.Computable {
    public void step(VStack stack, O subComputationResult) {
      O target = stack.caller();
      stack.return();
      option.get(target);
    }
  }
  
  void setAlternative(IVF1 option) {
    alternative = cloneStack();
    alternative.push(new ExecuteOption(option));
  }
  
  // Try to backtrack one step and return a new stack
  // Returns null if no backtracking possible
  public BStack<A> backtrack() ctex {
    // Apply undos in reverse order
    for (int i = l(undos)-1; i >= 0; i--)
      undos.get(i).close();
        
    ret alternative;
  }
  
  public A nextResult() {
    stepAll(this);
    return (A) latestResult;
  }
  
  // calculate next result and print stack at every step
  public A nextResultWithPrintStruct(long maxSteps) {
    stepMaxWithPrintIndentedStruct(this, maxSteps);
    return (A) latestResult;
  }
}

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: 242 / 593
Version history: 62 change(s)
Referenced in: [show references]