Crate bisetmap [] [src]

A fast and thread-safe two-way hash map of sets. It is best suited where you need to associate two collumns uniquely. Each key is associated to one or more other unique values.

A BisetMap<L, R> is a Multi-Hash-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 one or more right values and vice versa. Compare this to a HashMap, where every key is associated with exactly one value.

The structure is interior mutable and all operations are thread safe. Each clone provides access to the same underlying data. Serialize and Deserialize from serde are also implemented.

Internally, a BisetMap 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 methods 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 BisetMap using a Cell, RefCell, etc.


use bisetmap::BisetMap;

let subscriptions = BisetMap::new();

// insert client-ids and subscription topics
subscriptions.insert("Bob", "Tech");
subscriptions.insert("Bob", "Math");
subscriptions.insert("Alice", "Tech");
subscriptions.insert("Alice", "Romance");

// retrieve topic by client-id (left to right)
assert_eq!(subscriptions.get(&"Bob"), ["Math", "Tech"]);
assert_eq!(subscriptions.get(&"Alice"), ["Romance", "Tech"]);

// retrieve clients by topic (right to left)
assert_eq!(subscriptions.rev_get(&"Tech"), ["Alice", "Bob"]);
assert_eq!(subscriptions.rev_get(&"Math"), ["Bob"]);

// check membership
assert!(subscriptions.contains(&"Bob", &"Math"));
assert!(!subscriptions.contains(&"Bob", &"Romance"));

// check key/value existence

Insertion and Uniqueness

Consider the following example:

use bisetmap::BisetMap;

let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('a', 1); // what to do here?

Duplicate key-value pairs are ignored and inserted only once



A two-way map between keys (left) and values (right).