Skip to content

Commit 87bc22f

Browse files
committed
auto merge of #16177 : nham/rust/collections_15294_eq_ord, r=alexcrichton
This implements: - Eq and Ord for DList, RingBuf, TreeMap and TreeSet - FromIterator and Extendable for BitvSet cc #15294
2 parents 5bad333 + a043814 commit 87bc22f

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/libcollections/bitv.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,21 @@ impl Default for BitvSet {
978978
fn default() -> BitvSet { BitvSet::new() }
979979
}
980980

981+
impl FromIterator<bool> for BitvSet {
982+
fn from_iter<I:Iterator<bool>>(iterator: I) -> BitvSet {
983+
let mut ret = BitvSet::new();
984+
ret.extend(iterator);
985+
ret
986+
}
987+
}
988+
989+
impl Extendable<bool> for BitvSet {
990+
#[inline]
991+
fn extend<I: Iterator<bool>>(&mut self, iterator: I) {
992+
self.get_mut_ref().extend(iterator);
993+
}
994+
}
995+
981996
impl BitvSet {
982997
/// Create a new bit vector set with initially no contents.
983998
///
@@ -1958,6 +1973,17 @@ mod tests {
19581973
assert_eq!(bitv.to_string().as_slice(), "1011");
19591974
}
19601975

1976+
#[test]
1977+
fn test_bitv_set_from_bools() {
1978+
let bools = vec![true, false, true, true];
1979+
let a: BitvSet = bools.iter().map(|n| *n).collect();
1980+
let mut b = BitvSet::new();
1981+
b.insert(0);
1982+
b.insert(2);
1983+
b.insert(3);
1984+
assert_eq!(a, b);
1985+
}
1986+
19611987
#[test]
19621988
fn test_to_bools() {
19631989
let bools = vec!(false, false, true, false, false, true, true, false);
@@ -1977,7 +2003,7 @@ mod tests {
19772003
#[test]
19782004
fn test_bitv_set_iterator() {
19792005
let bools = [true, false, true, true];
1980-
let bitv = BitvSet::from_bitv(bools.iter().map(|n| *n).collect());
2006+
let bitv: BitvSet = bools.iter().map(|n| *n).collect();
19812007

19822008
let idxs: Vec<uint> = bitv.iter().collect();
19832009
assert_eq!(idxs, vec!(0, 2, 3));

src/libcollections/dlist.rs

+9
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,21 @@ impl<A: PartialEq> PartialEq for DList<A> {
683683
}
684684
}
685685

686+
impl<A: Eq> Eq for DList<A> {}
687+
686688
impl<A: PartialOrd> PartialOrd for DList<A> {
687689
fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
688690
iter::order::partial_cmp(self.iter(), other.iter())
689691
}
690692
}
691693

694+
impl<A: Ord> Ord for DList<A> {
695+
#[inline]
696+
fn cmp(&self, other: &DList<A>) -> Ordering {
697+
iter::order::cmp(self.iter(), other.iter())
698+
}
699+
}
700+
692701
impl<A: Clone> Clone for DList<A> {
693702
fn clone(&self) -> DList<A> {
694703
self.iter().map(|x| x.clone()).collect()

src/libcollections/ringbuf.rs

+9
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,21 @@ impl<A: PartialEq> PartialEq for RingBuf<A> {
452452
}
453453
}
454454

455+
impl<A: Eq> Eq for RingBuf<A> {}
456+
455457
impl<A: PartialOrd> PartialOrd for RingBuf<A> {
456458
fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
457459
iter::order::partial_cmp(self.iter(), other.iter())
458460
}
459461
}
460462

463+
impl<A: Ord> Ord for RingBuf<A> {
464+
#[inline]
465+
fn cmp(&self, other: &RingBuf<A>) -> Ordering {
466+
iter::order::cmp(self.iter(), other.iter())
467+
}
468+
}
469+
461470
impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
462471
fn hash(&self, state: &mut S) {
463472
self.len().hash(state);

src/libcollections/treemap.rs

+18
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,22 @@ impl<K: PartialEq + Ord, V: PartialEq> PartialEq for TreeMap<K, V> {
173173
}
174174
}
175175

176+
impl<K: Eq + Ord, V: Eq> Eq for TreeMap<K, V> {}
177+
176178
impl<K: Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> {
177179
#[inline]
178180
fn partial_cmp(&self, other: &TreeMap<K, V>) -> Option<Ordering> {
179181
iter::order::partial_cmp(self.iter(), other.iter())
180182
}
181183
}
182184

185+
impl<K: Ord, V: Ord> Ord for TreeMap<K, V> {
186+
#[inline]
187+
fn cmp(&self, other: &TreeMap<K, V>) -> Ordering {
188+
iter::order::cmp(self.iter(), other.iter())
189+
}
190+
}
191+
183192
impl<K: Ord + Show, V: Show> Show for TreeMap<K, V> {
184193
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
185194
try!(write!(f, "{{"));
@@ -1010,13 +1019,22 @@ impl<T: PartialEq + Ord> PartialEq for TreeSet<T> {
10101019
fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map }
10111020
}
10121021

1022+
impl<T: Eq + Ord> Eq for TreeSet<T> {}
1023+
10131024
impl<T: Ord> PartialOrd for TreeSet<T> {
10141025
#[inline]
10151026
fn partial_cmp(&self, other: &TreeSet<T>) -> Option<Ordering> {
10161027
self.map.partial_cmp(&other.map)
10171028
}
10181029
}
10191030

1031+
impl<T: Ord> Ord for TreeSet<T> {
1032+
#[inline]
1033+
fn cmp(&self, other: &TreeSet<T>) -> Ordering {
1034+
iter::order::cmp(self.iter(), other.iter())
1035+
}
1036+
}
1037+
10201038
impl<T: Ord + Show> Show for TreeSet<T> {
10211039
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10221040
try!(write!(f, "{{"));

0 commit comments

Comments
 (0)