1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
//! Insertion sort as its blocks: the graph of `sort` as rings over the
//! program column (the `blocks` lens: the outer loop's ring, the inner's
//! touching it at its header), the source and the disassembly under it,
//! the registers and a stack of twenty beside; on the map the same
//! drawing over the source, the disassembly linear beside it.
//!
//! How it moves (design/motion.md, "A block left"): it opens where `i` has
//! reached 12, the first value out of order, the swap's block not reached
//! yet. One trip round the inner loop is walked a block at a time: each block's
//! instructions a cascade onto its last row, then `leave` along the edge
//! that row takes (the compare's chips framed, the edge drawn gold, the
//! mark along it, the target's count struck): the swap's first `reach`,
//! then the inner ring's first `turn`; the disassembly unrolls for a beat,
//! the function whole. The next trip is a run; the long
//! middle a sprint on the map (`surface`, the gold going round the rings
//! and down the linear column, `dive`); the last value's trip is walked
//! again, and the way out run.
const anim = @import("anim");
const canim = @import("canim");
const recordings = @import("recordings");
pub const background = canim.background;
pub const scene = anim.Live(frame, struct {});
fn frame(s: *anim.Frame) void {
const rec = canim.open(recordings.sort);
const graph = canim.graph;
const t = canim.theme.time;
// The `while`, about to compare 31 with 12: the stop before the one that calls swap.
var start = rec.call("sort", 0).enter;
while (rec.next(.over, rec.next(.over, start)) < rec.call("swap", 0).enter) start = rec.next(.over, start);
// The inner loop's test (`j > 0`): where each of its trips begins.
const test_pc = rec.register(start, "rip");
var at = canim.cursor(s, rec, start);
at.levels = .{ .fire = .disasm, .source_rows = 6 };
const last = rec.call("swap", 9);
// The last value's trip: from its test, before the compare that leads to the last swap.
var last_trip = last.enter;
while (rec.register(last_trip, "rip") != test_pc) last_trip -= 1;
at.journey(.over, rec.call("sort", 0).exit.? - 1);
const ctx = canim.context(s, at, .{ .split = canim.layout.split.blocks, .graph = true });
const blocks = canim.add(s, graph.Blocks{ .cursor = at, .stage = ctx.stage, .focus = true });
var says: canim.Narrator = .{};
const joins = ctx.ties(s);
// A block at a time: its rows a cascade onto its last, then the edge it takes.
const block = struct {
fn walk(c: canim.Cursor, f: *anim.Frame) void {
graph.toEnd(c, f);
graph.leave(c, f);
}
}.walk;
canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, blocks, joins });
says.line(s, "`sort` as its blocks: each loop a ring, the inner touching\nthe outer. `j > 0`, so `jle` goes on to the compare.");
block(at, s);
says.line(s, "31 > 12: `setg` leaves 1, and `jne` takes the swap,\nthe first time it is reached.");
block(at, s);
block(at, s);
says.line(s, "The swap and `j--`; then the inner loop turns,\nback up to `j > 0`: its first turn.");
block(at, s);
// The whole function for a beat: the disassembly unrolled, each block's first row ruled with its visits.
says.line(s, "The function whole: each block under a rule, with its visits.");
ctx.map.unroll(s, .disasm);
// Held while the caption is read: rolled back in as its reading time ends.
s.wait(@max(says.read_by - s.cursor - t.unroll.roll - t.unroll.veil, t.unroll.hold));
ctx.map.roll(s);
// Pacing: the next trip at a run; then the long middle on the map, at a sprint.
says.line(s, "Once more at a run: 17 > 12, a second swap.");
at.hurry(s, .over, rec.lineMoment(12, rec.call("swap", 1).exit.?), .{ .settle = false });
says.line(s, "The rest on the map: the gold goes block to block,\nand the counts grow with every trip.");
ctx.map.surface(s);
at.hurry(s, .over, last_trip, .{ .pace = t.sprint, .from = t.run });
ctx.map.dive(s);
// Two captions: one would be up over 12 s across the three blocks.
says.line(s, "The last value, 20: `j > 0`, and 26 > 20,");
block(at, s);
block(at, s);
says.line(s, "so `jne` takes the swap.");
block(at, s);
says.line(s, "The swap, and the inner loop turns once more.");
block(at, s);
says.line(s, "17 > 20 is false: the inner loop is done, and with `i` at 8\nso is the outer. The array is sorted.");
at.hurry(s, .over, rec.call("sort", 0).exit.? - 1, .{});
says.close(s);
}
|