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.
You can arbitrarily choose what should be the types of the constituent
multimaps: for example, HashMap<&str, HashSet<&str>>, BTreeMap<i64, Vec<&str>>, indexmap::IndexMap<&str, Box<(i64, i64)> are all valid options.
Even better, these multimaps can have mixed types (e.g. HashMap of HashSets
pointing rightwards, BTreeMap of Boxes pointing leftwards), so you can also
have one-to-many or many-to-one bimaps as well (one-to-one bimap is obviously
also an option). This is possible because MultiBimap uses traits from
maplike, a Rust crate which allows to
have a generic interface over a large number of containers.
This bidirectional multimap relation that MultiBimap models 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.
This crate has no unsafe code and is compatible with serde
and undoredo libraries. It is also no_std-compatible, though
alloc is still required. There is no
mandatory third-party dependencies, but there is a first-party dependency on
maplike, a library also developed by
this library's authors.
Usage
Adding dependency
First, add multi_bimap as a dependency to your Cargo.toml:
[]
= "0.5.0"
Examples
Many-to-many bidirectional map
In academic publishing, the relation between authors and academic papers
is many-to-many; it is a bipartite graph: each author may have many papers,
and each paper may have many authors. A MultiBimap (here under alias
HashMultiBimap) can fully represent that:
use HashMultiBimap;
use HashSet;
One-to-one bidirectional map
A country and its capital form a one-to-one relation: each country has one
capital, and each capital belongs to one country. MultiBimap (here under alias
HashBimap) can represent that just as well without any additional logic:
use One;
use HashBimap;
Documentation
See the documentation for more information
on multi_bimap's usage.
Packaging
multi_bimap is published as a crate on the
Crates.io registry.
Contributing
We welcome issues, pull requests and any other contributions from anyone to our repository on GitHub.
Licence
Outbound licence
multi_bimap is dual-licensed as under
at your option.
Inbound licence
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you will be dual-licensed as described above, without any additional terms or conditions.