pub struct BisetMap<L: Eq + Hash + Clone, R: Eq + Hash + Clone> { /* private fields */ }Expand description
A two-way map between keys (left) and values (right).
See the module-level documentation for more details and examples.
Implementations§
Source§impl<L: Eq + Hash + Clone, R: Eq + Hash + Clone> BisetMap<L, R>
impl<L: Eq + Hash + Clone, R: Eq + Hash + Clone> BisetMap<L, R>
Sourcepub fn new() -> BisetMap<L, R>
pub fn new() -> BisetMap<L, R>
Creates an empty BisetMap.
§Examples
use bisetmap::BisetMap;
let bmap: BisetMap<char, u32> = BisetMap::new();Sourcepub fn with_capacity(capacity: usize) -> BisetMap<L, R>
pub fn with_capacity(capacity: usize) -> BisetMap<L, R>
Creates an empty BisetMap with the given capacity.
§Examples
use bisetmap::BisetMap;
let bmap: BisetMap<char, u32> = BisetMap::with_capacity(5);Sourcepub fn clear(&self)
pub fn clear(&self)
Removes all key-value pairs from the BisetMap, but keeps the allocated memory for reuse.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('b', 2);
bmap.insert('c', 3);
bmap.clear();
assert!(bmap.len() == 0);Sourcepub fn rev(&self) -> BisetMap<R, L>
pub fn rev(&self) -> BisetMap<R, L>
Returns a new BisetMap where keys and values are swapped.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('b', 2);
bmap.insert('c', 3);
let rmap = bmap.rev();
assert_eq!(rmap.get(&1), ['a']);Sourcepub fn collect(&self) -> Vec<(L, Vec<R>)>
pub fn collect(&self) -> Vec<(L, Vec<R>)>
Returns a vector of (key,values) pairs, where values itself is a vector. (Order of all pairs is unknown)
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('a', 2);
bmap.insert('b', 3);
assert_eq!(bmap.collect(), vec![('a',vec![1,2]), ('b',vec![3])]);Sourcepub fn flat_collect(&self) -> Vec<(L, R)>
pub fn flat_collect(&self) -> Vec<(L, R)>
Returns a vector of (key,value) pairs. (Order of pairs is unknown)
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('a', 2);
bmap.insert('b', 3);
assert_eq!(bmap.flat_collect(), [('a',1), ('a',2), ('b',3)]);Sourcepub fn insert(&self, k: L, v: R)
pub fn insert(&self, k: L, v: R)
Inserts the given key-value pair into the BisetMap.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('a', 1);
bmap.insert('a', 2);
bmap.insert('b', 3);
assert_eq!(bmap.flat_collect(), [('a',1), ('a',2), ('b',3)]);Sourcepub fn remove(&self, k: &L, v: &R)
pub fn remove(&self, k: &L, v: &R)
Removes the specified key-value pair from the BisetMap.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('a', 1);
bmap.insert('a', 2);
bmap.insert('c', 3);
assert_eq!(bmap.len(), 2);
assert_eq!(bmap.rev_len(), 3);
bmap.remove(&'a', &2);
assert_eq!(bmap.len(), 2);
assert_eq!(bmap.rev_len(), 2);Sourcepub fn contains(&self, k: &L, v: &R) -> bool
pub fn contains(&self, k: &L, v: &R) -> bool
Returns true if the map contains the given key-value pair and false otherwise.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
assert!(bmap.contains(&'a', &1));
assert!(!bmap.contains(&'b', &1));
assert!(!bmap.contains(&'a', &2));Sourcepub fn key_exists(&self, k: &L) -> bool
pub fn key_exists(&self, k: &L) -> bool
Returns true if the map contains the given key (left) value and false otherwise.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
assert!(bmap.key_exists(&'a'));
assert!(!bmap.key_exists(&'b'));Sourcepub fn value_exists(&self, v: &R) -> bool
pub fn value_exists(&self, v: &R) -> bool
Returns true if the map contains the given value (right) and false otherwise.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
assert!(bmap.value_exists(&1));
assert!(!bmap.value_exists(&2));Sourcepub fn get(&self, k: &L) -> Vec<R>
pub fn get(&self, k: &L) -> Vec<R>
Returns a vector of values (right) corresponding to the given key (left).
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
assert_eq!(bmap.get(&'a'), [1]);
assert_eq!(bmap.get(&'z'), []);Sourcepub fn rev_get(&self, v: &R) -> Vec<L>
pub fn rev_get(&self, v: &R) -> Vec<L>
Returns a vector of keys (left) corresponding to the given value (right).
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
assert_eq!(bmap.rev_get(&1), ['a']);
assert_eq!(bmap.rev_get(&2), []);Sourcepub fn delete(&self, k: &L) -> Vec<R>
pub fn delete(&self, k: &L) -> Vec<R>
Removes the specified key and all values associated with it.
Returns a vector of previous values associated with given key.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('a', 2);
bmap.insert('c', 3);
assert_eq!(bmap.delete(&'a'), [1, 2]);
assert_eq!(bmap.len(), 1);Sourcepub fn rev_delete(&self, v: &R) -> Vec<L>
pub fn rev_delete(&self, v: &R) -> Vec<L>
Removes the specified value and all keys associated with it.
Returns a vector of previous keys associated with given value.
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('b', 1);
bmap.insert('c', 2);
assert_eq!(bmap.rev_delete(&1), ['a', 'b']);
assert_eq!(bmap.len(), 1);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of unique keys (left).
§Examples
use bisetmap::BisetMap;
let bmap = BisetMap::new();
bmap.insert('a', 1);
bmap.insert('b', 2);
bmap.insert('c', 3);
bmap.insert('c', 4);
assert_eq!(bmap.len(), 3);