pub struct NumericalMultiset<T> { /* private fields */ }
Expand description
An ordered multiset of machine numbers.
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.
§Floating-point data
To build multisets of floating-point numbers, you will need to handle the
fact that NaN is unordered. This can be done using one of the Ord
float
wrappers available on crates.io, which work by either asserting absence of
NaNs
or making NaNs
ordered.
For optimal NumericalMultiset
performance, we advise…
- Preferring
NotNan
-like wrappers, whoseOrd
implementation can leverage fast hardware comparisons instead of implementing other ordering semantics in software. - Using them right from the point where your application receives inputs, to
avoid repeatedly checking your inputs for NaNs by having to rebuild such
wrappers every time a number is inserted into a
NumericalMultiset
.
§Terminology
Because multisets can hold multiple occurences of a value, it is useful to have concise wording to distinguish between unique values and (possibly duplicate) occurences of these values.
Throughout this documentation, we will use the following terminology:
- “values” refers to distinct values of type
T
as defined by theEq
implementation ofT
. - “items” 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 items that are equal to this value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
// Create a multiset
let mut mset = NumericalMultiset::new();
// Inserting items is handled much like a standard library set type,
// except we return an Option<NonZeroUsize> instead of a boolean.
assert!(mset.insert(123).is_none());
assert!(mset.insert(456).is_none());
// This allows us to report the number of pre-existing items
// that have the same value, if any.
assert_eq!(mset.insert(123), NonZeroUsize::new(1));
// It is possible to query the minimal and maximal values cheaply, along
// with their multiplicity within the multiset.
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.first(), Some((123, nonzero(2))));
assert_eq!(mset.last(), Some((456, nonzero(1))));
// ...and it is more generally possible to iterate over values and
// multiplicities in order, from the smallest value to the largest one:
for (elem, multiplicity) in &mset {
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 mset = NumericalMultiset::<i32>::new();
assert!(mset.is_empty());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the multiset, removing all items.
§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 items currently present in the multiset, including duplicate occurences of the same value.
See also num_values()
for a count of distinct
values, ignoring duplicates.
§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 items,
including duplicate occurences of the same 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 items
§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 distinct values in the multiset, i.e. the mathematical support of the multiset.
Values are emitted in ascending order, and the multiset cannot be used after calling this method.
Call into_iter()
(from the IntoIterator
trait) to get a variation
of this iterator that additionally tells you how many occurences of each
value were present in the multiset, in the usual (value, multiplicity)
format.
§Examples
use numerical_multiset::NumericalMultiset;
let mset = NumericalMultiset::from_iter([3, 1, 2, 2]);
assert!(mset.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.
Values are emitted in ascending order.
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 mset = NumericalMultiset::from_iter([3, 1, 2, 2]);
let mut iter = mset.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, i.e. the mathematical support of the multiset.
Values are emitted in ascending order.
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 mset = NumericalMultiset::from_iter([3, 1, 2, 2]);
let mut iter = mset.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 mset = NumericalMultiset::from_iter([1, 2, 2]);
assert_eq!(mset.contains(1), true);
assert_eq!(mset.contains(2), true);
assert_eq!(mset.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 mset = NumericalMultiset::from_iter([1, 2, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.multiplicity(1), Some(nonzero(1)));
assert_eq!(mset.multiplicity(2), Some(nonzero(2)));
assert_eq!(mset.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 items 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 this multiset is a subset of another, i.e., other
contains at least all the items 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 mset = NumericalMultiset::new();
assert!(mset.is_subset(&sup));
mset.insert(2);
assert!(mset.is_subset(&sup));
mset.insert(2);
assert!(mset.is_subset(&sup));
mset.insert(2);
assert!(!mset.is_subset(&sup));
Sourcepub fn is_superset(&self, other: &Self) -> bool
pub fn is_superset(&self, other: &Self) -> bool
Returns true
if this multiset is a superset of another, i.e., self
contains at least all the items 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 mset = NumericalMultiset::new();
assert!(!mset.is_superset(&sub));
mset.insert(3);
mset.insert(1);
assert!(!mset.is_superset(&sub));
mset.insert(2);
assert!(!mset.is_superset(&sub));
mset.insert(2);
assert!(mset.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 mset = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.pop_all_first(), Some((1, nonzero(2))));
assert_eq!(mset.pop_all_first(), Some((2, nonzero(1))));
assert_eq!(mset.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 mset = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.pop_all_last(), Some((2, nonzero(1))));
assert_eq!(mset.pop_all_last(), Some((1, nonzero(2))));
assert_eq!(mset.pop_all_last(), None);
Sourcepub fn insert(&mut self, value: T) -> Option<NonZeroUsize>
pub fn insert(&mut self, value: T) -> Option<NonZeroUsize>
Insert an item into the multiset, tell how many identical items were already present in the multiset before insertion.
See also insert_multiple()
for a more
efficient alternative if you need to insert multiple copies of a value.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut mset = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.insert(1), None);
assert_eq!(mset.insert(1), Some(nonzero(1)));
assert_eq!(mset.insert(1), Some(nonzero(2)));
assert_eq!(mset.insert(2), None);
assert_eq!(mset.len(), 4);
assert_eq!(mset.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 an item, tell how many identical items were already present in the multiset.
This method is typically used for the purpose of efficiently 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 mset = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.insert_multiple(1, nonzero(2)), None);
assert_eq!(mset.insert_multiple(1, nonzero(3)), Some(nonzero(2)));
assert_eq!(mset.insert_multiple(2, nonzero(2)), None);
assert_eq!(mset.len(), 7);
assert_eq!(mset.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 the value were previously present in the multiset.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut mset = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.replace_all(1, nonzero(2)), None);
assert_eq!(mset.replace_all(1, nonzero(3)), Some(nonzero(2)));
assert_eq!(mset.replace_all(2, nonzero(2)), None);
assert_eq!(mset.len(), 5);
assert_eq!(mset.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 item from the multiset, on success tell how many identical items were previously present in the multiset (including the one that was just removed).
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 mset = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.remove(1), Some(nonzero(2)));
assert_eq!(mset.remove(1), Some(nonzero(1)));
assert_eq!(mset.remove(1), None);
assert_eq!(mset.remove(2), Some(nonzero(1)));
assert_eq!(mset.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 items 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 mset = NumericalMultiset::from_iter([1, 1, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.remove_all(1), Some(nonzero(2)));
assert_eq!(mset.remove_all(1), None);
assert_eq!(mset.remove_all(2), Some(nonzero(1)));
assert_eq!(mset.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 multiset containing all items greater than or equal
to value
. The multiset on which this method was called will retain all
items 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
May panic if range start > end
, or if range start == end
and both
bounds are Excluded
.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mset = NumericalMultiset::from_iter([3, 3, 5, 8, 8]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(mset.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 items 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 item-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
self
contains more occurences ofv
thanother
(i.e.s > o
), then the difference will contains - o
occurences 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 items 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 item-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
self
contains as many occurences ofv
asother
(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 items 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 item-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 items 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 item-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 mset = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.first(), None);
mset.insert(2);
assert_eq!(mset.first(), Some((2, nonzero(1))));
mset.insert(2);
assert_eq!(mset.first(), Some((2, nonzero(2))));
mset.insert(1);
assert_eq!(mset.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 mset = NumericalMultiset::new();
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert_eq!(mset.last(), None);
mset.insert(1);
assert_eq!(mset.last(), Some((1, nonzero(1))));
mset.insert(1);
assert_eq!(mset.last(), Some((1, nonzero(2))));
mset.insert(2);
assert_eq!(mset.last(), Some((2, nonzero(1))));
Sourcepub fn pop_first(&mut self) -> Option<T>
pub fn pop_first(&mut self) -> Option<T>
Remove the smallest item 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 mset = NumericalMultiset::new();
mset.insert(1);
mset.insert(1);
mset.insert(2);
assert_eq!(mset.pop_first(), Some(1));
assert_eq!(mset.pop_first(), Some(1));
assert_eq!(mset.pop_first(), Some(2));
assert_eq!(mset.pop_first(), None);
Sourcepub fn pop_last(&mut self) -> Option<T>
pub fn pop_last(&mut self) -> Option<T>
Remove the largest item 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 mset = NumericalMultiset::new();
mset.insert(1);
mset.insert(1);
mset.insert(2);
assert_eq!(mset.pop_last(), Some(2));
assert_eq!(mset.pop_last(), Some(1));
assert_eq!(mset.pop_last(), Some(1));
assert_eq!(mset.pop_last(), None);
Sourcepub fn retain(&mut self, f: impl FnMut(T, &mut NonZeroUsize) -> bool)
pub fn retain(&mut self, f: impl FnMut(T, &mut NonZeroUsize) -> bool)
Retains only the items specified by the predicate.
For efficiency reasons, the filtering callback f
is not run once per
item, but once per distinct value present inside of the multiset.
However, it is also provided with the multiplicity of that value within
the multiset, which can be used as a filtering criterion.
Furthermore, you get read/write access to the multiplicity, which allows you to change it if you desire to do so.
In other words, this method removes all values v
with multiplicity m
for which f(v, m)
returns false
, and allows changing the
multiplicity for all values where f
returns true
.
Values are visited in ascending order.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mut mset = 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.
mset.retain(|value, multiplicity| value % 2 == multiplicity.get() % 2);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(mset.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 items 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§const fn clone_from(&mut self, source: &Self)
const 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 mset = NumericalMultiset::from_iter([1, 2, 3]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
mset.extend([(3, nonzero(3)), (4, nonzero(2))]);
assert_eq!(mset, 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.
Items are grouped by value and emitted in (value, multiplicity)
format, in ascending value order.
§Examples
use numerical_multiset::NumericalMultiset;
use std::num::NonZeroUsize;
let mset = NumericalMultiset::from_iter([3, 1, 2, 2]);
let nonzero = |x| NonZeroUsize::new(x).unwrap();
assert!(mset.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.