Skip to content

Commit

Permalink
Updated algorithms to avoid using start event
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas van den Heuvel committed Feb 2, 2018
1 parent 789bc5d commit 429e434
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions bubblesort.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def setup_swap(self):

def swap(self):
if self.i < len(self.swappers):
self.emit_to(self.swappers[self.i], 'start')
self.emit_to(self.swappers[self.i], 'swap')
self.i += 1
return self.listen

Expand All @@ -64,12 +64,15 @@ def __init__(self, ctl, ctx, a, i):
self.a = a
self.i = i

self.init_state = self.swap
self.init_state = self.setup

def __repr__(self):
return '<Swapper:i=%d,state=%s>' % (self.i,
self.current_state.__name__)

def setup(self):
self.when_machine_emits('swap', self.ctx, self.swap)

def swap(self):
if self.a[self.i - 1] > self.a[self.i]:
tmp = self.a[self.i]
Expand All @@ -84,5 +87,7 @@ def swap(self):

if __name__ == '__main__':
ctl = MachineControl(debug=False)
ctl.run(BubbleSort, [5, 3, 1, 8])
ctl.run(BubbleSort, [5, 4, 3, 2, 3, 4, 5, 67, 1, 1, 6, 9])
ctl.run(BubbleSort, list("test with string"))
ctl.run(BubbleSort, list('zpykxcerndu'))

0 comments on commit 429e434

Please sign in to comment.