pub struct VecSet<A: Array>(/* private fields */);
Expand description
A set backed by a SmallVec of elements.
A
the underlying storage. This must be an array. The size of this array is the maximum size this collection
can hold without allocating. VecSet is just a wrapper around a SmallVec, so it does not have additional memory
overhead.
Sets support comparison operations ( is_disjoint, is_subset, is_superset ) and set combine operations ( bitand, bitor, bitxor, sub ). They also support in place operations ( bitand_assign, bitor_assign, bitxor_assign, sub_assign ) that will try to avoid allocations.
§Creation
The best way to create a VecSet is to use FromIterator, via collect.
use vec_collections::VecSet;
let a: VecSet<[u32; 4]> = (0..4).collect(); // does not allocate
§General usage
use vec_collections::{VecSet, AbstractVecSet};
let a: VecSet<[u32; 4]> = (0..4).collect(); // does not allocate
let b: VecSet<[u32; 2]> = (4..6).collect(); // does not allocate
println!("{}", a.is_disjoint(&b)); // true
let c = &a | &b; // underlying smallvec will spill over to the heap
println!("{}", c.contains(&5)); // true
§In place operations
use vec_collections::{VecSet, AbstractVecSet};
let mut a: VecSet<[u32; 4]> = (0..4).collect(); // does not allocate
let b: VecSet<[u32; 4]> = (2..6).collect(); // does not allocate
a &= b; // in place intersection, will yield 2..4, will not allocate
println!("{}", a.contains(&3)); // true
§Accessing the elements as a slice
Since a VecSet is a succinct collection, you can get a reference to the contents as a slice.
§Example: choosing a random element
use vec_collections::VecSet;
use rand::seq::SliceRandom;
let mut a: VecSet<[u32; 4]> = (0..4).collect(); // does not allocate
let mut rng = rand::thread_rng();
let e = a.as_ref().choose(&mut rng).unwrap();
println!("{}", e);
Implementations§
Source§impl<A: Array> VecSet<A>
impl<A: Array> VecSet<A>
Sourcepub fn iter(&self) -> VecSetIter<Iter<'_, A::Item>> ⓘ
pub fn iter(&self) -> VecSetIter<Iter<'_, A::Item>> ⓘ
An iterator that returns references to the items of this set in sorted order
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink the underlying SmallVec
Sourcepub fn into_inner(self) -> SmallVec<A>
pub fn into_inner(self) -> SmallVec<A>
Returns the wrapped SmallVec.
Source§impl<A: Array> VecSet<A>
impl<A: Array> VecSet<A>
Sourcepub fn insert(&mut self, that: A::Item) -> bool
pub fn insert(&mut self, that: A::Item) -> bool
insert an element.
The time complexity of this is O(N), so building a large set using single element inserts will be slow! Prefer using from_iter when building a large VecSet from elements.
Source§impl<A: Array> VecSet<A>
impl<A: Array> VecSet<A>
pub fn union(&self, that: &impl AbstractVecSet<A::Item>) -> Self
pub fn intersection(&self, that: &impl AbstractVecSet<A::Item>) -> Self
pub fn symmetric_difference(&self, that: &impl AbstractVecSet<A::Item>) -> Self
pub fn difference(&self, that: &impl AbstractVecSet<A::Item>) -> Self
pub fn union_with(&mut self, that: &impl AbstractVecSet<A::Item>)
pub fn intersection_with(&mut self, that: &impl AbstractVecSet<A::Item>)
pub fn xor_with(&mut self, that: &impl AbstractVecSet<A::Item>)
pub fn difference_with(&mut self, that: &impl AbstractVecSet<A::Item>)
Trait Implementations§
Source§impl<A: Array> AbstractVecSet<<A as Array>::Item> for VecSet<A>
impl<A: Array> AbstractVecSet<<A as Array>::Item> for VecSet<A>
fn as_slice(&self) -> &[A::Item]
fn is_empty(&self) -> bool
fn contains(&self, value: &T) -> bool
Source§fn is_disjoint(&self, that: &impl AbstractVecSet<T>) -> bool
fn is_disjoint(&self, that: &impl AbstractVecSet<T>) -> bool
Source§fn is_subset(&self, that: &impl AbstractVecSet<T>) -> bool
fn is_subset(&self, that: &impl AbstractVecSet<T>) -> bool
Source§fn is_superset(&self, that: &impl AbstractVecSet<T>) -> bool
fn is_superset(&self, that: &impl AbstractVecSet<T>) -> bool
fn union<A: Array<Item = T>>(&self, that: &impl AbstractVecSet<T>) -> VecSet<A>where
T: Clone,
fn intersection<A: Array<Item = T>>(
&self,
that: &impl AbstractVecSet<T>,
) -> VecSet<A>where
T: Clone,
fn symmetric_difference<A: Array<Item = T>>(
&self,
that: &impl AbstractVecSet<T>,
) -> VecSet<A>where
T: Clone,
fn difference<A: Array<Item = T>>(
&self,
that: &impl AbstractVecSet<T>,
) -> VecSet<A>where
T: Clone,
Source§impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitAnd<&VecSet<B>> for &VecSet<A>
impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitAnd<&VecSet<B>> for &VecSet<A>
Source§impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitAndAssign<&VecSet<B>> for VecSet<A>
impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitAndAssign<&VecSet<B>> for VecSet<A>
Source§fn bitand_assign(&mut self, that: &VecSet<B>)
fn bitand_assign(&mut self, that: &VecSet<B>)
&=
operation. Read moreSource§impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitAndAssign<VecSet<B>> for VecSet<A>
impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitAndAssign<VecSet<B>> for VecSet<A>
Source§fn bitand_assign(&mut self, that: VecSet<B>)
fn bitand_assign(&mut self, that: VecSet<B>)
&=
operation. Read moreSource§impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitOr<&VecSet<B>> for &VecSet<A>
impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitOr<&VecSet<B>> for &VecSet<A>
Source§impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitOrAssign<&VecSet<B>> for VecSet<A>
impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitOrAssign<&VecSet<B>> for VecSet<A>
Source§fn bitor_assign(&mut self, that: &VecSet<B>)
fn bitor_assign(&mut self, that: &VecSet<B>)
|=
operation. Read moreSource§impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitOrAssign<VecSet<B>> for VecSet<A>
impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitOrAssign<VecSet<B>> for VecSet<A>
Source§fn bitor_assign(&mut self, that: VecSet<B>)
fn bitor_assign(&mut self, that: VecSet<B>)
|=
operation. Read moreSource§impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitXor<&VecSet<B>> for &VecSet<A>
impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitXor<&VecSet<B>> for &VecSet<A>
Source§impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitXorAssign<&VecSet<B>> for VecSet<A>
impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitXorAssign<&VecSet<B>> for VecSet<A>
Source§fn bitxor_assign(&mut self, that: &VecSet<B>)
fn bitxor_assign(&mut self, that: &VecSet<B>)
^=
operation. Read moreSource§impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitXorAssign<VecSet<B>> for VecSet<A>
impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitXorAssign<VecSet<B>> for VecSet<A>
Source§fn bitxor_assign(&mut self, that: VecSet<B>)
fn bitxor_assign(&mut self, that: VecSet<B>)
^=
operation. Read moreSource§impl<'de, A: Array> Deserialize<'de> for VecSet<A>
impl<'de, A: Array> Deserialize<'de> for VecSet<A>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl<T: Ord, A: Array<Item = T>> Extend<T> for VecSet<A>
impl<T: Ord, A: Array<Item = T>> Extend<T> for VecSet<A>
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, A: Array<Item = T>> From<BTreeSet<T>> for VecSet<A>
Provides a way to create a VecSet from a BTreeSet without having to sort again
impl<T: Ord, A: Array<Item = T>> From<BTreeSet<T>> for VecSet<A>
Provides a way to create a VecSet from a BTreeSet without having to sort again
Source§impl<T: Ord, A: Array<Item = T>> FromIterator<T> for VecSet<A>
Builds the set from an iterator.
impl<T: Ord, A: Array<Item = T>> FromIterator<T> for VecSet<A>
Builds the set from an iterator.
Uses a heuristic to deduplicate while building the set, so the intermediate storage will never be more than twice the size of the resulting set. This is the most efficient way to build a large VecSet, significantly more efficient than single element insertion.
Worst case performance is O(log(n)^2 * n), but performance for already partially sorted collections will be significantly better. For a fully sorted collection, performance will be O(n).
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<'a, A: Array> IntoIterator for &'a VecSet<A>
impl<'a, A: Array> IntoIterator for &'a VecSet<A>
Source§impl<A: Array> IntoIterator for VecSet<A>
impl<A: Array> IntoIterator for VecSet<A>
Source§impl<T: Ord, A: Array<Item = T>> Ord for VecSet<A>
impl<T: Ord, A: Array<Item = T>> Ord for VecSet<A>
Source§impl<T: PartialOrd, A: Array<Item = T>> PartialOrd for VecSet<A>
impl<T: PartialOrd, A: Array<Item = T>> PartialOrd for VecSet<A>
Source§impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> SubAssign<&VecSet<B>> for VecSet<A>
impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> SubAssign<&VecSet<B>> for VecSet<A>
Source§fn sub_assign(&mut self, that: &VecSet<B>)
fn sub_assign(&mut self, that: &VecSet<B>)
-=
operation. Read moreSource§impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> SubAssign<VecSet<B>> for VecSet<A>
impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> SubAssign<VecSet<B>> for VecSet<A>
Source§fn sub_assign(&mut self, that: VecSet<B>)
fn sub_assign(&mut self, that: VecSet<B>)
-=
operation. Read more