BidirMap

Struct BidirMap 

Source
pub struct BidirMap<Kv1: PartialEq, Kv2: PartialEq> { /* private fields */ }
Expand description

A bidirectional map.

Bidirectional maps allow for mapping from and to both types.

The interface is based on that of BTreeMap, except, that for all functions, where one would supply a key, there are two functions, each treating one of the types as keys (get() -> get_by_{first,second}()).

Performance: O(n), mostly.

Implementations§

Source§

impl<Kv1: PartialEq, Kv2: PartialEq> BidirMap<Kv1, Kv2>

Source

pub fn new() -> Self

Create a new empty instance of BidirMap

Source

pub fn with_capacity(capacity: usize) -> Self

Create a new empty instance of BidirMap with the specified capacity.

It will be able to hold at least capacity elements without reallocating.

Source

pub fn clear(&mut self)

Clears the map, removing all entries.

§Examples
use bidir_map::BidirMap;

let mut a = BidirMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());
Source

pub fn insert(&mut self, kv1: Kv1, kv2: Kv2) -> Option<(Kv1, Kv2)>

Inserts a K/V-K/V pair into the map.

If the map did not have this K/V-K/V pair present, None is returned.

If the map did have this K/V-K/V pair present, it’s updated and the old K/V-K/V pair is returned.

Source

pub fn iter(&self) -> Iter<'_, Kv1, Kv2>

Gets an iterator over the entries of the map.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "c");

for kv in map.iter() {
	println!("{}: {}", kv.0, kv.1);
}

let first = map.iter().next().unwrap();
assert_eq!(first, (&1, &"a"));
Source

pub fn iter_mut(&mut self) -> IterMut<'_, Kv1, Kv2>

Gets a mutable iterator over the entries of the map.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

// add 10 to the value if the key isn't "a"
for kv in map.iter_mut() {
	if *kv.0 != "a" {
		*kv.1 += 10;
	}
}
Source

pub fn first_col(&self) -> FirstColumn<'_, Kv1, Kv2>

Gets an iterator over the first K/V of the map.

§Examples
use bidir_map::BidirMap;

let mut a = BidirMap::new();
a.insert(1, "a");
a.insert(2, "b");

let keys: Vec<_> = a.first_col().cloned().collect();
assert_eq!(keys, [1, 2]);
Source

pub fn second_col(&self) -> SecondColumn<'_, Kv1, Kv2>

Gets an iterator over the second K/V of the map.

§Examples
use bidir_map::BidirMap;

let mut a = BidirMap::new();
a.insert(1, "a");
a.insert(2, "b");

let keys: Vec<_> = a.second_col().cloned().collect();
assert_eq!(keys, ["a", "b"]);
Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Examples
use bidir_map::BidirMap;

let mut a = BidirMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Examples
use bidir_map::BidirMap;

let mut a = BidirMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());
Source

pub fn get_by_first<Q>(&self, key: &Q) -> Option<&Kv2>
where Kv1: Borrow<Q>, Q: PartialEq<Kv1> + ?Sized,

Returns a reference to the second K/V corresponding to the first K/V.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.get_by_first(&1), Some(&"a"));
assert_eq!(map.get_by_first(&2), None);
Source

pub fn get_by_second<Q>(&self, key: &Q) -> Option<&Kv1>
where Kv2: Borrow<Q>, Q: PartialEq<Kv2> + ?Sized,

Returns a reference to the first K/V corresponding to the second K/V.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.get_by_second(&"a"), Some(&1));
assert_eq!(map.get_by_second(&"b"), None);
Source

pub fn contains_first_key<Q>(&self, key: &Q) -> bool
where Kv1: Borrow<Q>, Q: PartialEq<Kv1> + ?Sized,

Check if the map contains the first K/V

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.contains_first_key(&1), true);
assert_eq!(map.contains_first_key(&2), false);
Source

pub fn contains_second_key<Q>(&self, key: &Q) -> bool
where Kv2: Borrow<Q>, Q: PartialEq<Kv2> + ?Sized,

Check if the map contains the second K/V

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.contains_second_key(&"a"), true);
assert_eq!(map.contains_second_key(&"b"), false);
Source

pub fn get_mut_by_first<Q>(&mut self, key: &Q) -> Option<&mut Kv2>
where Kv1: Borrow<Q>, Q: PartialEq<Kv1> + ?Sized,

Returns a mutable reference to the second K/V corresponding to the first K/V.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut_by_first(&1) {
    *x = "b";
}
assert_eq!(map.get_by_first(&1), Some(&"b"));
Source

pub fn get_mut_by_second<Q>(&mut self, key: &Q) -> Option<&mut Kv1>
where Kv2: Borrow<Q>, Q: PartialEq<Kv2> + ?Sized,

Returns a mutable reference to the first K/V corresponding to the second K/V.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut_by_second(&"a") {
    *x = 2;
}
assert_eq!(map.get_by_second(&"a"), Some(&2));
Source

pub fn remove_by_first<Q>(&mut self, key: &Q) -> Option<(Kv1, Kv2)>
where Kv1: Borrow<Q>, Q: PartialEq<Kv1> + ?Sized,

Removes the pair corresponding to the first K/V from the map, returning it if the key was previously in the map.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.remove_by_first(&1), Some((1, "a")));
assert_eq!(map.remove_by_first(&1), None);
Source

pub fn remove_by_second<Q>(&mut self, key: &Q) -> Option<(Kv1, Kv2)>
where Kv2: Borrow<Q>, Q: PartialEq<Kv2> + ?Sized,

Removes the pair corresponding to the first K/V from the map, returning it if the key was previously in the map.

§Examples
use bidir_map::BidirMap;

let mut map = BidirMap::new();
map.insert(1, "a");
assert_eq!(map.remove_by_second(&"a"), Some((1, "a")));
assert_eq!(map.remove_by_second(&"b"), None);

Trait Implementations§

Source§

impl<Kv1: Clone + PartialEq, Kv2: Clone + PartialEq> Clone for BidirMap<Kv1, Kv2>

Source§

fn clone(&self) -> BidirMap<Kv1, Kv2>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Kv1: Debug + PartialEq, Kv2: Debug + PartialEq> Debug for BidirMap<Kv1, Kv2>

Source§

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

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

impl<Kv1: Default + PartialEq, Kv2: Default + PartialEq> Default for BidirMap<Kv1, Kv2>

Source§

fn default() -> BidirMap<Kv1, Kv2>

Returns the “default value” for a type. Read more
Source§

impl<Kv1: PartialEq, Kv2: PartialEq> Extend<(Kv1, Kv2)> for BidirMap<Kv1, Kv2>

Source§

fn extend<T: IntoIterator<Item = (Kv1, Kv2)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<Kv1: PartialEq, Kv2: PartialEq> FromIterator<(Kv1, Kv2)> for BidirMap<Kv1, Kv2>

Source§

fn from_iter<T: IntoIterator<Item = (Kv1, Kv2)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<Kv1: Hash + PartialEq, Kv2: Hash + PartialEq> Hash for BidirMap<Kv1, Kv2>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a, 'q, Kv1, Kv2: PartialEq, Q> Index<&'a ByFirst<'q, Q>> for BidirMap<Kv1, Kv2>
where Kv1: Borrow<Q> + PartialEq, Q: PartialEq<Kv1> + ?Sized + 'q,

Source§

type Output = Kv2

The returned type after indexing.
Source§

fn index(&self, key: &ByFirst<'_, Q>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, 'q, Kv1: PartialEq, Kv2, Q> Index<&'a BySecond<'q, Q>> for BidirMap<Kv1, Kv2>
where Kv2: Borrow<Q> + PartialEq, Q: PartialEq<Kv2> + ?Sized + 'q,

Source§

type Output = Kv1

The returned type after indexing.
Source§

fn index(&self, key: &BySecond<'_, Q>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'q, Kv1, Kv2: PartialEq, Q> Index<ByFirst<'q, Q>> for BidirMap<Kv1, Kv2>
where Kv1: Borrow<Q> + PartialEq, Q: PartialEq<Kv1> + ?Sized + 'q,

Source§

type Output = Kv2

The returned type after indexing.
Source§

fn index(&self, key: ByFirst<'_, Q>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'q, Kv1: PartialEq, Kv2, Q> Index<BySecond<'q, Q>> for BidirMap<Kv1, Kv2>
where Kv2: Borrow<Q> + PartialEq, Q: PartialEq<Kv2> + ?Sized + 'q,

Source§

type Output = Kv1

The returned type after indexing.
Source§

fn index(&self, key: BySecond<'_, Q>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<Kv1: PartialEq, Kv2: PartialEq> IntoIterator for BidirMap<Kv1, Kv2>

Source§

type Item = (Kv1, Kv2)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<<BidirMap<Kv1, Kv2> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<Kv1: PartialEq + PartialEq, Kv2: PartialEq + PartialEq> PartialEq for BidirMap<Kv1, Kv2>

Source§

fn eq(&self, other: &BidirMap<Kv1, Kv2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Kv1: Eq + PartialEq, Kv2: Eq + PartialEq> Eq for BidirMap<Kv1, Kv2>

Source§

impl<Kv1: PartialEq, Kv2: PartialEq> StructuralPartialEq for BidirMap<Kv1, Kv2>

Auto Trait Implementations§

§

impl<Kv1, Kv2> Freeze for BidirMap<Kv1, Kv2>

§

impl<Kv1, Kv2> RefUnwindSafe for BidirMap<Kv1, Kv2>
where Kv1: RefUnwindSafe, Kv2: RefUnwindSafe,

§

impl<Kv1, Kv2> Send for BidirMap<Kv1, Kv2>
where Kv1: Send, Kv2: Send,

§

impl<Kv1, Kv2> Sync for BidirMap<Kv1, Kv2>
where Kv1: Sync, Kv2: Sync,

§

impl<Kv1, Kv2> Unpin for BidirMap<Kv1, Kv2>
where Kv1: Unpin, Kv2: Unpin,

§

impl<Kv1, Kv2> UnwindSafe for BidirMap<Kv1, Kv2>
where Kv1: UnwindSafe, Kv2: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.