Skip to content

Instantly share code, notes, and snippets.

@nathansobo
Created March 28, 2016 05:37

Revisions

  1. Nathan Sobo created this gist Mar 28, 2016.
    27 changes: 27 additions & 0 deletions options-as-ref.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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();
    }
    }
    }
    })
    }