Struct VecSet

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

Source

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

A set with a single element.

Source

pub fn empty() -> Self

The empty set.

Source

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

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

Source

pub fn len(&self) -> usize

The number of elements in the set.

Source

pub fn shrink_to_fit(&mut self)

Shrink the underlying SmallVec to fit.

Source

pub fn is_empty(&self) -> bool

true if the set is empty.

Source

pub fn into_inner(self) -> SmallVec<A>

Returns the wrapped SmallVec.

Source§

impl<A: Array> VecSet<A>
where A::Item: Ord,

Source

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

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

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.

Source

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

Retain all elements matching a predicate.

Source§

impl<A: Array> VecSet<A>
where A::Item: Ord + Clone,

Source

pub fn union(&self, that: &impl AbstractVecSet<A::Item>) -> Self

Source

pub fn intersection(&self, that: &impl AbstractVecSet<A::Item>) -> Self

Source

pub fn symmetric_difference(&self, that: &impl AbstractVecSet<A::Item>) -> Self

Source

pub fn difference(&self, that: &impl AbstractVecSet<A::Item>) -> Self

Source

pub fn union_with(&mut self, that: &impl AbstractVecSet<A::Item>)

Source

pub fn intersection_with(&mut self, that: &impl AbstractVecSet<A::Item>)

Source

pub fn xor_with(&mut self, that: &impl AbstractVecSet<A::Item>)

Source

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>
where A::Item: Ord,

Source§

fn as_slice(&self) -> &[A::Item]

Source§

fn is_empty(&self) -> bool

Source§

fn contains(&self, value: &T) -> bool

Source§

fn is_disjoint(&self, that: &impl AbstractVecSet<T>) -> bool

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

fn is_subset(&self, that: &impl AbstractVecSet<T>) -> bool

true if this set is a subset of another set. Read more
Source§

fn is_superset(&self, that: &impl AbstractVecSet<T>) -> bool

true if this set is a superset of another set. Read more
Source§

fn union<A: Array<Item = T>>(&self, that: &impl AbstractVecSet<T>) -> VecSet<A>
where T: Clone,

Source§

fn intersection<A: Array<Item = T>>( &self, that: &impl AbstractVecSet<T>, ) -> VecSet<A>
where T: Clone,

Source§

fn symmetric_difference<A: Array<Item = T>>( &self, that: &impl AbstractVecSet<T>, ) -> VecSet<A>
where T: Clone,

Source§

fn difference<A: Array<Item = T>>( &self, that: &impl AbstractVecSet<T>, ) -> VecSet<A>
where T: Clone,

Source§

fn iter(&self) -> VecSetIter<Iter<'_, T>>

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

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

Source§

fn as_ref(&self) -> &[A::Item]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitAnd<&VecSet<B>> for &VecSet<A>

Source§

type Output = VecSet<A>

The resulting type after applying the & operator.
Source§

fn bitand(self, that: &VecSet<B>) -> Self::Output

Performs the & operation. Read more
Source§

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

Performs the &= operation. Read more
Source§

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

Performs the &= operation. Read more
Source§

impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitOr<&VecSet<B>> for &VecSet<A>

Source§

type Output = VecSet<A>

The resulting type after applying the | operator.
Source§

fn bitor(self, that: &VecSet<B>) -> Self::Output

Performs the | operation. Read more
Source§

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

Performs the |= operation. Read more
Source§

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

Performs the |= operation. Read more
Source§

impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> BitXor<&VecSet<B>> for &VecSet<A>

Source§

type Output = VecSet<A>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, that: &VecSet<B>) -> Self::Output

Performs the ^ operation. Read more
Source§

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

Performs the ^= operation. Read more
Source§

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

Performs the ^= operation. Read more
Source§

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

Source§

fn clone(&self) -> Self

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, A: Array<Item = T>> Debug for VecSet<A>

Source§

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

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

impl<A: Default + Array> Default for VecSet<A>

Source§

fn default() -> VecSet<A>

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

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

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

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

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

fn from(value: BTreeSet<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Ord, A: Array<Item = T>> From<Vec<T>> for VecSet<A>

Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<A: Array> From<VecMap<A>> for VecSet<A>

Source§

fn from(value: VecMap<A>) -> Self

Converts to this type from the input type.
Source§

impl<A: Array> From<VecSet<A>> for SmallVec<A>

Source§

fn from(value: VecSet<A>) -> Self

Converts to this type from the input type.
Source§

impl<A: Array> From<VecSet<A>> for Vec<A::Item>

Source§

fn from(value: VecSet<A>) -> Self

Converts to this type from the input type.
Source§

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

Creates a value from an iterator. Read more
Source§

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

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, A: Array> IntoIterator for &'a VecSet<A>

Source§

type Item = &'a <A as Array>::Item

The type of the elements being iterated over.
Source§

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

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<A: Array> IntoIterator for VecSet<A>

Source§

type Item = <A as Array>::Item

The type of the elements being iterated over.
Source§

type IntoIter = VecSetIter<IntoIter<A>>

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: Ord, A: Array<Item = T>> Ord for VecSet<A>

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, A: Array<Item = T>> PartialEq for VecSet<A>

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, A: Array<Item = T>> PartialOrd for VecSet<A>

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<A: Array> Serialize for VecSet<A>
where A::Item: Serialize,

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Ord + Clone, A: Array<Item = T>, B: Array<Item = T>> Sub<&VecSet<B>> for &VecSet<A>

Source§

type Output = VecSet<A>

The resulting type after applying the - operator.
Source§

fn sub(self, that: &VecSet<B>) -> Self::Output

Performs the - operation. Read more
Source§

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

Performs the -= operation. Read more
Source§

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

Performs the -= operation. Read more
Source§

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

Auto Trait Implementations§

§

impl<A> Freeze for VecSet<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for VecSet<A>

§

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§

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.
Source§

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