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> SmallSet<T, C>

source

pub fn new() -> Self

Create a new set.

source§

impl<T, const C: usize, S> SmallSet<T, C, S>

source

pub fn len(&self) -> usize

The number of values stored in the set.

source

pub fn is_empty(&self) -> bool

Returns true if the set is empty.

source

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

pub const fn is_inline(&self) -> bool

Is the data contained by this set stored inline (true) or on the heap (false).

source

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

Returns an iterator over the values in insertion order.

source§

impl<T, const C: usize, S> SmallSet<T, C, S>
where T: Hash + Eq, S: BuildHasher + Default,

source

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)
source

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>
where T: Hash + Eq, S: BuildHasher,

source

pub const fn from_keys(map: SmallMap<T, (), C, S>) -> Self

source

pub fn get_index(&self, index: usize) -> Option<&T>

Get a value by index, if it is present, else None.

Computational complexity: O(1)

source

pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
where Q: Hash + Equivalent<T> + ?Sized,

Return the item index, if it exists in the set, else None.

Computational complexity:

  • inline: O(n)
  • heap: O(1)
source

pub fn remove<Q>(&mut self, key: &Q) -> bool
where Q: Hash + Equivalent<T> + ?Sized,

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

source

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.

source

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.

source

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.

source

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.

source

pub fn contains<Q>(&self, value: &Q) -> bool
where Q: Hash + Equivalent<T> + ?Sized,

Return true if an equivalent to value exists in the set.

Computational complexity:

  • inline: O(n)
  • heap: O(1)

Trait Implementations§

source§

impl<T: Clone, const C: usize, S: Clone> Clone for SmallSet<T, C, S>

source§

fn clone(&self) -> SmallSet<T, C, S>

Returns a copy 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, const C: usize, S> Debug for SmallSet<T, C, S>
where T: Hash + Eq + Debug,

source§

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

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

impl<T: Default, const C: usize, S: Default> Default for SmallSet<T, C, S>

source§

fn default() -> SmallSet<T, C, S>

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

impl<T, const C: usize, S> FromIterator<T> for SmallSet<T, C, S>
where T: Hash + Eq, S: BuildHasher + Default,

source§

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

Creates a value from an iterator. Read more
source§

impl<T, const C: usize, S> Hash for SmallSet<T, C, S>
where T: Hash + Eq,

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, T, const C: usize, S> IntoIterator for &'a SmallSet<T, C, S>

§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
§

type Item = &'a T

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, const C: usize, S> IntoIterator for SmallSet<T, C, S>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T, C>

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, const C: usize, S> PartialEq for SmallSet<T, C, S>
where T: Hash + Eq,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, const C: usize, S> Eq for SmallSet<T, C, S>
where T: Hash + Eq,

Auto Trait Implementations§

§

impl<T, const C: usize, S> Freeze for SmallSet<T, C, S>
where S: Freeze, T: Freeze,

§

impl<T, const C: usize, S> RefUnwindSafe for SmallSet<T, C, S>

§

impl<T, const C: usize, S> Send for SmallSet<T, C, S>
where S: Send, T: Send,

§

impl<T, const C: usize, S> Sync for SmallSet<T, C, S>
where S: Sync, T: Sync,

§

impl<T, const C: usize, S> Unpin for SmallSet<T, C, S>
where S: Unpin, T: Unpin,

§

impl<T, const C: usize, S> UnwindSafe for SmallSet<T, C, S>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.