Struct isomorphism::BiMap
[−]
[src]
pub struct BiMap<L, R, LH = RandomState, RH = RandomState, B = DefaultBitField> { /* fields omitted */ }
The two way hashmap itself. See the crate level documentation for more information. Uses hopscotch hashing internally.
L and R are the left and right types being mapped to eachother. LH and RH are the hash builders used to hash the left keys and right keys. B is the bitfield used to store neighbourhoods.
Methods
impl<L, R> BiMap<L, R>
[src]
impl<L, R, LH, RH, B> BiMap<L, R, LH, RH, B>
[src]
pub fn capacity(&self) -> usize
[src]
Returns a lower bound on the number of elements that this hashmap can hold without needing to be resized.
let map: BiMap<String, String> = BiMap::new(); let capacity = map.capacity(); assert!(capacity >= 0);
pub fn len(&self) -> usize
[src]
Returns the number of pairs inside this hashmap. Each remove will decrement this count. Each insert will increment this count, but may then also decrement it by one or two if the keys being inserted already existed and were associated with other pairs.
let mut map = BiMap::new(); assert_eq!(0, map.len()); map.insert("Hello", "World"); map.insert("Hashmaps", "Are cool"); assert_eq!(2, map.len()); // this removes the ("Hello", "World") pair and the ("Hashmaps", "Are cool") pair, leaving // only the ("Hello", "Are cool") pair behind. map.insert("Hello", "Are cool"); assert_eq!(1, map.len());
pub fn is_empty(&self) -> bool
[src]
Returns true if the bimap contains no pairs.
let mut map = BiMap::new(); assert!(map.is_empty()); map.insert("Hello", "World"); assert!(!map.is_empty());
ⓘImportant traits for Iter<'a, L, R, B>pub fn iter(&self) -> Iter<L, R, B>
[src]
An iterator visiting all key-value pairs in an arbitrary order. The iterator element is type (&'a L, &'a R).
let mut map = BiMap::new(); map.insert("Hello", "World"); map.insert("Hashmaps", "Are cool"); for (&left, &right) in map.iter() { println!("{} {}", left, right); }
impl<L, R, LH, RH, B> BiMap<L, R, LH, RH, B> where
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,
[src]
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,
pub fn insert(&mut self, left: L, right: R) -> (Option<R>, Option<L>)
[src]
Inserts an (L, R) pair into the hashmap. Returned is a (R, L) tuple of options. The
Option<R>
is the value that was previously associated with the inserted L (or lack
thereof), and vice versa for the Option<L>
.
let mut map = BiMap::new(); // neither "Hello" nor 5 were previously mapped to anything, so nothing is returned. assert_eq!((None, None), map.insert("Hello", 5)); // "Hello" was previously mapped to 5, so remapping it to 7 means that the 5 got evicted // from the hashmap and is therefore returned. 7 was not previously mapped to anything, // though. assert_eq!((Some(5), None), map.insert("Hello", 7)); // Note now that inserting "Hello" with a new right value means that 5 no longer exists in // the hashmap. assert_eq!(None, map.get_right(&5));
pub fn get_left<'a, Q: ?Sized>(&'a self, left: &Q) -> Option<&'a R> where
L: Borrow<Q>,
Q: Hash + Eq,
[src]
L: Borrow<Q>,
Q: Hash + Eq,
Gets a key from the left of the hashmap. Returns the value from the right of the hashmap that associates with this key, if it exists.
let mut map = BiMap::new(); assert_eq!(None, map.get_left("Hello")); map.insert("Hello", 5); assert_eq!(Some(&5), map.get_left("Hello"));
pub fn get_right<'a, Q: ?Sized>(&'a self, right: &Q) -> Option<&'a L> where
R: Borrow<Q>,
Q: Hash + Eq,
[src]
R: Borrow<Q>,
Q: Hash + Eq,
Gets a key from the right of the hashmap. Returns the value from the left of the hashmap that associates with this key, if it exists.
let mut map = BiMap::new(); assert_eq!(None, map.get_right(&5)); map.insert("Hello", 5); assert_eq!(Some(&"Hello"), map.get_right(&5));
pub fn remove_left<Q: ?Sized>(&mut self, left: &Q) -> Option<R> where
L: Borrow<Q>,
Q: Hash + Eq,
[src]
L: Borrow<Q>,
Q: Hash + Eq,
Removes a key from the left of the hashmap. Returns the value from the right of the hashmap
that was associated with this key, if it existed. Will remove both the left and right sides
of the pair, if it exists, meaning that get_right
will no longer work for the value
associated with the key that is removed.
let mut map = BiMap::new(); map.insert("Hello", 5); assert_eq!(Some(&5), map.get_left("Hello")); assert_eq!(Some(&"Hello"), map.get_right(&5)); let old = map.remove_left("Hello"); assert_eq!(Some(5), old); assert_eq!(None, map.get_left("Hello")); assert_eq!(None, map.get_right(&5));
pub fn remove_right<Q: ?Sized>(&mut self, right: &Q) -> Option<L> where
R: Borrow<Q>,
Q: Hash + Eq,
[src]
R: Borrow<Q>,
Q: Hash + Eq,
Removes a key from the right of the hashmap. Returns the value from the left of the hashmap
that was associated with this key, if it existed. Will remove both the left and right sides
of the pair, if it exists, meaning that get_left
will no longer work for the value
associated with the key that is removed.
let mut map = BiMap::new(); map.insert("Hello", 5); assert_eq!(Some(&5), map.get_left("Hello")); assert_eq!(Some(&"Hello"), map.get_right(&5)); let old = map.remove_right(&5); assert_eq!(Some("Hello"), old); assert_eq!(None, map.get_left("Hello")); assert_eq!(None, map.get_right(&5));
Trait Implementations
impl<L, R> Default for BiMap<L, R>
[src]
impl<L, R, LH, RH, B> PartialEq for BiMap<L, R, LH, RH, B> where
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,
[src]
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,
fn eq(&self, other: &Self) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<L, R, LH, RH, B> Eq for BiMap<L, R, LH, RH, B> where
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,
[src]
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,
impl<L, R, LH, RH, B> Debug for BiMap<L, R, LH, RH, B> where
L: Debug,
R: Debug,
[src]
L: Debug,
R: Debug,
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more
impl<'a, L, R, LH, RH, B> IntoIterator for &'a BiMap<L, R, LH, RH, B>
[src]
type Item = (&'a L, &'a R)
The type of the elements being iterated over.
type IntoIter = Iter<'a, L, R, B>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more
impl<L, R, LH, RH, B> IntoIterator for BiMap<L, R, LH, RH, B>
[src]
type Item = (L, R)
The type of the elements being iterated over.
type IntoIter = IntoIter<L, R, B>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more
impl<L, R, LH, RH, B> FromIterator<(L, R)> for BiMap<L, R, LH, RH, B> where
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher + Default,
RH: BuildHasher + Default,
B: BitField,
[src]
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher + Default,
RH: BuildHasher + Default,
B: BitField,
fn from_iter<T: IntoIterator<Item = (L, R)>>(iter: T) -> Self
[src]
Creates a value from an iterator. Read more
impl<L, R, LH, RH, B> Extend<(L, R)> for BiMap<L, R, LH, RH, B> where
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,
[src]
L: Hash + Eq,
R: Hash + Eq,
LH: BuildHasher,
RH: BuildHasher,
B: BitField,