pub struct RBMap<K: PartialOrd, V> { /* private fields */ }
Expand description
A map implemented using a red black tree to store key-value pairs.
Implementations§
Source§impl<K: PartialOrd, V> RBMap<K, V>
impl<K: PartialOrd, V> RBMap<K, V>
Sourcepub fn new() -> RBMap<K, V>
pub fn new() -> RBMap<K, V>
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");
Sourcepub fn keyset(&self) -> RBTree<&K>
pub fn keyset(&self) -> RBTree<&K>
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"));
Sourcepub fn into_keyset(self) -> RBTree<K>
pub fn into_keyset(self) -> RBTree<K>
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"));
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
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());
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
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"));
Sourcepub fn drain(&mut self) -> Drain<K, V> ⓘ
pub fn drain(&mut self) -> Drain<K, V> ⓘ
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());
Sourcepub fn get(&self, key: &K) -> Option<&V>
pub fn get(&self, key: &K) -> Option<&V>
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");
Sourcepub fn get_pair(&self, key: &K) -> Option<(&K, &V)>
pub fn get_pair(&self, key: &K) -> Option<(&K, &V)>
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"));
Sourcepub fn get_pair_mut(&mut self, key: &K) -> Option<(&K, &mut V)>
pub fn get_pair_mut(&mut self, key: &K) -> Option<(&K, &mut V)>
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"));
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut V>
pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
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!");
Sourcepub fn peek(&self) -> Option<&V>
pub fn peek(&self) -> Option<&V>
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");
Sourcepub fn peek_back(&self) -> Option<&V>
pub fn peek_back(&self) -> Option<&V>
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");
Sourcepub fn peek_pair(&self) -> Option<(&K, &V)>
pub fn peek_pair(&self) -> Option<(&K, &V)>
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"));
Sourcepub fn peek_pair_back(&self) -> Option<(&K, &V)>
pub fn peek_pair_back(&self) -> Option<(&K, &V)>
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"));
Sourcepub fn insert(&mut self, key: K, val: V) -> Option<(K, V)>
pub fn insert(&mut self, key: K, val: V) -> Option<(K, V)>
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);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
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());
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
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);
Sourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
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);
Sourcepub fn remove_entry(&mut self, key: &K) -> Option<(K, V)>
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));
Sourcepub fn pop(&mut self) -> Option<V>
pub fn pop(&mut self) -> Option<V>
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");
Sourcepub fn pop_back(&mut self) -> Option<V>
pub fn pop_back(&mut self) -> Option<V>
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");
Sourcepub fn pop_pair(&mut self) -> Option<(K, V)>
pub fn pop_pair(&mut self) -> Option<(K, V)>
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"));
Sourcepub fn pop_pair_back(&mut self) -> Option<(K, V)>
pub fn pop_pair_back(&mut self) -> Option<(K, V)>
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"));
Sourcepub fn retain<F: FnMut(&K, &mut V) -> bool>(&mut self, logic: F)
pub fn retain<F: FnMut(&K, &mut V) -> bool>(&mut self, logic: F)
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);
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
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);
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ
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);
Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
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);
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
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);
Sourcepub fn keys(&self) -> Keys<'_, K, V> ⓘ
pub fn keys(&self) -> Keys<'_, K, V> ⓘ
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);
Source§impl<K: PartialOrd, V: PartialOrd> RBMap<K, V>
impl<K: PartialOrd, V: PartialOrd> RBMap<K, V>
Sourcepub fn valueset(&self) -> RBTree<&V>
pub fn valueset(&self) -> RBTree<&V>
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"));
Sourcepub fn into_sets(self) -> (RBTree<K>, RBTree<V>)
pub fn into_sets(self) -> (RBTree<K>, RBTree<V>)
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"));
Sourcepub fn into_valueset(self) -> RBTree<V>
pub fn into_valueset(self) -> RBTree<V>
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§
Source§impl<K: PartialOrd, V> Default for RBMap<K, V>
impl<K: PartialOrd, V> Default for RBMap<K, V>
Source§impl<'a, K: PartialOrd + Copy + 'a, V: Copy + 'a> Extend<(&'a K, &'a V)> for RBMap<K, V>
impl<'a, K: PartialOrd + Copy + 'a, V: Copy + 'a> Extend<(&'a K, &'a V)> for RBMap<K, V>
Source§fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<K: PartialOrd, V> Extend<(K, V)> for RBMap<K, V>
impl<K: PartialOrd, V> Extend<(K, V)> for RBMap<K, V>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)