Struct NumericalMultiset

Source
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 Eq implementation of type T
  • “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>

Source

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

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

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

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

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

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>

Source

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

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>

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

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

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

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

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

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

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

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>

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

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

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

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

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

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

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

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>

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 copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> 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 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)

🔬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 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§

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§

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.