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

123
LINES

< > BotCompany Repo | #1035418 // BStack_v2b - introducing a new sub-object, "Branch" [dev.]

JavaX fragment (include)

// A represents the return type of the backtrackable main computation
sclass BStack_v2b<A> extends VStack is IBStack<A> {
  static int idCounter;
  int stackID = ++idCounter; // for debugging
  
  // Last backtracking point where we will go next when backtracking
  // this stack.
  Branch branch;
  
  sclass Branch {
    // stack that the branch starts with
    BStack_v2b<A> stack;
    
    // remaining options to execute on top of stack
    Iterator<IVF1> options;
    
    // what to undo before branching
    L<AutoCloseable> undos;
    
    void addUndo(AutoCloseable undo) {
      if (undos == null) undos = new L;
      undos.add(undo);
    }
  }
  
  *() {}
  *(Computable<A> computation) { push(computation); }
  *(Iterable<Computable> l) { pushAll(l); }
  
  sclass NoOptionsException extends RuntimeException {}
  
  BStack_v2b<A> newBranch() {
    new BStack_v2b<A> b;
    b.branchsnapshot = snapshot;
    s.options = options;
    s.undos = undos;
    undos = null;
    s.stack = shallowCloneElements(stack);
    ret s;
  }
  
  public void addUndo(AutoCloseable undo) {
    if (undo == null || branch == null) ret;
    branch.addUndo(undo);
  }
  
  public <B extends VStack.Computable> void options(B function, IVF1<B>... options) {
    options(function, arrayIterator(options));
  }
    
  public <B extends VStack.Computable> void options(B function, Iterable<IVF1<B>> options) {
    Iterator<IVF1<B>> iterator = nonNullIterator(iterator(options));
    
    if (!iterator.hasNext())
      throw new NoOptionsException;
      
    IVF1<B> firstOption = iterator.next();
    
    // All options except first are stored as alternatives
    // in cloned stacks.
    
    setOptions(iterator);

    // Execute the first option
    firstOption.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);
    }
  }
  
  <B> void setOptions(Iterator<IVF1<B>> options) {
    branch = new Branch;
    snapshot = cloneStack();
    this.options = (Iterator) options;
  }
  
  // Try to backtrack one step and return a new stack
  // Returns null if no backtracking possible
  public BStack_v2b<A> backtrack() ctex {
    if (branch == null) null;
    ret branch.backtrack();
    
    // Apply undos in reverse order
    for (int i = l(undos)-1; i >= 0; i--)
      undos.get(i).close();
        
    // no more options? done
    if (options == null || !options.hasNext()) null;
    
    // get snapshot and option to execute
    BStack_v2b<A> nextStack = snapshot;
    var option = options.next();
    
    // If there are more options in the list, make another clone
    // of the snapshot
    bool moreOptions = options.hasNext();
    if (moreOptions) {
      nextStack = new BStack_v2;
      nextStack.stack = shallowCloneElements(snapshot.stack);
      nextStack.snapshot = snapshot;
      nextStack.options = options;
    }
    
    nextStack.push(new ExecuteOption(option));
    ret nextStack;
  }
  
  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 #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: 99 / 115
Referenced in: [show references]