This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.
fnsiftDown(a: usize, target: usize, b: usize, context: anytype) void {
varcur = target;
while (true) {
// When we don't overflow from the multiply below, the following expression equals (2*cur) - (2*a) + a + 1
// The `+ a + 1` is safe because:
// for `a > 0` then `2a >= a + 1`.
// for `a = 0`, the expression equals `2*cur+1`. `2*cur` is an even number, therefore adding 1 is safe.
varchild = (math.mul(usize, cur - a, 2) catchbreak) + a + 1;
// stop if we overshot the boundaryif (!(child < b)) break;
// `next_child` is at most `b`, therefore no overflow is possibleconstnext_child = child + 1;
// store the greater child in `child`if (next_child < bandcontext.lessThan(child, next_child)) {
child = next_child;
}
// stop if the Heap invariant holds at `cur`.if (context.lessThan(child, cur)) break;
// swap `cur` with the greater child,
// move one step down, and continue sifting.
context.swap(child, cur);
cur = child;
}
}