use core::fmt;
use core::mem;
use crate::raw::{Handle, RawOSBTreeMap};
pub enum Entry<'a, K: 'a, V: 'a> {
Vacant(VacantEntry<'a, K, V>),
Occupied(OccupiedEntry<'a, K, V>),
}
impl<K: fmt::Debug + Ord + Clone, V: fmt::Debug> fmt::Debug for Entry<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Entry::Vacant(v) => f.debug_tuple("Entry").field(v).finish(),
Entry::Occupied(o) => f.debug_tuple("Entry").field(o).finish(),
}
}
}
pub struct VacantEntry<'a, K, V> {
pub(crate) key: K,
pub(crate) tree: &'a mut RawOSBTreeMap<K, V>,
}
impl<K: fmt::Debug + Ord + Clone, V> fmt::Debug for VacantEntry<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VacantEntry").field("key", &self.key).finish()
}
}
pub struct OccupiedEntry<'a, K, V> {
pub(crate) key: K,
pub(crate) leaf_handle: Handle,
pub(crate) index: usize,
pub(crate) tree: &'a mut RawOSBTreeMap<K, V>,
}
impl<K: fmt::Debug + Ord + Clone, V: fmt::Debug> fmt::Debug for OccupiedEntry<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntry").field("key", self.key()).field("value", self.get()).finish()
}
}
impl<'a, K: Ord + Clone, V> Entry<'a, K, V> {
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => v.insert(default),
}
}
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => v.insert(default()),
}
}
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
match self {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => {
let value = default(&v.key);
v.insert(value)
}
}
}
#[allow(clippy::must_use_candidate)]
pub fn key(&self) -> &K {
match self {
Entry::Occupied(o) => o.key(),
Entry::Vacant(v) => v.key(),
}
}
#[allow(clippy::return_self_not_must_use)]
pub fn and_modify<F>(mut self, f: F) -> Self
where
F: FnOnce(&mut V),
{
if let Entry::Occupied(ref mut o) = self {
f(o.get_mut());
}
self
}
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
match self {
Entry::Occupied(mut o) => {
o.insert(value);
o
}
Entry::Vacant(v) => v.insert_entry(value),
}
}
}
impl<'a, K: Ord + Clone, V: Default> Entry<'a, K, V> {
#[allow(clippy::must_use_candidate)]
pub fn or_default(self) -> &'a mut V {
match self {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => v.insert(V::default()),
}
}
}
impl<'a, K: Ord + Clone, V> VacantEntry<'a, K, V> {
#[allow(clippy::must_use_candidate)]
pub fn key(&self) -> &K {
&self.key
}
#[allow(clippy::must_use_candidate)]
pub fn into_key(self) -> K {
self.key
}
pub fn insert(self, value: V) -> &'a mut V {
let key = self.key.clone();
self.tree.insert(self.key, value);
self.tree.get_mut(&key).expect("just inserted")
}
pub fn insert_entry(self, value: V) -> OccupiedEntry<'a, K, V> {
self.tree.insert(self.key.clone(), value);
let (leaf_handle, index) = self.tree.search(&self.key).expect("just inserted");
OccupiedEntry {
key: self.key,
leaf_handle,
index,
tree: self.tree,
}
}
}
impl<'a, K: Ord + Clone, V> OccupiedEntry<'a, K, V> {
#[must_use]
pub fn key(&self) -> &K {
let leaf = self.tree.node(self.leaf_handle).as_leaf();
leaf.key(self.index)
}
#[allow(clippy::must_use_candidate)]
pub fn remove_entry(self) -> (K, V) {
self.tree.remove_entry(&self.key).expect("entry must exist")
}
#[must_use]
pub fn get(&self) -> &V {
let leaf = self.tree.node(self.leaf_handle).as_leaf();
let value_handle = leaf.value(self.index);
self.tree.value(value_handle)
}
pub fn get_mut(&mut self) -> &mut V {
let leaf = self.tree.node(self.leaf_handle).as_leaf();
let value_handle = leaf.value(self.index);
self.tree.value_mut(value_handle)
}
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_mut(self) -> &'a mut V {
let leaf = self.tree.node(self.leaf_handle).as_leaf();
let value_handle = leaf.value(self.index);
self.tree.value_mut(value_handle)
}
pub fn insert(&mut self, value: V) -> V {
mem::replace(self.get_mut(), value)
}
#[allow(clippy::must_use_candidate)]
pub fn remove(self) -> V {
self.remove_entry().1
}
}