[][src]Struct bidir_map::BidirMap

pub struct BidirMap<Kv1: PartialEq, Kv2: PartialEq> { /* fields omitted */ }

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.

Methods

impl<Kv1: PartialEq, Kv2: PartialEq> BidirMap<Kv1, Kv2>
[src]

Create a new empty instance of BidirMap

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

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

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());

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.

Important traits for Iter<'a, 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"));

Important traits for IterMut<'a, 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;
    }
}

Important traits for FirstColumn<'a, 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]);

Important traits for SecondColumn<'a, 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"]);

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);

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());

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);

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);

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);

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);

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"));

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));

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);

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

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

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

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

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

impl<Kv1: PartialEq + PartialEq, Kv2: PartialEq + PartialEq> PartialEq<BidirMap<Kv1, Kv2>> for BidirMap<Kv1, Kv2>
[src]

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

This method tests for !=.

impl<Kv1: PartialEq, Kv2: PartialEq> IntoIterator for BidirMap<Kv1, Kv2>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

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

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

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

Formats the value using the given formatter. Read more

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

The returned type after indexing.

Performs the indexing (container[index]) operation.

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

The returned type after indexing.

Performs the indexing (container[index]) operation.

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

The returned type after indexing.

Performs the indexing (container[index]) operation.

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

The returned type after indexing.

Performs the indexing (container[index]) operation.

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

Creates a value from an iterator. Read more

Auto Trait Implementations

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

Blanket Implementations

impl<T> From for T
[src]

Performs the conversion.

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

Performs the conversion.

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

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

Immutably borrows from an owned value. Read more

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

Mutably borrows from an owned value. Read more

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more