[][src]Struct bimap::hash::BiHashMap

pub struct BiHashMap<L, R> { /* fields omitted */ }

A bimap backed by two HashMaps.

See the module-level documentation for more details and examples.

Methods

impl<L, R> BiHashMap<L, R> where
    L: Eq + Hash,
    R: Eq + Hash
[src]

pub fn new() -> Self[src]

Creates an empty BiHashMap.

Examples

use bimap::BiHashMap;

let bimap = BiHashMap::<char, i32>::new();

pub fn with_capacity(capacity: usize) -> Self[src]

Creates a new empty BiHashMap with the given capacity.

Examples

use bimap::BiHashMap;

let bimap = BiHashMap::<char, i32>::with_capacity(10);
assert!(bimap.capacity() >= 10);

pub fn len(&self) -> usize[src]

Returns the number of left-right pairs in the bimap.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);
assert_eq!(bimap.len(), 3);

pub fn is_empty(&self) -> bool[src]

Returns true if the bimap contains no left-right pairs, and false otherwise.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
assert!(bimap.is_empty());
bimap.insert('a', 1);
assert!(!bimap.is_empty());
bimap.remove_by_right(&1);
assert!(bimap.is_empty());

pub fn capacity(&self) -> usize[src]

Returns a lower bound on the number of left-right pairs the BiHashMap can store without reallocating memory.

Examples

use bimap::BiHashMap;

let bimap = BiHashMap::<char, i32>::with_capacity(10);
assert!(bimap.capacity() >= 10);

pub fn clear(&mut self)[src]

Removes all left-right pairs from the bimap.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);
bimap.clear();
assert!(bimap.len() == 0);

Important traits for Iter<'a, L, R>
pub fn iter(&self) -> Iter<L, R>[src]

Creates an iterator over the left-right pairs in the bimap in arbitrary order.

The iterator element type is (&L, &R).

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);

for (left, right) in bimap.iter() {
    println!("({}, {})", left, right);
}

Important traits for LeftValues<'a, L, R>
pub fn left_values(&self) -> LeftValues<L, R>[src]

Creates an iterator over the left values in the bimap in arbitrary order.

The iterator element type is &L.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);

for char_value in bimap.left_values() {
    println!("{}", char_value);
}

Important traits for RightValues<'a, L, R>
pub fn right_values(&self) -> RightValues<L, R>[src]

Creates an iterator over the right values in the bimap in arbitrary order.

The iterator element type is &R.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);

for int_value in bimap.right_values() {
    println!("{}", int_value);
}

pub fn get_by_left(&self, left: &L) -> Option<&R>[src]

Returns a reference to the right value corresponding to the given left value.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
assert_eq!(bimap.get_by_left(&'a'), Some(&1));
assert_eq!(bimap.get_by_left(&'z'), None);

pub fn get_by_right(&self, right: &R) -> Option<&L>[src]

Returns a reference to the left value corresponding to the given right value.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
assert_eq!(bimap.get_by_right(&1), Some(&'a'));
assert_eq!(bimap.get_by_right(&2), None);

pub fn contains_left(&self, left: &L) -> bool[src]

Returns true if the bimap contains the given left value and false otherwise.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
assert!(bimap.contains_left(&'a'));
assert!(!bimap.contains_left(&'b'));

pub fn contains_right(&self, right: &R) -> bool[src]

Returns true if the map contains the given right value and false otherwise.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
assert!(bimap.contains_right(&1));
assert!(!bimap.contains_right(&2));

pub fn remove_by_left(&mut self, left: &L) -> Option<(L, R)>[src]

Removes the left-right pair corresponding to the given left value.

Returns the previous left-right pair if the map contained the left value and None otherwise.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);

assert_eq!(bimap.remove_by_left(&'b'), Some(('b', 2)));
assert_eq!(bimap.remove_by_left(&'b'), None);

pub fn remove_by_right(&mut self, right: &R) -> Option<(L, R)>[src]

Removes the left-right pair corresponding to the given right value.

Returns the previous left-right pair if the map contained the right value and None otherwise.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);

assert_eq!(bimap.remove_by_right(&2), Some(('b', 2)));
assert_eq!(bimap.remove_by_right(&2), None);

pub fn insert(&mut self, left: L, right: R) -> Overwritten<L, R>[src]

Inserts the given left-right pair into the bimap.

Returns an enum Overwritten representing any left-right pairs that were overwritten by the call to insert. The example below details all possible enum variants that can be returned.

Warnings

Somewhat paradoxically, calling insert() can actually reduce the size of the bimap! This is because of the invariant that each left value maps to exactly one right value and vice versa.

Examples

use bimap::{BiHashMap, Overwritten};

let mut bimap = BiHashMap::new();
assert_eq!(bimap.len(), 0); // {}

// no values are overwritten.
assert_eq!(bimap.insert('a', 1), Overwritten::Neither);
assert_eq!(bimap.len(), 1); // {'a' <> 1}

// no values are overwritten.
assert_eq!(bimap.insert('b', 2), Overwritten::Neither);
assert_eq!(bimap.len(), 2); // {'a' <> 1, 'b' <> 2}

// ('a', 1) already exists, so inserting ('a', 4) overwrites 'a', the left value.
// the previous left-right pair ('a', 1) is returned.
assert_eq!(bimap.insert('a', 4), Overwritten::Left('a', 1));
assert_eq!(bimap.len(), 2); // {'a' <> 4, 'b' <> 2}

// ('b', 2) already exists, so inserting ('c', 2) overwrites 2, the right value.
// the previous left-right pair ('b', 2) is returned.
assert_eq!(bimap.insert('c', 2), Overwritten::Right('b', 2));
assert_eq!(bimap.len(), 2); // {'a' <> 1, 'c' <> 2}

// both ('a', 4) and ('c', 2) already exist, so inserting ('a', 2) overwrites both.
// ('a', 4) has the overwritten left value ('a'), so it's the first tuple returned.
// ('c', 2) has the overwritten right value (2), so it's the second tuple returned.
assert_eq!(bimap.insert('a', 2), Overwritten::Both(('a', 4), ('c', 2)));
assert_eq!(bimap.len(), 1); // {'a' <> 2} // bimap is smaller than before!

// ('a', 2) already exists, so inserting ('a', 2) overwrites the pair.
// the previous left-right pair ('a', 2) is returned.
assert_eq!(bimap.insert('a', 2), Overwritten::Pair('a', 2));
assert_eq!(bimap.len(), 1); // {'a' <> 2}

pub fn insert_no_overwrite(&mut self, left: L, right: R) -> Result<(), (L, R)>[src]

Inserts the given left-right pair into the bimap without overwriting any existing values.

Returns Ok(()) if the pair was successfully inserted into the bimap. If either value exists in the map, Err((left, right) is returned with the attempted left-right pair and the map is unchanged.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
assert_eq!(bimap.insert_no_overwrite('a', 1), Ok(()));
assert_eq!(bimap.insert_no_overwrite('b', 2), Ok(()));
assert_eq!(bimap.insert_no_overwrite('a', 3), Err(('a', 3)));
assert_eq!(bimap.insert_no_overwrite('c', 2), Err(('c', 2)));

pub fn retain<F>(&mut self, f: F) where
    F: FnMut(&L, &R) -> bool
[src]

Retains only the elements specified by the predicate.

In other words, remove all left-right pairs (l, r) such that f(&l, &r) returns false.

Examples

use bimap::BiHashMap;

let mut bimap = BiHashMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);
bimap.retain(|&l, &r| r >= 2);
assert_eq!(bimap.len(), 2);
assert_eq!(bimap.get_by_left(&'b'), Some(&2));
assert_eq!(bimap.get_by_left(&'c'), Some(&3));
assert_eq!(bimap.get_by_left(&'a'), None);

Trait Implementations

impl<L, R> PartialEq<BiHashMap<L, R>> for BiHashMap<L, R> where
    L: Eq + Hash,
    R: Eq + Hash
[src]

#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0
[src]

This method tests for !=.

impl<'a, L, R> IntoIterator for &'a BiHashMap<L, R> where
    L: Eq + Hash,
    R: Eq + Hash
[src]

type Item = (&'a L, &'a R)

The type of the elements being iterated over.

type IntoIter = Iter<'a, L, R>

Which kind of iterator are we turning this into?

impl<L, R> IntoIterator for BiHashMap<L, R> where
    L: Eq + Hash,
    R: Eq + Hash
[src]

type Item = (L, R)

The type of the elements being iterated over.

type IntoIter = IntoIter<L, R>

Which kind of iterator are we turning this into?

impl<L, R> Default for BiHashMap<L, R> where
    L: Eq + Hash,
    R: Eq + Hash
[src]

impl<L, R> Sync for BiHashMap<L, R> where
    L: Sync,
    R: Sync
[src]

impl<L, R> Clone for BiHashMap<L, R> where
    L: Clone + Eq + Hash,
    R: Clone + Eq + Hash
[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<L, R> Send for BiHashMap<L, R> where
    L: Send,
    R: Send
[src]

impl<L, R> Eq for BiHashMap<L, R> where
    L: Eq + Hash,
    R: Eq + Hash
[src]

impl<L, R> Debug for BiHashMap<L, R> where
    L: Debug + Eq + Hash,
    R: Debug + Eq + Hash
[src]

impl<L, R> FromIterator<(L, R)> for BiHashMap<L, R> where
    L: Eq + Hash,
    R: Eq + Hash
[src]

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]