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
EGHMST
Values
-5-1103421024
Associations
EGHMST
-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>

source

pub fn keys(&self) -> Slice<'_, K>

All keys present in this map, sorted.

source

pub fn values(&self) -> Slice<'_, V>

All values present in this map, sorted.

source

pub fn get(&self, key: &K) -> impl SortedIterator<Item = &V> + '_

Get all values associated with key.

source

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 + Debug, V: Eq + Ord + Debug> Debug for Bimultimap<K, V>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

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

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.