rust_dsa

Struct BiMap

Source
pub struct BiMap<L, R> { /* private fields */ }
Expand description

A bidirectional map.

§Example

use rust_dsa::BiMap;

// First, we create a new map.
let mut map = BiMap::new();

// Then we insert values.
map.insert(1, 'a');
map.insert(2, 'b');
map.insert(3, 'c');

assert_eq!(map.len(), 3);
assert!(map.contains_left(&1));
assert!(map.contains_right(&'c'));

// We can get values.
assert_eq!(map.get_by_left(&1), Some(&'a'));
assert_eq!(map.get_by_left(&2), Some(&'b'));
assert_eq!(map.get_by_right(&'c'), Some(&3));
assert_eq!(map.get_by_left(&99), None);
assert_eq!(map.get_by_right(&'z'), None);

use rust_dsa::Removed;

// Inserting returns the old elements.
let removed = map.insert(3, 'z');
assert_eq!(removed, Removed::Single((3, 'c')));

// (1, 'z') touches two pairs ((1, 'a') and (3, 'z'))
let removed = map.insert(1, 'z');
assert_eq!(removed, Removed::Double((1, 'a'), (3, 'z')));

// (5, 'm') touches nothing
let removed = map.insert(5, 'm');
assert_eq!(removed, Removed::Nothing);

assert_eq!(map.len(), 3);

// We can also remove items.
assert_eq!(map.remove_by_left(&1), Some((1, 'z')));
assert_eq!(map.remove_by_right(&'b'), Some((2, 'b')));
assert_eq!(map.remove_by_right(&'m'), Some((5, 'm')));
assert_eq!(map.remove_by_left(&99), None);

assert!(map.is_empty());

Implementations§

Source§

impl<L, R> BiMap<L, R>

Source

pub fn new() -> BiMap<L, R>

Creates a new map.

Source

pub fn with_capacity(capacity: usize) -> BiMap<L, R>

Creates a new map with the given capacity.

Source

pub fn insert(&mut self, left: L, right: R) -> Removed<L, R>
where L: Hash + Eq, R: Hash + Eq,

Inserts a pair into the map. Returns the removed items.

§Example
use rust_dsa::{BiMap, Removed};

let mut map = BiMap::new();

let removed = map.insert(1, 2);
assert_eq!(removed, Removed::Nothing);
let removed = map.insert(3, 4);
assert_eq!(removed, Removed::Nothing);

let removed = map.insert(1, 3);
assert_eq!(removed, Removed::Single((1, 2)));

let removed = map.insert(1, 4);
assert_eq!(removed, Removed::Double((1, 3), (3, 4)));

let removed = map.insert(1, 4);
assert_eq!(removed, Removed::Single((1, 4)));
Source

pub fn get_by_left(&self, left: &L) -> Option<&R>
where L: Hash + Eq,

Returns a reference to the right value based on the left value.

§Example
use rust_dsa::BiMap;

let map = BiMap::from([(1, 'a'), (2, 'b')]);

assert_eq!(map.get_by_left(&1), Some(&'a'));
assert_eq!(map.get_by_left(&2), Some(&'b'));
assert_eq!(map.get_by_left(&3), None);
Source

pub fn get_by_right(&self, right: &R) -> Option<&L>
where R: Hash + Eq,

Returns a reference to the left value based on the right value.

§Example
use rust_dsa::BiMap;

let map = BiMap::from([(1, 'a'), (2, 'b')]);

assert_eq!(map.get_by_right(&'a'), Some(&1));
assert_eq!(map.get_by_right(&'b'), Some(&2));
assert_eq!(map.get_by_right(&'c'), None);
Source

pub fn remove_by_left(&mut self, left: &L) -> Option<(L, R)>
where L: Hash + Eq, R: Hash + Eq,

Removes a pair from the map based on the left value.

§Example
use rust_dsa::BiMap;

let mut map = BiMap::from([(1, 'a'), (2, 'b')]);

assert_eq!(map.remove_by_left(&1), Some((1, 'a')));
assert_eq!(map.remove_by_left(&2), Some((2, 'b')));
assert_eq!(map.remove_by_left(&1), None);

assert!(map.is_empty());
Source

pub fn remove_by_right(&mut self, right: &R) -> Option<(L, R)>
where L: Hash + Eq, R: Hash + Eq,

Removes a pair from the map based on the right value.

§Example
use rust_dsa::BiMap;

let mut map = BiMap::from([(1, 'a'), (2, 'b')]);

assert_eq!(map.remove_by_right(&'a'), Some((1, 'a')));
assert_eq!(map.remove_by_right(&'b'), Some((2, 'b')));
assert_eq!(map.remove_by_right(&'a'), None);

assert!(map.is_empty());
Source

pub fn contains_left(&self, left: &L) -> bool
where L: Hash + Eq,

Returns true if the map contains the left value.

§Example
use rust_dsa::BiMap;

let map = BiMap::from([(1, 'a'), (2, 'b')]);

assert!(map.contains_left(&1));
assert!(map.contains_left(&2));
assert!(!map.contains_left(&3));
Source

pub fn contains_right(&self, right: &R) -> bool
where R: Hash + Eq,

Returns true if the map contains the right value.

§Example
use rust_dsa::BiMap;

let map = BiMap::from([(1, 'a'), (2, 'b')]);

assert!(map.contains_right(&'a'));
assert!(map.contains_right(&'b'));
assert!(!map.contains_right(&'c'));
Source

pub fn len(&self) -> usize

Returns the number of pairs in the map.

§Example
use rust_dsa::BiMap;

let mut map = BiMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);

assert_eq!(map.len(), 3);

map.remove_by_left(&1);

assert_eq!(map.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

§Example
use rust_dsa::BiMap;

let mut map = BiMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);

assert!(!map.is_empty());

map.clear();

assert!(map.is_empty());
Source

pub fn clear(&mut self)

Clears the map.

§Example
use rust_dsa::BiMap;

let mut map = BiMap::from([(1, 'a'), (2, 'b'), (3, 'c')]);

assert!(!map.is_empty());

map.clear();

assert!(map.is_empty());
Source

pub fn iter(&self) -> Iter<'_, L, R>

Returns an iterator over the items in the map.

Source

pub fn lefts(&self) -> Vec<&L>

Returns the left values in the map.

Source

pub fn into_lefts(self) -> Vec<L>

Returns the left values in the map.

Source

pub fn rights(&self) -> Vec<&R>

Returns right values in the map.

Source

pub fn into_rights(self) -> Vec<R>

Returns right values in the map.

Trait Implementations§

Source§

impl<L, R> Clone for BiMap<L, R>
where L: Clone + Hash + Eq, R: Clone + Hash + Eq,

Source§

fn clone(&self) -> BiMap<L, R>

Returns a copy 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<L, R> Debug for BiMap<L, R>
where L: Debug, R: Debug,

Source§

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

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

impl<L, R> Default for BiMap<L, R>

Source§

fn default() -> BiMap<L, R>

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

impl<L, R, const N: usize> From<[(L, R); N]> for BiMap<L, R>
where L: Hash + Eq, R: Hash + Eq,

Source§

fn from(array: [(L, R); N]) -> BiMap<L, R>

Converts to this type from the input type.
Source§

impl<L, R> FromIterator<(L, R)> for BiMap<L, R>
where L: Hash + Eq, R: Hash + Eq,

Source§

fn from_iter<I: IntoIterator<Item = (L, R)>>(iter: I) -> BiMap<L, R>

Creates a value from an iterator. Read more
Source§

impl<'a, L, R> IntoIterator for &'a BiMap<L, R>

Source§

type IntoIter = Iter<'a, L, R>

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

type Item = (&'a L, &'a R)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<L, R> IntoIterator for BiMap<L, R>

Source§

type IntoIter = IntoIter<L, R>

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

type Item = (L, R)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<L, R> PartialEq for BiMap<L, R>
where L: Hash + Eq, R: Hash + Eq,

Source§

fn eq(&self, other: &BiMap<L, R>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<L, R> Eq for BiMap<L, R>
where L: Hash + Eq, R: Hash + Eq,

Auto Trait Implementations§

§

impl<L, R> Freeze for BiMap<L, R>

§

impl<L, R> RefUnwindSafe for BiMap<L, R>

§

impl<L, R> !Send for BiMap<L, R>

§

impl<L, R> !Sync for BiMap<L, R>

§

impl<L, R> Unpin for BiMap<L, R>

§

impl<L, R> UnwindSafe for BiMap<L, R>

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

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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> 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.