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
This commit is contained in:
bors 2014-08-02 13:51:09 +00:00
commit 87bc22f587
4 changed files with 63 additions and 1 deletions

View File

@ -978,6 +978,21 @@ impl Default for BitvSet {
fn default() -> BitvSet { BitvSet::new() } fn default() -> BitvSet { BitvSet::new() }
} }
impl FromIterator<bool> for BitvSet {
fn from_iter<I:Iterator<bool>>(iterator: I) -> BitvSet {
let mut ret = BitvSet::new();
ret.extend(iterator);
ret
}
}
impl Extendable<bool> for BitvSet {
#[inline]
fn extend<I: Iterator<bool>>(&mut self, iterator: I) {
self.get_mut_ref().extend(iterator);
}
}
impl BitvSet { impl BitvSet {
/// Create a new bit vector set with initially no contents. /// Create a new bit vector set with initially no contents.
/// ///
@ -1958,6 +1973,17 @@ mod tests {
assert_eq!(bitv.to_string().as_slice(), "1011"); assert_eq!(bitv.to_string().as_slice(), "1011");
} }
#[test]
fn test_bitv_set_from_bools() {
let bools = vec![true, false, true, true];
let a: BitvSet = bools.iter().map(|n| *n).collect();
let mut b = BitvSet::new();
b.insert(0);
b.insert(2);
b.insert(3);
assert_eq!(a, b);
}
#[test] #[test]
fn test_to_bools() { fn test_to_bools() {
let bools = vec!(false, false, true, false, false, true, true, false); let bools = vec!(false, false, true, false, false, true, true, false);
@ -1977,7 +2003,7 @@ mod tests {
#[test] #[test]
fn test_bitv_set_iterator() { fn test_bitv_set_iterator() {
let bools = [true, false, true, true]; let bools = [true, false, true, true];
let bitv = BitvSet::from_bitv(bools.iter().map(|n| *n).collect()); let bitv: BitvSet = bools.iter().map(|n| *n).collect();
let idxs: Vec<uint> = bitv.iter().collect(); let idxs: Vec<uint> = bitv.iter().collect();
assert_eq!(idxs, vec!(0, 2, 3)); assert_eq!(idxs, vec!(0, 2, 3));

View File

@ -683,12 +683,21 @@ impl<A: PartialEq> PartialEq for DList<A> {
} }
} }
impl<A: Eq> Eq for DList<A> {}
impl<A: PartialOrd> PartialOrd for DList<A> { impl<A: PartialOrd> PartialOrd for DList<A> {
fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> { fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter()) iter::order::partial_cmp(self.iter(), other.iter())
} }
} }
impl<A: Ord> Ord for DList<A> {
#[inline]
fn cmp(&self, other: &DList<A>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
}
}
impl<A: Clone> Clone for DList<A> { impl<A: Clone> Clone for DList<A> {
fn clone(&self) -> DList<A> { fn clone(&self) -> DList<A> {
self.iter().map(|x| x.clone()).collect() self.iter().map(|x| x.clone()).collect()

View File

@ -452,12 +452,21 @@ impl<A: PartialEq> PartialEq for RingBuf<A> {
} }
} }
impl<A: Eq> Eq for RingBuf<A> {}
impl<A: PartialOrd> PartialOrd for RingBuf<A> { impl<A: PartialOrd> PartialOrd for RingBuf<A> {
fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> { fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter()) iter::order::partial_cmp(self.iter(), other.iter())
} }
} }
impl<A: Ord> Ord for RingBuf<A> {
#[inline]
fn cmp(&self, other: &RingBuf<A>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
}
}
impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> { impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
fn hash(&self, state: &mut S) { fn hash(&self, state: &mut S) {
self.len().hash(state); self.len().hash(state);

View File

@ -173,6 +173,8 @@ impl<K: PartialEq + Ord, V: PartialEq> PartialEq for TreeMap<K, V> {
} }
} }
impl<K: Eq + Ord, V: Eq> Eq for TreeMap<K, V> {}
impl<K: Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> { impl<K: Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> {
#[inline] #[inline]
fn partial_cmp(&self, other: &TreeMap<K, V>) -> Option<Ordering> { fn partial_cmp(&self, other: &TreeMap<K, V>) -> Option<Ordering> {
@ -180,6 +182,13 @@ impl<K: Ord, V: PartialOrd> PartialOrd for TreeMap<K, V> {
} }
} }
impl<K: Ord, V: Ord> Ord for TreeMap<K, V> {
#[inline]
fn cmp(&self, other: &TreeMap<K, V>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
}
}
impl<K: Ord + Show, V: Show> Show for TreeMap<K, V> { impl<K: Ord + Show, V: Show> Show for TreeMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{")); try!(write!(f, "{{"));
@ -1010,6 +1019,8 @@ impl<T: PartialEq + Ord> PartialEq for TreeSet<T> {
fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map } fn eq(&self, other: &TreeSet<T>) -> bool { self.map == other.map }
} }
impl<T: Eq + Ord> Eq for TreeSet<T> {}
impl<T: Ord> PartialOrd for TreeSet<T> { impl<T: Ord> PartialOrd for TreeSet<T> {
#[inline] #[inline]
fn partial_cmp(&self, other: &TreeSet<T>) -> Option<Ordering> { fn partial_cmp(&self, other: &TreeSet<T>) -> Option<Ordering> {
@ -1017,6 +1028,13 @@ impl<T: Ord> PartialOrd for TreeSet<T> {
} }
} }
impl<T: Ord> Ord for TreeSet<T> {
#[inline]
fn cmp(&self, other: &TreeSet<T>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
}
}
impl<T: Ord + Show> Show for TreeSet<T> { impl<T: Ord + Show> Show for TreeSet<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{")); try!(write!(f, "{{"));