Struct bimap::BiMap [] [src]

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

A two-way map between left values and right values.

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

Methods

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

[src]

Creates an empty BiMap.

Examples

use bimap::BiMap;

let mut bimap: BiMap<char, u32> = BiMap::new();

[src]

Creates an empty BiMap with the given capacity.

Examples

use bimap::BiMap;

let mut bimap: BiMap<char, u32> = BiMap::with_capacity(5);

[src]

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

Examples

use bimap::BiMap;

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

[src]

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

Examples

use bimap::BiMap;

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

[src]

Removes all left-right pairs from the BiMap, but keeps the allocated memory for reuse.

Examples

use bimap::BiMap;

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

[src]

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

Examples

use bimap::BiMap;

let mut bimap: BiMap<char, u32> = BiMap::with_capacity(5);
assert!(bimap.capacity() >= 5);

Important traits for Iter<'a, L, R>
[src]

Create an iterator over the left-right pairs in the BiMap in arbitrary order.

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

Examples

use bimap::BiMap;

let mut bimap = BiMap::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>
[src]

Create an iterator over the left values in the BiMap in arbitrary order.

The iterator element type is &'a L.

Examples

use bimap::BiMap;

let mut bimap = BiMap::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>
[src]

Create an iterator over the right values in the BiMap in arbitrary order.

The iterator element type is &'a R.

Examples

use bimap::BiMap;

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

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

[src]

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

Examples

use bimap::BiMap;

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

[src]

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

Examples

use bimap::BiMap;

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

[src]

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

Examples

use bimap::BiMap;

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

[src]

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

Examples

use bimap::BiMap;

let mut bimap: BiMap<char, u32> = BiMap::new();
bimap.insert('a', 1);
assert!(bimap.contains_right(&1));
assert!(!bimap.contains_right(&2));

[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::BiMap;

let mut bimap = BiMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);
assert_eq!(bimap.len(), 3);
assert_eq!(bimap.remove_by_left(&'b'), Some(('b', 2)));
assert_eq!(bimap.len(), 2);
assert_eq!(bimap.remove_by_left(&'b'), None);
assert_eq!(bimap.len(), 2);

[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::BiMap;

let mut bimap = BiMap::new();
bimap.insert('a', 1);
bimap.insert('b', 2);
bimap.insert('c', 3);
assert_eq!(bimap.len(), 3);
assert_eq!(bimap.remove_by_right(&2), Some(('b', 2)));
assert_eq!(bimap.len(), 2);
assert_eq!(bimap.remove_by_right(&2), None);
assert_eq!(bimap.len(), 2);

[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::{BiMap, Overwritten};

let mut bimap = BiMap::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}

[src]

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

Returns a boolean representing if the pair was successfully inserted into the BiMap. If either value exists in the map, false is returned and the map is unchanged. Otherwise, the pair is inserted and true is returned.

Examples

use bimap::BiMap;

let mut bimap = BiMap::new();
assert!(bimap.insert_no_overwrite('a', 1));
assert!(bimap.insert_no_overwrite('b', 2));
assert!(!bimap.insert_no_overwrite('a', 3));
assert!(!bimap.insert_no_overwrite('c', 2));

[src]

Retains only the elements specified by the predicate.

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

This is called for both internal HashMaps.

Examples

use bimap::BiMap;

let mut bimap = BiMap::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> Clone for BiMap<L, R> where
    L: Eq + Hash + Clone,
    R: Eq + Hash + Clone
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

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

[src]

Formats the value using the given formatter. Read more

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

[src]

Returns the "default value" for a type. Read more

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

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

[src]

Creates a value from an iterator. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Important traits for Iter<'a, L, R>
[src]

Creates an iterator from a value. Read more

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Important traits for IntoIter<L, R>
[src]

Creates an iterator from a value. Read more

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

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

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

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

Auto Trait Implementations