Skip to content

Commit 7375f4d

Browse files
committed
auto merge of #16038 : nham/rust/collections_partialord, r=alexcrichton
cc #15294
2 parents 23466b0 + 8ebd58c commit 7375f4d

File tree

3 files changed

+172
-2
lines changed

3 files changed

+172
-2
lines changed

src/libcollections/bitv.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use core::cmp;
6767
use core::default::Default;
6868
use core::fmt;
6969
use core::iter::Take;
70+
use core::iter;
7071
use core::ops::Index;
7172
use core::slice;
7273
use core::uint;
@@ -830,6 +831,20 @@ impl Clone for Bitv {
830831
}
831832
}
832833

834+
impl PartialOrd for Bitv {
835+
#[inline]
836+
fn partial_cmp(&self, other: &Bitv) -> Option<Ordering> {
837+
iter::order::partial_cmp(self.iter(), other.iter())
838+
}
839+
}
840+
841+
impl Ord for Bitv {
842+
#[inline]
843+
fn cmp(&self, other: &Bitv) -> Ordering {
844+
iter::order::cmp(self.iter(), other.iter())
845+
}
846+
}
847+
833848
impl fmt::Show for Bitv {
834849
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
835850
for bit in self.iter() {
@@ -955,7 +970,7 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
955970
/// assert!(bv.eq_vec([true, true, false, true,
956971
/// false, false, false, false]));
957972
/// ```
958-
#[deriving(Clone, PartialEq, Eq)]
973+
#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
959974
pub struct BitvSet(Bitv);
960975

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

2207+
#[test]
2208+
fn test_bitv_lt() {
2209+
let mut a = Bitv::with_capacity(5u, false);
2210+
let mut b = Bitv::with_capacity(5u, false);
2211+
2212+
assert!(!(a < b) && !(b < a));
2213+
b.set(2, true);
2214+
assert!(a < b);
2215+
a.set(3, true);
2216+
assert!(a < b);
2217+
a.set(2, true);
2218+
assert!(!(a < b) && b < a);
2219+
b.set(0, true);
2220+
assert!(a < b);
2221+
}
2222+
2223+
#[test]
2224+
fn test_ord() {
2225+
let mut a = Bitv::with_capacity(5u, false);
2226+
let mut b = Bitv::with_capacity(5u, false);
2227+
2228+
assert!(a <= b && a >= b);
2229+
a.set(1, true);
2230+
assert!(a > b && a >= b);
2231+
assert!(b < a && b <= a);
2232+
b.set(1, true);
2233+
b.set(2, true);
2234+
assert!(b > a && b >= a);
2235+
assert!(a < b && a <= b);
2236+
}
2237+
21922238
#[test]
21932239
fn test_bitv_clone() {
21942240
let mut a = BitvSet::new();

src/libcollections/smallintmap.rs

+46
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,20 @@ impl<V:Clone> SmallIntMap<V> {
373373
}
374374
}
375375

376+
impl<V: PartialOrd> PartialOrd for SmallIntMap<V> {
377+
#[inline]
378+
fn partial_cmp(&self, other: &SmallIntMap<V>) -> Option<Ordering> {
379+
iter::order::partial_cmp(self.iter(), other.iter())
380+
}
381+
}
382+
383+
impl<V: Ord> Ord for SmallIntMap<V> {
384+
#[inline]
385+
fn cmp(&self, other: &SmallIntMap<V>) -> Ordering {
386+
iter::order::cmp(self.iter(), other.iter())
387+
}
388+
}
389+
376390
impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {
377391
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
378392
try!(write!(f, "{{"));
@@ -770,6 +784,38 @@ mod test_map {
770784
assert!(a == b);
771785
}
772786

787+
#[test]
788+
fn test_lt() {
789+
let mut a = SmallIntMap::new();
790+
let mut b = SmallIntMap::new();
791+
792+
assert!(!(a < b) && !(b < a));
793+
assert!(b.insert(2u, 5i));
794+
assert!(a < b);
795+
assert!(a.insert(2, 7));
796+
assert!(!(a < b) && b < a);
797+
assert!(b.insert(1, 0));
798+
assert!(b < a);
799+
assert!(a.insert(0, 6));
800+
assert!(a < b);
801+
assert!(a.insert(6, 2));
802+
assert!(a < b && !(b < a));
803+
}
804+
805+
#[test]
806+
fn test_ord() {
807+
let mut a = SmallIntMap::new();
808+
let mut b = SmallIntMap::new();
809+
810+
assert!(a <= b && a >= b);
811+
assert!(a.insert(1u, 1i));
812+
assert!(a > b && a >= b);
813+
assert!(b < a && b <= a);
814+
assert!(b.insert(2, 2));
815+
assert!(b > a && b >= a);
816+
assert!(a < b && a <= b);
817+
}
818+
773819
#[test]
774820
fn test_hash() {
775821
let mut x = SmallIntMap::new();

src/libcollections/trie.rs

+79-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ impl<T: PartialEq> PartialEq for TrieMap<T> {
9393

9494
impl<T: Eq> Eq for TrieMap<T> {}
9595

96+
impl<T: PartialOrd> PartialOrd for TrieMap<T> {
97+
#[inline]
98+
fn partial_cmp(&self, other: &TrieMap<T>) -> Option<Ordering> {
99+
iter::order::partial_cmp(self.iter(), other.iter())
100+
}
101+
}
102+
103+
impl<T: Ord> Ord for TrieMap<T> {
104+
#[inline]
105+
fn cmp(&self, other: &TrieMap<T>) -> Ordering {
106+
iter::order::cmp(self.iter(), other.iter())
107+
}
108+
}
109+
96110
impl<T: Show> Show for TrieMap<T> {
97111
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98112
try!(write!(f, "{{"));
@@ -517,7 +531,7 @@ impl<S: Writer, T: Hash<S>> Hash<S> for TrieMap<T> {
517531
/// set.clear();
518532
/// assert!(set.is_empty());
519533
/// ```
520-
#[deriving(Clone, Hash, PartialEq, Eq)]
534+
#[deriving(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
521535
pub struct TrieSet {
522536
map: TrieMap<()>
523537
}
@@ -1309,6 +1323,38 @@ mod test_map {
13091323
assert!(a == b);
13101324
}
13111325

1326+
#[test]
1327+
fn test_lt() {
1328+
let mut a = TrieMap::new();
1329+
let mut b = TrieMap::new();
1330+
1331+
assert!(!(a < b) && !(b < a));
1332+
assert!(b.insert(2u, 5i));
1333+
assert!(a < b);
1334+
assert!(a.insert(2, 7));
1335+
assert!(!(a < b) && b < a);
1336+
assert!(b.insert(1, 0));
1337+
assert!(b < a);
1338+
assert!(a.insert(0, 6));
1339+
assert!(a < b);
1340+
assert!(a.insert(6, 2));
1341+
assert!(a < b && !(b < a));
1342+
}
1343+
1344+
#[test]
1345+
fn test_ord() {
1346+
let mut a = TrieMap::new();
1347+
let mut b = TrieMap::new();
1348+
1349+
assert!(a <= b && a >= b);
1350+
assert!(a.insert(1u, 1i));
1351+
assert!(a > b && a >= b);
1352+
assert!(b < a && b <= a);
1353+
assert!(b.insert(2, 2));
1354+
assert!(b > a && b >= a);
1355+
assert!(a < b && a <= b);
1356+
}
1357+
13121358
#[test]
13131359
fn test_hash() {
13141360
let mut x = TrieMap::new();
@@ -1513,4 +1559,36 @@ mod test_set {
15131559

15141560
assert!(a.clone() == a);
15151561
}
1562+
1563+
#[test]
1564+
fn test_lt() {
1565+
let mut a = TrieSet::new();
1566+
let mut b = TrieSet::new();
1567+
1568+
assert!(!(a < b) && !(b < a));
1569+
assert!(b.insert(2u));
1570+
assert!(a < b);
1571+
assert!(a.insert(3u));
1572+
assert!(!(a < b) && b < a);
1573+
assert!(b.insert(1));
1574+
assert!(b < a);
1575+
assert!(a.insert(0));
1576+
assert!(a < b);
1577+
assert!(a.insert(6));
1578+
assert!(a < b && !(b < a));
1579+
}
1580+
1581+
#[test]
1582+
fn test_ord() {
1583+
let mut a = TrieSet::new();
1584+
let mut b = TrieSet::new();
1585+
1586+
assert!(a <= b && a >= b);
1587+
assert!(a.insert(1u));
1588+
assert!(a > b && a >= b);
1589+
assert!(b < a && b <= a);
1590+
assert!(b.insert(2u));
1591+
assert!(b > a && b >= a);
1592+
assert!(a < b && a <= b);
1593+
}
15161594
}

0 commit comments

Comments
 (0)