Skip to content

Instantly share code, notes, and snippets.

@nathansobo
Created March 28, 2016 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansobo/01afbd5638e9e64af11c to your computer and use it in GitHub Desktop.
Save nathansobo/01afbd5638e9e64af11c to your computer and use it in GitHub Desktop.
Lots of `as_ref`... Am I doing this right?
fn find_lower_bound(&self, target: &Point) -> Option<Rc<Node<'a>>> {
self.root.as_ref().and_then(|root| {
let mut current_node: Rc<Node> = root.clone();
let mut max_lower_bound: Option<Rc<Node>> = None;
let mut left_ancestor_end = Point::zero();
loop {
let current_node_start = left_ancestor_end.traverse(&current_node.new_distance_from_left_ancestor);
if target < &current_node_start {
if current_node.left_child.is_none() {
return max_lower_bound;
} else {
current_node = current_node.left_child.as_ref().unwrap().clone();
}
} else {
max_lower_bound = Some(current_node.clone());
if current_node.right_child.is_none() {
return max_lower_bound;
} else {
left_ancestor_end = current_node_start.traverse(&current_node.new_extent);
current_node = current_node.right_child.as_ref().unwrap().clone();
}
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment