pub struct IndexSet<T> where
T: ToIndex, { /* fields omitted */ }
An ordered set that stores indices in a sparse bit field.
At first glance, IndexSet
looks a lot like a normal set, however there is
an important difference: rather than owning and storing elements, IndexSet
only stores their indices. Thus IndexSet
is less about storing elements,
and more about logical groupings of elements; IndexSet
s do not take
ownership of their elements, or even a reference to them, and elements can
appear in multiple IndexSet
s at once.
Of course, to store items by index requires them to be indexable in the
first place; IndexSet
works with any type that implements ToIndex
(and
optionally FromIndex
).
Basic usage:
use index_set::*;
struct User {
id: u32,
}
impl ToIndex for User {
type Index = u32;
#[inline]
fn to_index(&self) -> u32 { self.id }
}
let alice = User { id: 0 };
let bob = User { id: 1 };
let eve = User { id: 2 };
let mut admin_users = IndexSet::<User>::new();
admin_users.insert(&alice);
let mut trusted_users = IndexSet::<User>::new();
trusted_users.insert(&alice);
trusted_users.insert(&bob);
assert_eq!(admin_users.contains(&alice), true);
assert_eq!(trusted_users.contains(&alice), true);
assert_eq!(trusted_users.contains(&bob), true);
assert_eq!(trusted_users.contains(&eve), false);
For types that implement FromIndex
, IndexSet
can iterate through the
actual elements, working much like a normal collection:
use index_set::*;
#[derive(Debug, PartialEq, Eq)]
struct Token(u32);
impl ToIndex for Token {
type Index = u32;
#[inline]
fn to_index(&self) -> u32 { self.0 }
}
impl FromIndex for Token {
#[inline]
fn from_index(index: u32) -> Option<Token> { Some(Token(index)) }
}
let mut token_set = IndexSet::<Token>::new();
token_set.insert(&Token(0));
token_set.insert(&Token(1));
let tokens: Vec<Token> = token_set.iter().collect();
assert_eq!(tokens, [Token(0), Token(1)]);
ToIndex
and FromIndex
are defined for all the primitive integer
types:
use index_set::IndexSet;
let a: IndexSet<u32> = [0, 1, 2, 3, 4].iter().collect();
let b: IndexSet<u32> = [0, 2, 4, 6, 8].iter().collect();
let intersection: Vec<u32> = a.intersection(&b).collect();
assert_eq!(intersection, [0, 2, 4]);
ToIndex
and FromIndex
are defined for a few other useful types:
use index_set::IndexSet;
use std::net::Ipv4Addr;
let mut blacklist = IndexSet::<Ipv4Addr>::new();
blacklist.insert(&"127.0.0.1".parse().unwrap());
assert!(blacklist.contains(&"127.0.0.1".parse().unwrap()));
Creates an empty set.
use index_set::IndexSet;
let set = IndexSet::<u32>::new();
assert_eq!(set.len(), 0);
Creates a set from an iterator of indices.
This method does not panic, but using it to insert an invalid index (i.e.
one for which FromIndex::from_index
returns None
) will cause a panic
when iterating through the set.
use index_set::IndexSet;
let set = IndexSet::<u32>::from_indices([1, 2].iter().cloned());
assert!(set.contains_index(1));
assert!(set.contains_index(2));
assert!(!set.contains_index(3));
Removes all elements from the set.
use index_set::IndexSet;
let mut set: IndexSet<_> = [1, 64].iter().collect();
assert_eq!(set.len(), 2);
set.clear();
assert_eq!(set.len(), 0);
Returns true
if the set contains the given element.
use index_set::IndexSet;
let mut set = IndexSet::new();
assert!(!set.contains(&3));
set.insert(&3);
assert!(set.contains(&3));
Returns true
if the set contains the given index.
use index_set::IndexSet;
let mut set = IndexSet::new();
assert!(!set.contains_index(3));
set.insert(&3);
assert!(set.contains_index(3));
Inserts the given element into the set, returning true
if the set did not
already contain the element.
use index_set::IndexSet;
let mut set = IndexSet::new();
assert_eq!(set.insert(&3), true);
assert_eq!(set.insert(&3), false);
Inserts the given index into the set, returning true
if the set did not
already contain the index.
This method does not panic, but using it to insert an invalid index (i.e.
one for which FromIndex::from_index
returns None
) will cause a panic
when iterating through the set.
use index_set::IndexSet;
let mut set = IndexSet::<u32>::new();
assert_eq!(set.insert_index(3), true);
assert_eq!(set.insert_index(3), false);
Removes the given element from the set, returning true
if the set
previously contained the element.
use index_set::IndexSet;
let mut set = IndexSet::new();
set.insert(&42);
assert_eq!(set.remove(&42), true);
assert_eq!(set.remove(&42), false);
Removes the given index from the set, returning true
if the set
previously contained the index.
use index_set::IndexSet;
let mut set = IndexSet::new();
set.insert(&42);
assert_eq!(set.remove_index(42), true);
assert_eq!(set.remove_index(42), false);
Returns the number of elements in the set.
use index_set::IndexSet;
let set: IndexSet<_> = [1, 2, 3].iter().collect();
assert_eq!(set.len(), 3);
Returns true
if the set contains no eements.
use index_set::IndexSet;
let mut set = IndexSet::new();
assert!(set.is_empty());
set.insert(&42);
assert!(!set.is_empty());
Returns an iterator that yields the indices in the set, in ascending order.
use index_set::{IndexSet, ToIndex};
struct Token(u32);
impl ToIndex for Token {
type Index = u32;
fn to_index(&self) -> u32 { self.0 * 10 }
}
let mut set = IndexSet::new();
set.insert(&Token(1));
set.insert(&Token(2));
set.insert(&Token(3));
let indices: Vec<_> = set.indices().collect();
assert_eq!(indices, [10, 20, 30]);
Returns an iterator that yields the indices that are in self
but not in
other
, in ascending order.
use index_set::{IndexSet, ToIndex};
struct Token(u32);
impl ToIndex for Token {
type Index = u32;
fn to_index(&self) -> u32 { self.0 * 10 }
}
let mut a = IndexSet::new();
a.insert(&Token(1));
a.insert(&Token(2));
let mut b = IndexSet::new();
b.insert(&Token(2));
b.insert(&Token(3));
let difference: Vec<_> = a.difference_indices(&b).collect();
assert_eq!(difference, [10]);
Returns an iterator that yields the indices that are in self
or in other
but not in both, in ascending order.
use index_set::{IndexSet, ToIndex};
struct Token(u32);
impl ToIndex for Token {
type Index = u32;
fn to_index(&self) -> u32 { self.0 * 10 }
}
let mut a = IndexSet::new();
a.insert(&Token(1));
a.insert(&Token(2));
let mut b = IndexSet::new();
b.insert(&Token(2));
b.insert(&Token(3));
let symmetric_difference: Vec<_> = a.symmetric_difference_indices(&b).collect();
assert_eq!(symmetric_difference, [10, 30]);
Returns an iterator that yields the indices that are in both self
and
other
, in ascending order.
use index_set::{IndexSet, ToIndex};
struct Token(u32);
impl ToIndex for Token {
type Index = u32;
fn to_index(&self) -> u32 { self.0 * 10 }
}
let mut a = IndexSet::new();
a.insert(&Token(1));
a.insert(&Token(2));
let mut b = IndexSet::new();
b.insert(&Token(2));
b.insert(&Token(3));
let intersection: Vec<_> = a.intersection_indices(&b).collect();
assert_eq!(intersection, [20]);
Returns an iterator that yields the indices that are in either self
or
other
, in ascending order.
use index_set::{IndexSet, ToIndex};
struct Token(u32);
impl ToIndex for Token {
type Index = u32;
fn to_index(&self) -> u32 { self.0 * 10 }
}
let mut a = IndexSet::new();
a.insert(&Token(1));
a.insert(&Token(2));
let mut b = IndexSet::new();
b.insert(&Token(2));
b.insert(&Token(3));
let union: Vec<_> = a.union_indices(&b).collect();
assert_eq!(union, [10, 20, 30]);
Returns true
if self
and other
do not contain any of the same
elements.
use index_set::IndexSet;
let mut a: IndexSet<_> = [1, 2, 3].iter().collect();
let b: IndexSet<_> = [4, 5, 6].iter().collect();
assert!(a.is_disjoint(&b));
a.insert(&4);
assert!(!a.is_disjoint(&b));
Returns true
if other
contains at least all the elements in self
.
use index_set::IndexSet;
let mut a: IndexSet<_> = [1, 2].iter().collect();
let b: IndexSet<_> = [1, 2, 3].iter().collect();
assert!(a.is_subset(&b));
a.insert(&4);
assert!(!a.is_subset(&b));
Returns true
if self
contains at least all the elements in other
.
use index_set::IndexSet;
let mut a: IndexSet<_> = [1, 2].iter().collect();
let b: IndexSet<_> = [1, 2, 3].iter().collect();
assert!(b.is_superset(&a));
a.insert(&4);
assert!(!b.is_superset(&a));
Inserts the indices in the iterator into the set.
This method does not panic, but using it to insert an invalid index (i.e.
one for which FromIndex::from_index
returns None
) will cause a panic
when iterating through the set.
use index_set::IndexSet;
let mut set = IndexSet::<u32>::new();
set.extend_indices([1, 2].iter().cloned());
assert!(set.contains_index(1));
assert!(set.contains_index(2));
assert!(!set.contains_index(3));
Returns an iterator that yields the elements in the set, in ascending order.
If T
does not implement FromIndex
, then you will need to use the
indices
method instead.
Iterating through a set that contains an invalid index (i.e. one for which
FromIndex::from_index
returns None
) will cause a panic.
use index_set::IndexSet;
let set: IndexSet<_> = [3, 1, 2].iter().collect();
let mut iter = set.iter();
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), Some(3));
assert_eq!(iter.next(), None);
Returns an iterator that yields the elements that are in self
but not in
other
, in ascending order.
If T
does not implement FromIndex
, then you will need to use the
difference_indices
or sub
methods instead.
Iterating through a set that contains an invalid index (i.e. one for which
FromIndex::from_index
returns None
) will cause a panic.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 1000, 2000].iter().collect();
let b: IndexSet<_> = [2, 3, 2000, 3000].iter().collect();
let difference: Vec<_> = a.difference(&b).collect();
assert_eq!(difference, [1, 1000]);
Returns an iterator that yields the elements that are in self
or in
other
but not in both, in asccending order.
If T
does not implement FromIndex
, then you will need to use the
symmetric_difference_indices
or bitxor
methods instead.
Iterating through a set that contains an invalid index (i.e. one for which
FromIndex::from_index
returns None
) will cause a panic.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 1000, 2000].iter().collect();
let b: IndexSet<_> = [2, 3, 2000, 3000].iter().collect();
let symmetric_difference: Vec<_> = a.symmetric_difference(&b).collect();
assert_eq!(symmetric_difference, [1, 3, 1000, 3000]);
Returns an iterator that yields the elements that are in both self
and
other
, in asccending order.
If T
does not implement FromIndex
, then you will need to use the
intersection_indices
or bitand
methods instead.
Iterating through a set that contains an invalid index (i.e. one for which
FromIndex::from_index
returns None
) will cause a panic.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 1000, 2000].iter().collect();
let b: IndexSet<_> = [2, 3, 2000, 3000].iter().collect();
let intersection: Vec<_> = a.intersection(&b).collect();
assert_eq!(intersection, [2, 2000]);
Returns an iterator that yields the elements that are in either self
or
other
, in asccending order.
If T
does not implement FromIndex
, then you will need to use the
union_indices
or bitor
methods instead.
Iterating through a set that contains an invalid index (i.e. one for which
FromIndex::from_index
returns None
) will cause a panic.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 1000, 2000].iter().collect();
let b: IndexSet<_> = [2, 3, 2000, 3000].iter().collect();
let union: Vec<_> = a.union(&b).collect();
assert_eq!(union, [1, 2, 3, 1000, 2000, 3000]);
type Item = T
The type of the elements being iterated over.
type IntoIter = IntoIter<T>
Which kind of iterator are we turning this into?
type Item = T
The type of the elements being iterated over.
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
Extends a collection with the contents of an iterator. Read more
Extends a collection with the contents of an iterator. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Performs copy-assignment from source
. Read more
Compares the sets by comparing their indices lexicographically.
use index_set::IndexSet;
let mut a: IndexSet<_> = [1, 2, 4].iter().collect();
let mut b: IndexSet<_> = [2, 3, 4].iter().collect();
assert!(&a < &b);
b.insert(&1);
assert!(&a > &b);
a.insert(&3);
assert!(&a == &b);
fn max(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the minimum of two values. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
type Output = IndexSet<T>
The resulting type after applying the -
operator.
Returns the difference of self
and rhs
.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 3].iter().collect();
let b: IndexSet<_> = [3, 4, 5].iter().collect();
assert_eq!(&a - &b, [1, 2].iter().collect());
type Output = IndexSet<T>
The resulting type after applying the &
operator.
Returns the intersection of self
and rhs
.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 3].iter().collect();
let b: IndexSet<_> = [3, 4, 5].iter().collect();
assert_eq!(&a & &b, [3].iter().collect());
type Output = IndexSet<T>
The resulting type after applying the |
operator.
Returns the union of self
and rhs
.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 3].iter().collect();
let b: IndexSet<_> = [3, 4, 5].iter().collect();
assert_eq!(&a | &b, [1, 2, 3, 4, 5].iter().collect());
type Output = IndexSet<T>
The resulting type after applying the ^
operator.
Returns the symmetric difference of self
and rhs
.
use index_set::IndexSet;
let a: IndexSet<_> = [1, 2, 3].iter().collect();
let b: IndexSet<_> = [3, 4, 5].iter().collect();
assert_eq!(&a ^ &b, [1, 2, 4, 5].iter().collect());
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
type Owned = T
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
type Error = !
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static