Struct rb_tree::RBMap[][src]

pub struct RBMap<K: PartialOrd, V> { /* fields omitted */ }
Expand description

A map implemented using a red black tree to store key-value pairs.

Implementations

Creates and returns a new, empty RBMap

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert("Hello", "World");
assert_eq!(map.remove(&"Hello").unwrap(), "World");

Creates an RBTree set of the keys contained in this map.

Example:

use rb_tree::{RBMap, RBTree};

let mut map = RBMap::new();
map.insert("Hello", "World");
map.insert("Foo", "Bar");
let kset = map.keyset();
assert!(kset.contains(&&"Hello"));
assert!(kset.contains(&&"Foo"));
assert!(!kset.contains(&&"Bar"));

Creates a set from the keys in this map.

Example:

use rb_tree::{RBMap, RBTree};

let mut map = RBMap::new();
map.insert("Hello", "World");
map.insert("Foo", "Bar");
let kset = map.into_keyset();
assert!(kset.contains(&"Hello"));
assert!(kset.contains(&"Foo"));
assert!(!kset.contains(&"Bar"));

Clears all entries from the RBMap

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert("Hello", "world");
map.insert("Foo", "bar");
assert_eq!(map.len(), 2);
map.clear();
assert_eq!(map.len(), 0);
assert!(map.remove(&"Hello").is_none());

Returns true if the map contains an entry for key, false otherwise.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(!map.contains_key(&"Hello"));
map.insert("Hello", "world");
assert!(map.contains_key(&"Hello"));

Clears the map and returns an iterator over all key-value pairs that were contained in the order of their keys’ PartialOrd order.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert("Hello", "world");
map.insert("Foo", "bar");
let mut drain = map.drain();
assert_eq!(drain.next().unwrap(), ("Foo", "bar"));
assert_eq!(drain.next().unwrap(), ("Hello", "world"));
assert!(drain.next().is_none());

Returns an option containing a reference to the value associated with this key, or none if this key does not have an associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(map.get(&"Hello").is_none());
map.insert("Hello", "world");
assert_eq!(map.get(&"Hello").unwrap(), &"world");

Returns an option containing a reference to the key-value pair associated with this key, or none if this key does not have an associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(map.get(&"Hello").is_none());
map.insert("Hello", "world");
assert_eq!(map.get_pair(&"Hello").unwrap(), (&"Hello", &"world"));

Returns an option containing a reference to the key-value pair associated with this key of which the value is mutable. Returns none if this key does not have an associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(map.get(&"Hello").is_none());
map.insert("Hello", "world");
assert_eq!(map.get_pair(&"Hello").unwrap(), (&"Hello", &"world"));

Returns an option containing a mutable reference to the value associated with this key, or none if this key does not have an associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(map.get(&"Hello").is_none());
map.insert("Hello", "world");
*map.get_mut(&"Hello").unwrap() = "world!";
assert_eq!(map.get(&"Hello").unwrap(), &"world!");

Returns an option containing a reference to the value associated with the key that has the smallest PartialOrd value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.peek(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.peek().unwrap(), &"World");

Returns an option containing a reference to the value associated with the key that has the largest PartialOrd value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.peek_back(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.peek_back().unwrap(), &"Foo");

Returns an option containing a pair with a reference to the key with the smallest PartialOrd value and a reference to its associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.peek_pair(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.peek_pair().unwrap(), (&2, &"World"));

Returns an option containing a pair with a reference to the key with the largest PartialOrd value and a reference to its associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.peek_pair_back(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.peek_pair_back().unwrap(), (&7, &"Foo"));

Inserts a value to associate with the given key into the map, returning the previously-stored key-value pair if one existed, None otherwise.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert("Hello", "world");
map.insert("Foo", "bar");
assert_eq!(map.len(), 2);

Returns true if there are no key-value pairs stored in this RBMap, false otherwise.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(map.is_empty());
map.insert(1, 2);
assert!(!map.is_empty());

Returns the number of key-value pairs stored in this RBMap.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.len(), 0);
map.insert(1, 1);
assert_eq!(map.len(), 1);
map.insert(2, 4);
assert_eq!(map.len(), 2);
map.remove(&2);
assert_eq!(map.len(), 1);

Removes the key-value pair associated with key, if one exists, and returns the associated value, or None if the pair did not exist.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(map.remove(&2).is_none());
map.insert(2, 4);
assert_eq!(map.remove(&2).unwrap(), 4);

pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)>

Removes the key-value pair associated with key, if one exists, and returns it, or None if the pair did not exist.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert!(map.remove_entry(&2).is_none());
map.insert(2, 4);
assert_eq!(map.remove_entry(&2).unwrap(), (2, 4));

Removes the pair associated with the key that has the smallest PartialOrd value and returns the associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.pop(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.pop().unwrap(), "World");
assert_eq!(map.pop().unwrap(), "Hello");

Removes the pair associated with the key that has the largest PartialOrd value and returns the associated value.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.pop(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.pop_back().unwrap(), "Foo");
assert_eq!(map.pop_back().unwrap(), "Bar");

Removes the pair associated with the key that has the smallest PartialOrd value and returns it.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.pop_pair(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.pop_pair().unwrap(), (2, "World"));
assert_eq!(map.pop_pair().unwrap(), (5, "Hello"));

Removes the pair associated with the key that has the smallest PartialOrd value and returns it.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
assert_eq!(map.pop_pair_back(), None);

map.insert(5, "Hello");
map.insert(2, "World");
map.insert(7, "Foo");
map.insert(6, "Bar");

assert_eq!(map.pop_pair_back().unwrap(), (7, "Foo"));
assert_eq!(map.pop_pair_back().unwrap(), (6, "Bar"));

Removes all key-value pairs that do not return true for the provided method.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert(1, 1);
map.insert(2, 4);
map.insert(3, 9);
map.retain(|_, v| *v % 2 == 0);

let mut pairs = map.drain();
assert_eq!(pairs.next().unwrap(), (2, 4));
assert_eq!(pairs.next(), None);

An iterator that visits all key-value pairs in their key’s partialord order.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert(1, 1);
map.insert(2, 4);
map.insert(3, 9);

let mut pairs = map.iter();
assert_eq!(pairs.next().unwrap(), (&1, &1));
assert_eq!(pairs.next().unwrap(), (&2, &4));
assert_eq!(pairs.next().unwrap(), (&3, &9));
assert_eq!(pairs.next(), None);

An iterator that visits all key-value pairs in their key’s partialord order and presents the value only as mutable.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert(1, 1);
map.insert(2, 4);
map.insert(3, 9);

map.iter_mut().for_each(|(_, v)| *v *= 2);

let mut pairs = map.iter();
assert_eq!(pairs.next().unwrap(), (&1, &2));
assert_eq!(pairs.next().unwrap(), (&2, &8));
assert_eq!(pairs.next().unwrap(), (&3, &18));
assert_eq!(pairs.next(), None);

An iterator that visits all values in their key’s partialord order.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert(1, 1);
map.insert(2, 4);
map.insert(3, 9);

let mut vals = map.values();
assert_eq!(*vals.next().unwrap(), 1);
assert_eq!(*vals.next().unwrap(), 4);
assert_eq!(*vals.next().unwrap(), 9);
assert_eq!(vals.next(), None);

An iterator that visits all values in their key’s partialord order and presents them as mutable.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert(1, 1);
map.insert(2, 4);
map.insert(3, 9);

map.values_mut().for_each(|v| *v *= 2);

let mut vals = map.values();
assert_eq!(*vals.next().unwrap(), 2);
assert_eq!(*vals.next().unwrap(), 8);
assert_eq!(*vals.next().unwrap(), 18);
assert_eq!(vals.next(), None);

An iterator that visits all keys in their partialord order.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();
map.insert(1, 1);
map.insert(2, 4);
map.insert(3, 9);

let mut keys = map.keys();
assert_eq!(*keys.next().unwrap(), 1);
assert_eq!(*keys.next().unwrap(), 2);
assert_eq!(*keys.next().unwrap(), 3);
assert_eq!(keys.next(), None);

Provides an interface for ensuring values are allocated to the given key.

Example:

use rb_tree::RBMap;

let mut map = RBMap::new();

let val = map.entry(1).or_insert(2);
*val = 3;
assert_eq!(*map.get(&1).unwrap(), 3);

Creates an RBTree set of the values contained in this map.

Example:

use rb_tree::{RBMap, RBTree};

let mut map = RBMap::new();
map.insert("Hello", "World");
map.insert("Foo", "Bar");
let vset = map.valueset();
assert!(vset.contains(&&"World"));
assert!(vset.contains(&&"Bar"));
assert!(!vset.contains(&&"Foo"));

Creates a set of keys and a set of values from the given map.

Note: any mapping information is lost when this operation is performed.

Example:

use rb_tree::{RBMap, RBTree};

let mut map = RBMap::new();
map.insert("Hello", "World");
map.insert("Foo", "Bar");
let (kset, vset) = map.into_sets();
assert!(kset.contains(&"Hello"));
assert!(kset.contains(&"Foo"));
assert!(!kset.contains(&"Bar"));
assert!(vset.contains(&"World"));
assert!(vset.contains(&"Bar"));
assert!(!vset.contains(&"Foo"));

Creates an RBTree set from the values contained in this map.

Example:

use rb_tree::{RBMap, RBTree};

let mut map = RBMap::new();
map.insert("Hello", "World");
map.insert("Foo", "Bar");
let vset = map.into_valueset();
assert!(vset.contains(&"World"));
assert!(vset.contains(&"Bar"));
assert!(!vset.contains(&"Foo"));

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

Formats the value using the given formatter. 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

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

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

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

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

Converts the given value to a String. 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.