Skip to main content

IndexSet

Struct IndexSet 

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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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));
Source

pub fn len(&self) -> usize

Number of elements in the set

Source

pub fn is_empty(&self) -> bool

Check if the set is empty

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Iterate over values in insertion order.

§Examples
use tensor4all_tcicore::IndexSet;

let set = IndexSet::from_vec(vec![10, 20, 30]);
let collected: Vec<_> = set.iter().copied().collect();
assert_eq!(collected, vec![10, 20, 30]);
Source

pub fn values(&self) -> &[T]

Get all values as a slice in insertion order.

§Examples
use tensor4all_tcicore::IndexSet;

let set = IndexSet::from_vec(vec![10, 20, 30]);
assert_eq!(set.values(), &[10, 20, 30]);

Trait Implementations§

Source§

impl<T: Clone + Clone + Eq + Hash> Clone for IndexSet<T>

Source§

fn clone(&self) -> IndexSet<T>

Returns a duplicate 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: Debug + Clone + Eq + Hash> Debug for IndexSet<T>

Source§

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

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

impl<T: Clone + Eq + Hash> Default for IndexSet<T>

Source§

fn default() -> Self

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

impl<T: Clone + Eq + Hash> Index<usize> for IndexSet<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, i: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T: Clone + Eq + Hash> IntoIterator for &'a IndexSet<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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: Clone + Eq + Hash> IntoIterator for IndexSet<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<U> As for U

§

fn as_<T>(self) -> T
where 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 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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> DistributionExt for T
where T: ?Sized,

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

§

impl<T> DistributionExt for T
where T: ?Sized,

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

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

Source§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

§

impl<T> MaybeSend for T

§

impl<T> MaybeSendSync for T

§

impl<T> MaybeSync for T