Struct multiset::HashMultiSet

source ·
pub struct HashMultiSet<K> { /* private fields */ }
Expand description

A hash-based multiset.

Implementations§

Creates a new empty HashMultiSet.

Examples
use multiset::HashMultiSet;

let multiset: HashMultiSet<char> = HashMultiSet::new();

An iterator visiting all elements in arbitrary order, including each duplicate. The iterator element type is &'a K.

Examples
use multiset::HashMultiSet;
let mut multiset = HashMultiSet::new();
multiset.insert(0);
multiset.insert(0);
multiset.insert(1);

// Will print in an arbitrary order.
for x in multiset.iter() {
    println!("{}", x);
}
assert_eq!(3, multiset.iter().count());

Returns true if the multiset contains no elements.

Examples
use multiset::HashMultiSet;

let mut multiset = HashMultiSet::new();
assert!(multiset.is_empty());
multiset.insert(1);
assert!(!multiset.is_empty());

Returns true if the multiset contains a value.

The value may be any borrowed form of the set’s value type, but Hash and Eq on the borrowed form must match those for the value type.

Examples
use multiset::HashMultiSet;

let set: HashMultiSet<_> = [1, 2, 3].iter().cloned().collect();
assert_eq!(set.contains(&1), true);
assert_eq!(set.contains(&4), false);

Counts all the elements, including each duplicate.

Examples

A new empty HashMultiSet with 0 total elements:

use multiset::HashMultiSet;

let multiset: HashMultiSet<char> = HashMultiSet::new();
assert_eq!(0, multiset.len());

A HashMultiSet from vec![1,1,2] has 3 total elements:

use multiset::HashMultiSet;
use std::iter::FromIterator;

let multiset: HashMultiSet<i32> = FromIterator::from_iter(vec![1,1,2]);
assert_eq!(3, multiset.len());

Returns all the distinct elements in the HashMultiSet.

Examples

A HashMultiSet from vec![1,1,2] has 2 distinct elements, namely 1 and 2, but not 3:

use multiset::HashMultiSet;
use std::collections::HashSet;
use std::iter::FromIterator;

let multiset: HashMultiSet<i64> = FromIterator::from_iter(vec![1,1,2]);
let distinct = multiset.distinct_elements().collect::<HashSet<_>>();
assert_eq!(2, distinct.len());
assert!(distinct.contains(&1));
assert!(distinct.contains(&2));
assert!(!distinct.contains(&3));

Inserts an element.

Examples

Insert 5 into a new HashMultiSet:

use multiset::HashMultiSet;

let mut multiset: HashMultiSet<i32> = HashMultiSet::new();
assert_eq!(0, multiset.count_of(&5));
multiset.insert(5);
assert_eq!(1, multiset.count_of(&5));

Inserts an element n times.

Examples

Insert three 5s into a new HashMultiSet:

use multiset::HashMultiSet;

let mut multiset: HashMultiSet<i32> = HashMultiSet::new();
assert_eq!(0, multiset.count_of(&5));
multiset.insert_times(5,3);
assert_eq!(3, multiset.count_of(&5));

Remove an element. Removal of a nonexistent element has no effect.

Examples

Remove 5 from a new HashMultiSet:

use multiset::HashMultiSet;

let mut multiset: HashMultiSet<i32> = HashMultiSet::new();
multiset.insert(5);
assert_eq!(1, multiset.count_of(&5));
assert!(multiset.remove(&5));
assert_eq!(0, multiset.count_of(&5));
assert!(!multiset.remove(&5));

Remove an element n times. If an element is removed as many or more times than it appears, it is entirely removed from the multiset.

Examples

Remove 5s from a HashMultiSet containing 3 of them.

use multiset::HashMultiSet;

let mut multiset: HashMultiSet<i32> = HashMultiSet::new();
multiset.insert_times(5, 3);
assert!(multiset.count_of(&5) == 3);
assert!(multiset.remove_times(&5, 2) == 2);
assert!(multiset.len() == 1);
assert!(multiset.count_of(&5) == 1);
assert!(multiset.remove_times(&5, 1) == 1);
assert!(multiset.len() == 0);
assert!(multiset.count_of(&5) == 0);
assert!(multiset.remove_times(&5, 1) == 0);
assert!(multiset.count_of(&5) == 0);

Remove all of an element from the multiset.

Examples

Remove all 5s from a HashMultiSet containing 3 of them.

use multiset::HashMultiSet;

let mut multiset: HashMultiSet<i32> = HashMultiSet::new();
multiset.insert_times(5,3);
assert!(multiset.count_of(&5) == 3);
multiset.remove_all(&5);
assert!(multiset.count_of(&5) == 0);

Counts the occurrences of val.

Examples
use multiset::HashMultiSet;

let mut multiset: HashMultiSet<u8> = HashMultiSet::new();
multiset.insert(0);
multiset.insert(0);
multiset.insert(1);
multiset.insert(0);
assert_eq!(3, multiset.count_of(&0));
assert_eq!(1, multiset.count_of(&1));

Trait Implementations§

Combine two HashMultiSets by adding the number of each distinct element.

Examples
use multiset::HashMultiSet;
use std::iter::FromIterator;

let lhs: HashMultiSet<isize> = FromIterator::from_iter(vec![1,2,3]);
let rhs: HashMultiSet<isize> = FromIterator::from_iter(vec![1,1,4]);
let combined = lhs + rhs;
assert_eq!(3, combined.count_of(&1));
assert_eq!(1, combined.count_of(&2));
assert_eq!(1, combined.count_of(&3));
assert_eq!(1, combined.count_of(&4));
assert_eq!(0, combined.count_of(&5));
The resulting type after applying the + operator.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Creates a new HashMultiSet from the elements in an iterable.

Examples

Count occurrences of each char in "hello world":

use multiset::HashMultiSet;
use std::iter::FromIterator;

let vals = vec!['h','e','l','l','o',' ','w','o','r','l','d'];
let multiset: HashMultiSet<char> = FromIterator::from_iter(vals);
assert_eq!(1, multiset.count_of(&'h'));
assert_eq!(3, multiset.count_of(&'l'));
assert_eq!(0, multiset.count_of(&'z'));
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Combine two HashMultiSets by removing elements in the second multiset from the first. As with remove() (and set difference), excess elements in the second multiset are ignored.

Examples
use multiset::HashMultiSet;
use std::iter::FromIterator;

let lhs: HashMultiSet<isize> = FromIterator::from_iter(vec![1,2,3]);
let rhs: HashMultiSet<isize> = FromIterator::from_iter(vec![1,1,4]);
let combined = lhs - rhs;
assert_eq!(0, combined.count_of(&1));
assert_eq!(1, combined.count_of(&2));
assert_eq!(1, combined.count_of(&3));
assert_eq!(0, combined.count_of(&4));
The resulting type after applying the - operator.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.