Crate bimap [−] [src]
A fast two-way bijective map.
A Bimap<L, R>
is a bijective map between values of type L
, called left values, and values
of type R
, called right values. This means every left value is associated with exactly one
right value and vice versa. Compare this to a HashMap<K, V>
, where every key is associated
with exactly one value but a value can be associated with more than one key.
Internally, a Bimap
is composed of two HashMaps
, one for the left-to-right direction and
one for right-to-left. As such, the big-O performance of the get()
, remove()
, insert()
,
and contains()
functions are the same as those of a HashMap
.
As with HashMap
, it is considered a logic error to modify a value's hash while it is in the
Bimap
using a Cell
, RefCell
, etc.
Examples
use bimap::Bimap; let mut elements = Bimap::new(); // insert chemicals and their corresponding symbols elements.insert("hydrogen", "H"); elements.insert("carbon", "C"); elements.insert("bromine", "Br"); elements.insert("neodymium", "Nd"); // retrieve chemical symbol by name (left to right) assert_eq!(elements.get_by_left(&"bromine"), Some(&"Br")); assert_eq!(elements.get_by_left(&"oxygen"), None); // retrieve name by chemical symbol (right to left) assert_eq!(elements.get_by_right(&"C"), Some(&"carbon")); assert_eq!(elements.get_by_right(&"Al"), None); // check membership assert!(elements.contains_left(&"hydrogen")); assert!(!elements.contains_right(&"He")); // remove elements assert_eq!(elements.remove_by_left(&"neodymium"), Some(("neodymium", "Nd"))); assert_eq!(elements.remove_by_right(&"Nd"), None); // iterate over elements for (left, right) in &elements { println!("the chemical symbol for {} is {}", left, right); }
Structs
Bimap |
A two-way map between left values and right values. |
IntoIter |
An owning iterator over the left-right pairs in a |
Iter |
An iterator over the left-right pairs in a |
LeftValues |
An iterator over the left values in a |
RightValues |
An iterator over the right values in a |
Enums
Overwritten |
The previous left-right pairs, if any, that were overwritten by a call to |