orx_v

Trait Lookup

Source
pub trait Lookup<Idx, T>: Default {
    // Required methods
    fn len(&self) -> usize;
    fn contains_key(&self, idx: &Idx) -> bool;
    fn get(&self, idx: &Idx) -> Option<&T>;
    fn insert(&mut self, idx: Idx, value: T);
    fn entry_or_insert(&mut self, idx: Idx, value: T) -> &mut T;
    fn values_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>
       where T: 'a;
    fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = (&'a Idx, &'a mut T)>
       where T: 'a,
             Idx: 'a;
    fn clear(&mut self);

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

A map of indices to values.

Note that HashMap (with std) or BTreeMap (when no-std) satisfy the requirements and implement the Lookup trait, and hence, can be used as the storage in sparse vectors.

Alternatively, more advanced lookup structures can be provided depending on the use case.

Required Methods§

Source

fn len(&self) -> usize

Number of elements stored in the lookup.

Source

fn contains_key(&self, idx: &Idx) -> bool

Returns whether or not the idx is present as a key in the lookup.

Source

fn get(&self, idx: &Idx) -> Option<&T>

Returns a reference to the value with the given idx if it exists in the lookup; returns None otherwise.

Source

fn insert(&mut self, idx: Idx, value: T)

Inserts the value with key idx. If the key already existed, its value is updated.

Source

fn entry_or_insert(&mut self, idx: Idx, value: T) -> &mut T

If the lookup contains an element with the given idx, the method returns a mutable reference to the value.

Otherwise,

  • inserts (idx, value) to the lookup, and
  • returns a mutable reference to the newly inserted value.
Source

fn values_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>
where T: 'a,

Returns an iterator yielding mutable references to the values in the lookup.

Source

fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = (&'a Idx, &'a mut T)>
where T: 'a, Idx: 'a,

Returns an iterator yielding indices and mutable references to the values in the lookup.

Source

fn clear(&mut self)

Clears the lookup.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true if the lookup is empty.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<Idx: Eq + Hash, T> Lookup<Idx, T> for HashMap<Idx, T>

Source§

fn len(&self) -> usize

Source§

fn contains_key(&self, idx: &Idx) -> bool

Source§

fn get(&self, idx: &Idx) -> Option<&T>

Source§

fn insert(&mut self, idx: Idx, value: T)

Source§

fn entry_or_insert(&mut self, idx: Idx, value: T) -> &mut T

Source§

fn values_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>
where T: 'a,

Source§

fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = (&'a Idx, &'a mut T)>
where T: 'a, Idx: 'a,

Source§

fn clear(&mut self)

Source§

impl<Idx: Ord, T> Lookup<Idx, T> for BTreeMap<Idx, T>

Source§

fn len(&self) -> usize

Source§

fn contains_key(&self, idx: &Idx) -> bool

Source§

fn get(&self, idx: &Idx) -> Option<&T>

Source§

fn insert(&mut self, idx: Idx, value: T)

Source§

fn entry_or_insert(&mut self, idx: Idx, value: T) -> &mut T

Source§

fn values_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T>
where T: 'a,

Source§

fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = (&'a Idx, &'a mut T)>
where T: 'a, Idx: 'a,

Source§

fn clear(&mut self)

Implementors§