Expand description
This crate implements containers that can have duplicate values.
If you have ever written a HashMap<K, HashSet<V>>, this crate is for you.
This crate is unstable and its API is subject to change at any time until 1.0.0.
This crate is comparable in spirit to these containers in other languages:
- Java’s
Guavalibrary’sMultimapandMultiset(which heavily inspired this crate). - Python’s
collections.defaultdict(set)andcollections.Counter. - C++’s
std::(unordered_)mutlimapandstd::(unordered_)multiset.
§Usage
The primary containers are MultiMap and MultiSet. See examples.rs for more examples.
§MultiMap
MultiMap is a wrapper around Map<K, Set<V>>.
You can either use the provided HashMultiMap or BTreeMultiMap, or provide your own types with MultiMapBuilder.
The API is similar to what you would expect from HashMap<K, HashSet<V>>, with some additional methods related to the multiple values.
For bookkeeping reasons, the inner sets are not exposed mutably.
MultiMap also provides ways of iterating over (&K, &Set<V>), or over (&K, &V).
use multi_containers::HashMultiMap;
let mut map = HashMultiMap::new();
map.insert("a".to_string(), 1);
map.insert("a".to_string(), 2);
map.insert("b".to_string(), 3);
assert_eq!(map.get("a").unwrap().len(), 2);
assert_eq!(map.get("b").unwrap().len(), 1);§MultiSet
MultiSet is a wrapper around Map<V, usize>. It offers the semantics of a set, but allows for duplicate values.
It offers iterators over unique (&V, usize), and non-unique &V.
use multi_containers::HashMultiSet;
let mut set = HashMultiSet::new();
set.insert(1);
set.insert(1);
set.insert_some(2, 3);
assert_eq!(set.count(&1), 2);
assert_eq!(set.count(&2), 3);Re-exports§
pub use crate::multimap::MultiMap;pub use crate::multimap_builder::MultiMapBuilder;pub use crate::multiset::MultiSet;pub use crate::multiset_builder::MultiSetBuilder;
Modules§
- maps
- Traits for working with maps.
- multimap
- Defines the
MultiMaptype. - multimap_
builder - Provides a convenient way to construct multi-maps.
- multiset
- Defines the
MultiSettype. - multiset_
builder - Provides a convenient way to construct multi-sets.
- sets
- Traits for working with sets.
Type Aliases§
- BTree
Multi Map - A multi-map that uses
BTreeMapfor the keys andBTreeSetfor the values. - BTree
Multi Set - A multi-set that uses
BTreeMapfor the keys. - Hash
Multi Map - A multi-map that uses
HashMapfor the keys andHashSetfor the values. - Hash
Multi Set - A multi-set that uses
HashMapfor the keys.