pub struct SmallSet<T, const N: usize> { /* private fields */ }Expand description
A set that lives on the stack for N items, then automatically spills to the heap.
§Implementation Details
This is a wrapper around SmallMap<T, (), N>. Since () is a zero-sized type in Rust,
this wrapper has zero memory overhead compared to a raw set implementation.
- Stack State: Zero allocations. Extremely fast FNV hashing.
- Heap State: Standard
HashMapperformance. - Spill: Zero-allocation move. Keys are moved, not cloned.
Implementations§
Source§impl<T, const N: usize> SmallSet<T, N>
impl<T, const N: usize> SmallSet<T, N>
Sourcepub fn is_on_stack(&self) -> bool
pub fn is_on_stack(&self) -> bool
Returns true if the set is currently storing data on the stack.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the set, removing all values.
This keeps the allocated memory (if on heap) for reuse.
Sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Adds a value to the set.
Returns true if the value was newly inserted.
Returns false if the value was already present.
If the stack capacity N is exceeded, this triggers a spill to the heap.
Sourcepub fn contains<Q>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
Returns true if the set contains a value.
This method is generic over Q to allow looking up String keys with &str.
Sourcepub fn remove<Q>(&mut self, value: &Q) -> bool
pub fn remove<Q>(&mut self, value: &Q) -> bool
Removes a value from the set. Returns true if the value was present.
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements specified by the predicate.
In other words, remove all elements e such that f(&e) returns false.
This method operates in O(n) time.
Sourcepub fn iter(&self) -> SetRefIter<'_, T> ⓘ
pub fn iter(&self) -> SetRefIter<'_, T> ⓘ
Returns an iterator visiting all elements in arbitrary order.
Sourcepub fn difference<'a, S>(&'a self, other: &'a S) -> impl Iterator<Item = &'a T>where
S: AnySet<T>,
pub fn difference<'a, S>(&'a self, other: &'a S) -> impl Iterator<Item = &'a T>where
S: AnySet<T>,
Visits the values representing the difference, i.e., the values that are in self but not in other.
other can be any collection implementing AnySet (SmallSet, HashSet, BTreeSet).
Sourcepub fn intersection<'a, S>(
&'a self,
other: &'a S,
) -> impl Iterator<Item = &'a T>where
S: AnySet<T>,
pub fn intersection<'a, S>(
&'a self,
other: &'a S,
) -> impl Iterator<Item = &'a T>where
S: AnySet<T>,
Visits the values representing the intersection, i.e., the values that are both in self and other.
Sourcepub fn union<'a, I>(&'a self, other: I) -> impl Iterator<Item = &'a T>
pub fn union<'a, I>(&'a self, other: I) -> impl Iterator<Item = &'a T>
Visits the values representing the union, i.e., all the values in self or other, without duplicates.
other can be any iterator yielding &T.
Sourcepub fn is_disjoint<S>(&self, other: &S) -> boolwhere
S: AnySet<T>,
pub fn is_disjoint<S>(&self, other: &S) -> boolwhere
S: AnySet<T>,
Returns true if self has no elements in common with other.
Sourcepub fn is_subset<S>(&self, other: &S) -> boolwhere
S: AnySet<T>,
pub fn is_subset<S>(&self, other: &S) -> boolwhere
S: AnySet<T>,
Returns true if self is a subset of other.
other must be a Set (implement AnySet) to ensure O(1) lookups.
Sourcepub fn is_superset<'a, I>(&self, other: I) -> boolwhere
T: 'a,
I: IntoIterator<Item = &'a T>,
pub fn is_superset<'a, I>(&self, other: I) -> boolwhere
T: 'a,
I: IntoIterator<Item = &'a T>,
Returns true if self is a superset of other.
other can be any iterator. We must ensure T lives as long as the iterator 'a.
Sourcepub fn symmetric_difference<'a>(
&'a self,
other: &'a SmallSet<T, N>,
) -> impl Iterator<Item = &'a T>
pub fn symmetric_difference<'a>( &'a self, other: &'a SmallSet<T, N>, ) -> impl Iterator<Item = &'a T>
Visits the values representing the symmetric difference,
i.e., values that are in self or other but not in both.
Trait Implementations§
Source§impl<'a, T, const N: usize> Extend<&'a T> for SmallSet<T, N>
impl<'a, T, const N: usize> Extend<&'a T> for SmallSet<T, N>
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a 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, const N: usize> Extend<T> for SmallSet<T, N>
impl<T, const N: usize> Extend<T> for SmallSet<T, N>
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: Eq + Hash, const N: usize> FromIterator<T> for SmallSet<T, N>
impl<T: Eq + Hash, const N: usize> FromIterator<T> for SmallSet<T, 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, T, const N: usize> IntoIterator for &'a SmallSet<T, N>
impl<'a, T, const N: usize> IntoIterator for &'a SmallSet<T, N>
impl<T, const N: usize> Eq for SmallSet<T, N>
Auto Trait Implementations§
impl<T, const N: usize> Freeze for SmallSet<T, N>where
T: Freeze,
impl<T, const N: usize> RefUnwindSafe for SmallSet<T, N>where
T: RefUnwindSafe,
impl<T, const N: usize> Send for SmallSet<T, N>where
T: Send,
impl<T, const N: usize> Sync for SmallSet<T, N>where
T: Sync,
impl<T, const N: usize> Unpin for SmallSet<T, N>where
T: Unpin,
impl<T, const N: usize> UnsafeUnpin for SmallSet<T, N>where
T: UnsafeUnpin,
impl<T, const N: usize> UnwindSafe for SmallSet<T, N>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.