Struct mexset::MexSet [−][src]
Expand description
The MexSet data structure is an implementation of a set that can quickly find MEX.
The MEX (Minimum EXcluded) of a subset of the set of natural numbers is the minimum natural number missing from this subset. That is, it is the minimum value of the complement set.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); assert_eq!(set.minimum_excluded(), 0); // The MEX of an empty set is 0 set.insert(2); set.insert(1); assert_eq!(set.minimum_excluded(), 0); // 0 is the smallest missing number set.insert(0); assert_eq!(set.minimum_excluded(), 3); // mex({0, 1, 2}) = 3 set.insert(4); assert_eq!(set.minimum_excluded(), 3); // mex({0, 1, 2, 4}) = 3 set.insert(3); assert_eq!(set.minimum_excluded(), 5); // mex({0, 1, 2, 3, 4}) = 5 set.remove(&1); assert_eq!(set.minimum_excluded(), 1); // mex({0, 2, 3, 4}) = 1
The insert
, remove
and minimum_excluded
methods have a runtime of O(log(N)),
where N is the number of elements in the MexSet.
If you are interested in learning more about MEX, I suggest you watch this video.
Implementations
Get the number of unique elements in a set.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); assert_eq!(set.len(), 0); set.insert(1); set.insert(1); set.insert(2); assert_eq!(set.len(), 2);
Returns true
if the set contains no elements.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); assert!(set.is_empty()); set.insert(1); assert!(!set.is_empty());
Clear the MexSet.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); set.insert(1); set.insert(2); set.insert(3); set.clear(); assert_eq!(set.len(), 0);
Adds a value to the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); assert!(set.insert(1)); assert!(!set.insert(1)); assert_eq!(set.len(), 1);
Find set’s MEX.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); assert_eq!(set.minimum_excluded(), 0); // { } set.extend(vec![0, 1, 2].into_iter()); assert_eq!(set.minimum_excluded(), 3); // {0, 1, 2, _ } set.extend(vec![4, 5].into_iter()); assert_eq!(set.minimum_excluded(), 3); // {0, 1, 2, _ , 4, 5} set.insert(3); assert_eq!(set.minimum_excluded(), 6); // {0, 1, 2, 3, 4, 5, _ }
Returns true if the set contains a value.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); assert!(!set.contains(&1)); set.insert(1); assert!(set.contains(&1));
Removes a value from the set. Returns whether the value was present in the set.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); set.insert(1); assert_eq!(set.remove(&1), true); assert_eq!(set.remove(&1), false);
An iterator visiting all elements in arbitrary order.
The iterator element type is &'a T
.
Examples
use mexset::MexSet; use std::collections::HashSet; let mut set: MexSet<u32> = MexSet::new(); set.insert(1); set.insert(2); assert_eq!( set.iter().collect::<HashSet<&u32>>(), vec![&1, &2].into_iter().collect::<HashSet<&u32>>(), );
Create a MexSet with elements [0; n).
This function is a much faster alternative to sequential insert()
calls.
Examples
use mexset::MexSet; assert_eq!( MexSet::<u32>::with_range(5), vec![0, 1, 2, 3, 4].into_iter().collect::<MexSet<u32>>(), );
Trait Implementations
Extend the MexSet with elements from an iterator.
Examples
use mexset::MexSet; let mut set: MexSet<u32> = MexSet::new(); set.extend(vec![2, 3, 5].into_iter()); assert!(set.contains(&2)); assert!(set.contains(&3)); assert!(set.contains(&5));
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Сreating a MexSet from an iterator.
Examples
use mexset::MexSet; use std::iter::FromIterator; let set: MexSet<u32> = MexSet::from_iter(vec![2, 3, 5].into_iter()); assert!(set.contains(&2)); assert!(set.contains(&3)); assert!(set.contains(&5)); let set: MexSet<u32> = vec![5, 3, 2].into_iter().collect(); assert!(set.contains(&2)); assert!(set.contains(&3)); assert!(set.contains(&5));
Creates a consuming iterator, that is, one that moves each value out of the set in arbitrary order. The set cannot be used after calling this.
Examples
use mexset::MexSet; use std::collections::HashSet; let mut set: MexSet<u32> = MexSet::new(); set.insert(1); set.insert(2); assert_eq!( set.into_iter().collect::<HashSet<u32>>(), vec![1, 2].into_iter().collect::<HashSet<u32>>(), );
type Item = T
type Item = T
The type of the elements being iterated over.
Auto Trait Implementations
impl<T> RefUnwindSafe for MexSet<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for MexSet<T> where
T: RefUnwindSafe + UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more