Struct splay_tree::SplayMap [] [src]

pub struct SplayMap<K, V> {
    // some fields omitted
}

A map based on a splay tree.

A splay tree based map is a self-adjusting data structure. It performs insertion, removal and look-up in O(log n) amortized time.

It is a logic error for a key to be modified in such a way that the key's ordering relative to any other key, as determined by the Ord trait, changes while it is in the map. This is normally only possible through Cell, RefCell, global state, I/O, or unsafe code.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();

map.insert("foo", 1);
map.insert("bar", 2);
map.insert("baz", 3);

assert_eq!(map.get("foo"), Some(&1));
assert_eq!(map.remove("foo"), Some(1));
assert_eq!(map.get("foo"), None);

for (k, v) in &map {
    println!("{}: {}", k, v);
}

SplayMap implements an Entry API which allows for more complex methods of getting, setting, updating and removing keys and their values: ``` use splay_tree::SplayMap; use rand;

let mut count = SplayMap::new(); for _ in 0..1000 { let k = rand::random::(); *count.entry(k).or_insert(0) += 1; } for k in 0..0x100 { prinln!("{}: {}", counter.get(&k).unwrap_or(0)); } ```

Methods

impl<K, V> SplayMap<K, V> where K: Ord
[src]

fn new() -> Self

Makes a new empty SplayMap.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert("foo", 1);
assert_eq!(map.len(), 1);

fn clear(&mut self)

Clears the map, removing all values.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert("foo", 1);
map.clear();
assert!(map.is_empty());

fn contains_key<Q: ?Sized>(&mut self, key: &Q) -> bool where K: Borrow<Q>, Q: Ord

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Notice

Because SplayMap is a self-adjusting amortized data structure, this function requires the mut qualifier for self.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert("foo", 1);
assert!(map.contains_key("foo"));
assert!(!map.contains_key("bar"));

fn get<Q: ?Sized>(&mut self, key: &Q) -> Option<&V> where K: Borrow<Q>, Q: Ord

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Notice

Because SplayMap is a self-adjusting amortized data structure, this function requires the mut qualifier for self.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert("foo", 1);
assert_eq!(map.get("foo"), Some(&1));
assert_eq!(map.get("bar"), None);

fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V> where K: Borrow<Q>, Q: Ord

Returns a mutable reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert("foo", 1);
map.get_mut("foo").map(|v| *v = 2);
assert_eq!(map.get("foo"), Some(&2));

fn find_lower_bound_key<Q: ?Sized>(&mut self, key: &Q) -> Option<&K> where K: Borrow<Q>, Q: Ord

Finds a minimum key which satisfies "greater than or equal to key" condition in the map.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert(1, ());
map.insert(3, ());

assert_eq!(map.find_lower_bound_key(&0), Some(&1));
assert_eq!(map.find_lower_bound_key(&1), Some(&1));
assert_eq!(map.find_lower_bound_key(&4), None);

fn find_upper_bound_key<Q: ?Sized>(&mut self, key: &Q) -> Option<&K> where K: Borrow<Q>, Q: Ord

Finds a minimum key which satisfies "greater than key" condition in the map.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert(1, ());
map.insert(3, ());

assert_eq!(map.find_upper_bound_key(&0), Some(&1));
assert_eq!(map.find_upper_bound_key(&1), Some(&3));
assert_eq!(map.find_upper_bound_key(&4), None);

fn insert(&mut self, key: K, value: V) -> Option<V>

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
assert_eq!(map.insert("foo", 1), None);
assert_eq!(map.get("foo"), Some(&1));
assert_eq!(map.insert("foo", 2), Some(1));
assert_eq!(map.get("foo"), Some(&2));

fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V> where K: Borrow<Q>, Q: Ord

Removes a key from the map, returning the value at the key if the key was previously in the map.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert("foo", 1);
assert_eq!(map.remove("foo"), Some(1));
assert_eq!(map.remove("foo"), None);

fn entry(&mut self, key: K) -> Entry<K, V>

Gets the given key's corresponding entry in the map for in-place manipulation.

Examples

use splay_tree::SplayMap;

let mut count = SplayMap::new();

// count the number of occurrences of letters in the vec
for x in vec!["a", "b", "a", "c", "a", "b"] {
    *count.entry(x).or_insert(0) += 1;
}

assert_eq!(count.get("a"), Some(&3));

impl<K, V> SplayMap<K, V>
[src]

fn len(&self) -> usize

Returns the number of elements in the map.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
map.insert("foo", 1);
map.insert("bar", 2);
assert_eq!(map.len(), 2);

fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Examples

use splay_tree::SplayMap;

let mut map = SplayMap::new();
assert!(map.is_empty());

map.insert("foo", 1);
assert!(!map.is_empty());

map.clear();
assert!(map.is_empty());

fn iter(&self) -> Iter<K, V>

Gets an iterator over the entries of the map, sorted by key.

Examples

use splay_tree::SplayMap;

let map: SplayMap<_, _> =
    vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect();
assert_eq!(vec![(&"bar", &2), (&"baz", &3), (&"foo", &1)],
           map.iter().collect::<Vec<_>>());

fn iter_mut(&mut self) -> IterMut<K, V>

Gets a mutable iterator over the entries of the map, soretd by key.

Examples

use splay_tree::SplayMap;

let mut map: SplayMap<_, _> =
    vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect();
for (_, v) in map.iter_mut() {
   *v += 10;
}
assert_eq!(map.get("bar"), Some(&12));

fn keys(&self) -> Keys<K, V>

Gets an iterator over the keys of the map, in sorted order.

Examples

use splay_tree::SplayMap;

let map: SplayMap<_, _> =
    vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect();
assert_eq!(vec!["bar", "baz", "foo"],
           map.keys().cloned().collect::<Vec<_>>());

fn values(&self) -> Values<K, V>

Gets an iterator over the values of the map, in order by key.

Examples

use splay_tree::SplayMap;

let map: SplayMap<_, _> =
    vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect();
assert_eq!(vec![2, 3, 1],
           map.values().cloned().collect::<Vec<_>>());

fn values_mut(&mut self) -> ValuesMut<K, V>

Gets a mutable iterator over the values of the map, in order by key.

Examples

use splay_tree::SplayMap;

let mut map: SplayMap<_, _> =
    vec![("foo", 1), ("bar", 2), ("baz", 3)].into_iter().collect();
for v in map.values_mut() {
    *v += 10;
}
assert_eq!(vec![12, 13, 11],
           map.values().cloned().collect::<Vec<_>>());

Trait Implementations

impl<K: Ord, V: Ord> Ord for SplayMap<K, V>
[src]

fn cmp(&self, __arg_0: &SplayMap<K, V>) -> Ordering

This method returns an Ordering between self and other. Read more

impl<K: PartialOrd, V: PartialOrd> PartialOrd for SplayMap<K, V>
[src]

fn partial_cmp(&self, __arg_0: &SplayMap<K, V>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more

fn lt(&self, __arg_0: &SplayMap<K, V>) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more

fn le(&self, __arg_0: &SplayMap<K, V>) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

fn gt(&self, __arg_0: &SplayMap<K, V>) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more

fn ge(&self, __arg_0: &SplayMap<K, V>) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<K: Eq, V: Eq> Eq for SplayMap<K, V>
[src]

impl<K: PartialEq, V: PartialEq> PartialEq for SplayMap<K, V>
[src]

fn eq(&self, __arg_0: &SplayMap<K, V>) -> bool

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

fn ne(&self, __arg_0: &SplayMap<K, V>) -> bool

This method tests for !=.

impl<K: Hash, V: Hash> Hash for SplayMap<K, V>
[src]

fn hash<__HKV: Hasher>(&self, __arg_0: &mut __HKV)

Feeds this value into the state given, updating the hasher as necessary.

fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0

Feeds a slice of this type into the state provided.

impl<K: Clone, V: Clone> Clone for SplayMap<K, V>
[src]

fn clone(&self) -> SplayMap<K, V>

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: Debug, V: Debug> Debug for SplayMap<K, V>
[src]

fn fmt(&self, __arg_0: &mut Formatter) -> Result

Formats the value using the given formatter.

impl<K, V> Default for SplayMap<K, V> where K: Ord
[src]

fn default() -> Self

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

impl<K, V> FromIterator<(K, V)> for SplayMap<K, V> where K: Ord
[src]

fn from_iter<I>(iter: I) -> Self where I: IntoIterator<Item=(K, V)>

Creates a value from an iterator. Read more

impl<'a, K, V> IntoIterator for &'a SplayMap<K, V> where K: 'a, V: 'a
[src]

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) -> Self::IntoIter

Creates an iterator from a value. Read more

impl<'a, K, V> IntoIterator for &'a mut SplayMap<K, V> where K: 'a, V: 'a
[src]

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) -> Self::IntoIter

Creates an iterator from a value. Read more

impl<K, V> IntoIterator for SplayMap<K, V>
[src]

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) -> Self::IntoIter

Creates an iterator from a value. Read more

impl<K, V> Extend<(K, V)> for SplayMap<K, V> where K: Ord
[src]

fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item=(K, V)>

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

impl<'a, K, V> Extend<(&'a K, &'a V)> for SplayMap<K, V> where K: 'a + Copy + Ord, V: 'a + Copy
[src]

fn extend<T>(&mut self, iter: T) where T: IntoIterator<Item=(&'a K, &'a V)>

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