Struct bimap::btree::BiBTreeMap

source ·
pub struct BiBTreeMap<L, R> { /* private fields */ }
Expand description

A bimap backed by two BTreeMaps.

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

Implementations§

Creates an empty BiBTreeMap.

Examples
use bimap::BiBTreeMap;

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

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

Examples
use bimap::BiBTreeMap;

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

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

Examples
use bimap::BiBTreeMap;

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

Removes all left-right pairs from the bimap.

Examples
use bimap::BiBTreeMap;

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

Creates an iterator over the left-right pairs in the bimap in ascending order by left value.

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

Examples
use bimap::BiBTreeMap;

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

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

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

The iterator element type is &L.

Examples
use bimap::BiBTreeMap;

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

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

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

The iterator element type is &R.

Examples
use bimap::BiBTreeMap;

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

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

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

The input may be any borrowed form of the bimap’s left type, but the ordering on the borrowed form must match the ordering on the left type.

Examples
use bimap::BiBTreeMap;

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

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

The input may be any borrowed form of the bimap’s right type, but the ordering on the borrowed form must match the ordering on the right type.

Examples
use bimap::BiBTreeMap;

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

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

The input may be any borrowed form of the bimap’s left type, but the ordering on the borrowed form must match the ordering on the left type.

Examples
use bimap::BiBTreeMap;

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

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

The input may be any borrowed form of the bimap’s right type, but the ordering on the borrowed form must match the ordering on the right type.

Examples
use bimap::BiBTreeMap;

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

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.

The input may be any borrowed form of the bimap’s left type, but the ordering on the borrowed form must match the ordering on the left type.

Examples
use bimap::BiBTreeMap;

let mut bimap = BiBTreeMap::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);

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.

The input may be any borrowed form of the bimap’s right type, but the ordering on the borrowed form must match the ordering on the right type.

Examples
use bimap::BiBTreeMap;

let mut bimap = BiBTreeMap::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);

Retains only elements specified by a predicate

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

Example
use bimap::BiBTreeMap;

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

bimap.retain(|&l, _| l != 'b');
assert_eq!(bimap.len(), 2);
assert_eq!(bimap.get_by_right(&1), Some(&'a'));
assert_eq!(bimap.get_by_right(&2), None);
assert_eq!(bimap.get_by_right(&3), Some(&'c'));

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

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

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::BiBTreeMap;

let mut bimap = BiBTreeMap::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)));

Creates an iterator over the left-right pairs lying within a range of left values in the bimap in ascending order by left.

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

The range bounds may be any borrowed form of the bimap’s left type, but the ordering on the borrowed form must match the ordering on the left type.

Examples
use bimap::BiBTreeMap;

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

for (left, right) in bimap.left_range('b'..'d') {
    println!("({}, {})", left, right);
}

Creates an iterator over the left-right pairs lying within a range of right values in the bimap in ascending order by right.

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

The range bounds may be any borrowed form of the bimap’s right type, but the ordering on the borrowed form must match the ordering on the right type.

Examples
use bimap::BiBTreeMap;

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

for (left, right) in bimap.right_range(2..4) {
    println!("({}, {})", left, right);
}

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
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
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
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.