HashMultiSet

Struct HashMultiSet 

Source
pub struct HashMultiSet<K>
where K: Eq + Hash,
{ /* private fields */ }
Expand description

A hash-based multiset.

Implementations§

Source§

impl<K> HashMultiSet<K>
where K: Eq + Hash,

Source

pub fn new() -> Self

Creates a new empty HashMultiSet.

§Examples
use modern_multiset::HashMultiSet;

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

pub fn iter(&self) -> Iter<'_, K>

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

§Examples
use modern_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());
Source

pub fn is_empty(&self) -> bool

Returns true if the multiset contains no elements.

§Examples
use modern_multiset::HashMultiSet;

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

pub fn contains<Q>(&self, value: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

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 modern_multiset::HashMultiSet;

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

pub fn len(&self) -> usize

Counts all the elements, including each duplicate.

§Examples

A new empty HashMultiSet with 0 total elements:

use modern_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 modern_multiset::HashMultiSet;
use std::iter::FromIterator;

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

pub fn distinct_elements(&self) -> Keys<'_, K, usize>

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 modern_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));
Source

pub fn insert(&mut self, val: K)

Inserts an element.

§Examples

Insert 5 into a new HashMultiSet:

use modern_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));
Source

pub fn insert_times(&mut self, val: K, n: usize)

Inserts an element n times.

§Examples

Insert three 5s into a new HashMultiSet:

use modern_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));
Source

pub fn remove(&mut self, val: &K) -> bool

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

§Examples

Remove 5 from a new HashMultiSet:

use modern_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));
Source

pub fn remove_times(&mut self, val: &K, times: usize) -> usize

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 modern_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);
Source

pub fn remove_all(&mut self, val: &K)

Remove all of an element from the multiset.

§Examples

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

use modern_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);
assert!(multiset.len() == 0);
Source

pub fn count_of(&self, val: &K) -> usize

Counts the occurrences of val.

§Examples
use modern_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§

Source§

impl<T> Add for HashMultiSet<T>
where T: Eq + Hash + Clone,

Source§

fn add(self, rhs: HashMultiSet<T>) -> HashMultiSet<T>

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

§Examples
use modern_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));
Source§

type Output = HashMultiSet<T>

The resulting type after applying the + operator.
Source§

impl<K> Clone for HashMultiSet<K>
where K: Eq + Hash + Clone,

Source§

fn clone(&self) -> HashMultiSet<K>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for HashMultiSet<T>
where T: Eq + Hash + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K> Default for HashMultiSet<K>
where K: Eq + Hash + Default,

Source§

fn default() -> HashMultiSet<K>

Returns the “default value” for a type. Read more
Source§

impl<A> FromIterator<A> for HashMultiSet<A>
where A: Eq + Hash,

Source§

fn from_iter<T>(iterable: T) -> HashMultiSet<A>
where T: IntoIterator<Item = A>,

Creates a new HashMultiSet from the elements in an iterable.

§Examples

Count occurrences of each char in "hello world":

use modern_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'));
Source§

impl<T> PartialEq for HashMultiSet<T>
where T: Eq + Hash,

Source§

fn eq(&self, other: &HashMultiSet<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Sub for HashMultiSet<T>
where T: Eq + Hash + Clone,

Source§

fn sub(self, rhs: HashMultiSet<T>) -> HashMultiSet<T>

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 modern_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));
Source§

type Output = HashMultiSet<T>

The resulting type after applying the - operator.
Source§

impl<K> Eq for HashMultiSet<K>
where K: Eq + Hash + Eq,

Auto Trait Implementations§

§

impl<K> Freeze for HashMultiSet<K>

§

impl<K> RefUnwindSafe for HashMultiSet<K>
where K: RefUnwindSafe,

§

impl<K> Send for HashMultiSet<K>
where K: Send,

§

impl<K> Sync for HashMultiSet<K>
where K: Sync,

§

impl<K> Unpin for HashMultiSet<K>
where K: Unpin,

§

impl<K> UnwindSafe for HashMultiSet<K>
where K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.