pub struct RBTreeMap<K, V, A = Global>{ /* private fields */ }
Implementations§
Source§impl<K, V, A> RBTreeMap<K, V, A>
impl<K, V, A> RBTreeMap<K, V, A>
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all elements.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
map.clear();
assert!(map.is_empty());
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true
if the map contains no elements.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
assert!(map.is_empty());
map.insert(1, "a");
assert!(!map.is_empty());
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the map.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
assert_eq!(map.len(), 0);
map.insert(1, "a");
assert_eq!(map.len(), 1);
Sourcepub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V, A>>
pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V, A>>
Returns the first entry in the map for in-place manipulation. The key of this entry is the minimum key in the map.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
map.insert(2, "b");
if let Some(mut entry) = map.first_entry() {
if *entry.key() > 0 {
entry.insert("first");
}
}
assert_eq!(*map.get(&1).unwrap(), "first");
assert_eq!(*map.get(&2).unwrap(), "b");
Sourcepub fn first_key_value(&self) -> Option<(&K, &V)>
pub fn first_key_value(&self) -> Option<(&K, &V)>
Returns the first key-value pair in the map. The key in this pair is the minimum key in the map.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
assert_eq!(map.first_key_value(), None);
map.insert(1, "b");
map.insert(2, "a");
assert_eq!(map.first_key_value(), Some((&1, &"b")));
Sourcepub fn last_key_value(&self) -> Option<(&K, &V)>
pub fn last_key_value(&self) -> Option<(&K, &V)>
Returns the last key-value pair in the map. The key in this pair is the maximum key in the map.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "b");
map.insert(2, "a");
assert_eq!(map.last_key_value(), Some((&2, &"a")));
Sourcepub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V, A>>
pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V, A>>
Returns the last entry in the map for in-place manipulation. The key of this entry is the maximum key in the map.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
map.insert(2, "b");
if let Some(mut entry) = map.last_entry() {
if *entry.key() > 0 {
entry.insert("last");
}
}
assert_eq!(*map.get(&1).unwrap(), "a");
assert_eq!(*map.get(&2).unwrap(), "last");
Sourcepub fn pop_first(&mut self) -> Option<(K, V)>
pub fn pop_first(&mut self) -> Option<(K, V)>
Removes and returns the first element in the map. The key of this element is the minimum key that was in the map.
§Examples
Draining elements in ascending order, while keeping a usable map each iteration.
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
map.insert(2, "b");
while let Some((key, _val)) = map.pop_first() {
assert!(map.iter().all(|(k, _v)| *k > key));
}
assert!(map.is_empty());
Sourcepub fn pop_last(&mut self) -> Option<(K, V)>
pub fn pop_last(&mut self) -> Option<(K, V)>
Removes and returns the last element in the map. The key of this element is the maximum key that was in the map.
§Examples
Draining elements in descending order, while keeping a usable map each iteration.
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
map.insert(2, "b");
while let Some((key, _val)) = map.pop_last() {
assert!(map.iter().all(|(k, _v)| *k < key));
}
assert!(map.is_empty());
Sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
Gets an iterator over the entries of the map, sorted by key.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(3, "c");
map.insert(2, "b");
map.insert(1, "a");
for (key, value) in map.iter() {
println!("{key}: {value}");
}
let (first_key, first_value) = map.iter().next().unwrap();
assert_eq!((*first_key, *first_value), (1, "a"));
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
Gets a mutable iterator over the entries of the map, sorted by key.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::from([
("a", 1),
("b", 2),
("c", 3),
]);
// add 10 to the value if the key isn't "a"
for (key, value) in map.iter_mut() {
if key != &"a" {
*value += 10;
}
}
Source§impl<K, V, A> RBTreeMap<K, V, A>
impl<K, V, A> RBTreeMap<K, V, A>
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
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. See the module-level
documentation for more.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);
map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, A>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, A>
Gets the given key’s corresponding entry in the map for in-place manipulation.
§Examples
use xsl::collections::RBTreeMap;
let mut count: RBTreeMap<&str, usize> = RBTreeMap::new();
// count the number of occurrences of letters in the vec
for x in ["a", "b", "a", "c", "a", "b"] {
count.entry(x).and_modify(|curr| *curr += 1).or_insert(1);
}
assert_eq!(count["a"], 3);
assert_eq!(count["b"], 2);
assert_eq!(count["c"], 1);
Source§impl<K, V, A> RBTreeMap<K, V, A>
impl<K, V, A> RBTreeMap<K, V, A>
Sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
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.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);
Sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
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 xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
*x = "b";
}
assert_eq!(map[&1], "b");
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
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.
§Examples
use xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
Sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Returns the key-value pair corresponding to the supplied key.
The supplied 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 xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);
Sourcepub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
Removes a key from the map, returning the stored key and value 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 xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove_entry(&1), None);
Sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
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 xsl::collections::RBTreeMap;
let mut map = RBTreeMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);
Sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
Gets an iterator over the values of the map, in order by key.
§Examples
use xsl::collections::RBTreeMap;
let mut a = RBTreeMap::new();
a.insert(1, "hello");
a.insert(2, "goodbye");
let values: Vec<&str> = a.values().cloned().collect();
assert_eq!(values, ["hello", "goodbye"]);
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
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 xsl::collections::RBTreeMap;
let mut a = RBTreeMap::new();
a.insert(1, String::from("hello"));
a.insert(2, String::from("goodbye"));
for value in a.values_mut() {
value.push_str("!");
}
let values: Vec<String> = a.values().cloned().collect();
assert_eq!(values, [String::from("hello!"),
String::from("goodbye!")]);
Source§impl<K, V, A> RBTreeMap<K, V, A>
impl<K, V, A> RBTreeMap<K, V, A>
pub fn bulk_build_from_sorted_iter<I>(iter: I, alloc: A) -> Self
Trait Implementations§
Source§impl<'a, K: Ord + Copy, V: Copy, A: Allocator + Clone> Extend<(&'a K, &'a V)> for RBTreeMap<K, V, A>
impl<'a, K: Ord + Copy, V: Copy, A: Allocator + Clone> Extend<(&'a K, &'a V)> for RBTreeMap<K, V, A>
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: Ord, V, A: Allocator + Clone> Extend<(K, V)> for RBTreeMap<K, V, A>
impl<K: Ord, V, A: Allocator + Clone> Extend<(K, V)> for RBTreeMap<K, V, A>
Source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
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, V, const N: usize> From<[(K, V); N]> for RBTreeMap<K, V>where
K: Ord,
impl<K, V, const N: usize> From<[(K, V); N]> for RBTreeMap<K, V>where
K: Ord,
Source§fn from(arr: [(K, V); N]) -> Self
fn from(arr: [(K, V); N]) -> Self
Converts a [(K, V); N]
into a BTreeMap<(K, V)>
.
use xsl::collections::RBTreeMap;
let map1 = RBTreeMap::from([(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]);
let map2: RBTreeMap<_, _> = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)].into();
print!("{:?}", map1);
assert_eq!(map1, map2);