pub struct SparseSet<K> { /* private fields */ }Expand description
A sparse set of keys convertible to indices.
SparseSet provides an efficient storage mechanism for sets
where keys can be converted to usize indices. It uses a bitmap to track
which indices are present, achieving both memory efficiency and fast lookup
times.
The key type K must implement Into<usize> and From<usize> to
convert between keys and indices. This makes it ideal for enum keys,
small integers, or other types with a natural index representation.
§Examples
use omp_core::{sparse_index::TrySparseIndex, sparse_set::SparseSet};
#[repr(usize)]
#[derive(Copy, Clone, Debug, PartialEq)]
enum Status {
Active = 0,
Pending = 1,
Closed = 2,
}
#[derive(Debug)]
struct StatusError(&'static str);
impl std::fmt::Display for StatusError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for StatusError {}
impl TrySparseIndex for Status {
type Error = StatusError;
fn index(&self) -> usize {
*self as usize
}
fn try_from_index(index: usize) -> Result<Self, Self::Error> {
match index {
0 => Ok(Status::Active),
1 => Ok(Status::Pending),
2 => Ok(Status::Closed),
_ => Err(StatusError("Invalid status value")),
}
}
}
let mut set = SparseSet::new();
set.insert(Status::Active);
set.insert(Status::Closed);
assert!(set.contains(Status::Active));
assert!(!set.contains(Status::Pending));Implementations§
Source§impl<K> SparseSet<K>
impl<K> SparseSet<K>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new sparse index set with the specified capacity.
§Arguments
capacity- The maximum index that might be stored
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the set as much as possible.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be
inserted in the set.
Sourcepub fn into_parts(self) -> SmolBitmap
pub fn into_parts(self) -> SmolBitmap
Sourcepub const fn from_parts(bits: SmolBitmap) -> Self
pub const fn from_parts(bits: SmolBitmap) -> Self
Constructs a sparse set from its raw bitmap.
§Arguments
bits- The bitmap tracking which indices are present
Sourcepub fn is_superset(&self, other: &Self) -> bool
pub fn is_superset(&self, other: &Self) -> bool
Returns true if the set is a superset of another.
Sourcepub fn is_disjoint(&self, other: &Self) -> bool
pub fn is_disjoint(&self, other: &Self) -> bool
Returns true if the set has no elements in common with another.
Sourcepub fn intersection(&self, other: &Self) -> Self
pub fn intersection(&self, other: &Self) -> Self
Computes the intersection with another set.
Sourcepub fn difference(&self, other: &Self) -> Self
pub fn difference(&self, other: &Self) -> Self
Computes the difference with another set.
Sourcepub fn symmetric_difference(&self, other: &Self) -> Self
pub fn symmetric_difference(&self, other: &Self) -> Self
Computes the symmetric difference with another set.
Source§impl<K: TrySparseIndex> SparseSet<K>
impl<K: TrySparseIndex> SparseSet<K>
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 keys k such that f(&k) returns false.
Sourcepub fn iter(&self) -> Iter<'_, K>
pub fn iter(&self) -> Iter<'_, K>
Returns an iterator over the keys of the set.
The iterator yields keys in the order of their index values.
Sourcepub fn first(&self) -> Option<K>
pub fn first(&self) -> Option<K>
Returns the minimum (first) element in the set, or None if the set is
empty.
Trait Implementations§
Source§impl<'de, K: TrySparseIndex + Deserialize<'de>> Deserialize<'de> for SparseSet<K>
impl<'de, K: TrySparseIndex + Deserialize<'de>> Deserialize<'de> for SparseSet<K>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
impl<K> Eq for SparseSet<K>
Source§impl<K: TrySparseIndex> Extend<K> for SparseSet<K>
impl<K: TrySparseIndex> Extend<K> for SparseSet<K>
Source§fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = K>>(&mut self, iter: T)
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<K: TrySparseIndex> FromIterator<K> for SparseSet<K>
impl<K: TrySparseIndex> FromIterator<K> for SparseSet<K>
Source§fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = K>>(iter: T) -> Self
Source§impl<'a, K: TrySparseIndex> IntoIterator for &'a SparseSet<K>
impl<'a, K: TrySparseIndex> IntoIterator for &'a SparseSet<K>
Source§type IntoIter = impl DoubleEndedIterator + ExactSizeIterator + FusedIterator + Clone
type IntoIter = impl DoubleEndedIterator + ExactSizeIterator + FusedIterator + Clone
Source§impl<K: TrySparseIndex> IntoIterator for SparseSet<K>
impl<K: TrySparseIndex> IntoIterator for SparseSet<K>
Source§type IntoIter = impl DoubleEndedIterator + ExactSizeIterator + FusedIterator + Clone
type IntoIter = impl DoubleEndedIterator + ExactSizeIterator + FusedIterator + Clone
Auto Trait Implementations§
impl<K> Freeze for SparseSet<K>
impl<K> RefUnwindSafe for SparseSet<K>where
K: RefUnwindSafe,
impl<K> Send for SparseSet<K>where
K: Send,
impl<K> Sync for SparseSet<K>where
K: Sync,
impl<K> Unpin for SparseSet<K>where
K: Unpin,
impl<K> UnsafeUnpin for SparseSet<K>
impl<K> UnwindSafe for SparseSet<K>where
K: 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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more