mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-24 00:31:34 +11:00
Implement insert correctly
This commit is contained in:
parent
24b61f51fe
commit
c7db20c1df
|
@ -102,7 +102,23 @@ impl<K, V> HashMap<K, V>
|
||||||
where
|
where
|
||||||
K: Eq + Hash,
|
K: Eq + Hash,
|
||||||
{
|
{
|
||||||
pub fn insert(&mut self, key: K, value: V) -> &mut V {
|
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
|
||||||
|
let hash = self.hash(&key);
|
||||||
|
|
||||||
|
if let Some(location) = self.nodes.get_location(&key, hash) {
|
||||||
|
Some(self.nodes.replace_at_location(location, key, value))
|
||||||
|
} else {
|
||||||
|
if self.nodes.capacity() * 85 / 100 <= self.len() {
|
||||||
|
self.resize(self.nodes.capacity() * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.nodes.insert_new(key, value, hash);
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_and_get(&mut self, key: K, value: V) -> &'_ mut V {
|
||||||
let hash = self.hash(&key);
|
let hash = self.hash(&key);
|
||||||
|
|
||||||
let location = if let Some(location) = self.nodes.get_location(&key, hash) {
|
let location = if let Some(location) = self.nodes.get_location(&key, hash) {
|
||||||
|
@ -254,7 +270,7 @@ impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
|
||||||
where
|
where
|
||||||
K: Hash + Eq,
|
K: Hash + Eq,
|
||||||
{
|
{
|
||||||
self.map.insert(self.key, value)
|
self.map.insert_and_get(self.key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue