Implement PartialOrd for TrieMap and TrieSet

This commit is contained in:
nham 2014-07-27 23:51:28 -04:00
parent 79e9f14abf
commit 16acc10bf9

View File

@ -93,6 +93,13 @@ fn eq(&self, other: &TrieMap<T>) -> bool {
impl<T: Eq> Eq for TrieMap<T> {}
impl<T: PartialOrd> PartialOrd for TrieMap<T> {
#[inline]
fn partial_cmp(&self, other: &TrieMap<T>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
}
}
impl<T: Show> Show for TrieMap<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));
@ -517,7 +524,7 @@ fn hash(&self, state: &mut S) {
/// set.clear();
/// assert!(set.is_empty());
/// ```
#[deriving(Clone, Hash, PartialEq, Eq)]
#[deriving(Clone, Hash, PartialEq, Eq, PartialOrd)]
pub struct TrieSet {
map: TrieMap<()>
}
@ -1309,6 +1316,38 @@ fn test_eq() {
assert!(a == b);
}
#[test]
fn test_lt() {
let mut a = TrieMap::new();
let mut b = TrieMap::new();
assert!(!(a < b) && !(b < a));
assert!(b.insert(2u, 5i));
assert!(a < b);
assert!(a.insert(2, 7));
assert!(!(a < b) && b < a);
assert!(b.insert(1, 0));
assert!(b < a);
assert!(a.insert(0, 6));
assert!(a < b);
assert!(a.insert(6, 2));
assert!(a < b && !(b < a));
}
#[test]
fn test_ord() {
let mut a = TrieMap::new();
let mut b = TrieMap::new();
assert!(a <= b && a >= b);
assert!(a.insert(1u, 1i));
assert!(a > b && a >= b);
assert!(b < a && b <= a);
assert!(b.insert(2, 2));
assert!(b > a && b >= a);
assert!(a < b && a <= b);
}
#[test]
fn test_hash() {
let mut x = TrieMap::new();
@ -1513,4 +1552,36 @@ fn test_clone() {
assert!(a.clone() == a);
}
#[test]
fn test_lt() {
let mut a = TrieSet::new();
let mut b = TrieSet::new();
assert!(!(a < b) && !(b < a));
assert!(b.insert(2u));
assert!(a < b);
assert!(a.insert(3u));
assert!(!(a < b) && b < a);
assert!(b.insert(1));
assert!(b < a);
assert!(a.insert(0));
assert!(a < b);
assert!(a.insert(6));
assert!(a < b && !(b < a));
}
#[test]
fn test_ord() {
let mut a = TrieSet::new();
let mut b = TrieSet::new();
assert!(a <= b && a >= b);
assert!(a.insert(1u));
assert!(a > b && a >= b);
assert!(b < a && b <= a);
assert!(b.insert(2u));
assert!(b > a && b >= a);
assert!(a < b && a <= b);
}
}