Skip to content

Implement PartialOrd for TrieMap, TrieSet, SmallIntMap, Bitv, and BitvSet #16038

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 4 commits into from
Jul 29, 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
48 changes: 47 additions & 1 deletion src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@ use core::cmp;
use core::default::Default;
use core::fmt;
use core::iter::Take;
use core::iter;
use core::ops::Index;
use core::slice;
use core::uint;
@@ -830,6 +831,20 @@ impl Clone for Bitv {
}
}

impl PartialOrd for Bitv {
#[inline]
fn partial_cmp(&self, other: &Bitv) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
}
}

impl Ord for Bitv {
#[inline]
fn cmp(&self, other: &Bitv) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
}
}

impl fmt::Show for Bitv {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
for bit in self.iter() {
@@ -955,7 +970,7 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
/// assert!(bv.eq_vec([true, true, false, true,
/// false, false, false, false]));
/// ```
#[deriving(Clone, PartialEq, Eq)]
#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct BitvSet(Bitv);

impl Default for BitvSet {
@@ -2189,6 +2204,37 @@ mod tests {
assert_eq!(a.capacity(), uint::BITS);
}

#[test]
fn test_bitv_lt() {
let mut a = Bitv::with_capacity(5u, false);
let mut b = Bitv::with_capacity(5u, false);

assert!(!(a < b) && !(b < a));
b.set(2, true);
assert!(a < b);
a.set(3, true);
assert!(a < b);
a.set(2, true);
assert!(!(a < b) && b < a);
b.set(0, true);
assert!(a < b);
}

#[test]
fn test_ord() {
let mut a = Bitv::with_capacity(5u, false);
let mut b = Bitv::with_capacity(5u, false);

assert!(a <= b && a >= b);
a.set(1, true);
assert!(a > b && a >= b);
assert!(b < a && b <= a);
b.set(1, true);
b.set(2, true);
assert!(b > a && b >= a);
assert!(a < b && a <= b);
}

#[test]
fn test_bitv_clone() {
let mut a = BitvSet::new();
46 changes: 46 additions & 0 deletions src/libcollections/smallintmap.rs
Original file line number Diff line number Diff line change
@@ -373,6 +373,20 @@ impl<V:Clone> SmallIntMap<V> {
}
}

impl<V: PartialOrd> PartialOrd for SmallIntMap<V> {
#[inline]
fn partial_cmp(&self, other: &SmallIntMap<V>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
}
}

impl<V: Ord> Ord for SmallIntMap<V> {
#[inline]
fn cmp(&self, other: &SmallIntMap<V>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
}
}

impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));
@@ -770,6 +784,38 @@ mod test_map {
assert!(a == b);
}

#[test]
fn test_lt() {
let mut a = SmallIntMap::new();
let mut b = SmallIntMap::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 = SmallIntMap::new();
let mut b = SmallIntMap::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 = SmallIntMap::new();
80 changes: 79 additions & 1 deletion src/libcollections/trie.rs
Original file line number Diff line number Diff line change
@@ -93,6 +93,20 @@ impl<T: PartialEq> PartialEq for TrieMap<T> {

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: Ord> Ord for TrieMap<T> {
#[inline]
fn cmp(&self, other: &TrieMap<T>) -> Ordering {
iter::order::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 +531,7 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
/// set.clear();
/// assert!(set.is_empty());
/// ```
#[deriving(Clone, Hash, PartialEq, Eq)]
#[deriving(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct TrieSet {
map: TrieMap<()>
}
@@ -1309,6 +1323,38 @@ mod test_map {
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 +1559,36 @@ mod test_set {

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);
}
}