use crate::generic::{
map::{BTreeExt, BTreeExtMut, BTreeMap},
node::{Address, Item, Node},
};
use cc_traits::{SimpleCollectionMut, SimpleCollectionRef, Slab, SlabMut};
use std::fmt;
pub enum Entry<'a, K, V, C = slab::Slab<Node<K, V>>> {
Vacant(VacantEntry<'a, K, V, C>),
Occupied(OccupiedEntry<'a, K, V, C>),
}
use Entry::*;
impl<'a, K, V, C: Slab<Node<K, V>>> Entry<'a, K, V, C>
where
C: SimpleCollectionRef,
{
#[inline]
pub fn address(&self) -> Address {
match self {
Occupied(entry) => entry.address(),
Vacant(entry) => entry.address(),
}
}
#[inline]
pub fn key(&self) -> &K {
match *self {
Occupied(ref entry) => entry.key(),
Vacant(ref entry) => entry.key(),
}
}
}
impl<'a, K, V, C: SlabMut<Node<K, V>>> Entry<'a, K, V, C>
where
C: SimpleCollectionRef,
C: SimpleCollectionMut,
{
#[inline]
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default),
}
}
#[inline]
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(default()),
}
}
#[inline]
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => {
let value = default(entry.key());
entry.insert(value)
}
}
}
#[inline]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Occupied(mut entry) => {
f(entry.get_mut());
Occupied(entry)
}
Vacant(entry) => Vacant(entry),
}
}
#[inline]
pub fn or_default(self) -> &'a mut V
where
V: Default,
{
match self {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => entry.insert(Default::default()),
}
}
}
impl<'a, K: fmt::Debug, V: fmt::Debug, C: Slab<Node<K, V>>> fmt::Debug for Entry<'a, K, V, C>
where
C: SimpleCollectionRef,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Occupied(entry) => entry.fmt(f),
Vacant(entry) => entry.fmt(f),
}
}
}
pub struct VacantEntry<'a, K, V, C = slab::Slab<Node<K, V>>> {
pub(crate) map: &'a mut BTreeMap<K, V, C>,
pub(crate) key: K,
pub(crate) addr: Address,
}
impl<'a, K, V, C: Slab<Node<K, V>>> VacantEntry<'a, K, V, C> {
#[inline]
pub fn address(&self) -> Address {
self.addr
}
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn into_key(self) -> K {
self.key
}
}
impl<'a, K, V, C: SlabMut<Node<K, V>>> VacantEntry<'a, K, V, C>
where
C: SimpleCollectionRef,
C: SimpleCollectionMut,
{
#[inline]
pub fn insert(self, value: V) -> &'a mut V {
let addr = self.map.insert_at(self.addr, Item::new(self.key, value));
self.map.item_mut(addr).unwrap().value_mut()
}
}
impl<'a, K: fmt::Debug, V, C: Slab<Node<K, V>>> fmt::Debug for VacantEntry<'a, K, V, C> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("VacantEntry").field(self.key()).finish()
}
}
pub struct OccupiedEntry<'a, K, V, C = slab::Slab<Node<K, V>>> {
pub(crate) map: &'a mut BTreeMap<K, V, C>,
pub(crate) addr: Address,
}
impl<'a, K, V, C: Slab<Node<K, V>>> OccupiedEntry<'a, K, V, C>
where
C: SimpleCollectionRef,
{
#[inline]
pub fn address(&self) -> Address {
self.addr
}
#[inline]
pub fn get(&self) -> &V {
self.map.item(self.addr).unwrap().value()
}
#[inline]
pub fn key(&self) -> &K {
self.map.item(self.addr).unwrap().key()
}
}
impl<'a, K, V, C: SlabMut<Node<K, V>>> OccupiedEntry<'a, K, V, C>
where
C: SimpleCollectionRef,
C: SimpleCollectionMut,
{
#[inline]
pub fn get_mut(&mut self) -> &mut V {
self.map.item_mut(self.addr).unwrap().value_mut()
}
#[inline]
pub fn insert(&mut self, value: V) -> V {
self.map.item_mut(self.addr).unwrap().set_value(value)
}
#[inline]
pub fn into_mut(self) -> &'a mut V {
self.map.item_mut(self.addr).unwrap().value_mut()
}
#[inline]
pub fn remove(self) -> V {
self.map.remove_at(self.addr).unwrap().0.into_value()
}
#[inline]
pub fn remove_entry(self) -> (K, V) {
self.map.remove_at(self.addr).unwrap().0.into_pair()
}
}
impl<'a, K: fmt::Debug, V: fmt::Debug, C: Slab<Node<K, V>>> fmt::Debug
for OccupiedEntry<'a, K, V, C>
where
C: SimpleCollectionRef,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("OccupiedEntry")
.field("key", self.key())
.field("value", self.get())
.finish()
}
}