Skip to content

Commit

Permalink
Compare values in TreeMap's 'lt' function
Browse files Browse the repository at this point in the history
Closes #5194
  • Loading branch information
alexcrichton committed Jul 2, 2013
1 parent 77b9824 commit 87b6129
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/libextra/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,25 @@ impl<K: Eq + TotalOrd, V: Eq> Eq for TreeMap<K, V> {
}

// Lexicographical comparison
fn lt<K: Ord + TotalOrd, V>(a: &TreeMap<K, V>,
fn lt<K: Ord + TotalOrd, V: Ord>(a: &TreeMap<K, V>,
b: &TreeMap<K, V>) -> bool {
let mut x = a.iter();
let mut y = b.iter();

let (a_len, b_len) = (a.len(), b.len());
for uint::min(a_len, b_len).times {
let (key_a,_) = x.next().unwrap();
let (key_b,_) = y.next().unwrap();
let (key_a, value_a) = x.next().unwrap();
let (key_b, value_b) = y.next().unwrap();
if *key_a < *key_b { return true; }
if *key_a > *key_b { return false; }
};
if *value_a < *value_b { return true; }
if *value_a > *value_b { return false; }
}

a_len < b_len
}

impl<K: Ord + TotalOrd, V> Ord for TreeMap<K, V> {
impl<K: Ord + TotalOrd, V: Ord> Ord for TreeMap<K, V> {
#[inline]
fn lt(&self, other: &TreeMap<K, V>) -> bool { lt(self, other) }
#[inline]
Expand Down Expand Up @@ -935,7 +937,7 @@ mod test_treemap {
assert!(b.insert(0, 5));
assert!(a < b);
assert!(a.insert(0, 7));
assert!(!(a < b) && !(b < a));
assert!(!(a < b) && b < a);
assert!(b.insert(-2, 0));
assert!(b < a);
assert!(a.insert(-5, 2));
Expand Down

5 comments on commit 87b6129

@bors
Copy link
Contributor

@bors bors commented on 87b6129 Jul 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at alexcrichton@87b6129

@bors
Copy link
Contributor

@bors bors commented on 87b6129 Jul 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/issue-5194 = 87b6129 into auto

@bors
Copy link
Contributor

@bors bors commented on 87b6129 Jul 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/issue-5194 = 87b6129 merged ok, testing candidate = 8c50ee3

@bors
Copy link
Contributor

@bors bors commented on 87b6129 Jul 5, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 8c50ee3

Please sign in to comment.