Skip to content
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

collections: Implement more missing traits #16177

Merged
merged 3 commits into from
Aug 2, 2014
Merged
Show file tree
Hide file tree
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
28 changes: 27 additions & 1 deletion src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,21 @@ impl Default for BitvSet {
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 {
/// Create a new bit vector set with initially no contents.
///
Expand Down Expand Up @@ -1958,6 +1973,17 @@ mod tests {
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]
fn test_to_bools() {
let bools = vec!(false, false, true, false, false, true, true, false);
Expand All @@ -1977,7 +2003,7 @@ mod tests {
#[test]
fn test_bitv_set_iterator() {
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();
assert_eq!(idxs, vec!(0, 2, 3));
Expand Down
9 changes: 9 additions & 0 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
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> {
fn clone(&self) -> DList<A> {
self.iter().map(|x| x.clone()).collect()
Expand Down
9 changes: 9 additions & 0 deletions src/libcollections/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
fn partial_cmp(&self, other: &RingBuf<A>) -> Option<Ordering> {
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> {
fn hash(&self, state: &mut S) {
self.len().hash(state);
Expand Down
18 changes: 18 additions & 0 deletions src/libcollections/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,22 @@ 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> {
#[inline]
fn partial_cmp(&self, other: &TreeMap<K, V>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
}
}

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> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));
Expand Down Expand Up @@ -1010,13 +1019,22 @@ impl<T: PartialEq + Ord> PartialEq for TreeSet<T> {
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> {
#[inline]
fn partial_cmp(&self, other: &TreeSet<T>) -> Option<Ordering> {
self.map.partial_cmp(&other.map)
}
}

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> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));
Expand Down