use core::alloc::Layout;
use core::borrow::Borrow;
use super::BBTreeMap;
use crate::allocator::{Allocator, InsertError};
use crate::inline_key::InlineKeyStrategy;
use crate::tree::latched::WithLeaf;
use crate::tree::node::annexed_leaf::AnnexedLeaf;
impl<K, V, S: InlineKeyStrategy<K>, A: Allocator> BBTreeMap<K, V, S, A> {
#[inline]
pub fn update_or_insert_with_leaf<R>(
&self,
key: K,
value: V,
occupied: impl FnOnce(AnnexedLeaf<'_, K, V, S>, &K, &mut V) -> Result<R, Layout>,
vacant: impl FnOnce(AnnexedLeaf<'_, K, V, S>, &K, &V) -> Result<R, Layout>,
) -> Result<R, InsertError<K, V>>
where
K: Ord,
{
self.tree
.insert(key, value, WithLeaf(occupied, vacant), &self.alloc)
}
#[inline]
pub fn remove_with_leaf<Q>(
&self,
key: &Q,
f: impl FnOnce(AnnexedLeaf<'_, K, V, S>, &K, &V),
) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
self.tree.remove_entry(key, |_, _| true, f, &self.alloc)
}
}