Struct tree::map::Map
[−]
[src]
pub struct Map<K, V, C = Natural<K>> where
C: Compare<K>, { /* fields omitted */ }
An ordered map based on a binary search tree.
The behavior of this map is undefined if a key's ordering relative to any other key changes
while the key is in the map. This is normally only possible through Cell
, RefCell
, or
unsafe code.
Methods
impl<K, V> Map<K, V> where
K: Ord,
[src]
K: Ord,
fn new() -> Self
Creates an empty map ordered according to the natural order of its keys.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); let mut it = map.iter(); assert_eq!(it.next(), Some((&1, &"a"))); assert_eq!(it.next(), Some((&2, &"b"))); assert_eq!(it.next(), Some((&3, &"c"))); assert_eq!(it.next(), None);
impl<K, V, C> Map<K, V, C> where
C: Compare<K>,
[src]
C: Compare<K>,
fn with_cmp(cmp: C) -> Self
Creates an empty map ordered according to the given comparator.
Examples
use compare::{Compare, natural}; let mut map = tree::Map::with_cmp(natural().rev()); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); let mut it = map.iter(); assert_eq!(it.next(), Some((&3, &"c"))); assert_eq!(it.next(), Some((&2, &"b"))); assert_eq!(it.next(), Some((&1, &"a"))); assert_eq!(it.next(), None);
fn is_empty(&self) -> bool
Checks if the map is empty.
Examples
let mut map = tree::Map::new(); assert!(map.is_empty()); map.insert(2, "b"); assert!(!map.is_empty());
fn len(&self) -> usize
Returns the number of entries in the map.
Examples
let mut map = tree::Map::new(); assert_eq!(map.len(), 0); map.insert(2, "b"); assert_eq!(map.len(), 1);
fn cmp(&self) -> &C
Returns a reference to the map's comparator.
Examples
use compare::{Compare, natural}; let map: tree::Map<i32, &str> = tree::Map::new(); assert!(map.cmp().compares_lt(&1, &2)); let map: tree::Map<i32, &str, _> = tree::Map::with_cmp(natural().rev()); assert!(map.cmp().compares_gt(&1, &2));
fn clear(&mut self)
Removes all entries from the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.len(), 3); assert_eq!(map.iter().next(), Some((&1, &"a"))); map.clear(); assert_eq!(map.len(), 0); assert_eq!(map.iter().next(), None);
fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts an entry into the map, returning the previous value, if any, associated with the key.
Examples
let mut map = tree::Map::new(); assert_eq!(map.insert(1, "a"), None); assert_eq!(map.get(&1), Some(&"a")); assert_eq!(map.insert(1, "b"), Some("a")); assert_eq!(map.get(&1), Some(&"b"));
fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)> where
C: Compare<Q, K>,
C: Compare<Q, K>,
Removes and returns the entry whose key is equal to the given key, returning
None
if the map does not contain the key.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.len(), 3); assert_eq!(map.get(&1), Some(&"a")); assert_eq!(map.remove(&1), Some((1, "a"))); assert_eq!(map.len(), 2); assert_eq!(map.get(&1), None); assert_eq!(map.remove(&1), None);
fn entry(&mut self, key: K) -> Entry<K, V>
Returns the map's entry corresponding to the given key.
Examples
let mut counts = tree::Map::new(); for s in vec!["a", "b", "a", "c", "a", "b"] { *counts.entry(s).or_insert(0) += 1; } assert_eq!(counts[&"a"], 3); assert_eq!(counts[&"b"], 2); assert_eq!(counts[&"c"], 1);
fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where
C: Compare<Q, K>,
C: Compare<Q, K>,
Checks if the map contains the given key.
Examples
let mut map = tree::Map::new(); assert!(!map.contains_key(&1)); map.insert(1, "a"); assert!(map.contains_key(&1));
fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where
C: Compare<Q, K>,
C: Compare<Q, K>,
Returns a reference to the value associated with the given key, or None
if the
map does not contain the key.
Examples
let mut map = tree::Map::new(); assert_eq!(map.get(&1), None); map.insert(1, "a"); assert_eq!(map.get(&1), Some(&"a"));
fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where
C: Compare<Q, K>,
C: Compare<Q, K>,
Returns a mutable reference to the value associated with the given key, or None
if the map does not contain the key.
Examples
let mut map = tree::Map::new(); assert_eq!(map.get(&1), None); map.insert(1, "a"); { let value = map.get_mut(&1).unwrap(); assert_eq!(*value, "a"); *value = "b"; } assert_eq!(map.get(&1), Some(&"b"));
fn max(&self) -> Option<(&K, &V)>
Returns a reference to the map's maximum key and a reference to its associated
value, or None
if the map is empty.
Examples
let mut map = tree::Map::new(); assert_eq!(map.max(), None); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.max(), Some((&3, &"c")));
fn max_mut(&mut self) -> Option<(&K, &mut V)>
Returns a reference to the map's maximum key and a mutable reference to its
associated value, or None
if the map is empty.
Examples
let mut map = tree::Map::new(); assert_eq!(map.max(), None); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); { let max = map.max_mut().unwrap(); assert_eq!(max, (&3, &mut "c")); *max.1 = "cc"; } assert_eq!(map.max(), Some((&3, &"cc")));
fn remove_max(&mut self) -> Option<(K, V)>
Removes the map's maximum key and returns it and its associated value, or None
if the map
is empty.
Examples
let mut map = tree::Map::new(); assert_eq!(map.remove_max(), None); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.remove_max(), Some((3, "c")));
fn max_entry(&mut self) -> Option<OccupiedEntry<K, V>>
Returns the map's entry corresponding to its maximum key.
Examples
let mut map = tree::Map::new(); assert!(map.max_entry().is_none()); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); { let mut e = map.max_entry().unwrap(); assert_eq!(*e.key(), 3); assert_eq!(e.insert("cc"), "c"); } assert_eq!(map[&3], "cc");
fn min(&self) -> Option<(&K, &V)>
Returns a reference to the map's minimum key and a reference to its associated
value, or None
if the map is empty.
Examples
let mut map = tree::Map::new(); assert_eq!(map.min(), None); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.min(), Some((&1, &"a")));
fn min_mut(&mut self) -> Option<(&K, &mut V)>
Returns a reference to the map's minimum key and a mutable reference to its
associated value, or None
if the map is empty.
Examples
let mut map = tree::Map::new(); assert_eq!(map.min(), None); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); { let min = map.min_mut().unwrap(); assert_eq!(min, (&1, &mut "a")); *min.1 = "aa"; } assert_eq!(map.min(), Some((&1, &"aa")));
fn remove_min(&mut self) -> Option<(K, V)>
Removes the map's minimum key and returns it and its associated value, or None
if the map
is empty.
Examples
let mut map = tree::Map::new(); assert_eq!(map.remove_min(), None); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.remove_min(), Some((1, "a")));
fn min_entry(&mut self) -> Option<OccupiedEntry<K, V>>
Returns the map's entry corresponding to its minimum key.
Examples
let mut map = tree::Map::new(); assert!(map.min_entry().is_none()); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); { let mut e = map.min_entry().unwrap(); assert_eq!(*e.key(), 1); assert_eq!(e.insert("aa"), "a"); } assert_eq!(map[&1], "aa");
fn pred<Q: ?Sized>(&self, key: &Q, inclusive: bool) -> Option<(&K, &V)> where
C: Compare<Q, K>,
C: Compare<Q, K>,
Returns a reference to the predecessor of the given key and a
reference to its associated value, or None
if no such key is present in the map.
If inclusive
is false
, this method finds the greatest key that is strictly less than
the given key. If inclusive
is true
, this method finds the greatest key that is less
than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.pred(&0, false), None); assert_eq!(map.pred(&1, false), None); assert_eq!(map.pred(&2, false), Some((&1, &"a"))); assert_eq!(map.pred(&3, false), Some((&2, &"b"))); assert_eq!(map.pred(&4, false), Some((&3, &"c"))); assert_eq!(map.pred(&0, true), None); assert_eq!(map.pred(&1, true), Some((&1, &"a"))); assert_eq!(map.pred(&2, true), Some((&2, &"b"))); assert_eq!(map.pred(&3, true), Some((&3, &"c"))); assert_eq!(map.pred(&4, true), Some((&3, &"c")));
fn pred_mut<Q: ?Sized>(
&mut self,
key: &Q,
inclusive: bool
) -> Option<(&K, &mut V)> where
C: Compare<Q, K>,
&mut self,
key: &Q,
inclusive: bool
) -> Option<(&K, &mut V)> where
C: Compare<Q, K>,
Returns a reference to the predecessor of the given key and a
mutable reference to its associated value, or None
if no such key is present in the map.
If inclusive
is false
, this method finds the greatest key that is strictly less than
the given key. If inclusive
is true
, this method finds the greatest key that is less
than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); { let pred = map.pred_mut(&2, false).unwrap(); assert_eq!(pred, (&1, &mut "a")); *pred.1 = "aa"; } assert_eq!(map.pred(&2, false), Some((&1, &"aa"))); { let pred_or_eq = map.pred_mut(&1, true).unwrap(); assert_eq!(pred_or_eq, (&1, &mut "aa")); *pred_or_eq.1 = "aaa"; } { let pred_or_eq = map.pred_mut(&4, true).unwrap(); assert_eq!(pred_or_eq, (&3, &mut "c")); *pred_or_eq.1 = "cc"; } assert_eq!(map.pred(&1, true), Some((&1, &"aaa"))); assert_eq!(map.pred(&4, true), Some((&3, &"cc")));
fn remove_pred<Q: ?Sized>(&mut self, key: &Q, inclusive: bool) -> Option<(K, V)> where
C: Compare<Q, K>,
C: Compare<Q, K>,
Removes the predecessor of the given key from the map and returns it and its associated
value, or None
if no such key is present in the map.
If inclusive
is false
, this method removes the greatest key that is strictly less than
the given key. If inclusive
is true
, this method removes the greatest key that is less
than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.remove_pred(&1, false), None); assert!(map.contains_key(&1)); assert_eq!(map.remove_pred(&2, false), Some((1, "a"))); assert!(!map.contains_key(&1)); assert_eq!(map.remove_pred(&2, true), Some((2, "b"))); assert!(!map.contains_key(&2));
fn pred_entry<Q: ?Sized>(
&mut self,
key: &Q,
inclusive: bool
) -> Option<OccupiedEntry<K, V>> where
C: Compare<Q, K>,
&mut self,
key: &Q,
inclusive: bool
) -> Option<OccupiedEntry<K, V>> where
C: Compare<Q, K>,
Returns the entry corresponding to the predecessor of the given key.
If inclusive
is false
, this method returns the entry corresponding to the greatest key
that is strictly less than the given key. If inclusive
is true
, this method returns
the entry corresponding to the greatest key that is less than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert!(map.pred_entry(&1, false).is_none()); { let mut e = map.pred_entry(&4, true).unwrap(); assert_eq!(*e.key(), 3); assert_eq!(e.insert("cc"), "c"); } assert_eq!(map[&3], "cc"); { let e = map.pred_entry(&3, false).unwrap(); assert_eq!(e.remove(), (2, "b")); } assert!(!map.contains_key(&2));
fn succ<Q: ?Sized>(&self, key: &Q, inclusive: bool) -> Option<(&K, &V)> where
C: Compare<Q, K>,
C: Compare<Q, K>,
Returns a reference to the successor of the given key and a
reference to its associated value, or None
if no such key is present in the map.
If inclusive
is false
, this method finds the smallest key that is strictly greater than
the given key. If inclusive
is true
, this method finds the smallest key that is greater
than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.succ(&0, false), Some((&1, &"a"))); assert_eq!(map.succ(&1, false), Some((&2, &"b"))); assert_eq!(map.succ(&2, false), Some((&3, &"c"))); assert_eq!(map.succ(&3, false), None); assert_eq!(map.succ(&4, false), None); assert_eq!(map.succ(&0, true), Some((&1, &"a"))); assert_eq!(map.succ(&1, true), Some((&1, &"a"))); assert_eq!(map.succ(&2, true), Some((&2, &"b"))); assert_eq!(map.succ(&3, true), Some((&3, &"c"))); assert_eq!(map.succ(&4, true), None);
fn succ_mut<Q: ?Sized>(
&mut self,
key: &Q,
inclusive: bool
) -> Option<(&K, &mut V)> where
C: Compare<Q, K>,
&mut self,
key: &Q,
inclusive: bool
) -> Option<(&K, &mut V)> where
C: Compare<Q, K>,
Returns a reference to the successor of the given key and a
mutable reference to its associated value, or None
if no such key is present in the map.
If inclusive
is false
, this method finds the smallest key that is strictly greater than
the given key. If inclusive
is true
, this method finds the smallest key that is greater
than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); { let succ = map.succ_mut(&2, false).unwrap(); assert_eq!(succ, (&3, &mut "c")); *succ.1 = "cc"; } assert_eq!(map.succ(&2, false), Some((&3, &"cc"))); { let succ_or_eq = map.succ_mut(&0, true).unwrap(); assert_eq!(succ_or_eq, (&1, &mut "a")); *succ_or_eq.1 = "aa"; } { let succ_or_eq = map.succ_mut(&3, true).unwrap(); assert_eq!(succ_or_eq, (&3, &mut "cc")); *succ_or_eq.1 = "ccc"; } assert_eq!(map.succ(&0, true), Some((&1, &"aa"))); assert_eq!(map.succ(&3, true), Some((&3, &"ccc")));
fn remove_succ<Q: ?Sized>(&mut self, key: &Q, inclusive: bool) -> Option<(K, V)> where
C: Compare<Q, K>,
C: Compare<Q, K>,
Removes the successor of the given key from the map and returns it and its associated
value, or None
if no such key is present in the map.
If inclusive
is false
, this method removes the smallest key that is strictly greater
than the given key. If inclusive
is true
, this method removes the smallest key that is
greater than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert_eq!(map.remove_succ(&3, false), None); assert!(map.contains_key(&3)); assert_eq!(map.remove_succ(&2, false), Some((3, "c"))); assert!(!map.contains_key(&3)); assert_eq!(map.remove_succ(&2, true), Some((2, "b"))); assert!(!map.contains_key(&2));
fn succ_entry<Q: ?Sized>(
&mut self,
key: &Q,
inclusive: bool
) -> Option<OccupiedEntry<K, V>> where
C: Compare<Q, K>,
&mut self,
key: &Q,
inclusive: bool
) -> Option<OccupiedEntry<K, V>> where
C: Compare<Q, K>,
Returns the entry corresponding to the successor of the given key.
If inclusive
is false
, this method returns the entry corresponding to the smallest key
that is strictly greater than the given key. If inclusive
is true
, this method returns
the entry corresponding to the smallest key that is greater than or equal to the given key.
The given key need not itself be present in the map.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); assert!(map.succ_entry(&3, false).is_none()); { let mut e = map.succ_entry(&0, true).unwrap(); assert_eq!(*e.key(), 1); assert_eq!(e.insert("aa"), "a"); } assert_eq!(map[&1], "aa"); { let e = map.succ_entry(&1, false).unwrap(); assert_eq!(e.remove(), (2, "b")); } assert!(!map.contains_key(&2));
fn iter(&self) -> Iter<K, V>
Returns an iterator over the map's entries with immutable references to the values.
The iterator yields the entries in ascending order according to the map's comparator.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); let mut it = map.iter(); assert_eq!(it.next(), Some((&1, &"a"))); assert_eq!(it.next(), Some((&2, &"b"))); assert_eq!(it.next(), Some((&3, &"c"))); assert_eq!(it.next(), None);
fn iter_mut(&mut self) -> IterMut<K, V>
Returns an iterator over the map's entries with mutable references to the values.
The iterator yields the entries in ascending order according to the map's comparator.
Examples
let mut map = tree::Map::new(); map.insert("b", 2); map.insert("a", 1); map.insert("c", 3); let mut i = 1; for (_, value) in map.iter_mut() { assert_eq!(i, *value); *value *= 2; i += 1; } assert_eq!(map[&"a"], 2); assert_eq!(map[&"b"], 4); assert_eq!(map[&"c"], 6);
Trait Implementations
impl<K: Clone, V: Clone, C: Clone> Clone for Map<K, V, C> where
C: Compare<K>,
[src]
C: Compare<K>,
fn clone(&self) -> Map<K, V, C>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<K, V, C> Debug for Map<K, V, C> where
K: Debug,
V: Debug,
C: Compare<K>,
[src]
K: Debug,
V: Debug,
C: Compare<K>,
impl<K, V, C> Default for Map<K, V, C> where
C: Compare<K> + Default,
[src]
C: Compare<K> + Default,
impl<K, V, C> Extend<(K, V)> for Map<K, V, C> where
C: Compare<K>,
[src]
C: Compare<K>,
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, it: I)
Extends a collection with the contents of an iterator. Read more
impl<K, V, C> FromIterator<(K, V)> for Map<K, V, C> where
C: Compare<K> + Default,
[src]
C: Compare<K> + Default,
fn from_iter<I: IntoIterator<Item = (K, V)>>(it: I) -> Self
Creates a value from an iterator. Read more
impl<K, V, C> Hash for Map<K, V, C> where
K: Hash,
V: Hash,
C: Compare<K>,
[src]
K: Hash,
V: Hash,
C: Compare<K>,
fn hash<H: Hasher>(&self, h: &mut H)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<'a, K, V, C, Q: ?Sized> Index<&'a Q> for Map<K, V, C> where
C: Compare<K> + Compare<Q, K>,
[src]
C: Compare<K> + Compare<Q, K>,
type Output = V
The returned type after indexing
fn index(&self, key: &Q) -> &V
The method for the indexing (container[index]
) operation
impl<'a, K, V, C> IntoIterator for &'a Map<K, V, C> where
C: Compare<K>,
[src]
C: Compare<K>,
type Item = (&'a K, &'a V)
The type of the elements being iterated over.
type IntoIter = Iter<'a, K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Iter<'a, K, V>
Creates an iterator from a value. Read more
impl<'a, K, V, C> IntoIterator for &'a mut Map<K, V, C> where
C: Compare<K>,
[src]
C: Compare<K>,
type Item = (&'a K, &'a mut V)
The type of the elements being iterated over.
type IntoIter = IterMut<'a, K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> IterMut<'a, K, V>
Creates an iterator from a value. Read more
impl<K, V, C> IntoIterator for Map<K, V, C> where
C: Compare<K>,
[src]
C: Compare<K>,
type Item = (K, V)
The type of the elements being iterated over.
type IntoIter = IntoIter<K, V>
Which kind of iterator are we turning this into?
fn into_iter(self) -> IntoIter<K, V>
Returns an iterator that consumes the map.
The iterator yields the entries in ascending order according to the map's comparator.
Examples
let mut map = tree::Map::new(); map.insert(2, "b"); map.insert(1, "a"); map.insert(3, "c"); let mut it = map.into_iter(); assert_eq!(it.next(), Some((1, "a"))); assert_eq!(it.next(), Some((2, "b"))); assert_eq!(it.next(), Some((3, "c"))); assert_eq!(it.next(), None);
impl<K, V, C> PartialEq for Map<K, V, C> where
V: PartialEq,
C: Compare<K>,
[src]
V: PartialEq,
C: Compare<K>,
fn eq(&self, other: &Self) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<K, V, C> Eq for Map<K, V, C> where
V: Eq,
C: Compare<K>,
[src]
V: Eq,
C: Compare<K>,
impl<K, V, C> PartialOrd for Map<K, V, C> where
V: PartialOrd,
C: Compare<K>,
[src]
V: PartialOrd,
C: Compare<K>,
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<K, V, C> Ord for Map<K, V, C> where
V: Ord,
C: Compare<K>,
[src]
V: Ord,
C: Compare<K>,
fn cmp(&self, other: &Self) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl<K, V, C> Arbitrary for Map<K, V, C> where
K: Arbitrary,
V: Arbitrary,
C: 'static + Clone + Compare<K> + Default + Send,
[src]
K: Arbitrary,
V: Arbitrary,
C: 'static + Clone + Compare<K> + Default + Send,