diff options
| author | blake2-ppc <blake2-ppc> | 2013-08-06 17:27:39 +0200 |
|---|---|---|
| committer | Corey Richardson <corey@octayn.net> | 2013-08-07 22:41:14 -0400 |
| commit | 4ab05f91f49b1248aab7d21630cfb38d6dafacfa (patch) | |
| tree | ad125a6f111646e96af9f97307e5be61a1083c4a | |
| parent | 240f8f03c9da7e574fd0fce0eb72e6ade97628ff (diff) | |
| download | rust-4ab05f91f49b1248aab7d21630cfb38d6dafacfa.tar.gz rust-4ab05f91f49b1248aab7d21630cfb38d6dafacfa.zip | |
extra: Simplify Eq/Ord in treemap
Write the Eq and Ord impls for TreeMap in a more straightforward way using iterator::Zip
| -rw-r--r-- | src/libextra/treemap.rs | 26 |
1 files changed, 5 insertions, 21 deletions
diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs index ab7d47255da..2e20752754a 100644 --- a/src/libextra/treemap.rs +++ b/src/libextra/treemap.rs @@ -42,39 +42,23 @@ pub struct TreeMap<K, V> { impl<K: Eq + TotalOrd, V: Eq> Eq for TreeMap<K, V> { fn eq(&self, other: &TreeMap<K, V>) -> bool { - if self.len() != other.len() { - false - } else { - let mut x = self.iter(); - let mut y = other.iter(); - for _ in range(0u, self.len()) { - if x.next().unwrap() != y.next().unwrap() { - return false - } - } - true - } + self.len() == other.len() && + self.iter().zip(other.iter()).all(|(a, b)| a == b) } - fn ne(&self, other: &TreeMap<K, V>) -> bool { !self.eq(other) } } // Lexicographical comparison 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 _ in range(0u, num::min(a_len, b_len)) { - let (key_a, value_a) = x.next().unwrap(); - let (key_b, value_b) = y.next().unwrap(); + // the Zip iterator is as long as the shortest of a and b. + for ((key_a, value_a), (key_b, value_b)) in a.iter().zip(b.iter()) { 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 + a.len() < b.len() } impl<K: Ord + TotalOrd, V: Ord> Ord for TreeMap<K, V> { |
