The finale needs a graph that exercises every hard case at once. Today you build it - a shared node, a root-reachable cycle, an unreachable cycle, and a dead subgraph - and confirm exactly which objects are reachable before any collection runs.
Construct the capstone object graph and assert its reachable and garbage sets.
The capstone graph is deliberately built to break a lesser collector. It contains
every case the project taught: a shared node S referenced by two parents, a
root-reachable cycle A to B to A that must survive, an unreachable
cycle X to Y to X that must be reclaimed despite each object having an
incoming reference, and a dead subgraph D to E reachable from nothing. The
survivors and the garbage are deliberately interleaved across the slots - 0, 2, 4, 6
live, 1, 3, 5, 7 dead - so a compacting collector has visible work to do.
Before collecting anything, confirm the graph is wired exactly right by checking
reachability directly: {0, 2, 4, 6} reachable, [1, 3, 5, 7] garbage. This is the
setup both capstone collections run against, so pinning it now means any surprise in
the next two lessons is the collector’s doing, not a mistake in the graph. The next
lesson turns the mark-sweep collector loose on it; the last runs the copying collector
on the same shape.
R, X, A, Y := h.New(2), h.New(1), h.New(2), h.New(1) // 0,1,2,3B, D, S, E := h.New(2), h.New(1), h.New(0), h.New(0) // 4,5,6,7h.SetField(R,0,A) // root -> Ah.SetField(A,0,B); h.SetField(A,1,S) // A <-> B, both -> Sh.SetField(B,0,A); h.SetField(B,1,S)h.SetField(X,0,Y); h.SetField(Y,0,X) // unreachable cycleh.SetField(D,0,E) // dead subgraphh.AddRoot(R)