Skip to content

Implement len() for EnumSet. #18740

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

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 20 additions & 1 deletion src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use core::fmt;

// FIXME(conventions): implement BitXor
// FIXME(contentions): implement union family of methods? (general design may be wrong here)
// FIXME(conventions): implement len

#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// A specialized `Set` implementation to use enum types.
Expand Down Expand Up @@ -92,6 +91,12 @@ impl<E:CLike> EnumSet<E> {
EnumSet {bits: 0}
}

/// Returns the number of elements in the given `EnumSet`.
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn len(&self) -> uint {
self.bits.count_ones()
}

/// Returns true if the `EnumSet` is empty.
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -269,6 +274,20 @@ mod test {
assert_eq!("{A, C}", e.to_string().as_slice());
}

#[test]
fn test_len() {
let mut e = EnumSet::new();
assert_eq!(e.len(), 0);
e.insert(A);
e.insert(B);
e.insert(C);
assert_eq!(e.len(), 3);
e.remove(&A);
assert_eq!(e.len(), 2);
e.clear();
assert_eq!(e.len(), 0);
}

///////////////////////////////////////////////////////////////////////////
// intersect

Expand Down