pub struct NumericalMultiset<T> { /* private fields */ }Expand description
An ordered multiset implementation for primitive number types based on sparse histograms.
You can learn more about the design rationale and overall capabilities of this data structure in the crate-level documentation.
At the time of writing, this data structure is based on the standard
library’s BTreeMap, and many points of the BTreeMap documentation
also apply to it. In particular, it is a logic error to modify the order of
values stored inside of the multiset using internal mutability tricks.
In all the following documentation, we will use the following terminology:
- “values” refers to a unique value as defined by equality of the
Eqimplementation of typeT - “elements” refers to possibly duplicate occurences of a value within the multiset.
- “multiplicity” refers to the number of occurences of a value within the multiset, i.e. the number of elements that are equal to this value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
// Create a multiset
let mut set = NumericalMultiset::new();
// Inserting elements that do not exist yet is handled much like a standard
// library set type, except we return an Option instead of a boolean...
assert!(set.insert(123).is_none());
assert!(set.insert(456).is_none());
// ...which allows us to report the number of pre-existing elements, if any
assert_eq!(set.insert(123), NonZeroUsize::new(1));
// It is possible to query the minimal and maximal elements cheaply, along
// with their multiplicity within the multiset.
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.first(), Some((123, nonzero(2))));
assert_eq!(set.last(), Some((456, nonzero(1))));
// ...and it is more generally possible to iterate over elements in order,
// from the smallest to the largest:
for (elem, multiplicity) in &set {
println!("{elem} with multiplicity {multiplicity}");
}Implementations§
Source§impl<T> NumericalMultiset<T>
impl<T> NumericalMultiset<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Makes a new, empty NumericalMultiset.
Does not allocate anything on its own.
§Examples
use numerical_multiset::NumericalMultiset;
let mut set: NumericalMultiset<i32> = NumericalMultiset::new();Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the multiset, removing all elements.
§Examples
use numerical_multiset::NumericalMultiset;
let mut v = NumericalMultiset::from_iter([1, 2, 3]);
v.clear();
assert!(v.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Number of elements currently present in the multiset, including duplicate occurences of a value.
See also num_values() for a count of distinct
values, ignoring duplicate elements.
§Examples
use numerical_multiset::NumericalMultiset;
let mut v = NumericalMultiset::new();
assert_eq!(v.len(), 0);
v.insert(1);
assert_eq!(v.len(), 1);
v.insert(1);
assert_eq!(v.len(), 2);
v.insert(2);
assert_eq!(v.len(), 3);Sourcepub fn num_values(&self) -> usize
pub fn num_values(&self) -> usize
Number of distinct values currently present in the multiset
See also len() for a count of multiset elements,
including duplicates of each value.
§Examples
use numerical_multiset::NumericalMultiset;
let mut v = NumericalMultiset::new();
assert_eq!(v.num_values(), 0);
v.insert(1);
assert_eq!(v.num_values(), 1);
v.insert(1);
assert_eq!(v.num_values(), 1);
v.insert(2);
assert_eq!(v.num_values(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Truth that the multiset contains no elements
§Examples
use numerical_multiset::NumericalMultiset;
let mut v = NumericalMultiset::new();
assert!(v.is_empty());
v.insert(1);
assert!(!v.is_empty());Sourcepub fn into_values(
self,
) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator + FusedIterator
pub fn into_values( self, ) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator + FusedIterator
Creates a consuming iterator visiting all the distinct values, in sorted order. The multiset cannot be used after calling this method.
Call into_iter() for a variation of this
iterator that additionally tells how many occurences of each value were
present in the multiset, in the usual (value, multiplicity) format.
§Examples
use numerical_multiset::NumericalMultiset;
let set = NumericalMultiset::from_iter([3, 1, 2, 2]);
assert!(set.into_values().eq([1, 2, 3]));Source§impl<T: Copy> NumericalMultiset<T>
impl<T: Copy> NumericalMultiset<T>
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Iterator over all distinct values in the multiset, along with their multiplicities. Output is sorted by ascending value.
See also values() for a more efficient alternative if
you do not need to know how many occurences of each value are present.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let set = NumericalMultiset::from_iter([3, 1, 2, 2]);
let mut iter = set.iter();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(iter.next(), Some((1, nonzero(1))));
assert_eq!(iter.next(), Some((2, nonzero(2))));
assert_eq!(iter.next(), Some((3, nonzero(1))));
assert_eq!(iter.next(), None);Sourcepub fn values(
&self,
) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator + FusedIterator + Clone
pub fn values( &self, ) -> impl DoubleEndedIterator<Item = T> + ExactSizeIterator + FusedIterator + Clone
Iterator over all distinct values in the multiset. Output is sorted by ascending value.
See also iter() if you need to know how many occurences
of each value are present in the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
let set = NumericalMultiset::from_iter([3, 1, 2, 2]);
let mut iter = set.values();
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);Source§impl<T: Ord> NumericalMultiset<T>
impl<T: Ord> NumericalMultiset<T>
Sourcepub fn contains(&self, value: T) -> bool
pub fn contains(&self, value: T) -> bool
Returns true if the multiset contains at least one occurence of a
value.
See also multiplicity() if you need to know how
many occurences of a value are present inside of the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
let set = NumericalMultiset::from_iter([1, 2, 2]);
assert_eq!(set.contains(1), true);
assert_eq!(set.contains(2), true);
assert_eq!(set.contains(3), false);Sourcepub fn multiplicity(&self, value: T) -> Option<NonZeroUsize>
pub fn multiplicity(&self, value: T) -> Option<NonZeroUsize>
Returns the number of occurences of a value inside of the multiset, or
None if this value is not present.
See also contains() for a more efficient alternative
if you only need to know whether at least one occurence of value is
present inside of the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let set = NumericalMultiset::from_iter([1, 2, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.multiplicity(1), Some(nonzero(1)));
assert_eq!(set.multiplicity(2), Some(nonzero(2)));
assert_eq!(set.multiplicity(3), None);Sourcepub fn is_disjoint(&self, other: &Self) -> bool
pub fn is_disjoint(&self, other: &Self) -> bool
Returns true if self has no elements in common with other. This is
logically equivalent to checking for an empty intersection, but may be
more efficient.
§Examples
use numerical_multiset::NumericalMultiset;
let a = NumericalMultiset::from_iter([1, 2, 2]);
let mut b = NumericalMultiset::new();
assert!(a.is_disjoint(&b));
b.insert(3);
assert!(a.is_disjoint(&b));
b.insert(2);
assert!(!a.is_disjoint(&b));Sourcepub fn is_subset(&self, other: &Self) -> bool
pub fn is_subset(&self, other: &Self) -> bool
Returns true if the set is a subset of another, i.e., other contains
at least all the elements in self.
In a multiset context, this means that if self contains N occurences
of a certain value, then other must contain at least N occurences of
that value.
§Examples
use numerical_multiset::NumericalMultiset;
let sup = NumericalMultiset::from_iter([1, 2, 2]);
let mut set = NumericalMultiset::new();
assert!(set.is_subset(&sup));
set.insert(2);
assert!(set.is_subset(&sup));
set.insert(2);
assert!(set.is_subset(&sup));
set.insert(2);
assert!(!set.is_subset(&sup));Sourcepub fn is_superset(&self, other: &Self) -> bool
pub fn is_superset(&self, other: &Self) -> bool
Returns true if the set is a superset of another, i.e., self
contains at least all the elements in other.
In a multiset context, this means that if other contains N occurences
of a certain value, then self must contain at least N occurences of
that value.
§Examples
use numerical_multiset::NumericalMultiset;
let sub = NumericalMultiset::from_iter([1, 2, 2]);
let mut set = NumericalMultiset::new();
assert!(!set.is_superset(&sub));
set.insert(3);
set.insert(1);
assert!(!set.is_superset(&sub));
set.insert(2);
assert!(!set.is_superset(&sub));
set.insert(2);
assert!(set.is_superset(&sub));Sourcepub fn pop_all_first(&mut self) -> Option<(T, NonZeroUsize)>
pub fn pop_all_first(&mut self) -> Option<(T, NonZeroUsize)>
Remove all occurences of the smallest value from the multiset, if any.
Returns the former smallest value along with the number of occurences of this value that were previously present in the multiset.
See also pop_first() if you only want to remove one
occurence of the smallest value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.pop_all_first(), Some((1, nonzero(2))));
assert_eq!(set.pop_all_first(), Some((2, nonzero(1))));
assert_eq!(set.pop_all_first(), None);Sourcepub fn pop_all_last(&mut self) -> Option<(T, NonZeroUsize)>
pub fn pop_all_last(&mut self) -> Option<(T, NonZeroUsize)>
Remove all occurences of the largest value from the multiset, if any
Returns the former largest value along with the number of occurences of this value that were previously present in the multiset.
See also pop_last() if you only want to remove one
occurence of the largest value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.pop_all_last(), Some((2, nonzero(1))));
assert_eq!(set.pop_all_last(), Some((1, nonzero(2))));
assert_eq!(set.pop_all_last(), None);Sourcepub fn insert(&mut self, value: T) -> Option<NonZeroUsize>
pub fn insert(&mut self, value: T) -> Option<NonZeroUsize>
Insert an element into the multiset, tell how many identical elements were already present in the multiset before insertion.
See also insert_multiple() if you need to
insert multiple copies of a value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.insert(1), None);
assert_eq!(set.insert(1), Some(nonzero(1)));
assert_eq!(set.insert(1), Some(nonzero(2)));
assert_eq!(set.insert(2), None);
assert_eq!(set.len(), 4);
assert_eq!(set.num_values(), 2);Sourcepub fn insert_multiple(
&mut self,
value: T,
count: NonZeroUsize,
) -> Option<NonZeroUsize>
pub fn insert_multiple( &mut self, value: T, count: NonZeroUsize, ) -> Option<NonZeroUsize>
Insert multiple copies of a value, tell how many identical elements were already present in the multiset.
This method is typically used when transferring all copies of a value from one multiset to another.
See also insert() for a convenience shortcut in cases
where you only need to insert one copy of a value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.insert_multiple(1, nonzero(2)), None);
assert_eq!(set.insert_multiple(1, nonzero(3)), Some(nonzero(2)));
assert_eq!(set.insert_multiple(2, nonzero(2)), None);
assert_eq!(set.len(), 7);
assert_eq!(set.num_values(), 2);Sourcepub fn replace_all(
&mut self,
value: T,
count: NonZeroUsize,
) -> Option<NonZeroUsize>
pub fn replace_all( &mut self, value: T, count: NonZeroUsize, ) -> Option<NonZeroUsize>
Insert multiple copies of a value, replacing all occurences of this
value that were previously present in the multiset. Tell how many
occurences of value were previously present in the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.replace_all(1, nonzero(2)), None);
assert_eq!(set.replace_all(1, nonzero(3)), Some(nonzero(2)));
assert_eq!(set.replace_all(2, nonzero(2)), None);
assert_eq!(set.len(), 5);
assert_eq!(set.num_values(), 2);Sourcepub fn remove(&mut self, value: T) -> Option<NonZeroUsize>
pub fn remove(&mut self, value: T) -> Option<NonZeroUsize>
Attempt to remove one element from the multiset, on success tell how many identical elements were previously present in the multiset.
See also remove_all() if you want to remove all
occurences of a value from the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.remove(1), Some(nonzero(2)));
assert_eq!(set.remove(1), Some(nonzero(1)));
assert_eq!(set.remove(1), None);
assert_eq!(set.remove(2), Some(nonzero(1)));
assert_eq!(set.remove(2), None);Sourcepub fn remove_all(&mut self, value: T) -> Option<NonZeroUsize>
pub fn remove_all(&mut self, value: T) -> Option<NonZeroUsize>
Attempt to remove all occurences of a value from the multiset, on success tell how many elements were removed from the multiset.
See also remove() if you only want to remove one
occurence of a value from the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.remove_all(1), Some(nonzero(2)));
assert_eq!(set.remove_all(1), None);
assert_eq!(set.remove_all(2), Some(nonzero(1)));
assert_eq!(set.remove_all(2), None);Sourcepub fn split_off(&mut self, value: T) -> Self
pub fn split_off(&mut self, value: T) -> Self
Splits the collection into two at the specified value.
This returns a new collection with all elements greater than or equal to
value. The multiset on which this method was called will retain all
elements strictly smaller than value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut a = NumericalMultiset::from_iter([1, 2, 2, 3, 3, 3, 4]);
let b = a.split_off(3);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(a.iter().eq([
(1, nonzero(1)),
(2, nonzero(2)),
]));
assert!(b.iter().eq([
(3, nonzero(3)),
(4, nonzero(1)),
]));Source§impl<T: Copy + Ord> NumericalMultiset<T>
impl<T: Copy + Ord> NumericalMultiset<T>
Sourcepub fn range<R>(
&self,
range: R,
) -> impl DoubleEndedIterator<Item = (T, NonZeroUsize)> + FusedIteratorwhere
R: RangeBounds<T>,
pub fn range<R>(
&self,
range: R,
) -> impl DoubleEndedIterator<Item = (T, NonZeroUsize)> + FusedIteratorwhere
R: RangeBounds<T>,
Double-ended iterator over a sub-range of values and their multiplicities
The simplest way is to use the range syntax min..max, thus
range(min..max) will yield values from min (inclusive) to max
(exclusive).
The range may also be entered as (Bound<T>, Bound<T>), so
for example range((Excluded(4), Included(10))) will yield a
left-exclusive, right-inclusive value range from 4 to 10.
§Panics
Panics if range start > end. Panics if range start == end and both
bounds are Excluded.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let set = NumericalMultiset::from_iter([3, 3, 5, 8, 8]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(set.range(4..).eq([
(5, nonzero(1)),
(8, nonzero(2)),
]));Sourcepub fn difference<'a>(
&'a self,
other: &'a Self,
) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
pub fn difference<'a>( &'a self, other: &'a Self, ) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
Visits the elements representing the difference, i.e., those that are in
self but not in other. They are sorted in ascending value order and
emitted in the usual deduplicated (value, multiplicity) format.
The difference is computed element-wise, not value-wise, so if both
self and other contain occurences of a certain value v with
respective multiplicities s and o, then…
- If
selfcontains more occurences ofvthanother(i.e.s > o), then the difference will contains - ooccurences ofv. - Otherwise (if
s <= o) the difference will not contain any occurence ofv.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(a.difference(&b).eq([
(1, nonzero(2)),
(2, nonzero(1)),
]));Sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a Self,
) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
pub fn symmetric_difference<'a>( &'a self, other: &'a Self, ) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
Visits the elements representing the symmetric difference, i.e., those
that are in self or in other but not in both. They are sorted in
ascending value order and emitted in the usual deduplicated (value, multiplicity) format.
The symmetric difference is computed element-wise, not value-wise, so if
both self and other contain occurences of a certain value v with
respective multiplicities s and o, then…
- If
selfcontains as many occurences ofvasother(i.e.s == o), then the symmetric difference will not contain any occurence ofv. - Otherwise (if
s != o) the symmetric difference will contains.abs_diff(o)occurences ofv.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(a.symmetric_difference(&b).eq([
(1, nonzero(2)),
(2, nonzero(1)),
(4, nonzero(1)),
]));Sourcepub fn intersection<'a>(
&'a self,
other: &'a Self,
) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
pub fn intersection<'a>( &'a self, other: &'a Self, ) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
Visits the elements representing the intersection, i.e., those that are
both in self and other. They are sorted in ascending value order and
emitted in the usual deduplicated (value, multiplicity) format.
The intersection is computed element-wise, not value-wise, so if both
self and other contain occurences of a certain value v with
respective multiplicities s and o, then the intersection will
contain s.min(o) occurences of v.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(a.intersection(&b).eq([
(2, nonzero(1)),
(3, nonzero(1)),
]));Sourcepub fn union<'a>(
&'a self,
other: &'a Self,
) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
pub fn union<'a>( &'a self, other: &'a Self, ) -> impl Iterator<Item = (T, NonZeroUsize)> + Clone + 'a
Visits the elements representing the union, i.e., those that are in
either self or other, without counting values that are present in
both multisets twice. They are sorted in ascending value order and
emitted in the usual deduplicated (value, multiplicity) format.
The union is computed element-wise, not value-wise, so if both
self and other contain occurences of a certain value v with
respective multiplicities s and o, then the union will contain
s.max(o) occurences of v.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(a.union(&b).eq([
(1, nonzero(2)),
(2, nonzero(2)),
(3, nonzero(1)),
(4, nonzero(1)),
]));Sourcepub fn first(&self) -> Option<(T, NonZeroUsize)>
pub fn first(&self) -> Option<(T, NonZeroUsize)>
Minimal value present in the multiset, if any, along with its multiplicity.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.first(), None);
set.insert(2);
assert_eq!(set.first(), Some((2, nonzero(1))));
set.insert(2);
assert_eq!(set.first(), Some((2, nonzero(2))));
set.insert(1);
assert_eq!(set.first(), Some((1, nonzero(1))));Sourcepub fn last(&self) -> Option<(T, NonZeroUsize)>
pub fn last(&self) -> Option<(T, NonZeroUsize)>
Maximal value present in the multiset, if any, along with its multiplicity.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(set.last(), None);
set.insert(1);
assert_eq!(set.last(), Some((1, nonzero(1))));
set.insert(1);
assert_eq!(set.last(), Some((1, nonzero(2))));
set.insert(2);
assert_eq!(set.last(), Some((2, nonzero(1))));Sourcepub fn pop_first(&mut self) -> Option<T>
pub fn pop_first(&mut self) -> Option<T>
Remove the smallest element from the multiset.
See also pop_all_first() if you want to remove
all occurences of the smallest value from the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
let mut set = NumericalMultiset::new();
set.insert(1);
set.insert(1);
set.insert(2);
assert_eq!(set.pop_first(), Some(1));
assert_eq!(set.pop_first(), Some(1));
assert_eq!(set.pop_first(), Some(2));
assert_eq!(set.pop_first(), None);Sourcepub fn pop_last(&mut self) -> Option<T>
pub fn pop_last(&mut self) -> Option<T>
Remove the largest element from the multiset.
See also pop_all_last() if you want to remove
all occurences of the smallest value from the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
let mut set = NumericalMultiset::new();
set.insert(1);
set.insert(1);
set.insert(2);
assert_eq!(set.pop_last(), Some(2));
assert_eq!(set.pop_last(), Some(1));
assert_eq!(set.pop_last(), Some(1));
assert_eq!(set.pop_last(), None);Sourcepub fn retain(&mut self, f: impl FnMut(T, NonZeroUsize) -> bool)
pub fn retain(&mut self, f: impl FnMut(T, NonZeroUsize) -> bool)
Retains only the elements specified by the predicate.
For efficiency reasons, the filtering callback f is not run once per
element, but once per distinct value present inside of the multiset.
However, it is also provided with the number of occurences of that value
within the multiset, which can be used as a filtering criterion.
In other words, this method removes all values v with multiplicity m
for which f(v, m) returns false. The values are visited in ascending
order.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::from_iter([1, 1, 2, 3, 4, 4, 5, 5, 5]);
// Keep even values with an even multiplicity
// and odd values with an odd multiplicity.
set.retain(|value, multiplicity| value % 2 == multiplicity.get() % 2);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(set.iter().eq([
(3, nonzero(1)),
(4, nonzero(2)),
(5, nonzero(3)),
]));Sourcepub fn append(&mut self, other: &mut Self)
pub fn append(&mut self, other: &mut Self)
Moves all elements from other into self, leaving other empty.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut a = NumericalMultiset::from_iter([1, 1, 2, 3]);
let mut b = NumericalMultiset::from_iter([3, 3, 4, 5]);
a.append(&mut b);
assert_eq!(a.len(), 8);
assert!(b.is_empty());
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(a.iter().eq([
(1, nonzero(2)),
(2, nonzero(1)),
(3, nonzero(3)),
(4, nonzero(1)),
(5, nonzero(1)),
]));Trait Implementations§
Source§impl<T: Copy + Ord> BitAnd<&NumericalMultiset<T>> for &NumericalMultiset<T>
impl<T: Copy + Ord> BitAnd<&NumericalMultiset<T>> for &NumericalMultiset<T>
Source§fn bitand(self, rhs: &NumericalMultiset<T>) -> Self::Output
fn bitand(self, rhs: &NumericalMultiset<T>) -> Self::Output
Returns the intersection of self and rhs as a new NumericalMultiset<T>.
§Examples
use numerical_multiset::NumericalMultiset;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
assert_eq!(
&a & &b,
NumericalMultiset::from_iter([2, 3])
);Source§type Output = NumericalMultiset<T>
type Output = NumericalMultiset<T>
& operator.Source§impl<T: Copy + Ord> BitOr<&NumericalMultiset<T>> for &NumericalMultiset<T>
impl<T: Copy + Ord> BitOr<&NumericalMultiset<T>> for &NumericalMultiset<T>
Source§fn bitor(self, rhs: &NumericalMultiset<T>) -> Self::Output
fn bitor(self, rhs: &NumericalMultiset<T>) -> Self::Output
Returns the union of self and rhs as a new NumericalMultiset<T>.
§Examples
use numerical_multiset::NumericalMultiset;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
assert_eq!(
&a | &b,
NumericalMultiset::from_iter([1, 1, 2, 2, 3, 4])
);Source§type Output = NumericalMultiset<T>
type Output = NumericalMultiset<T>
| operator.Source§impl<T: Copy + Ord> BitXor<&NumericalMultiset<T>> for &NumericalMultiset<T>
impl<T: Copy + Ord> BitXor<&NumericalMultiset<T>> for &NumericalMultiset<T>
Source§fn bitxor(self, rhs: &NumericalMultiset<T>) -> Self::Output
fn bitxor(self, rhs: &NumericalMultiset<T>) -> Self::Output
Returns the symmetric difference of self and rhs as a new NumericalMultiset<T>.
§Examples
use numerical_multiset::NumericalMultiset;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
assert_eq!(
&a ^ &b,
NumericalMultiset::from_iter([1, 1, 2, 4])
);Source§type Output = NumericalMultiset<T>
type Output = NumericalMultiset<T>
^ operator.Source§impl<T: Clone> Clone for NumericalMultiset<T>
impl<T: Clone> Clone for NumericalMultiset<T>
Source§fn clone(&self) -> NumericalMultiset<T>
fn clone(&self) -> NumericalMultiset<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for NumericalMultiset<T>
impl<T: Debug> Debug for NumericalMultiset<T>
Source§impl<T: Default> Default for NumericalMultiset<T>
impl<T: Default> Default for NumericalMultiset<T>
Source§fn default() -> NumericalMultiset<T>
fn default() -> NumericalMultiset<T>
Source§impl<T: Ord> Extend<(T, NonZero<usize>)> for NumericalMultiset<T>
impl<T: Ord> Extend<(T, NonZero<usize>)> for NumericalMultiset<T>
Source§fn extend<I: IntoIterator<Item = (T, NonZeroUsize)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (T, NonZeroUsize)>>(&mut self, iter: I)
More efficient alternative to Extend<T> for cases where you know in
advance that you are going to insert several copies of a value
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut set = NumericalMultiset::from_iter([1, 2, 3]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
set.extend([(3, nonzero(3)), (4, nonzero(2))]);
assert_eq!(set, NumericalMultiset::from_iter([1, 2, 3, 3, 3, 3, 4, 4]));Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T: Ord> Extend<T> for NumericalMultiset<T>
impl<T: Ord> Extend<T> for NumericalMultiset<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T: Ord> FromIterator<(T, NonZero<usize>)> for NumericalMultiset<T>
impl<T: Ord> FromIterator<(T, NonZero<usize>)> for NumericalMultiset<T>
Source§fn from_iter<I: IntoIterator<Item = (T, NonZeroUsize)>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = (T, NonZeroUsize)>>(iter: I) -> Self
More efficient alternative to FromIterator<T> for cases where you
know in advance that you are going to insert several copies of a value
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(
NumericalMultiset::from_iter([1, 2, 2, 2, 3, 3]),
NumericalMultiset::from_iter([
(1, nonzero(1)),
(2, nonzero(3)),
(3, nonzero(2)),
])
);Source§impl<T: Ord> FromIterator<T> for NumericalMultiset<T>
impl<T: Ord> FromIterator<T> for NumericalMultiset<T>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<T: Hash> Hash for NumericalMultiset<T>
impl<T: Hash> Hash for NumericalMultiset<T>
Source§impl<'a, T: Copy> IntoIterator for &'a NumericalMultiset<T>
impl<'a, T: Copy> IntoIterator for &'a NumericalMultiset<T>
Source§impl<T> IntoIterator for NumericalMultiset<T>
impl<T> IntoIterator for NumericalMultiset<T>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Gets an iterator for moving out the NumericalMultiset’s contents in
ascending order.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let set = NumericalMultiset::from_iter([3, 1, 2, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(set.into_iter().eq([
(1, nonzero(1)),
(2, nonzero(2)),
(3, nonzero(1))
]));Source§impl<T: Ord> Ord for NumericalMultiset<T>
impl<T: Ord> Ord for NumericalMultiset<T>
Source§impl<T: PartialEq> PartialEq for NumericalMultiset<T>
impl<T: PartialEq> PartialEq for NumericalMultiset<T>
Source§impl<T: PartialOrd> PartialOrd for NumericalMultiset<T>
impl<T: PartialOrd> PartialOrd for NumericalMultiset<T>
Source§impl<T: Copy + Ord> Sub<&NumericalMultiset<T>> for &NumericalMultiset<T>
impl<T: Copy + Ord> Sub<&NumericalMultiset<T>> for &NumericalMultiset<T>
Source§fn sub(self, rhs: &NumericalMultiset<T>) -> Self::Output
fn sub(self, rhs: &NumericalMultiset<T>) -> Self::Output
Returns the difference of self and rhs as a new NumericalMultiset<T>.
§Examples
use numerical_multiset::NumericalMultiset;
let a = NumericalMultiset::from_iter([1, 1, 2, 2, 3]);
let b = NumericalMultiset::from_iter([2, 3, 4]);
assert_eq!(
&a - &b,
NumericalMultiset::from_iter([1, 1, 2])
);Source§type Output = NumericalMultiset<T>
type Output = NumericalMultiset<T>
- operator.