Struct SplayMap

Source
pub struct SplayMap<K, V> { /* private fields */ }
Expand description

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;

let mut count = SplayMap::new();
for _ in 0..1000 {
    let k = rand::random::<u8>();
    *count.entry(k).or_insert(0) += 1;
}
for k in 0..=255 {
    println!("{}: {}", k, count.get(&k).unwrap_or(&0));
}

Implementations§

Source§

impl<K, V> SplayMap<K, V>
where K: Ord,

Source

pub 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);
Source

pub 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());
Source

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

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"));
Source

pub fn contains_key_immut<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Ord + ?Sized,

Immutable version of SplayMap::contains_key().

Note that this method could be less efficient than the mutable version.

§Examples
use splay_tree::SplayMap;

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

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

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);
Source

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

Immutable version of SplayMap::get().

Note that this method could be less efficient than the mutable version.

§Examples
use splay_tree::SplayMap;

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

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

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));
Source

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

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);
Source

pub fn find_lower_bound_key_immut<Q>(&self, key: &Q) -> Option<&K>
where K: Borrow<Q>, Q: Ord + ?Sized,

Immutable version of SplayMap::find_lower_bound_key().

Note that this method could be less efficient than the mutable version.

§Examples
use splay_tree::SplayMap;

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

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

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

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);
Source

pub fn find_upper_bound_key_immut<Q>(&self, key: &Q) -> Option<&K>
where K: Borrow<Q>, Q: Ord + ?Sized,

Immutable version of SplayMap::find_upper_bound_key().

Note that this method could be less efficient than the mutable version.

§Examples
use splay_tree::SplayMap;

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

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

pub fn smallest(&mut self) -> Option<(&K, &V)>

Gets the entry which have the minimum key in the map.

§Examples
use splay_tree::SplayMap;

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

assert_eq!(map.smallest(), Some((&1, &())));
Source

pub fn smallest_immut(&self) -> Option<(&K, &V)>

Immutable version of SplayMap::smallest().

Note that this method could be less efficient than the mutable version.

§Examples
use splay_tree::SplayMap;

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

assert_eq!(map.smallest_immut(), Some((&1, &())));
Source

pub fn take_smallest(&mut self) -> Option<(K, V)>

Takes the entry which have the minimum key in the map.

§Examples
use splay_tree::SplayMap;

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

assert_eq!(map.take_smallest(), Some((1, ())));
assert_eq!(map.take_smallest(), Some((3, ())));
assert_eq!(map.take_smallest(), None);
Source

pub fn largest(&mut self) -> Option<(&K, &V)>

Gets the entry which have the maximum key in the map.

§Examples
use splay_tree::SplayMap;

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

assert_eq!(map.largest(), Some((&3, &())));
Source

pub fn largest_immut(&self) -> Option<(&K, &V)>

Immutable version of SplayMap::largest().

Note that this method could be less efficient than the mutable version.

§Examples
use splay_tree::SplayMap;

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

assert_eq!(map.largest_immut(), Some((&3, &())));
Source

pub fn take_largest(&mut self) -> Option<(K, V)>

Takes the entry which have the maximum key in the map.

§Examples
use splay_tree::SplayMap;

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

assert_eq!(map.take_largest(), Some((3, ())));
assert_eq!(map.take_largest(), Some((1, ())));
assert_eq!(map.take_largest(), None);
Source

pub 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));
Source

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

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);
Source

pub 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));
Source§

impl<K, V> SplayMap<K, V>

Source

pub 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);
Source

pub 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());
Source

pub 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<_>>());
Source

pub 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));
Source

pub 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<_>>());
Source

pub 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<_>>());
Source

pub 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§

Source§

impl<K: Clone, V: Clone> Clone for SplayMap<K, V>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for SplayMap<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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

Source§

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
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

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

Source§

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

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

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

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

Source§

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

Creates a value from an iterator. Read more
Source§

impl<K: Hash, V: Hash> Hash for SplayMap<K, V>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

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

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

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

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> IntoIterator for SplayMap<K, V>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Ord, V: Ord> Ord for SplayMap<K, V>

Source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<K: PartialEq, V: PartialEq> PartialEq for SplayMap<K, V>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: PartialOrd, V: PartialOrd> PartialOrd for SplayMap<K, V>

Source§

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

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<K: Eq, V: Eq> Eq for SplayMap<K, V>

Source§

impl<K, V> StructuralPartialEq for SplayMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for SplayMap<K, V>

§

impl<K, V> RefUnwindSafe for SplayMap<K, V>

§

impl<K, V> Send for SplayMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for SplayMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for SplayMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for SplayMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.