pub struct IndexSet<T: Clone + Eq + Hash> { /* private fields */ }Expand description
A bidirectional index set for efficient lookup
Provides O(1) lookup from integer index to value and from value to integer index.
§Examples
use tensor4all_tcicore::IndexSet;
let mut set: IndexSet<String> = IndexSet::new();
set.push("alpha".to_string());
set.push("beta".to_string());
set.push("alpha".to_string()); // duplicate, ignored
assert_eq!(set.len(), 2);
assert_eq!(set.get(0), Some(&"alpha".to_string()));
assert_eq!(set.pos(&"beta".to_string()), Some(1));
assert!(set.contains(&"alpha".to_string()));
assert!(!set.contains(&"gamma".to_string()));Implementations§
Source§impl<T: Clone + Eq + Hash> IndexSet<T>
impl<T: Clone + Eq + Hash> IndexSet<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty index set.
§Examples
use tensor4all_tcicore::IndexSet;
let set: IndexSet<usize> = IndexSet::new();
assert!(set.is_empty());
assert_eq!(set.len(), 0);Sourcepub fn from_vec(values: Vec<T>) -> Self
pub fn from_vec(values: Vec<T>) -> Self
Create an index set from a vector
Duplicate values are removed, keeping the first occurrence.
§Examples
use tensor4all_tcicore::IndexSet;
let set = IndexSet::from_vec(vec![10usize, 20, 10, 30]);
assert_eq!(set.len(), 3);
assert_eq!(set[0], 10);
assert_eq!(set[1], 20);
assert_eq!(set[2], 30);Sourcepub fn get(&self, i: usize) -> Option<&T>
pub fn get(&self, i: usize) -> Option<&T>
Get the value at a positional index, or None if out of range.
§Examples
use tensor4all_tcicore::IndexSet;
let set = IndexSet::from_vec(vec![10, 20, 30]);
assert_eq!(set.get(0), Some(&10));
assert_eq!(set.get(2), Some(&30));
assert_eq!(set.get(3), None);Sourcepub fn pos(&self, value: &T) -> Option<usize>
pub fn pos(&self, value: &T) -> Option<usize>
Get the positional index of a value, or None if not present.
§Examples
use tensor4all_tcicore::IndexSet;
let set = IndexSet::from_vec(vec![10, 20, 30]);
assert_eq!(set.pos(&20), Some(1));
assert_eq!(set.pos(&99), None);Sourcepub fn positions(&self, values: &[T]) -> Option<Vec<usize>>
pub fn positions(&self, values: &[T]) -> Option<Vec<usize>>
Get positional indices for a slice of values.
Returns None if any value is not present in the set.
§Examples
use tensor4all_tcicore::IndexSet;
let set = IndexSet::from_vec(vec![10, 20, 30]);
assert_eq!(set.positions(&[30, 10]), Some(vec![2, 0]));
assert_eq!(set.positions(&[10, 99]), None);Sourcepub fn push(&mut self, value: T)
pub fn push(&mut self, value: T)
Push a new value to the set.
If the value already exists in the set, this is a no-op.
§Examples
use tensor4all_tcicore::IndexSet;
let mut set = IndexSet::new();
set.push(10);
set.push(20);
set.push(10); // duplicate, ignored
assert_eq!(set.len(), 2);
assert_eq!(set[0], 10);
assert_eq!(set[1], 20);Sourcepub fn contains(&self, value: &T) -> bool
pub fn contains(&self, value: &T) -> bool
Check if the set contains a value.
§Examples
use tensor4all_tcicore::IndexSet;
let set = IndexSet::from_vec(vec![10, 20]);
assert!(set.contains(&10));
assert!(!set.contains(&30));Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for IndexSet<T>
impl<T> RefUnwindSafe for IndexSet<T>where
T: RefUnwindSafe,
impl<T> Send for IndexSet<T>where
T: Send,
impl<T> Sync for IndexSet<T>where
T: Sync,
impl<T> Unpin for IndexSet<T>where
T: Unpin,
impl<T> UnsafeUnpin for IndexSet<T>
impl<T> UnwindSafe for IndexSet<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
Casts
self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more