Skip to content

Commit

Permalink
src: don't call recalcHeight in recalcHeight when it's not needed. (
Browse files Browse the repository at this point in the history
#14)

* src: don't call `recalcHeight` in `recalcHeight` when it's not needed.
  • Loading branch information
avdva authored Sep 27, 2024
1 parent 4785db8 commit 1caf8b1
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/avl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,8 @@ pub fn TreeWithOptions(comptime K: type, comptime V: type, comptime Cmp: fn (a:

fn updateCounts(loc: Location) void {
var mutLoc: ?Location = loc;
while (true) {
var l = mutLoc orelse break;
recalcCounts(l);
while (mutLoc) |*l| {
recalcCounts(l.*);
mutLoc = l.parent();
}
}
Expand Down Expand Up @@ -735,34 +734,32 @@ pub fn TreeWithOptions(comptime K: type, comptime V: type, comptime Cmp: fn (a:

fn checkBalance(self: *Self, loc: ?Location, all_way_up: bool) void {
var mutLoc = loc;
while (true) {
var l = mutLoc orelse break;
const heightChanged = recalcHeight(l);
while (mutLoc) |*l| {
const parent = l.parent();
switch (balance(l)) {
switch (balance(l.*)) {
-2 => {
switch (balance(l.child(.left).?)) {
-1, 0 => self.rr(l),
1 => self.lr(l),
switch (balance(l.*.child(.left).?)) {
-1, 0 => self.rr(l.*),
1 => self.lr(l.*),
else => unreachable,
}
},
2 => {
switch (balance(l.child(.right).?)) {
-1 => self.rl(l),
0, 1 => self.ll(l),
switch (balance(l.*.child(.right).?)) {
-1 => self.rl(l.*),
0, 1 => self.ll(l.*),
else => unreachable,
}
},
else => {
if (!heightChanged and !all_way_up) {
if (!recalcHeight(l.*) and !all_way_up) {
if (options.countChildren) {
updateCounts(l);
updateCounts(l.*);
}
return;
}
if (options.countChildren) {
recalcCounts(l);
recalcCounts(l.*);
}
},
}
Expand Down

0 comments on commit 1caf8b1

Please sign in to comment.