Give index and get_mut the Borrow treatment

This commit is contained in:
Gwilym Inzani 2023-04-23 18:44:03 +01:00
parent 39edc4ab36
commit 11b98eab29

View file

@ -376,7 +376,25 @@ where
/// Returns a mutable reference to the value corresponding to the key. Return [`None`] if /// Returns a mutable reference to the value corresponding to the key. Return [`None`] if
/// there is no element in the map with the given key. /// there is no element in the map with the given key.
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { ///
/// # Example
/// ```
/// use agb_hashmap::HashMap;
///
/// let mut map = HashMap::new();
/// map.insert("a".to_string(), "A");
///
/// if let Some(x) = map.get_mut("a") {
/// *x = "b";
/// }
///
/// assert_eq!(map["a"], "b");
/// ```
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash(key); let hash = self.hash(key);
if let Some(location) = self.nodes.location(key, hash) { if let Some(location) = self.nodes.location(key, hash) {
@ -732,28 +750,18 @@ where
} }
} }
impl<K, V, ALLOCATOR: ClonableAllocator> Index<&K> for HashMap<K, V, ALLOCATOR> impl<K, V, Q, ALLOCATOR: ClonableAllocator> Index<&Q> for HashMap<K, V, ALLOCATOR>
where where
K: Eq + Hash, K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash + ?Sized,
{ {
type Output = V; type Output = V;
fn index(&self, key: &K) -> &V { fn index(&self, key: &Q) -> &V {
self.get(key).expect("no entry found for key") self.get(key).expect("no entry found for key")
} }
} }
impl<K, V, ALLOCATOR: ClonableAllocator> Index<K> for HashMap<K, V, ALLOCATOR>
where
K: Eq + Hash,
{
type Output = V;
fn index(&self, key: K) -> &V {
self.get(&key).expect("no entry found for key")
}
}
const fn number_before_resize(capacity: usize) -> usize { const fn number_before_resize(capacity: usize) -> usize {
capacity * 85 / 100 capacity * 85 / 100
} }