useself::RustcEntry::*; usecrate::alloc::{Allocator, Global}; usecrate::map::{Drain, HashMap, IntoIter, Iter, IterMut, make_hash, make_hasher}; usecrate::raw::{Bucket, RawTable}; use core::fmt::{self, Debug}; use core::hash::{BuildHasher, Hash}; use core::mem;
impl<K, V, S, A> HashMap<K, V, S, A> where
K: Eq + Hash,
S: BuildHasher,
A: Allocator,
{ /// Gets the given key's corresponding entry in the map for in-place manipulation. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut letters = HashMap::new(); /// /// for ch in "a short treatise on fungi".chars() { /// let counter = letters.rustc_entry(ch).or_insert(0); /// *counter += 1; /// } /// /// assert_eq!(letters[&'s'], 2); /// assert_eq!(letters[&'t'], 3); /// assert_eq!(letters[&'u'], 1); /// assert_eq!(letters.get(&'y'), None); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn rustc_entry(&mutself, key: K) -> RustcEntry<'_, K, V, A> { let hash = make_hash(&self.hash_builder, &key); iflet Some(elem) = self.table.find(hash, |q| q.0.eq(&key)) {
RustcEntry::Occupied(RustcOccupiedEntry {
elem,
table: &mutself.table,
})
} else { // Ideally we would put this in VacantEntry::insert, but Entry is not // generic over the BuildHasher and adding a generic parameter would be // a breaking change. self.reserve(1);
/// Tries to insert a key-value pair into the map, and returns /// a mutable reference to the value in the entry. /// /// # Errors /// /// If the map already had this key present, nothing is updated, and an error /// containing the occupied entry, the key, and the value is returned. /// /// # Examples /// /// Basic usage: /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcOccupiedError; /// /// let mut map = HashMap::new(); /// assert_eq!(map.rustc_try_insert(37, "a").ok().unwrap(), &"a"); /// /// match map.rustc_try_insert(37, "b") { /// Err(RustcOccupiedError { entry, key, value, .. }) => { /// assert_eq!(entry.key(), &37); /// assert_eq!(entry.get(), &"a"); /// assert_eq!(key, 37); /// assert_eq!(value, "b"); /// } /// _ => panic!() /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn rustc_try_insert(
&mutself,
key: K,
value: V,
) -> Result<&mut V, RustcOccupiedError<'_, K, V, A>> { let hash = make_hash(&self.hash_builder, &key); iflet Some(elem) = self.table.find(hash, |q| q.0.eq(&key)) { let entry = RustcOccupiedEntry {
elem,
table: &mutself.table,
};
Err(RustcOccupiedError { entry, key, value })
} else { let hasher = make_hasher(&self.hash_builder); let entry = self.table.insert_entry(hash, (key, value), hasher);
Ok(&mut entry.1)
}
}
}
/// The error returned by [`rustc_try_insert`](HashMap::rustc_try_insert) when the key already exists. /// /// Note: There are no impls for this type, because we expect the standard library will /// immediately reconstruct these errors into its own `OccupiedError`. #[non_exhaustive] pubstruct RustcOccupiedError<'a, K, V, A = Global> where
A: Allocator,
{ /// The entry in the map that was already occupied. pub entry: RustcOccupiedEntry<'a, K, V, A>, /// The key which was not inserted, because the entry was already occupied. pub key: K, /// The value which was not inserted, because the entry was already occupied. pub value: V,
}
/// A view into a single entry in a map, which may either be vacant or occupied. /// /// This `enum` is constructed from the [`rustc_entry`] method on [`HashMap`]. /// /// [`rustc_entry`]: HashMap::rustc_entry pubenum RustcEntry<'a, K, V, A = Global> where
A: Allocator,
{ /// An occupied entry.
Occupied(RustcOccupiedEntry<'a, K, V, A>),
/// A vacant entry.
Vacant(RustcVacantEntry<'a, K, V, A>),
}
/// A view into an occupied entry in a `HashMap`. /// It is part of the [`RustcEntry`] enum. pubstruct RustcOccupiedEntry<'a, K, V, A = Global> where
A: Allocator,
{
elem: Bucket<(K, V)>,
table: &'a mut RawTable<(K, V), A>,
}
unsafeimpl<K, V, A> Send for RustcOccupiedEntry<'_, K, V, A> where
K: Send,
V: Send,
A: Allocator + Send,
{
} unsafeimpl<K, V, A> Sync for RustcOccupiedEntry<'_, K, V, A> where
K: Sync,
V: Sync,
A: Allocator + Sync,
{
}
/// A view into a vacant entry in a `HashMap`. /// It is part of the [`RustcEntry`] enum. pubstruct RustcVacantEntry<'a, K, V, A = Global> where
A: Allocator,
{
hash: u64,
key: K,
table: &'a mut RawTable<(K, V), A>,
}
impl<'a, K, V, A: Allocator> RustcEntry<'a, K, V, A> { /// Sets the value of the entry, and returns a RustcOccupiedEntry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// let entry = map.rustc_entry("horseyland").insert(37); /// /// assert_eq!(entry.key(), &"horseyland"); /// ``` pubfn insert(self, value: V) -> RustcOccupiedEntry<'a, K, V, A> { matchself {
Vacant(entry) => entry.insert_entry(value),
Occupied(mut entry) => {
entry.insert(value);
entry
}
}
}
/// Ensures a value is in the entry by inserting the default if empty, and returns /// a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// map.rustc_entry("poneyland").or_insert(3); /// assert_eq!(map["poneyland"], 3); /// /// *map.rustc_entry("poneyland").or_insert(10) *= 2; /// assert_eq!(map["poneyland"], 6); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert(self, default: V) -> &'a mut V where
K: Hash,
{ matchself {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default),
}
}
/// 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. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, String> = HashMap::new(); /// let s = "hoho".to_string(); /// /// map.rustc_entry("poneyland").or_insert_with(|| s); /// /// assert_eq!(map["poneyland"], "hoho".to_string()); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V where
K: Hash,
{ matchself {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default()),
}
}
/// Returns a reference to this entry's key. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// assert_eq!(map.rustc_entry("poneyland").key(), &"poneyland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn key(&self) -> &K { match *self {
Occupied(ref entry) => entry.key(),
Vacant(ref entry) => entry.key(),
}
}
impl<'a, K, V: Default, A: Allocator> RustcEntry<'a, K, V, A> { /// Ensures a value is in the entry by inserting the default value if empty, /// and returns a mutable reference to the value in the entry. /// /// # Examples /// /// ``` /// # fn main() { /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, Option<u32>> = HashMap::new(); /// map.rustc_entry("poneyland").or_default(); /// /// assert_eq!(map["poneyland"], None); /// # } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn or_default(self) -> &'a mut V where
K: Hash,
{ matchself {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(Default::default()),
}
}
}
impl<'a, K, V, A: Allocator> RustcOccupiedEntry<'a, K, V, A> { /// Gets a reference to the key in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.rustc_entry("poneyland").or_insert(12); /// assert_eq!(map.rustc_entry("poneyland").key(), &"poneyland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn key(&self) -> &K { unsafe { &self.elem.as_ref().0 }
}
/// Take the ownership of the key and value from the map. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.rustc_entry("poneyland").or_insert(12); /// /// if let RustcEntry::Occupied(o) = map.rustc_entry("poneyland") { /// // We delete the entry from the map. /// o.remove_entry(); /// } /// /// assert_eq!(map.contains_key("poneyland"), false); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn remove_entry(self) -> (K, V) { unsafe { self.table.remove(self.elem).0 }
}
/// Gets a reference to the value in the entry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.rustc_entry("poneyland").or_insert(12); /// /// if let RustcEntry::Occupied(o) = map.rustc_entry("poneyland") { /// assert_eq!(o.get(), &12); /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn get(&self) -> &V { unsafe { &self.elem.as_ref().1 }
}
/// Gets a mutable reference to the value in the entry. /// /// If you need a reference to the `RustcOccupiedEntry` which may outlive the /// destruction of the `RustcEntry` value, see [`into_mut`]. /// /// [`into_mut`]: #method.into_mut /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.rustc_entry("poneyland").or_insert(12); /// /// assert_eq!(map["poneyland"], 12); /// if let RustcEntry::Occupied(mut o) = map.rustc_entry("poneyland") { /// *o.get_mut() += 10; /// assert_eq!(*o.get(), 22); /// /// // We can use the same RustcEntry multiple times. /// *o.get_mut() += 2; /// } /// /// assert_eq!(map["poneyland"], 24); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn get_mut(&mutself) -> &mut V { unsafe { &mutself.elem.as_mut().1 }
}
/// Converts the RustcOccupiedEntry into a mutable reference to the value in the entry /// with a lifetime bound to the map itself. /// /// If you need multiple references to the `RustcOccupiedEntry`, see [`get_mut`]. /// /// [`get_mut`]: #method.get_mut /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.rustc_entry("poneyland").or_insert(12); /// /// assert_eq!(map["poneyland"], 12); /// if let RustcEntry::Occupied(o) = map.rustc_entry("poneyland") { /// *o.into_mut() += 10; /// } /// /// assert_eq!(map["poneyland"], 22); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn into_mut(self) -> &'a mut V { unsafe { &mutself.elem.as_mut().1 }
}
/// Sets the value of the entry, and returns the entry's old value. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.rustc_entry("poneyland").or_insert(12); /// /// if let RustcEntry::Occupied(mut o) = map.rustc_entry("poneyland") { /// assert_eq!(o.insert(15), 12); /// } /// /// assert_eq!(map["poneyland"], 15); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(&mutself, value: V) -> V {
mem::replace(self.get_mut(), value)
}
/// Takes the value out of the entry, and returns it. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// map.rustc_entry("poneyland").or_insert(12); /// /// if let RustcEntry::Occupied(o) = map.rustc_entry("poneyland") { /// assert_eq!(o.remove(), 12); /// } /// /// assert_eq!(map.contains_key("poneyland"), false); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn remove(self) -> V { self.remove_entry().1
}
}
impl<'a, K, V, A: Allocator> RustcVacantEntry<'a, K, V, A> { /// Gets a reference to the key that would be used when inserting a value /// through the `RustcVacantEntry`. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// assert_eq!(map.rustc_entry("poneyland").key(), &"poneyland"); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn key(&self) -> &K {
&self.key
}
/// Take ownership of the key. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// if let RustcEntry::Vacant(v) = map.rustc_entry("poneyland") { /// v.into_key(); /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn into_key(self) -> K { self.key
}
/// Sets the value of the entry with the RustcVacantEntry's key, /// and returns a mutable reference to it. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// if let RustcEntry::Vacant(o) = map.rustc_entry("poneyland") { /// o.insert(37); /// } /// assert_eq!(map["poneyland"], 37); /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert(self, value: V) -> &'a mut V { unsafe { let bucket = self.table.insert_no_grow(self.hash, (self.key, value));
&mut bucket.as_mut().1
}
}
/// Sets the value of the entry with the RustcVacantEntry's key, /// and returns a RustcOccupiedEntry. /// /// # Examples /// /// ``` /// use hashbrown::HashMap; /// use hashbrown::hash_map::RustcEntry; /// /// let mut map: HashMap<&str, u32> = HashMap::new(); /// /// if let RustcEntry::Vacant(v) = map.rustc_entry("poneyland") { /// let o = v.insert_entry(37); /// assert_eq!(o.get(), &37); /// } /// ``` #[cfg_attr(feature = "inline-more", inline)] pubfn insert_entry(self, value: V) -> RustcOccupiedEntry<'a, K, V, A> { let bucket = unsafe { self.table.insert_no_grow(self.hash, (self.key, value)) };
RustcOccupiedEntry {
elem: bucket,
table: self.table,
}
}
}
impl<K, V> IterMut<'_, K, V> { /// Returns a iterator of references over the remaining items. #[cfg_attr(feature = "inline-more", inline)] pubfn rustc_iter(&self) -> Iter<'_, K, V> { self.iter()
}
}
impl<K, V, A: Allocator> IntoIter<K, V, A> { /// Returns a iterator of references over the remaining items. #[cfg_attr(feature = "inline-more", inline)] pubfn rustc_iter(&self) -> Iter<'_, K, V> { self.iter()
}
}
impl<K, V, A: Allocator> Drain<'_, K, V, A> { /// Returns a iterator of references over the remaining items. #[cfg_attr(feature = "inline-more", inline)] pubfn rustc_iter(&self) -> Iter<'_, K, V> { self.iter()
}
}
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.