[](https://github.com/mikwielgus/multi_bimap)
[](https://docs.rs/multi_bimap/)
[](https://crates.io/crates/multi_bimap)
[](#licence)
# multi_bimap
Many-to-many bidirectional map in Rust.
This crate provides a `MultiBimap` struct, a bidirectional multimap that is
implemented as two antiparallel multimaps that are kept in sync.
This structure is also known under many other names: *bi-multimap*,
*multi-bimap*, or sometimes even just *bimap*; in set theory, it's simply called
a *relation*; in graph theory, it's the same as a *bipartite graph*.
## Usage
### Adding dependency
First, add `multi_bimap` as a dependency to your `Cargo.toml`:
```toml
[dependencies]
multi_bimap = "0.2.0"
```
### Example
A `MultiBimap` keeps two antiparallel multimaps in sync; each side may map a key
to multiple values. You can look up associations in either direction.
```rust
use multi_bimap::MultiBimap;
use std::collections::{HashMap, HashSet};
let mut m: MultiBimap<HashMap<&str, HashSet<i32>>, HashMap<i32, HashSet<&str>>> = MultiBimap::new();
m.insert("a", 1);
m.insert("a", 2);
m.insert("b", 1);
// Look up all right values for a left key.
assert_eq!(m.get_by_left("a"), Some(&HashSet::from([1, 2])));
// Look up all left values for a right key.
assert_eq!(m.get_by_right(&1), Some(&HashSet::from(["a", "b"])));
// Remove one association; empty keys are dropped automatically.
assert_eq!(m.remove(&"a", &1), Some(("a", 1)));
```