pub struct Set<T: PartialEq, const N: usize> { /* private fields */ }Expand description
A faster alternative of std::collections::HashSet.
For example, this is how you make a set, which is allocated on stack and is capable of storing up to eight key-values pairs:
let mut m : micromap_rawl::Set<u64, 8> = micromap_rawl::Set::new();
m.insert(1);
m.insert(2);
assert_eq!(2, m.len());It is faster because it doesn’t use a hash function at all. It simply keeps
all pairs in an array and when it’s necessary to find a value, it goes through
all pairs comparing the needle with each pair available. Also it is faster
because it doesn’t use heap. When a Set is being created, it allocates the necessary
space on stack. That’s why the maximum size of the set must be provided in
compile time.
It is also faster because it doesn’t grow in size. When a Set is created,
its size is fixed on stack. If an attempt is made to insert too many keys
into it, it simply panics. Moreover, in the “release” mode it doesn’t panic,
but its behaviour is undefined. In the “release” mode all boundary checks
are disabled, for the sake of higher performance.
Implementations§
Source§impl<T: PartialEq, const N: usize> Set<T, N>
impl<T: PartialEq, const N: usize> Set<T, N>
Sourcepub fn drain(&mut self) -> SetDrain<'_, T> ⓘ
pub fn drain(&mut self) -> SetDrain<'_, T> ⓘ
Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.
If the returned iterator is dropped before being fully consumed, it drops the remaining elements. The returned iterator keeps a mutable borrow on the set to optimize its implementation.
Sourcepub fn contains_key<Q: PartialEq + ?Sized>(&self, k: &Q) -> boolwhere
T: Borrow<Q>,
pub fn contains_key<Q: PartialEq + ?Sized>(&self, k: &Q) -> boolwhere
T: Borrow<Q>,
Does the set contain this key?
Sourcepub fn remove<Q: PartialEq + ?Sized>(&mut self, k: &Q) -> boolwhere
T: Borrow<Q>,
pub fn remove<Q: PartialEq + ?Sized>(&mut self, k: &Q) -> boolwhere
T: Borrow<Q>,
Removes a value from the set. Returns whether the value was present in the set.
Sourcepub fn insert(&mut self, k: T) -> bool
pub fn insert(&mut self, k: T) -> bool
Adds a value to the set.
Returns whether the value was newly inserted. That is:
If the set did not previously contain this value, true is returned. If the set already contained this value, false is returned, and the set is not modified: original value is not replaced, and the value passed as argument is dropped.
§Panics
It may panic if there are too many pairs in the set already. Pay attention,
it panics only in the “debug” mode. In the “release” mode, you are going to get
undefined behavior. This is done for the sake of performance, in order to
avoid a repetitive check for the boundary condition on every insert().
Sourcepub fn get<Q: PartialEq + ?Sized>(&self, k: &Q) -> Option<&T>where
T: Borrow<Q>,
pub fn get<Q: PartialEq + ?Sized>(&self, k: &Q) -> Option<&T>where
T: Borrow<Q>,
Get a reference to a single value.
Trait Implementations§
Source§impl<T: PartialEq, const N: usize> FromIterator<T> for Set<T, N>
impl<T: PartialEq, const N: usize> FromIterator<T> for Set<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<T: PartialEq, const N: usize> PartialEq for Set<T, N>
impl<T: PartialEq, const N: usize> PartialEq for Set<T, N>
Source§fn eq(&self, other: &Self) -> bool
fn eq(&self, other: &Self) -> bool
Two sets can be compared.
For example:
let mut m1: micromap_rawl::Set<u8, 10> = micromap_rawl::Set::new();
let mut m2: micromap_rawl::Set<u8, 10> = micromap_rawl::Set::new();
m1.insert(1);
m2.insert(1);
assert_eq!(m1, m2);
// two sets with different order of key-value pairs are still equal:
m1.insert(2);
m1.insert(3);
m2.insert(3);
m2.insert(2);
assert_eq!(m1, m2);