Add default implementation

This commit is contained in:
Gwilym Kuiper 2022-03-18 00:21:55 +00:00
parent 6ff4cbe4f1
commit 5edd46e085

View file

@ -171,6 +171,12 @@ impl<K, V> HashMap<K, V> {
} }
} }
impl<K, V> Default for HashMap<K, V> {
fn default() -> Self {
Self::new()
}
}
const fn fast_mod(len: usize, hash: HashType) -> usize { const fn fast_mod(len: usize, hash: HashType) -> usize {
debug_assert!(len.is_power_of_two(), "Length must be a power of 2"); debug_assert!(len.is_power_of_two(), "Length must be a power of 2");
(hash as usize) & (len - 1) (hash as usize) & (len - 1)
@ -235,6 +241,13 @@ where
.map(|location| &self.nodes.0[location].as_ref().unwrap().value) .map(|location| &self.nodes.0[location].as_ref().unwrap().value)
} }
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
let hash = self.hash(key);
self.get_location(key, hash)
.map(|location| &mut self.nodes.0[location].as_mut().unwrap().value)
}
pub fn remove(&mut self, key: &K) -> Option<V> { pub fn remove(&mut self, key: &K) -> Option<V> {
let hash = self.hash(key); let hash = self.hash(key);