Struct vec_collections::VecSet [−][src]
pub struct VecSet<A: Array>(_);
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
pub fn iter(&self) -> VecSetIter<Iter<'_, A::Item>>ⓘNotable traits for VecSetIter<I>impl<I: Iterator> Iterator for VecSetIter<I> type Item = I::Item;
pub fn iter(&self) -> VecSetIter<Iter<'_, A::Item>>ⓘNotable traits for VecSetIter<I>impl<I: Iterator> Iterator for VecSetIter<I> type Item = I::Item;
impl<I: Iterator> Iterator for VecSetIter<I> type Item = I::Item;
An iterator that returns references to the items of this set in sorted order
Shrink the underlying SmallVec
Returns the wrapped SmallVec.
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.
Remove an element.
The time complexity of this is O(N), so removing many elements using single element removes inserts will be slow! Prefer using retain when removing a large number of elements.
Trait Implementations
true if this set has no common elements with another set.
true if this set is a subset of another set. Read more
true if this set is a superset of another set. Read more
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,
fn iter(&self) -> VecSetIter<Iter<'_, T>>ⓘNotable traits for VecSetIter<I>impl<I: Iterator> Iterator for VecSetIter<I> type Item = I::Item;
fn iter(&self) -> VecSetIter<Iter<'_, T>>ⓘNotable traits for VecSetIter<I>impl<I: Iterator> Iterator for VecSetIter<I> type Item = I::Item;
impl<I: Iterator> Iterator for VecSetIter<I> type Item = I::Item;
An iterator that returns references to the items of this set in sorted order
Performs the &=
operation. Read more
Performs the &=
operation. Read more
Performs the |=
operation. Read more
Performs the |=
operation. Read more
Performs the ^=
operation. Read more
Performs the ^=
operation. Read more
Deserialize this value from the given Serde deserializer. Read more
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Provides a way to create a VecSet from a BTreeSet without having to sort again
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).
Creates a value from an iterator. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Performs the -=
operation. Read more
Performs the -=
operation. Read more
Auto Trait Implementations
impl<A> RefUnwindSafe for VecSet<A> where
A: RefUnwindSafe,
<A as Array>::Item: RefUnwindSafe,
impl<A> UnwindSafe for VecSet<A> where
A: UnwindSafe,
<A as Array>::Item: RefUnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more