fn rotate(self: *Self, node: *Node, right: bool) void 261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 fn rotate(self: *Self, node: *Node, right: bool) void {
// if right, converts the following:
// parent -> (node (target YY adjacent) XX)
// parent -> (target YY (node adjacent XX))
//
// if left (!right), converts the following:
// parent -> (node (target YY adjacent) XX)
// parent -> (target YY (node adjacent XX))
const parent = node.parent;
const target = node.children[@intFromBool(!right)] orelse unreachable;
const adjacent = target.children[@intFromBool(right)];
// rotate the children
target.children[@intFromBool(right)] = node;
node.children[@intFromBool(!right)] = adjacent;
// rotate the parents
node.parent = target;
target.parent = parent;
if (adjacent) |adj| adj.parent = node;
// fix the parent link
const link = if (parent) |p| &p.children[@intFromBool(p.children[1] == node)] else &self.root;
assert(link.* == node);
link.* = target;
}