Skip to content

Removing recursion from find_mut in treemap #15540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<K: Ord, V> Mutable for TreeMap<K, V> {

impl<K: Ord, V> Map<K, V> for TreeMap<K, V> {
fn find<'a>(&'a self, key: &K) -> Option<&'a V> {
let mut current: &'a Option<Box<TreeNode<K, V>>> = &self.root;
let mut current = &self.root;
loop {
match *current {
Some(ref r) => {
Expand All @@ -108,7 +108,20 @@ impl<K: Ord, V> Map<K, V> for TreeMap<K, V> {
impl<K: Ord, V> MutableMap<K, V> for TreeMap<K, V> {
#[inline]
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V> {
find_mut(&mut self.root, key)
let mut current = &mut self.root;
loop {
let temp = current; // hack to appease borrowck
match *temp {
Some(ref mut r) => {
match key.cmp(&r.key) {
Less => current = &mut r.left,
Greater => current = &mut r.right,
Equal => return Some(&mut r.value)
}
}
None => return None
}
}
}

fn swap(&mut self, key: K, value: V) -> Option<V> {
Expand Down Expand Up @@ -840,21 +853,6 @@ fn split<K: Ord, V>(node: &mut Box<TreeNode<K, V>>) {
}
}

fn find_mut<'r, K: Ord, V>(node: &'r mut Option<Box<TreeNode<K, V>>>,
key: &K)
-> Option<&'r mut V> {
match *node {
Some(ref mut x) => {
match key.cmp(&x.key) {
Less => find_mut(&mut x.left, key),
Greater => find_mut(&mut x.right, key),
Equal => Some(&mut x.value),
}
}
None => None
}
}

fn insert<K: Ord, V>(node: &mut Option<Box<TreeNode<K, V>>>,
key: K, value: V) -> Option<V> {
match *node {
Expand Down