// Copyright 2017 Serde Developers // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
#[cfg(not(feature = "preserve_order"))] use alloc::collections::{btree_map, BTreeMap}; use core::borrow::Borrow; use core::fmt::{self, Debug}; use core::hash::Hash; use core::iter::FromIterator; use core::ops;
#[cfg(feature = "preserve_order")] use indexmap::{self, IndexMap};
#[cfg(not(feature = "preserve_order"))] type MapImpl<K, V> = BTreeMap<K, V>; #[cfg(all(feature = "preserve_order", not(feature = "fast_hash")))] type RandomState = std::collections::hash_map::RandomState; #[cfg(all(feature = "preserve_order", feature = "fast_hash"))] type RandomState = foldhash::fast::RandomState; #[cfg(feature = "preserve_order")] type MapImpl<K, V> = IndexMap<K, V, RandomState>;
impl<K, V> Map<K, V> where
K: Ord + Hash,
{ /// Makes a new empty Map. #[inline] pubfn new() -> Self { Self { #[cfg(feature = "preserve_order")]
map: MapImpl::with_hasher(RandomState::default()), #[cfg(not(feature = "preserve_order"))]
map: MapImpl::new(),
dotted: false,
implicit: false,
inline: false,
}
}
#[cfg(not(feature = "preserve_order"))] /// Makes a new empty Map with the given initial capacity. #[inline] pubfn with_capacity(capacity: usize) -> Self { // does not support with_capacity let _ = capacity; Self::new()
}
#[cfg(feature = "preserve_order")] /// Makes a new empty Map with the given initial capacity. #[inline] pubfn with_capacity(capacity: usize) -> Self { Self {
map: IndexMap::with_capacity_and_hasher(capacity, RandomState::default()),
dotted: false,
implicit: false,
inline: false,
}
}
/// Clears the map, removing all values. #[inline] pubfn clear(&mutself) { self.map.clear();
}
/// 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. #[inline] pubfn get<Q>(&self, key: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Ord + Eq + Hash + ?Sized,
{ self.map.get(key)
}
/// 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. #[inline] pubfn contains_key<Q>(&self, key: &Q) -> bool where
K: Borrow<Q>,
Q: Ord + Eq + Hash + ?Sized,
{ self.map.contains_key(key)
}
/// 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. #[inline] pubfn get_mut<Q>(&mutself, key: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Ord + Eq + Hash + ?Sized,
{ self.map.get_mut(key)
}
/// Returns the key-value pair matching the given 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. #[inline] pubfn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)> where
K: Borrow<Q>,
Q: ?Sized + Ord + Eq + Hash,
{ self.map.get_key_value(key)
}
/// 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. #[inline] pubfn insert(&mutself, k: K, v: V) -> Option<V> { self.map.insert(k, 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. #[inline] pubfn remove<Q>(&mutself, key: &Q) -> Option<V> where
K: Borrow<Q>,
Q: Ord + Eq + Hash + ?Sized,
{ #[cfg(not(feature = "preserve_order"))]
{ self.map.remove(key)
} #[cfg(feature = "preserve_order")]
{ self.map.shift_remove(key)
}
}
/// Removes a key from the map, returning the stored key and value if the key was previously in the map. #[inline] pubfn remove_entry<Q>(&mutself, key: &Q) -> Option<(K, V)> where
K: Borrow<Q>,
Q: Ord + Eq + Hash + ?Sized,
{ #[cfg(not(feature = "preserve_order"))]
{ self.map.remove_entry(key)
} #[cfg(feature = "preserve_order")]
{ self.map.shift_remove_entry(key)
}
}
/// Retains only the elements specified by the `keep` predicate. /// /// In other words, remove all pairs `(k, v)` for which `keep(&k, &mut v)` /// returns `false`. /// /// The elements are visited in iteration order. #[inline] pubfn retain<F>(&mutself, mut keep: F) where
K: AsRef<str>,
F: FnMut(&str, &mut V) -> bool,
{ self.map.retain(|key, value| keep(key.as_ref(), value));
}
/// Gets the given key's corresponding entry in the map for in-place /// manipulation. pubfn entry<S>(&mutself, key: S) -> Entry<'_, K, V> where
S: Into<K>,
{ #[cfg(not(feature = "preserve_order"))] use alloc::collections::btree_map::Entry as EntryImpl; #[cfg(feature = "preserve_order")] use indexmap::map::Entry as EntryImpl;
/// Returns the number of elements in the map. #[inline] pubfn len(&self) -> usize { self.map.len()
}
/// Returns true if the map contains no elements. #[inline] pubfn is_empty(&self) -> bool { self.map.is_empty()
}
/// Gets an iterator over the entries of the map. #[inline] pubfn iter(&self) -> Iter<'_, K, V> {
Iter {
iter: self.map.iter(),
}
}
/// Gets a mutable iterator over the entries of the map. #[inline] pubfn iter_mut(&mutself) -> IterMut<'_, K, V> {
IterMut {
iter: self.map.iter_mut(),
}
}
/// Gets an iterator over the keys of the map. #[inline] pubfn keys(&self) -> Keys<'_, K, V> {
Keys {
iter: self.map.keys(),
}
}
/// Gets an iterator over the values of the map. #[inline] pubfn values(&self) -> Values<'_, K, V> {
Values {
iter: self.map.values(),
}
}
/// Scan through each key-value pair in the map and keep those where the /// closure `keep` returns `true`. /// /// The elements are visited in order, and remaining elements keep their /// order. /// /// Computes in **O(n)** time (average). #[allow(unused_mut)] pub(crate) fn mut_entries<F>(&mutself, mut op: F) where
F: FnMut(&mut K, &mut V),
{ #[cfg(feature = "preserve_order")]
{ use indexmap::map::MutableKeys as _; for (key, value) inself.map.iter_mut2() {
op(key, value);
}
} #[cfg(not(feature = "preserve_order"))]
{ self.map = core::mem::take(&mutself.map)
.into_iter()
.map(move |(mut k, mut v)| {
op(&mut k, &mut v);
(k, v)
})
.collect();
}
}
}
/// Access an element of this map. Panics if the given key is not present in the /// map. impl<K, V, Q> ops::Index<&Q> for Map<K, V> where
K: Borrow<Q> + Ord,
Q: Ord + Eq + Hash + ?Sized,
{ type Output = V;
/// Mutably access an element of this map. Panics if the given key is not /// present in the map. impl<K, V, Q> ops::IndexMut<&Q> for Map<K, V> where
K: Borrow<Q> + Ord,
Q: Ord + Eq + Hash + ?Sized,
{ fn index_mut(&mutself, index: &Q) -> &mut V { self.map.get_mut(index).expect("no entry found for key")
}
}
/// A view into a single entry in a map, which may either be vacant or occupied. /// This enum is constructed from the [`entry`] method on [`Map`]. /// /// [`entry`]: struct.Map.html#method.entry /// [`Map`]: struct.Map.html pubenum Entry<'a, K, V> { /// A vacant Entry.
Vacant(VacantEntry<'a, K, V>), /// An occupied Entry.
Occupied(OccupiedEntry<'a, K, V>),
}
/// A vacant Entry. It is part of the [`Entry`] enum. /// /// [`Entry`]: enum.Entry.html pubstruct VacantEntry<'a, K, V> {
vacant: VacantEntryImpl<'a, K, V>,
}
/// An occupied Entry. It is part of the [`Entry`] enum. /// /// [`Entry`]: enum.Entry.html pubstruct OccupiedEntry<'a, K, V> {
occupied: OccupiedEntryImpl<'a, K, V>,
}
impl<'a, K: Ord, V> Entry<'a, K, V> { /// Returns a reference to this entry's key. pubfn key(&self) -> &K { match *self {
Entry::Vacant(ref e) => e.key(),
Entry::Occupied(ref e) => e.key(),
}
}
/// Ensures a value is in the entry by inserting the default if empty, and /// returns a mutable reference to the value in the entry. pubfn or_insert(self, default: V) -> &'a mut V { matchself {
Entry::Vacant(entry) => entry.insert(default),
Entry::Occupied(entry) => entry.into_mut(),
}
}
/// Ensures a value is in the entry by inserting the result of the default /// function if empty, and returns a mutable reference to the value in the /// entry. pubfn or_insert_with<F>(self, default: F) -> &'a mut V where
F: FnOnce() -> V,
{ matchself {
Entry::Vacant(entry) => entry.insert(default()),
Entry::Occupied(entry) => entry.into_mut(),
}
}
}
impl<'a, K: Ord, V> VacantEntry<'a, K, V> { /// Gets a reference to the key that would be used when inserting a value /// through the `VacantEntry`. #[inline] pubfn key(&self) -> &K { self.vacant.key()
}
/// Sets the value of the entry with the `VacantEntry`'s key, and returns a /// mutable reference to it. #[inline] pubfn insert(self, value: V) -> &'a mut V { self.vacant.insert(value)
}
}
impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> { /// Gets a reference to the key in the entry. #[inline] pubfn key(&self) -> &K { self.occupied.key()
}
/// Gets a reference to the value in the entry. #[inline] pubfn get(&self) -> &V { self.occupied.get()
}
/// Gets a mutable reference to the value in the entry. #[inline] pubfn get_mut(&mutself) -> &mut V { self.occupied.get_mut()
}
/// Converts the entry into a mutable reference to its value. #[inline] pubfn into_mut(self) -> &'a mut V { self.occupied.into_mut()
}
/// Sets the value of the entry with the `OccupiedEntry`'s key, and returns /// the entry's old value. #[inline] pubfn insert(&mutself, value: V) -> V { self.occupied.insert(value)
}
/// Takes the value of the entry out of the map, and returns it. #[inline] pubfn remove(self) -> V { #[cfg(not(feature = "preserve_order"))]
{ self.occupied.remove()
} #[cfg(feature = "preserve_order")]
{ self.occupied.shift_remove()
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.