//! 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 {}); /// Each a pigment, the same on its tag in the list and in the table. const tracked = [_][]const u8{ "prev", "cur", "next" }; 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 = .{ .cpu = false, .idea = .data, .follow = t.move, .source_rows = 5 }; 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, .quiet = true }); const lens = split.lens; const list = canim.nodes(s, .{ .cursor = at, .box = .{ .x = lens.x, .y = lens.y, .w = lens.w, .h = 192 }, .root = "head", .track = &tracked, .focus = true }); const bytes = canim.hexdump(s, .{ .cursor = at, .box = .{ .x = lens.x, .y = lens.y + 204, .w = lens.w, .h = lens.h - 204 }, .base = rec.allocations[0].ptr - 16 }); var says: canim.Narrator = .{}; const joins = canim.ties(s, .{ .cursor = at }); canim.establish(s, .{ ctx.clock, ctx.source, ctx.disasm, ctx.registers, ctx.stack, ctx.backtrace, list, bytes, joins }); 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, then the first trip round the loop, in full. at.walk(s, .line, rec.lineMoment(20, rec.lineMoment(24, reverse.enter))); says.line(s, "`next` remembers the way on, `cur->next` is turned round,\nand `prev` and `cur` each step along."); // Pacing: the second trip is the same, so hurried; it keeps its rest. at.hurry(s, .line, rec.lineMoment(20, rec.lineMoment(24, at.moment(s))), .{}); // The third in full again: the last `next` turns, and `cur` runs off the end. at.walk(s, .line, rec.lineMoment(26, at.moment(s))); 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); s.wait(t.before_cut); }