in src/index.rs [278:307]
fn get_dir_index(&self, dir: ChildDir) -> Option<TreeIndex> {
let mut opp_dir = ChildDir::Left;
let mut dir_bit = 1;
if dir == ChildDir::Left {
opp_dir = ChildDir::Right;
dir_bit = 0;
}
// Gets the closest ancestor that has a dir child not on the path from the root to the input index.
// Retrieve the dir child, which is the root of the subtree that contains the dir index.
let mut index = *self;
for i in (0..self.height).rev() {
if self.get_bit(i) == 1 - dir_bit {
index = index.get_prefix(i).get_child_index_by_dir(dir);
break;
}
}
// Gets the opp_dir-most child, which is the desired index.
while index.get_height() < self.height {
index = index.get_child_index_by_dir(opp_dir);
}
if index == *self {
// If the result index is the same as the input, the input is the dir-most leaf.
// So the dir node to the input doesn't exist, return None.
None
} else {
Some(index)
}
}