summaryrefslogtreecommitdiff
path: root/anim/scenes/list.zig
blob: d6f1a76b56c00d5837c3c78b00bb14b0d75199ac (plain) (blame)
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
//! A linked list reversed in place: no node moves, each `next` is rewritten
//! to point the other way. The debugger's context, and beside it the lens:
//! the nodes and their arrows over a hexdump of the heap they are made of.
//!
//! How it moves (design/motion.md): the picture is established; the first
//! trip round the loop is walked (each local strikes on the stack and in
//! the table, then its tag slides to its node; the pointer's rewrite is
//! armed with its old arrow going dashed, strikes, and the new arrow grows
//! to the node it lands on); the second trip is hurried, with a rest; the
//! third is walked again, and `head` takes the last node.
const anim = @import("anim");
const canim = @import("canim");
const recordings = @import("recordings");

pub const background = canim.background;
pub const scene = anim.Live(frame, struct {});

/// The variables whose tags the nodes carry: the three the loop moves, and `head`.
const tracked = [_][]const u8{ "prev", "cur", "next", "head" };

fn frame(s: *anim.Frame) void {
    const rec = canim.open(recordings.list);
    const t = canim.theme.time;
    const reverse = rec.call("reverse", 0);
    // From reverse's first line (the list of three is built) to its last.
    var at = canim.cursor(s, rec, rec.lineMoment(18, reverse.enter));
    // The nodes are the idea; a tag slides and an arrow grows after its strike.
    at.levels = .{ .idea = .data, .follow = t.move, .source_rows = 10 };
    const end = rec.lineMoment(27, reverse.enter);
    at.journey(.line, end);
    // The idea is the subject: the lens a size up, the nodes over the bytes they are.
    const split = canim.layout.split.subject;
    const ctx = canim.context(s, at, .{ .split = split });
    const list = canim.nodes(s, .{ .cursor = at, .stage = ctx.stage, .root = "head", .track = &tracked });
    var says: canim.Narrator = .{};

    const joins = ctx.ties(s);
    // The nodes come in beside their bytes, one by one: three of them.
    ctx.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, list, joins }, 3);
    says.line(s, "A list is nodes on the heap, each holding the address of the next.\nReversing it moves no node: it rewrites every `next`.");
    // Three names are set up.
    const first_trip = rec.lineMoment(21, reverse.enter);
    at.walk(s, .line, first_trip);
    // The loop's caption leads the loop.
    says.line(s, "`next` remembers the way on, `cur->next` is turned round,\nand `prev` and `cur` each step along.");
    const second_trip = rec.lineMoment(21, rec.lineMoment(24, first_trip));
    at.walk(s, .line, second_trip);
    // Pacing: the second trip is the same, so hurried; it keeps its rest.
    says.line(s, "The second node, the same way, faster:\nits `next` turns back to the first.");
    const third_trip = rec.lineMoment(21, rec.lineMoment(24, second_trip));
    at.hurry(s, .line, third_trip, .{});
    // The third in full again: the last `next` turns, and `cur` runs off the end.
    says.line(s, "The last node: its `next` turns too,\nand `cur` runs off the end of the list.");
    at.walk(s, .line, rec.lineMoment(26, third_trip));
    says.line(s, "`head` takes the last node: the same three nodes,\nin the same places, read the other way.");
    at.walk(s, .line, end);
    says.close(s);
}