Struct smolset::SmolSet[][src]

pub struct SmolSet<A: Array> where
    A::Item: PartialEq + Eq
{ /* fields omitted */ }
Expand description

A SmolSet is an unordered set of elements. It is designed to work best for very small sets (no more than ten or so elements). In order to support small sets very efficiently, it stores elements in a simple unordered array. When the set is smaller than the size of the array A, all elements are stored inline, without heap allocation. This is accomplished by using a smallvec::SmallVec.

The insert, remove, and query methods on SmolSet have O(n) time complexity in the current set size: they perform a linear scan to determine if the element in question is present. This is inefficient for large sets, but fast and cache-friendly for small sets.

Example usage:

use smolset::SmolSet;

// `s` and its elements will be completely stack-allocated in this example.
let mut s: SmolSet<[u32; 4]> = SmolSet::new();
s.insert(1);
s.insert(2);
s.insert(3);
assert_eq!(s.len(), 3);
assert!(s.contains(&1));

TODO: Add the ability to switch modes explicitly.

Implementations

impl<A: Array> SmolSet<A> where
    A::Item: PartialEq + Eq + Hash
[src]

pub fn new() -> SmolSet<A>[src]

Creates a new, empty SmolSet.

pub fn mode(&self) -> SetMode[src]

pub fn is_empty(&self) -> bool[src]

Returns the number of elements in this set.

pub fn insert(&mut self, elem: A::Item) -> bool[src]

Inserts elem into the set if not yet present. Returns true if the set did not have this element present, or false if it already had this element present.

pub fn remove(&mut self, elem: &A::Item) -> bool[src]

Removes elem from the set. Returns true if the element was removed, or false if it was not found.

pub fn contains(&self, elem: &A::Item) -> bool[src]

Tests whether elem is present. Returns true if it is present, or false if not.

pub fn iter(&self) -> SmolSetIter<'_, A>

Notable traits for SmolSetIter<'a, A>

impl<'a, A: Array> Iterator for SmolSetIter<'a, A> where
    A::Item: PartialEq + Eq + Hash + 'a, 
type Item = &'a A::Item;
[src]

Returns an iterator over the set elements. Elements will be returned in an arbitrary (unsorted) order.

pub fn len(&self) -> usize[src]

Returns the current length of the set.

pub fn clear(&mut self)[src]

Clears the set.

pub fn get(&self, elem: &A::Item) -> Option<&A::Item>[src]

If the given elem exists in the set, returns the reference to the value inside the set. Where they are equal (in the case where the set is in stack mode) or they hash equally (if the set is in heap mode).

pub fn take(&mut self, value: &A::Item) -> Option<A::Item>[src]

If the given elem exists in the set, returns the value inside the set where they are either equal or hash equally. Then, remove that value from the set.

pub fn replace(&mut self, value: A::Item) -> Option<A::Item>[src]

Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.

pub fn drain(&mut self) -> SmallDrain<A::Item>

Notable traits for SmallDrain<T>

impl<T> Iterator for SmallDrain<T> type Item = T;
[src]

Empties the set and returns an iterator over it.

pub fn retain<F>(&mut self, f: F) where
    F: FnMut(&mut A::Item) -> bool + for<'r> FnMut(&'r <A as Array>::Item) -> bool
[src]

Removes all elements in the set that does not satisfy the given predicate f.

pub fn intersection<'a>(
    &'a self,
    other: &'a Self
) -> SmallIntersection<'a, A::Item>

Notable traits for SmallIntersection<'a, T>

impl<'a, T> Iterator for SmallIntersection<'a, T> type Item = &'a T;
[src]

Returns an iterator over the intersection of the 2 sets.

pub fn union<'a>(&'a self, other: &'a Self) -> SmallUnion<'a, A::Item>

Notable traits for SmallUnion<'a, T>

impl<'a, T> Iterator for SmallUnion<'a, T> type Item = &'a T;
[src]

Returns an iterator over the union of the 2 sets.

pub fn difference<'a>(&'a self, other: &'a Self) -> SmallDifference<'a, A::Item>

Notable traits for SmallDifference<'a, T>

impl<'a, T> Iterator for SmallDifference<'a, T> type Item = &'a T;
[src]

Returns an iterator over the difference of the 2 sets.

pub fn symmetric_difference<'a>(
    &'a self,
    other: &'a Self
) -> SmallSymmetricDifference<'a, A::Item>

Notable traits for SmallSymmetricDifference<'a, T>

impl<'a, T> Iterator for SmallSymmetricDifference<'a, T> type Item = &'a T;
[src]

Returns an iterator over the symmetric difference of the 2 sets.

Trait Implementations

impl<A: Array> Clone for SmolSet<A> where
    A::Item: PartialEq + Eq + Clone
[src]

fn clone(&self) -> SmolSet<A>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<A: Array> Debug for SmolSet<A> where
    A::Item: PartialEq + Eq + Debug
[src]

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

Formats the value using the given formatter. Read more

impl<A: Array> Default for SmolSet<A> where
    A::Item: PartialEq + Eq + Hash
[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

impl<A: Array> FromIterator<<A as Array>::Item> for SmolSet<A> where
    A::Item: PartialEq + Eq + Hash
[src]

fn from_iter<T>(iter: T) -> Self where
    T: IntoIterator<Item = A::Item>, 
[src]

Creates a value from an iterator. Read more

impl<A> IntoIterator for SmolSet<A> where
    A: Array,
    A::Item: PartialEq + Eq + Hash
[src]

Implement into iterator for the SmolSet

type Item = A::Item

The type of the elements being iterated over.

type IntoIter = SmolSetIntoIter<A>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<A: Array> PartialEq<SmolSet<A>> for SmolSet<A> where
    A::Item: Eq + PartialEq + Hash
[src]

fn eq(&self, other: &Self) -> bool[src]

This method tests for self and other values to be equal, and is used by ==. Read more

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]

This method tests for !=.

Auto Trait Implementations

impl<A> RefUnwindSafe for SmolSet<A> where
    A: RefUnwindSafe,
    <A as Array>::Item: RefUnwindSafe

impl<A> Send for SmolSet<A> where
    <A as Array>::Item: Send

impl<A> Sync for SmolSet<A> where
    A: Sync,
    <A as Array>::Item: Sync

impl<A> Unpin for SmolSet<A> where
    A: Unpin,
    <A as Array>::Item: Unpin

impl<A> UnwindSafe for SmolSet<A> where
    A: UnwindSafe,
    <A as Array>::Item: RefUnwindSafe + UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.