Struct datazoo::bimultimap::Bimultimap
source · pub struct Bimultimap<K: Eq + Ord, V: Eq + Ord> { /* private fields */ }
Expand description
A read-only bi-directional multimap.
This has very good perf for small sets of key and values that themselves shouldn’t take much memory.
Furthermore, not only can you get all values
associated with a given key
,
but also all keys
associated with a given value
.
See Bimultimap::get_keys_of
and Bimultimap::get
.
Design
Consider K = char
and V = i64
. A Bimultimap
stores a limited subset of
char
and i64
and an association list.
Keys and values are stored in two sorted lists, associations in a bitset matrix.
Keys
E | G | H | M | S | T |
---|
Values
-5 | -1 | 10 | 342 | 1024 |
---|
Associations
E | G | H | M | S | T | |
---|---|---|---|---|---|---|
-5 | █ | █ | █ | |||
-1 | █ | █ | █ | |||
10 | █ | █ | ||||
342 | █ | █ | █ | |||
1024 | █ |
Example
use datazoo::Bimultimap;
let associations: Vec<(char, i64)> = vec![
('E', -1),
('G', -1), ('G', 342),
('H', -5), ('H', 10), ('H', 342),
('M', -5), ('M', 10),
('S', 342),
('T',-5), ('T',-1), ('T',1024),
];
let map: Bimultimap<char, i64> = associations.into_iter().collect();
let assocs = map.get(&'V').copied().collect::<Vec<_>>();
assert_eq!(&assocs, &[]);
let assocs = map.get(&'T').copied().collect::<Vec<_>>();
assert_eq!(&assocs, &[-5, -1, 1024]);
let assocs = map.get_keys_of(&-1).copied().collect::<Vec<_>>();
assert_eq!(&assocs, &['E', 'G', 'T']);
Implementations§
source§impl<K: Eq + Ord, V: Eq + Ord> Bimultimap<K, V>
impl<K: Eq + Ord, V: Eq + Ord> Bimultimap<K, V>
sourcepub fn get(&self, key: &K) -> impl SortedIterator<Item = &V> + '_
pub fn get(&self, key: &K) -> impl SortedIterator<Item = &V> + '_
Get all values associated with key
.
sourcepub fn get_keys_of(&self, value: &V) -> impl SortedIterator<Item = &K> + '_
pub fn get_keys_of(&self, value: &V) -> impl SortedIterator<Item = &K> + '_
Get all keys associated with value
.
Trait Implementations§
source§impl<K: Eq + Ord + Clone, V: Eq + Ord + Clone> FromIterator<(K, V)> for Bimultimap<K, V>
impl<K: Eq + Ord + Clone, V: Eq + Ord + Clone> FromIterator<(K, V)> for Bimultimap<K, V>
source§fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self
Create a Bimultimap
with all associations.
Note that this takes into account
Auto Trait Implementations§
impl<K, V> RefUnwindSafe for Bimultimap<K, V>where K: RefUnwindSafe, V: RefUnwindSafe,
impl<K, V> Send for Bimultimap<K, V>where K: Send, V: Send,
impl<K, V> Sync for Bimultimap<K, V>where K: Sync, V: Sync,
impl<K, V> Unpin for Bimultimap<K, V>
impl<K, V> UnwindSafe for Bimultimap<K, V>where K: UnwindSafe, V: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more