Struct vec_collections::VecSet[][src]

pub struct VecSet<A: Array>(_);

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

impl<A: Array> VecSet<A>[src]

pub fn single(value: A::Item) -> Self[src]

A set with a single element.

pub fn empty() -> Self[src]

The empty set.

pub fn iter(&self) -> VecSetIter<Iter<'_, A::Item>>

Notable traits for VecSetIter<I>

impl<I: Iterator> Iterator for VecSetIter<I> type Item = I::Item;
[src]

An iterator that returns references to the items of this set in sorted order

pub fn len(&self) -> usize[src]

The number of elements in the set.

pub fn shrink_to_fit(&mut self)[src]

Shrink the underlying SmallVec to fit.

pub fn is_empty(&self) -> bool[src]

true if the set is empty.

pub fn into_inner(self) -> SmallVec<A>[src]

Returns the wrapped SmallVec.

impl<A: Array> VecSet<A> where
    A::Item: Ord
[src]

pub fn insert(&mut self, that: A::Item) -> bool[src]

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.

pub fn remove(&mut self, that: &A::Item) -> bool[src]

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.

pub fn retain<F: FnMut(&A::Item) -> bool>(&mut self, f: F)[src]

Retain all elements matching a predicate.

pub fn is_disjoint<B: Array<Item = A::Item>>(&self, that: &VecSet<B>) -> bool[src]

true if this set has no common elements with another set.

pub fn is_subset<B: Array<Item = A::Item>>(&self, that: &VecSet<B>) -> bool[src]

true if this set is a subset of another set.

A set is considered to be a subset of itself.

pub fn is_superset<B: Array<Item = A::Item>>(&self, that: &VecSet<B>) -> bool[src]

true if this set is a superset of another set.

A set is considered to be a superset of itself.

pub fn contains(&self, value: &A::Item) -> bool[src]

true if this set contains the item.

Time complexity is O(log N). Binary search.

Trait Implementations

impl<A: Array> AsRef<[<A as Array>::Item]> for VecSet<A>[src]

impl<T: Ord + Clone, Arr: Array<Item = T>, B: Array<Item = T>> BitAnd<&'_ VecSet<B>> for &VecSet<Arr>[src]

type Output = VecSet<Arr>

The resulting type after applying the & operator.

impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitAndAssign<VecSet<B>> for VecSet<A>[src]

impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitOr<&'_ VecSet<B>> for &VecSet<A>[src]

type Output = VecSet<A>

The resulting type after applying the | operator.

impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitOrAssign<VecSet<B>> for VecSet<A>[src]

impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitXor<&'_ VecSet<B>> for &VecSet<A>[src]

type Output = VecSet<A>

The resulting type after applying the ^ operator.

impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> BitXorAssign<VecSet<B>> for VecSet<A>[src]

impl<T: Clone, A: Array<Item = T>> Clone for VecSet<A>[src]

impl<T: Debug, A: Array<Item = T>> Debug for VecSet<A>[src]

impl<A: Default + Array> Default for VecSet<A>[src]

impl<'de, A: Array> Deserialize<'de> for VecSet<A> where
    A::Item: Deserialize<'de> + Ord
[src]

impl<T: Eq, A: Array<Item = T>> Eq for VecSet<A>[src]

impl<T: Ord, A: Array<Item = T>> Extend<T> for VecSet<A>[src]

impl<T: Ord, A: Array<Item = T>> From<BTreeSet<T>> for VecSet<A>[src]

Provides a way to create a VecSet from a BTreeSet without having to sort again

impl<T: Ord, A: Array<Item = T>> From<Vec<T, Global>> for VecSet<A>[src]

impl<A: Array> From<VecMap<A>> for VecSet<A>[src]

impl<T: Ord, A: Array<Item = T>> FromIterator<T> for VecSet<A>[src]

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).

impl<T: Hash, A: Array<Item = T>> Hash for VecSet<A>[src]

impl<A: Array> Into<SmallVec<A>> for VecSet<A>[src]

impl<A: Array> Into<Vec<<A as Array>::Item, Global>> for VecSet<A>[src]

impl<'a, A: Array> IntoIterator for &'a VecSet<A>[src]

type Item = &'a A::Item

The type of the elements being iterated over.

type IntoIter = VecSetIter<Iter<'a, A::Item>>

Which kind of iterator are we turning this into?

impl<A: Array> IntoIterator for VecSet<A>[src]

type Item = A::Item

The type of the elements being iterated over.

type IntoIter = VecSetIter<IntoIter<A>>

Which kind of iterator are we turning this into?

impl<T: Ord, A: Array<Item = T>> Ord for VecSet<A>[src]

impl<T: PartialEq, A: Array<Item = T>> PartialEq<VecSet<A>> for VecSet<A>[src]

impl<T: PartialOrd, A: Array<Item = T>> PartialOrd<VecSet<A>> for VecSet<A>[src]

impl<A: Array> Serialize for VecSet<A> where
    A::Item: Serialize
[src]

impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> Sub<&'_ VecSet<B>> for &VecSet<A>[src]

type Output = VecSet<A>

The resulting type after applying the - operator.

impl<T: Ord, A: Array<Item = T>, B: Array<Item = T>> SubAssign<VecSet<B>> for VecSet<A>[src]

Auto Trait Implementations

impl<A> RefUnwindSafe for VecSet<A> where
    A: RefUnwindSafe,
    <A as Array>::Item: RefUnwindSafe

impl<A> Send for VecSet<A> where
    <A as Array>::Item: Send

impl<A> Sync for VecSet<A> where
    A: Sync

impl<A> Unpin for VecSet<A> where
    A: Unpin

impl<A> UnwindSafe for VecSet<A> where
    A: UnwindSafe,
    <A as Array>::Item: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.