pub struct BidirMap<Kv1: PartialEq, Kv2: PartialEq> { /* private fields */ }Expand description
A bidirectional map.
Bidirectional maps allow for mapping from and to both types.
The interface is based on that of BTreeMap, except, that for all functions, where one would supply a key, there are two functions,
each treating one of the types as keys (get() -> get_by_{first,second}()).
Performance: O(n), mostly.
Implementations§
Source§impl<Kv1: PartialEq, Kv2: PartialEq> BidirMap<Kv1, Kv2>
impl<Kv1: PartialEq, Kv2: PartialEq> BidirMap<Kv1, Kv2>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create a new empty instance of BidirMap with the specified capacity.
It will be able to hold at least capacity elements without reallocating.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all entries.
§Examples
use bidir_map::BidirMap;
let mut a = BidirMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());Sourcepub fn insert(&mut self, kv1: Kv1, kv2: Kv2) -> Option<(Kv1, Kv2)>
pub fn insert(&mut self, kv1: Kv1, kv2: Kv2) -> Option<(Kv1, Kv2)>
Inserts a K/V-K/V pair into the map.
If the map did not have this K/V-K/V pair present, None is returned.
If the map did have this K/V-K/V pair present, it’s updated and the old K/V-K/V pair is returned.
Sourcepub fn iter(&self) -> Iter<'_, Kv1, Kv2> ⓘ
pub fn iter(&self) -> Iter<'_, Kv1, Kv2> ⓘ
Gets an iterator over the entries of the map.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "c");
for kv in map.iter() {
println!("{}: {}", kv.0, kv.1);
}
let first = map.iter().next().unwrap();
assert_eq!(first, (&1, &"a"));Sourcepub fn iter_mut(&mut self) -> IterMut<'_, Kv1, Kv2> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, Kv1, Kv2> ⓘ
Gets a mutable iterator over the entries of the map.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
// add 10 to the value if the key isn't "a"
for kv in map.iter_mut() {
if *kv.0 != "a" {
*kv.1 += 10;
}
}Sourcepub fn first_col(&self) -> FirstColumn<'_, Kv1, Kv2> ⓘ
pub fn first_col(&self) -> FirstColumn<'_, Kv1, Kv2> ⓘ
Gets an iterator over the first K/V of the map.
§Examples
use bidir_map::BidirMap;
let mut a = BidirMap::new();
a.insert(1, "a");
a.insert(2, "b");
let keys: Vec<_> = a.first_col().cloned().collect();
assert_eq!(keys, [1, 2]);Sourcepub fn second_col(&self) -> SecondColumn<'_, Kv1, Kv2> ⓘ
pub fn second_col(&self) -> SecondColumn<'_, Kv1, Kv2> ⓘ
Gets an iterator over the second K/V of the map.
§Examples
use bidir_map::BidirMap;
let mut a = BidirMap::new();
a.insert(1, "a");
a.insert(2, "b");
let keys: Vec<_> = a.second_col().cloned().collect();
assert_eq!(keys, ["a", "b"]);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
§Examples
use bidir_map::BidirMap;
let mut a = BidirMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
§Examples
use bidir_map::BidirMap;
let mut a = BidirMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());Sourcepub fn get_by_first<Q>(&self, key: &Q) -> Option<&Kv2>
pub fn get_by_first<Q>(&self, key: &Q) -> Option<&Kv2>
Returns a reference to the second K/V corresponding to the first K/V.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.get_by_first(&1), Some(&"a"));
assert_eq!(map.get_by_first(&2), None);Sourcepub fn get_by_second<Q>(&self, key: &Q) -> Option<&Kv1>
pub fn get_by_second<Q>(&self, key: &Q) -> Option<&Kv1>
Returns a reference to the first K/V corresponding to the second K/V.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.get_by_second(&"a"), Some(&1));
assert_eq!(map.get_by_second(&"b"), None);Sourcepub fn contains_first_key<Q>(&self, key: &Q) -> bool
pub fn contains_first_key<Q>(&self, key: &Q) -> bool
Check if the map contains the first K/V
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.contains_first_key(&1), true);
assert_eq!(map.contains_first_key(&2), false);Sourcepub fn contains_second_key<Q>(&self, key: &Q) -> bool
pub fn contains_second_key<Q>(&self, key: &Q) -> bool
Check if the map contains the second K/V
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.contains_second_key(&"a"), true);
assert_eq!(map.contains_second_key(&"b"), false);Sourcepub fn get_mut_by_first<Q>(&mut self, key: &Q) -> Option<&mut Kv2>
pub fn get_mut_by_first<Q>(&mut self, key: &Q) -> Option<&mut Kv2>
Returns a mutable reference to the second K/V corresponding to the first K/V.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut_by_first(&1) {
*x = "b";
}
assert_eq!(map.get_by_first(&1), Some(&"b"));Sourcepub fn get_mut_by_second<Q>(&mut self, key: &Q) -> Option<&mut Kv1>
pub fn get_mut_by_second<Q>(&mut self, key: &Q) -> Option<&mut Kv1>
Returns a mutable reference to the first K/V corresponding to the second K/V.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut_by_second(&"a") {
*x = 2;
}
assert_eq!(map.get_by_second(&"a"), Some(&2));Sourcepub fn remove_by_first<Q>(&mut self, key: &Q) -> Option<(Kv1, Kv2)>
pub fn remove_by_first<Q>(&mut self, key: &Q) -> Option<(Kv1, Kv2)>
Removes the pair corresponding to the first K/V from the map, returning it if the key was previously in the map.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.remove_by_first(&1), Some((1, "a")));
assert_eq!(map.remove_by_first(&1), None);Sourcepub fn remove_by_second<Q>(&mut self, key: &Q) -> Option<(Kv1, Kv2)>
pub fn remove_by_second<Q>(&mut self, key: &Q) -> Option<(Kv1, Kv2)>
Removes the pair corresponding to the first K/V from the map, returning it if the key was previously in the map.
§Examples
use bidir_map::BidirMap;
let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.remove_by_second(&"a"), Some((1, "a")));
assert_eq!(map.remove_by_second(&"b"), None);Trait Implementations§
Source§impl<Kv1: PartialEq, Kv2: PartialEq> Extend<(Kv1, Kv2)> for BidirMap<Kv1, Kv2>
impl<Kv1: PartialEq, Kv2: PartialEq> Extend<(Kv1, Kv2)> for BidirMap<Kv1, Kv2>
Source§fn extend<T: IntoIterator<Item = (Kv1, Kv2)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (Kv1, Kv2)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)