pub struct BiMap<L, R> { /* private fields */ }Expand description
§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>
impl<L, R> BiMap<L, R>
Sourcepub fn with_capacity(capacity: usize) -> BiMap<L, R>
pub fn with_capacity(capacity: usize) -> BiMap<L, R>
Creates a new map with the given capacity.
Sourcepub fn insert(&mut self, left: L, right: R) -> Removed<L, R>
pub fn insert(&mut self, left: L, right: R) -> Removed<L, R>
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)));Sourcepub fn get_by_left(&self, left: &L) -> Option<&R>
pub fn get_by_left(&self, left: &L) -> Option<&R>
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);Sourcepub fn get_by_right(&self, right: &R) -> Option<&L>
pub fn get_by_right(&self, right: &R) -> Option<&L>
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);Sourcepub fn remove_by_left(&mut self, left: &L) -> Option<(L, R)>
pub fn remove_by_left(&mut self, left: &L) -> Option<(L, R)>
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());Sourcepub fn remove_by_right(&mut self, right: &R) -> Option<(L, R)>
pub fn remove_by_right(&mut self, right: &R) -> Option<(L, R)>
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());Sourcepub fn contains_left(&self, left: &L) -> bool
pub fn contains_left(&self, left: &L) -> bool
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));Sourcepub fn contains_right(&self, right: &R) -> bool
pub fn contains_right(&self, right: &R) -> bool
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'));Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn clear(&mut self)
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());Sourcepub fn into_lefts(self) -> Vec<L>
pub fn into_lefts(self) -> Vec<L>
Returns the left values in the map.
Sourcepub fn into_rights(self) -> Vec<R>
pub fn into_rights(self) -> Vec<R>
Returns right values in the map.
Trait Implementations§
Source§impl<L, R> FromIterator<(L, R)> for BiMap<L, R>
impl<L, R> FromIterator<(L, R)> for BiMap<L, R>
Source§impl<'a, L, R> IntoIterator for &'a BiMap<L, R>
impl<'a, L, R> IntoIterator for &'a BiMap<L, R>
Source§impl<L, R> IntoIterator for BiMap<L, R>
impl<L, R> IntoIterator for BiMap<L, R>
impl<L, R> Eq for BiMap<L, R>
Auto Trait Implementations§
impl<L, R> Freeze for BiMap<L, R>
impl<L, R> RefUnwindSafe for BiMap<L, R>where
L: RefUnwindSafe,
R: RefUnwindSafe,
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>where
L: RefUnwindSafe,
R: RefUnwindSafe,
Blanket Implementations§
Source§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