use crate::error::InvalidHandle;
use crate::{AddressableHeap, Heap, MeldableAddressableHeap, MeldableHeap};
use super::soft_heap_core::{SoftHandle, SoftHeapCore, SoftHeapError, SoftMeldError};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BinaryTreeSoftAddressableHeap<K, V = ()> {
core: SoftHeapCore<K, V>,
}
impl<K: Ord + Clone, V> BinaryTreeSoftAddressableHeap<K, V> {
pub fn new(error_rate: f64) -> Result<Self, SoftHeapError> {
Ok(Self {
core: SoftHeapCore::new(error_rate)?,
})
}
}
impl<K: Ord + Clone, V> BinaryTreeSoftAddressableHeap<K, V> {
#[must_use]
pub const fn rank_limit(&self) -> usize {
self.core.rank_limit()
}
pub fn insert(&mut self, key: K, value: V) -> SoftHandle {
self.core.insert(key, value)
}
#[must_use]
pub fn peek_entry(&self) -> Option<(SoftHandle, &K, &V)> {
self.core.peek_entry()
}
pub fn pop_entry(&mut self) -> Option<(K, V)> {
self.core.pop_item().map(|item| item.into_pair())
}
pub fn key(&self, handle: SoftHandle) -> Result<&K, InvalidHandle> {
self.core.key(handle)
}
pub fn value(&self, handle: SoftHandle) -> Result<&V, InvalidHandle> {
self.core.value(handle)
}
pub fn value_mut(&mut self, handle: SoftHandle) -> Result<&mut V, InvalidHandle> {
self.core.value_mut(handle)
}
pub fn delete(&mut self, handle: SoftHandle) -> Result<(K, V), InvalidHandle> {
self.core.delete(handle).map(|item| item.into_pair())
}
#[must_use]
pub const fn len(&self) -> usize {
self.core.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.core.len() == 0
}
pub fn clear(&mut self) {
self.core.clear();
}
}
impl<K: Ord + Clone, V> BinaryTreeSoftAddressableHeap<K, V> {
pub fn meld(&mut self, other: Self) -> Result<(), SoftMeldError> {
if self.rank_limit() != other.rank_limit() {
return Err(SoftMeldError::IncompatibleErrorRate);
}
self.core.meld_from(other.core);
Ok(())
}
}
impl<K: Ord + Clone, V> AddressableHeap<K, V> for BinaryTreeSoftAddressableHeap<K, V> {
type Handle = SoftHandle;
fn insert(&mut self, key: K, value: V) -> Self::Handle {
Self::insert(self, key, value)
}
fn peek(&self) -> Option<(Self::Handle, &K, &V)> {
Self::peek_entry(self)
}
fn pop(&mut self) -> Option<(K, V)> {
Self::pop_entry(self)
}
fn key(&self, handle: Self::Handle) -> Result<&K, InvalidHandle> {
Self::key(self, handle)
}
fn value(&self, handle: Self::Handle) -> Result<&V, InvalidHandle> {
Self::value(self, handle)
}
fn value_mut(&mut self, handle: Self::Handle) -> Result<&mut V, InvalidHandle> {
Self::value_mut(self, handle)
}
fn delete(&mut self, handle: Self::Handle) -> Result<(K, V), InvalidHandle> {
Self::delete(self, handle)
}
fn len(&self) -> usize {
Self::len(self)
}
fn clear(&mut self) {
Self::clear(self);
}
}
impl<K: Ord + Clone, V> MeldableAddressableHeap<K, V> for BinaryTreeSoftAddressableHeap<K, V> {
type MeldError = SoftMeldError;
fn meld(&mut self, other: Self) -> Result<(), Self::MeldError> {
Self::meld(self, other)
}
}
impl<K: Ord + Clone> BinaryTreeSoftAddressableHeap<K, ()> {
pub fn push(&mut self, key: K) -> SoftHandle {
self.insert(key, ())
}
#[must_use]
pub fn peek(&self) -> Option<&K> {
self.peek_entry().map(|(_, key, _)| key)
}
pub fn pop(&mut self) -> Option<K> {
self.pop_entry().map(|(key, ())| key)
}
}
impl<T: Ord + Clone> Heap<T> for BinaryTreeSoftAddressableHeap<T, ()> {
fn push(&mut self, value: T) {
Self::push(self, value);
}
fn peek(&self) -> Option<&T> {
Self::peek(self)
}
fn pop(&mut self) -> Option<T> {
Self::pop(self)
}
fn len(&self) -> usize {
Self::len(self)
}
fn clear(&mut self) {
Self::clear(self);
}
}
impl<T: Ord + Clone> MeldableHeap<T> for BinaryTreeSoftAddressableHeap<T, ()> {
type MeldError = SoftMeldError;
fn meld(&mut self, other: Self) -> Result<(), Self::MeldError> {
Self::meld(self, other)
}
}