Struct more_collections::small_set::SmallSet
source · pub struct SmallSet<T, const C: usize, S = RandomState> { /* private fields */ }
Expand description
A set-like container that can store a specified number of elements inline.
SmallSet
shares most of its API with, and behaves like,
IndexSet
. It can store a limited amount of data
inline, backed by SmallVec
. If the data exceeds the
limit C
, SmallSet
will move all its data over to the heap in the form
of an IndexSet
. For performance reasons, transitions between heap and
inline storage should generally be avoided.
The SmallSet
datastructure is meant for situations where the data does not
exceed C
most of the time but it still needs to support cases where the
data does exceed C
.
§Example
use more_collections::SmallSet;
let mut set = SmallSet::<usize, 3>::new();
// The set can hold up to three items inline
set.insert(0);
set.insert(1);
set.insert(2);
assert_eq!(3, set.len());
assert!(set.is_inline());
// Adding the fourth element will move the set to the heap
set.insert(3);
assert_eq!(4, set.len());
assert!(!set.is_inline());
Implementations§
source§impl<T, const C: usize, S> SmallSet<T, C, S>
impl<T, const C: usize, S> SmallSet<T, C, S>
sourcepub const fn inline_capacity(&self) -> usize
pub const fn inline_capacity(&self) -> usize
The memory capacity that will be allocated inline. If the nubmer of values exceeds the inline capacity, the set will move to the heap.
source§impl<T, const C: usize, S> SmallSet<T, C, S>
impl<T, const C: usize, S> SmallSet<T, C, S>
sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Inserts the specified value into this set.
If an equivalent item already exists in the set, it returns false
leaving the original value in the set and without altering its insertion
order. Otherwise, it inserts the new item and returns true
.
If a new value is added that causes the size of the SmallSet
to exceed
the inline capacity, all existing data and the new value is moved to the
heap.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn insert_full(&mut self, value: T) -> (usize, bool)
pub fn insert_full(&mut self, value: T) -> (usize, bool)
Inserts the specified value into this set, and get their index.
If an equivalent item already exists in the set, it returns the index of
the existing item and false
, leaving the original value in the set and
without altering its insertion order. Otherwise, it inserts the new
item and returns the index of the inserted item and true
.
If a new value is added that causes the size of the SmallSet
to exceed
the inline capacity, all existing data and the new value is moved to the
heap.
Computational complexity:
- inline: O(n)
- heap: O(1)
source§impl<T, const C: usize, S> SmallSet<T, C, S>
impl<T, const C: usize, S> SmallSet<T, C, S>
pub const fn from_keys(map: SmallMap<T, (), C, S>) -> Self
sourcepub fn get_index(&self, index: usize) -> Option<&T>
pub fn get_index(&self, index: usize) -> Option<&T>
Get a value by index, if it is present, else None
.
Computational complexity: O(1)
sourcepub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
Return the item index, if it exists in the set, else None
.
Computational complexity:
- inline: O(n)
- heap: O(1)
sourcepub fn remove<Q>(&mut self, key: &Q) -> bool
pub fn remove<Q>(&mut self, key: &Q) -> bool
Remove the key-value pair equivalent to key
and return
its value.
NOTE: This is equivalent to .swap_remove(key)
, if you need to
preserve the order of the keys in the map, use .shift_remove(key)
instead.
Computes in O(1) time (average).
sourcepub fn difference<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> Difference<'a, T, C2, S2> ⓘwhere
S2: BuildHasher,
pub fn difference<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> Difference<'a, T, C2, S2> ⓘwhere
S2: BuildHasher,
Return an iterator over the values that are in self
but not other
.
Values are produced in the same order that they appear in self
.
sourcepub fn symmetric_difference<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> SymmetricDifference<'a, T, C, S, C2, S2> ⓘwhere
S2: BuildHasher,
pub fn symmetric_difference<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> SymmetricDifference<'a, T, C, S, C2, S2> ⓘwhere
S2: BuildHasher,
Return an iterator over the values that are in self
or other
,
but not in both.
Values from self
are produced in their original order, followed by
values from other
in their original order.
sourcepub fn intersection<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> Intersection<'a, T, C2, S2> ⓘwhere
S2: BuildHasher,
pub fn intersection<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> Intersection<'a, T, C2, S2> ⓘwhere
S2: BuildHasher,
Return an iterator over the values that are in both self
and other
.
Values are produced in the same order that they appear in self
.
sourcepub fn union<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> Union<'a, T, C, S> ⓘwhere
S2: BuildHasher,
pub fn union<'a, const C2: usize, S2>(
&'a self,
other: &'a SmallSet<T, C2, S2>,
) -> Union<'a, T, C, S> ⓘwhere
S2: BuildHasher,
Return an iterator over all values that are in self
or other
.
Values from self
are produced in their original order, followed by
values that are unique to other
in their original order.
Trait Implementations§
source§impl<T, const C: usize, S> FromIterator<T> for SmallSet<T, C, S>
impl<T, const C: usize, S> FromIterator<T> for SmallSet<T, C, S>
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 C: usize, S> IntoIterator for &'a SmallSet<T, C, S>
impl<'a, T, const C: usize, S> IntoIterator for &'a SmallSet<T, C, S>
source§impl<T, const C: usize, S> IntoIterator for SmallSet<T, C, S>
impl<T, const C: usize, S> IntoIterator for SmallSet<T, C, S>
source§impl<T, const C: usize, S> PartialEq for SmallSet<T, C, S>
impl<T, const C: usize, S> PartialEq for SmallSet<T, C, S>
impl<T, const C: usize, S> Eq for SmallSet<T, C, S>
Auto Trait Implementations§
impl<T, const C: usize, S> Freeze for SmallSet<T, C, S>
impl<T, const C: usize, S> RefUnwindSafe for SmallSet<T, C, S>where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, const C: usize, S> Send for SmallSet<T, C, S>
impl<T, const C: usize, S> Sync for SmallSet<T, C, S>
impl<T, const C: usize, S> Unpin for SmallSet<T, C, S>
impl<T, const C: usize, S> UnwindSafe for SmallSet<T, C, S>
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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.