Skip to content

Instantly share code, notes, and snippets.

@jgillis
Created March 9, 2021 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgillis/e2bc8cace39929b35edcae4a801902fc to your computer and use it in GitHub Desktop.
Save jgillis/e2bc8cace39929b35edcae4a801902fc to your computer and use it in GitHub Desktop.
class Node:
def __init__(self,val):
self.val = val
self.nodes = []
class AutoBrancher:
OPEN = 0
DONE = 1
def __init__(self):
self.root = Node(AutoBrancher.OPEN)
self.trace = [self.root]
@property
def current(self):
return self.trace[-1]
def branch(self, alternatives = [True, False]):
alternatives = list(alternatives)
nodes = self.current.nodes
if len(nodes)==0:
nodes += [None]*len(alternatives)
for i,n in enumerate(nodes):
if n is None:
nodes[i] = Node(AutoBrancher.OPEN)
self.trace.append(nodes[i])
self.this_branch.append(alternatives[i])
return alternatives[i]
else:
if n.val == AutoBrancher.OPEN:
self.trace.append(nodes[i])
self.this_branch.append(alternatives[i])
return alternatives[i]
def __iter__(self):
cnt = 0
while self.root.val==AutoBrancher.OPEN:
self.this_branch = []
cnt+=1
yield self
# Indicate that current leaf is done
self.current.val = AutoBrancher.DONE
# Close leaves when subleaves are done
for n in reversed(self.trace[:-1]):
finished = True
for e in n.nodes:
finished = finished and e and e.val==AutoBrancher.DONE
if finished:
n.val = AutoBrancher.DONE
# Reset trace
self.trace = [self.root]
print("Evaluated branch",self.this_branch)
print("Evaluated",cnt,"branches")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment