Set

Struct Set 

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

Source

pub const fn new() -> Self

Make it.

The size of the set is defined by the generic argument. For example, this is how you make a set of four key-values pairs:

Source§

impl<T: PartialEq, const N: usize> Set<T, N>

Source

pub const fn capacity(&self) -> usize

Get its total capacity.

Source

pub const fn is_empty(&self) -> bool

Is it empty?

Source

pub const fn len(&self) -> usize

Return the total number of pairs inside.

Source

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.

Source

pub fn contains_key<Q: PartialEq + ?Sized>(&self, k: &Q) -> bool
where T: Borrow<Q>,

Does the set contain this key?

Source

pub fn remove<Q: PartialEq + ?Sized>(&mut self, k: &Q) -> bool
where T: Borrow<Q>,

Removes a value from the set. Returns whether the value was present in the set.

Source

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

Source

pub fn get<Q: PartialEq + ?Sized>(&self, k: &Q) -> Option<&T>
where T: Borrow<Q>,

Get a reference to a single value.

Source

pub fn clear(&mut self)

Remove all pairs from it, but keep the space intact for future use.

Source

pub fn retain<F: Fn(&T) -> bool>(&mut self, f: F)

Retains only the elements specified by the predicate.

Source

pub fn take<Q: PartialEq + ?Sized>(&mut self, k: &Q) -> Option<T>
where T: Borrow<Q>,

Removes a key from the set, returning the stored key and value if the key was previously in the set.

Source§

impl<T: PartialEq, const N: usize> Set<T, N>

Source

pub fn iter(&self) -> SetIter<'_, T>

Make an iterator over all pairs.

Trait Implementations§

Source§

impl<T: Clone + PartialEq, const N: usize> Clone for Set<T, N>

Source§

fn clone(&self) -> Self

Returns a duplicate 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: PartialEq + Debug, const N: usize> Debug for Set<T, N>

Source§

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

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

impl<T: PartialEq, const N: usize> Default for Set<T, N>

Source§

fn default() -> Self

Make a default empty Set.

Source§

impl<T: PartialEq + Display, const N: usize> Display for Set<T, N>

Source§

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

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

impl<T: PartialEq, const N: usize> From<[T; N]> for Set<T, N>

Source§

fn from(arr: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T: PartialEq, const N: usize> FromIterator<T> for Set<T, N>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, T: PartialEq, const N: usize> IntoIterator for &'a Set<T, N>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = SetIter<'a, T>

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: PartialEq, const N: usize> IntoIterator for Set<T, N>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = SetIntoIter<T, N>

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: PartialEq, const N: usize> PartialEq for Set<T, N>

Source§

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);
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: Eq, const N: usize> Eq for Set<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Set<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for Set<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for Set<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Set<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for Set<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for Set<T, N>
where T: UnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.