fn doSiftUp(self: *Self, start_index: usize, target_order: Order) void 121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 fn doSiftUp(self: *Self, start_index: usize, target_order: Order) void {
var child_index = start_index;
while (child_index > 2) {
const grandparent_index = grandparentIndex(child_index);
const child = self.items[child_index];
const grandparent = self.items[grandparent_index];
// If the grandparent is already better or equal, we have gone as far as we need to
if (compareFn(self.context, child, grandparent) != target_order) break;
// Otherwise swap the item with it's grandparent
self.items[grandparent_index] = child;
self.items[child_index] = grandparent;
child_index = grandparent_index;
}
}