pub struct IndexSet<T, S, const N: usize> { /* private fields */ }
Expand description
Fixed capacity IndexSet
.
Note that you cannot use IndexSet
directly, since it is generic around the hashing algorithm
in use. Pick a concrete instantiation like FnvIndexSet
instead
or create your own.
Note that the capacity of the IndexSet
must be a power of 2.
Examples
Since IndexSet
cannot be used directly, we’re using its FnvIndexSet
instantiation
for this example.
use heapless::FnvIndexSet;
// A hash set with a capacity of 16 elements allocated on the stack
let mut books = FnvIndexSet::<_, 16>::new();
// Add some books.
books.insert("A Dance With Dragons").unwrap();
books.insert("To Kill a Mockingbird").unwrap();
books.insert("The Odyssey").unwrap();
books.insert("The Great Gatsby").unwrap();
// Check for a specific one.
if !books.contains("The Winds of Winter") {
println!("We have {} books, but The Winds of Winter ain't one.",
books.len());
}
// Remove a book.
books.remove("The Odyssey");
// Iterate over everything.
for book in &books {
println!("{}", book);
}
Implementations
sourceimpl<T, S, const N: usize> IndexSet<T, BuildHasherDefault<S>, N>
impl<T, S, const N: usize> IndexSet<T, BuildHasherDefault<S>, N>
sourceimpl<T, S, const N: usize> IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher,
impl<T, S, const N: usize> IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher,
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the set can hold
Examples
use heapless::FnvIndexSet;
let set = FnvIndexSet::<i32, 16>::new();
assert_eq!(set.capacity(), 16);
sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Return an iterator over the values of the set, in their order
Examples
use heapless::FnvIndexSet;
let mut set = FnvIndexSet::<_, 16>::new();
set.insert("a").unwrap();
set.insert("b").unwrap();
// Will print in an arbitrary order.
for x in set.iter() {
println!("{}", x);
}
sourcepub fn difference<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> Difference<'a, T, S2, N2> where
S2: BuildHasher,
pub fn difference<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> Difference<'a, T, S2, N2> where
S2: BuildHasher,
Visits the values representing the difference, i.e. the values that are in self
but not in
other
.
Examples
use heapless::FnvIndexSet;
let mut a: FnvIndexSet<_, 16> = [1, 2, 3].iter().cloned().collect();
let mut b: FnvIndexSet<_, 16> = [4, 2, 3, 4].iter().cloned().collect();
// Can be seen as `a - b`.
for x in a.difference(&b) {
println!("{}", x); // Print 1
}
let diff: FnvIndexSet<_, 16> = a.difference(&b).collect();
assert_eq!(diff, [1].iter().collect::<FnvIndexSet<_, 16>>());
// Note that difference is not symmetric,
// and `b - a` means something else:
let diff: FnvIndexSet<_, 16> = b.difference(&a).collect();
assert_eq!(diff, [4].iter().collect::<FnvIndexSet<_, 16>>());
sourcepub fn symmetric_difference<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> impl Iterator<Item = &'a T> where
S2: BuildHasher,
pub fn symmetric_difference<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> impl Iterator<Item = &'a T> where
S2: BuildHasher,
Visits the values representing the symmetric difference, i.e. the values that are in self
or in other
but not in both.
Examples
use heapless::FnvIndexSet;
let mut a: FnvIndexSet<_, 16> = [1, 2, 3].iter().cloned().collect();
let mut b: FnvIndexSet<_, 16> = [4, 2, 3, 4].iter().cloned().collect();
// Print 1, 4 in that order order.
for x in a.symmetric_difference(&b) {
println!("{}", x);
}
let diff1: FnvIndexSet<_, 16> = a.symmetric_difference(&b).collect();
let diff2: FnvIndexSet<_, 16> = b.symmetric_difference(&a).collect();
assert_eq!(diff1, diff2);
assert_eq!(diff1, [1, 4].iter().collect::<FnvIndexSet<_, 16>>());
sourcepub fn intersection<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> Intersection<'a, T, S2, N2> where
S2: BuildHasher,
pub fn intersection<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> Intersection<'a, T, S2, N2> where
S2: BuildHasher,
Visits the values representing the intersection, i.e. the values that are both in self
and
other
.
Examples
use heapless::FnvIndexSet;
let mut a: FnvIndexSet<_, 16> = [1, 2, 3].iter().cloned().collect();
let mut b: FnvIndexSet<_, 16> = [4, 2, 3, 4].iter().cloned().collect();
// Print 2, 3 in that order.
for x in a.intersection(&b) {
println!("{}", x);
}
let intersection: FnvIndexSet<_, 16> = a.intersection(&b).collect();
assert_eq!(intersection, [2, 3].iter().collect::<FnvIndexSet<_, 16>>());
sourcepub fn union<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> impl Iterator<Item = &'a T> where
S2: BuildHasher,
pub fn union<'a, S2, const N2: usize>(
&'a self,
other: &'a IndexSet<T, S2, N2>
) -> impl Iterator<Item = &'a T> where
S2: BuildHasher,
Visits the values representing the union, i.e. all the values in self
or other
, without
duplicates.
Examples
use heapless::FnvIndexSet;
let mut a: FnvIndexSet<_, 16> = [1, 2, 3].iter().cloned().collect();
let mut b: FnvIndexSet<_, 16> = [4, 2, 3, 4].iter().cloned().collect();
// Print 1, 2, 3, 4 in that order.
for x in a.union(&b) {
println!("{}", x);
}
let union: FnvIndexSet<_, 16> = a.union(&b).collect();
assert_eq!(union, [1, 2, 3, 4].iter().collect::<FnvIndexSet<_, 16>>());
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the set.
Examples
use heapless::FnvIndexSet;
let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
assert_eq!(v.len(), 0);
v.insert(1).unwrap();
assert_eq!(v.len(), 1);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the set contains no elements.
Examples
use heapless::FnvIndexSet;
let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
assert!(v.is_empty());
v.insert(1).unwrap();
assert!(!v.is_empty());
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the set, removing all values.
Examples
use heapless::FnvIndexSet;
let mut v: FnvIndexSet<_, 16> = FnvIndexSet::new();
v.insert(1).unwrap();
v.clear();
assert!(v.is_empty());
sourcepub fn contains<Q>(&self, value: &Q) -> bool where
T: Borrow<Q>,
Q: ?Sized + Eq + Hash,
pub fn contains<Q>(&self, value: &Q) -> bool where
T: Borrow<Q>,
Q: ?Sized + Eq + Hash,
Returns true
if the set contains a value.
The value may be any borrowed form of the set’s value type, but Hash
and Eq
on the
borrowed form must match those for the value type.
Examples
use heapless::FnvIndexSet;
let set: FnvIndexSet<_, 16> = [1, 2, 3].iter().cloned().collect();
assert_eq!(set.contains(&1), true);
assert_eq!(set.contains(&4), false);
sourcepub fn is_disjoint<S2, const N2: usize>(
&self,
other: &IndexSet<T, S2, N2>
) -> bool where
S2: BuildHasher,
pub fn is_disjoint<S2, const N2: usize>(
&self,
other: &IndexSet<T, S2, N2>
) -> bool where
S2: BuildHasher,
Returns true
if self
has no elements in common with other
. This is equivalent to
checking for an empty intersection.
Examples
use heapless::FnvIndexSet;
let a: FnvIndexSet<_, 16> = [1, 2, 3].iter().cloned().collect();
let mut b = FnvIndexSet::<_, 16>::new();
assert_eq!(a.is_disjoint(&b), true);
b.insert(4).unwrap();
assert_eq!(a.is_disjoint(&b), true);
b.insert(1).unwrap();
assert_eq!(a.is_disjoint(&b), false);
sourcepub fn is_subset<S2, const N2: usize>(
&self,
other: &IndexSet<T, S2, N2>
) -> bool where
S2: BuildHasher,
pub fn is_subset<S2, const N2: usize>(
&self,
other: &IndexSet<T, S2, N2>
) -> bool where
S2: BuildHasher,
Returns true
if the set is a subset of another, i.e. other
contains at least all the
values in self
.
Examples
use heapless::FnvIndexSet;
let sup: FnvIndexSet<_, 16> = [1, 2, 3].iter().cloned().collect();
let mut set = FnvIndexSet::<_, 16>::new();
assert_eq!(set.is_subset(&sup), true);
set.insert(2).unwrap();
assert_eq!(set.is_subset(&sup), true);
set.insert(4).unwrap();
assert_eq!(set.is_subset(&sup), false);
sourcepub fn is_superset<S2, const N2: usize>(
&self,
other: &IndexSet<T, S2, N2>
) -> bool where
S2: BuildHasher,
pub fn is_superset<S2, const N2: usize>(
&self,
other: &IndexSet<T, S2, N2>
) -> bool where
S2: BuildHasher,
Examples
use heapless::FnvIndexSet;
let sub: FnvIndexSet<_, 16> = [1, 2].iter().cloned().collect();
let mut set = FnvIndexSet::<_, 16>::new();
assert_eq!(set.is_superset(&sub), false);
set.insert(0).unwrap();
set.insert(1).unwrap();
assert_eq!(set.is_superset(&sub), false);
set.insert(2).unwrap();
assert_eq!(set.is_superset(&sub), true);
sourcepub fn insert(&mut self, value: T) -> Result<bool, T>
pub fn insert(&mut self, value: T) -> Result<bool, T>
Adds a value to the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
Examples
use heapless::FnvIndexSet;
let mut set = FnvIndexSet::<_, 16>::new();
assert_eq!(set.insert(2).unwrap(), true);
assert_eq!(set.insert(2).unwrap(), false);
assert_eq!(set.len(), 1);
sourcepub fn remove<Q>(&mut self, value: &Q) -> bool where
T: Borrow<Q>,
Q: ?Sized + Eq + Hash,
pub fn remove<Q>(&mut self, value: &Q) -> bool where
T: Borrow<Q>,
Q: ?Sized + Eq + Hash,
Removes a value from the set. Returns true
if the value was present in the set.
The value may be any borrowed form of the set’s value type, but Hash
and Eq
on the
borrowed form must match those for the value type.
Examples
use heapless::FnvIndexSet;
let mut set = FnvIndexSet::<_, 16>::new();
set.insert(2).unwrap();
assert_eq!(set.remove(&2), true);
assert_eq!(set.remove(&2), false);
Trait Implementations
sourceimpl<T, S, const N: usize> Debug for IndexSet<T, S, N> where
T: Eq + Hash + Debug,
S: BuildHasher,
impl<T, S, const N: usize> Debug for IndexSet<T, S, N> where
T: Eq + Hash + Debug,
S: BuildHasher,
sourceimpl<T, S, const N: usize> Default for IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher + Default,
impl<T, S, const N: usize> Default for IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher + Default,
sourceimpl<'de, T, S, const N: usize> Deserialize<'de> for IndexSet<T, BuildHasherDefault<S>, N> where
T: Eq + Hash + Deserialize<'de>,
S: Hasher + Default,
impl<'de, T, S, const N: usize> Deserialize<'de> for IndexSet<T, BuildHasherDefault<S>, N> where
T: Eq + Hash + Deserialize<'de>,
S: Hasher + Default,
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<'a, T, S, const N: usize> Extend<&'a T> for IndexSet<T, S, N> where
T: 'a + Eq + Hash + Copy,
S: BuildHasher,
impl<'a, T, S, const N: usize> Extend<&'a T> for IndexSet<T, S, N> where
T: 'a + Eq + Hash + Copy,
S: BuildHasher,
sourcefn extend<I>(&mut self, iterable: I) where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iterable: I) where
I: IntoIterator<Item = &'a T>,
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T, S, const N: usize> Extend<T> for IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher,
impl<T, S, const N: usize> Extend<T> for IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher,
sourcefn extend<I>(&mut self, iterable: I) where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iterable: I) where
I: IntoIterator<Item = T>,
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T, S, const N: usize> FromIterator<T> for IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher + Default,
impl<T, S, const N: usize> FromIterator<T> for IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher + Default,
sourcefn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = T>,
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = T>,
Creates a value from an iterator. Read more
sourceimpl<'a, T, S, const N: usize> IntoIterator for &'a IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher,
impl<'a, T, S, const N: usize> IntoIterator for &'a IndexSet<T, S, N> where
T: Eq + Hash,
S: BuildHasher,
sourceimpl<T, S1, S2, const N1: usize, const N2: usize> PartialEq<IndexSet<T, S2, N2>> for IndexSet<T, S1, N1> where
T: Eq + Hash,
S1: BuildHasher,
S2: BuildHasher,
impl<T, S1, S2, const N1: usize, const N2: usize> PartialEq<IndexSet<T, S2, N2>> for IndexSet<T, S1, N1> where
T: Eq + Hash,
S1: BuildHasher,
S2: BuildHasher,
Auto Trait Implementations
impl<T, S, const N: usize> RefUnwindSafe for IndexSet<T, S, N> where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, S, const N: usize> Send for IndexSet<T, S, N> where
S: Send,
T: Send,
impl<T, S, const N: usize> Sync for IndexSet<T, S, N> where
S: Sync,
T: Sync,
impl<T, S, const N: usize> Unpin for IndexSet<T, S, N> where
S: Unpin,
T: Unpin,
impl<T, S, const N: usize> UnwindSafe for IndexSet<T, S, N> where
S: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more