use std::{
cmp::Ordering,
fmt,
marker::PhantomData,
ops::{Deref, DerefMut},
sync::Arc,
};
use crate::{
map::{BLinkMap, DEFAULT_PAGE_CAPACITY},
node::{NodeKind, NodeReadGuard, NodeWriteGuard},
};
#[repr(transparent)]
pub(crate) struct KeyHandle<K> {
inner: Arc<K>,
}
impl<K> KeyHandle<K> {
pub(crate) fn new(key: K) -> Self {
Self {
inner: Arc::new(key),
}
}
#[inline]
pub(crate) fn get(&self) -> &K {
&self.inner
}
}
impl<K> AsRef<K> for KeyHandle<K> {
fn as_ref(&self) -> &K {
self.get()
}
}
impl<K> Deref for KeyHandle<K> {
type Target = K;
fn deref(&self) -> &K {
self.get()
}
}
impl<K> Clone for KeyHandle<K> {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl<K: PartialEq> PartialEq for KeyHandle<K> {
fn eq(&self, other: &Self) -> bool {
self.inner.eq(&other.inner)
}
}
impl<K: Eq> Eq for KeyHandle<K> {}
impl<K: PartialOrd> PartialOrd for KeyHandle<K> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.inner.partial_cmp(&other.inner)
}
}
impl<K: Ord> Ord for KeyHandle<K> {
fn cmp(&self, other: &Self) -> Ordering {
self.inner.cmp(&other.inner)
}
}
impl<K: std::hash::Hash> std::hash::Hash for KeyHandle<K> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}
#[must_use = "the entry remains read-locked only while this guard is held"]
pub struct EntryRef<'map, K, V, const N: usize = DEFAULT_PAGE_CAPACITY> {
guard: NodeReadGuard<K, V, N>,
index: usize,
_map: PhantomData<&'map BLinkMap<K, V, N>>,
}
impl<K, V, const N: usize> EntryRef<'_, K, V, N> {
pub(crate) fn new(guard: NodeReadGuard<K, V, N>, index: usize) -> Self {
Self {
guard,
index,
_map: PhantomData,
}
}
pub fn key(&self) -> &K {
&self.entry().key
}
pub fn value(&self) -> &V {
&self.entry().value
}
fn entry(&self) -> &crate::node::Entry<K, V> {
match &self.guard.kind {
NodeKind::Internal(_) | NodeKind::Redirect(_) => {
unreachable!("an entry guard must hold a leaf")
},
NodeKind::Leaf(leaf) => &leaf.entries[self.index],
}
}
}
impl<K, V, const N: usize> Deref for EntryRef<'_, K, V, N> {
type Target = V;
fn deref(&self) -> &V {
self.value()
}
}
impl<K: fmt::Debug, V: fmt::Debug, const N: usize> fmt::Debug
for EntryRef<'_, K, V, N>
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("EntryRef")
.field("key", self.key())
.field("value", self.value())
.finish()
}
}
#[must_use = "the entry remains write-locked only while this guard is held"]
pub struct EntryMut<'map, K, V, const N: usize = DEFAULT_PAGE_CAPACITY> {
guard: NodeWriteGuard<K, V, N>,
index: usize,
_map: PhantomData<&'map BLinkMap<K, V, N>>,
}
impl<K, V, const N: usize> EntryMut<'_, K, V, N> {
pub(crate) fn new(guard: NodeWriteGuard<K, V, N>, index: usize) -> Self {
Self {
guard,
index,
_map: PhantomData,
}
}
pub fn key(&self) -> &K {
&self.entry().key
}
pub fn value(&self) -> &V {
&self.entry().value
}
pub fn value_mut(&mut self) -> &mut V {
&mut self.entry_mut().value
}
fn entry(&self) -> &crate::node::Entry<K, V> {
match &self.guard.kind {
NodeKind::Internal(_) | NodeKind::Redirect(_) => {
unreachable!("an entry guard must hold a leaf")
},
NodeKind::Leaf(leaf) => &leaf.entries[self.index],
}
}
fn entry_mut(&mut self) -> &mut crate::node::Entry<K, V> {
match &mut self.guard.kind {
NodeKind::Internal(_) | NodeKind::Redirect(_) => {
unreachable!("an entry guard must hold a leaf")
},
NodeKind::Leaf(leaf) => &mut leaf.entries[self.index],
}
}
}
impl<K, V, const N: usize> Deref for EntryMut<'_, K, V, N> {
type Target = V;
fn deref(&self) -> &V {
self.value()
}
}
impl<K, V, const N: usize> DerefMut for EntryMut<'_, K, V, N> {
fn deref_mut(&mut self) -> &mut V {
self.value_mut()
}
}
impl<K: fmt::Debug, V: fmt::Debug, const N: usize> fmt::Debug
for EntryMut<'_, K, V, N>
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("EntryMut")
.field("key", self.key())
.field("value", self.value())
.finish()
}
}