Struct NumericalMultiset

Source
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, whose Ord 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 the Eq implementation of T.
  • “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>

Source

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

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

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

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

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

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>

Source

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

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>

Source

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

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

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

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

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

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

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

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

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

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

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

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

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>

Source

pub fn range<R>( &self, range: R, ) -> impl DoubleEndedIterator<Item = (T, NonZeroUsize)> + FusedIterator
where 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)),
]));
Source

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 of v than other (i.e. s > o), then the difference will contain s - o occurences of v.
  • Otherwise (if s <= o) the difference will not contain any occurence 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.difference(&b).eq([
    (1, nonzero(2)),
    (2, nonzero(1)),
]));
Source

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 of v as other (i.e. s == o), then the symmetric difference will not contain any occurence of v.
  • Otherwise (if s != o) the symmetric difference will contain s.abs_diff(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.symmetric_difference(&b).eq([
    (1, nonzero(2)),
    (2, nonzero(1)),
    (4, nonzero(1)),
]));
Source

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)),
]));
Source

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)),
]));
Source

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

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

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

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

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)),
]));
Source

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>

Source§

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>

The resulting type after applying the & operator.
Source§

impl<T: Copy + Ord> BitOr<&NumericalMultiset<T>> for &NumericalMultiset<T>

Source§

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>

The resulting type after applying the | operator.
Source§

impl<T: Copy + Ord> BitXor<&NumericalMultiset<T>> for &NumericalMultiset<T>

Source§

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>

The resulting type after applying the ^ operator.
Source§

impl<T: Clone> Clone for NumericalMultiset<T>

Source§

fn clone(&self) -> NumericalMultiset<T>

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

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for NumericalMultiset<T>

Source§

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

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

impl<T: Default> Default for NumericalMultiset<T>

Source§

fn default() -> NumericalMultiset<T>

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

impl<T: Ord> Extend<(T, NonZero<usize>)> for NumericalMultiset<T>

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Ord> Extend<T> for NumericalMultiset<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Ord> FromIterator<(T, NonZero<usize>)> for NumericalMultiset<T>

Source§

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>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for NumericalMultiset<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, T: Copy> IntoIterator for &'a NumericalMultiset<T>

Source§

type Item = (T, NonZero<usize>)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for NumericalMultiset<T>

Source§

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§

type Item = (T, NonZero<usize>)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

impl<T: Ord> Ord for NumericalMultiset<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for NumericalMultiset<T>

Source§

fn eq(&self, other: &Self) -> bool

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

const 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: PartialOrd> PartialOrd for NumericalMultiset<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy + Ord> Sub<&NumericalMultiset<T>> for &NumericalMultiset<T>

Source§

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>

The resulting type after applying the - operator.
Source§

impl<T: Eq> Eq for NumericalMultiset<T>

Auto Trait Implementations§

§

impl<T> Freeze for NumericalMultiset<T>

§

impl<T> RefUnwindSafe for NumericalMultiset<T>
where T: RefUnwindSafe,

§

impl<T> Send for NumericalMultiset<T>
where T: Send,

§

impl<T> Sync for NumericalMultiset<T>
where T: Sync,

§

impl<T> Unpin for NumericalMultiset<T>

§

impl<T> UnwindSafe for NumericalMultiset<T>
where T: RefUnwindSafe,

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.