mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-23 15:46:33 +11:00
Start implementing the individual entry types
This commit is contained in:
parent
921a338c59
commit
631e1e9bc2
1 changed files with 31 additions and 10 deletions
|
@ -161,9 +161,21 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a> OccupiedEntry<'a, K, V> {
|
impl<'a, K: 'a, V: 'a> OccupiedEntry<'a, K, V> {
|
||||||
fn value_mut(self) -> &'a mut V {
|
pub fn key(&self) -> &K {
|
||||||
|
&self.key
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self) -> &V {
|
||||||
|
self.entry.value_ref().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_mut(self) -> &'a mut V {
|
||||||
self.entry.value_mut().unwrap()
|
self.entry.value_mut().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, value: V) -> V {
|
||||||
|
self.entry.replace_value(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct VacantEntry<'a, K: 'a, V: 'a> {
|
pub struct VacantEntry<'a, K: 'a, V: 'a> {
|
||||||
|
@ -172,7 +184,7 @@ pub struct VacantEntry<'a, K: 'a, V: 'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
|
impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
|
||||||
fn insert(self, value: V) -> &'a mut V
|
pub fn insert(self, value: V) -> &'a mut V
|
||||||
where
|
where
|
||||||
K: Hash + Eq,
|
K: Hash + Eq,
|
||||||
{
|
{
|
||||||
|
@ -191,7 +203,7 @@ where
|
||||||
{
|
{
|
||||||
pub fn or_insert(self, value: V) -> &'a mut V {
|
pub fn or_insert(self, value: V) -> &'a mut V {
|
||||||
match self {
|
match self {
|
||||||
Entry::Occupied(e) => e.value_mut(),
|
Entry::Occupied(e) => e.into_mut(),
|
||||||
Entry::Vacant(e) => e.insert(value),
|
Entry::Vacant(e) => e.insert(value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -201,7 +213,7 @@ where
|
||||||
F: FnOnce() -> V,
|
F: FnOnce() -> V,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Entry::Occupied(e) => e.value_mut(),
|
Entry::Occupied(e) => e.into_mut(),
|
||||||
Entry::Vacant(e) => e.insert(f()),
|
Entry::Vacant(e) => e.insert(f()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -211,7 +223,7 @@ where
|
||||||
F: FnOnce(&K) -> V,
|
F: FnOnce(&K) -> V,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Entry::Occupied(e) => e.value_mut(),
|
Entry::Occupied(e) => e.into_mut(),
|
||||||
Entry::Vacant(e) => {
|
Entry::Vacant(e) => {
|
||||||
let value = f(&e.key);
|
let value = f(&e.key);
|
||||||
e.insert(value)
|
e.insert(value)
|
||||||
|
@ -237,7 +249,7 @@ where
|
||||||
V: Default,
|
V: Default,
|
||||||
{
|
{
|
||||||
match self {
|
match self {
|
||||||
Entry::Occupied(e) => e.value_mut(),
|
Entry::Occupied(e) => e.into_mut(),
|
||||||
Entry::Vacant(e) => e.insert(Default::default()),
|
Entry::Vacant(e) => e.insert(Default::default()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -395,8 +407,8 @@ impl<K, V> NodeStorage<K, V> {
|
||||||
new_node_storage
|
new_node_storage
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_at_location(&mut self, location: usize, key: K, value: V) -> Option<V> {
|
fn replace_at_location(&mut self, location: usize, key: K, value: V) -> V {
|
||||||
self.nodes[location].replace(key, value).map(|(k, v)| v)
|
self.nodes[location].replace(key, value).1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,12 +481,21 @@ impl<K, V> Node<K, V> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace(&mut self, key: K, value: V) -> Option<(K, V)> {
|
fn replace_value(&mut self, value: V) -> V {
|
||||||
|
if self.has_value() {
|
||||||
|
let old_value = mem::replace(&mut self.value, MaybeUninit::new(value));
|
||||||
|
unsafe { old_value.assume_init() }
|
||||||
|
} else {
|
||||||
|
panic!("Cannot replace an unininitalised node");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace(&mut self, key: K, value: V) -> (K, V) {
|
||||||
if self.has_value() {
|
if self.has_value() {
|
||||||
let old_key = mem::replace(&mut self.key, MaybeUninit::new(key));
|
let old_key = mem::replace(&mut self.key, MaybeUninit::new(key));
|
||||||
let old_value = mem::replace(&mut self.value, MaybeUninit::new(value));
|
let old_value = mem::replace(&mut self.value, MaybeUninit::new(value));
|
||||||
|
|
||||||
Some(unsafe { (old_key.assume_init(), old_value.assume_init()) })
|
unsafe { (old_key.assume_init(), old_value.assume_init()) }
|
||||||
} else {
|
} else {
|
||||||
panic!("Cannot replace an uninitialised node");
|
panic!("Cannot replace an uninitialised node");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue