Stepper#
Stepper drives a generator-based algorithm one visible step per frame.
Classical algorithms written as loops run to completion within a single frame,
so their intermediate states are never visible on screen. Stepper solves this by
letting the algorithm be written as a generator that yields once per step;
the stepper advances it frame by frame (or on demand).
Each yielded value is a (name, payload) step tuple. The stepper records every step
so it can replay them forward and backward.
Automatic frame-based driving#
stepper.run(world) # register an act hook; advances one step per frame
stepper.on_frame() # call from your own act hook
Complete example#
from miniworlds_data import ListWorld, Stepper
world = ListWorld([5, 2, 4, 1, 3])
initial = list(world.values)
def bubble_sort(data):
n = len(data.values)
for i in range(n):
for j in range(n - i - 1):
yield "compare", (j, j + 1)
if data.values[j] > data.values[j + 1]:
yield "swap", (j, j + 1)
yield "sorted", n - 1 - i
def apply_step(step):
name, payload = step
if name == "compare":
world.compare(*payload)
elif name == "swap":
world.swap(*payload)
elif name == "sorted":
world.mark_sorted(payload)
def on_reset():
for i, v in enumerate(initial):
world.set_value(i, v)
world.reset_all()
stepper = Stepper(
bubble_sort(world),
world,
on_step=apply_step,
on_reset=on_reset,
frames_per_step=10, # slow down: one step every 10 frames
)
stepper.run()
world.run()
Constructor parameters#
Parameter |
Description |
|---|---|
|
Generator that yields |
|
World to register the |
|
Callback |
|
Callback |
|
Advance one step every N frames (default: 1). |
Properties#
Property |
Description |
|---|---|
|
All steps recorded so far, in execution order. |
|
Index of the currently applied step ( |
|
|
|
|
API Reference#
- class miniworlds_data.stepper.Stepper(algorithm, world=None, *, on_step=None, on_reset=None, frames_per_step=1)[Quellcode]#
Drives a generator-based algorithm one visible step per frame.
Classical algorithms written as loops run entirely within a single frame, so their intermediate states are never visible.
Steppersolves this by letting the algorithm be written as a generator that yields once per step; the stepper advances it frame by frame (or on demand).Each yielded step is a
(name, payload)tuple (seeStep). The stepper records every step so it can replay them forward and backward. For backward stepping (prev_step,reset,goto) you must pass anon_resetcallback that restores the visualization to its initial state, since algorithm steps like „swap“ are not idempotent.Example
from miniworlds_data import ListWorld, Stepper world = ListWorld([5, 2, 4, 1, 3]) def bubble_sort(data): n = len(data.values) for i in range(n): for j in range(n - i - 1): yield "compare", (j, j + 1) if data.values[j] > data.values[j + 1]: yield "swap", (j, j + 1) stepper = Stepper(bubble_sort(world), world, on_step=apply_step) stepper.run() # advances one step per frame automatically
- goto(index)[Quellcode]#
Move to an absolute step index.
A negative index rewinds to the start; an index past the end advances as far as the generator allows. Backward jumps require
on_reset.- Rückgabetyp:
- next_step()[Quellcode]#
Advance the algorithm by exactly one step and apply it.
Returns the applied step, or
Nonewhen the algorithm is finished.
- on_frame()[Quellcode]#
Advance one step every
frames_per_stepframes.Call this manually from your own
acthook if you do not userun().
- prev_step()[Quellcode]#
Undo the most recently applied step and move backward.
Returns the step that was undone, or
Noneat the beginning.Requires an
on_resetcallback, because steps like „swap“ cannot be undone in isolation.
- reset()[Quellcode]#
Rewind to the beginning and apply no steps.
Requires an
on_resetcallback.- Rückgabetyp:
- run(world=None)[Quellcode]#
Register an
acthook on the world that advances one step per frame.- Rückgabetyp:
- Parameter:
world – World to register on. Defaults to the world passed at construction time.