Struct RBTreeMap

Source
pub struct RBTreeMap<K, V, A = Global>
where A: Allocator + Clone,
{ /* private fields */ }

Implementations§

Source§

impl<K, V, A> RBTreeMap<K, V, A>
where K: Ord, A: Allocator + Clone,

Source

pub fn check(&self)

Source§

impl<K, V, A> RBTreeMap<K, V, A>
where A: Allocator + Clone,

Source

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

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

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

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

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

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

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

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

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

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

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>
where K: Ord, A: Allocator + Clone,

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

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>
where A: Allocator + Clone,

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: ?Sized + 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.

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

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: ?Sized + 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 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");
Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: ?Sized + 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.

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

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

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

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

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

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: ?Sized + 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 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);
Source

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"]);
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 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> RBTreeMap<K, V>

Source

pub fn new() -> Self

Source§

impl<K, V, A> RBTreeMap<K, V, A>
where A: Allocator + Clone,

Source

pub fn bulk_build_from_sorted_iter<I>(iter: I, alloc: A) -> Self
where I: IntoIterator<Item = (K, V)>, K: Ord, A: Allocator + Clone,

Source§

impl<K, V, A> RBTreeMap<K, V, A>
where A: Allocator + Clone,

Source

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

Source

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

Trait Implementations§

Source§

impl<K, V, A> Clone for RBTreeMap<K, V, A>
where K: Clone, V: Clone, A: Allocator + Clone,

Source§

fn clone(&self) -> Self

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

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

Performs copy-assignment from source. Read more
Source§

impl<K, V, A> Debug for RBTreeMap<K, V, A>
where K: Debug, V: Debug, A: Allocator + Clone,

Source§

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

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

impl<K, V> Display for RBTreeMap<K, V>
where K: Display, V: Display,

Source§

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

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

impl<K, V, A> Drop for RBTreeMap<K, V, A>
where A: Allocator + Clone,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

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)

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

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, const N: usize> From<[(K, V); N]> for RBTreeMap<K, V>
where K: Ord,

Source§

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

impl<K, V, Q> Index<&Q> for RBTreeMap<K, V>
where K: Borrow<Q> + Ord, Q: ?Sized + Ord,

Source§

fn index(&self, index: &Q) -> &Self::Output

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

§Panics

Panics if the key is not present in the BTreeMap.

Source§

type Output = V

The returned type after indexing.
Source§

impl<'a, K, V, A> IntoIterator for &'a RBTreeMap<K, V, A>
where A: Allocator + Clone,

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) -> Iter<'a, K, V>

Creates an iterator from a value. Read more
Source§

impl<K, V, A> PartialEq for RBTreeMap<K, V, A>
where K: PartialEq, V: PartialEq, A: Allocator + Clone,

Source§

fn eq(&self, other: &RBTreeMap<K, V, A>) -> 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, V, A> PartialOrd for RBTreeMap<K, V, A>
where K: PartialOrd, V: PartialOrd, A: Allocator + Clone,

Source§

fn partial_cmp(&self, other: &RBTreeMap<K, V, A>) -> 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, V, A> Eq for RBTreeMap<K, V, A>
where K: Eq, V: Eq, A: Allocator + Clone,

Auto Trait Implementations§

§

impl<K, V, A> Freeze for RBTreeMap<K, V, A>
where A: Freeze,

§

impl<K, V, A> RefUnwindSafe for RBTreeMap<K, V, A>

§

impl<K, V, A = Global> !Send for RBTreeMap<K, V, A>

§

impl<K, V, A = Global> !Sync for RBTreeMap<K, V, A>

§

impl<K, V, A> Unpin for RBTreeMap<K, V, A>
where A: Unpin,

§

impl<K, V, A> UnwindSafe for RBTreeMap<K, V, A>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.