mod core;
pub mod iter;
mod mem;
pub use iter::{Drain, IntoIter, Iter};
use std::fmt;
use std::ops::{Deref, DerefMut};
pub struct Beap<T> {
data: Vec<T>,
height: usize,
}
pub struct PeekMut<'a, T: 'a + Ord> {
beap: &'a mut Beap<T>,
sift: bool,
}
impl<T: Ord + fmt::Debug> fmt::Debug for PeekMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("PeekMut").field(&self.beap.data[0]).finish()
}
}
impl<T> Default for Beap<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: Ord> Drop for PeekMut<'_, T> {
fn drop(&mut self) {
if self.sift {
self.beap.siftdown(0, 1);
}
}
}
impl<T: Ord> Deref for PeekMut<'_, T> {
type Target = T;
fn deref(&self) -> &T {
debug_assert!(!self.beap.is_empty());
self.beap.data.first().unwrap()
}
}
impl<T: Ord> DerefMut for PeekMut<'_, T> {
fn deref_mut(&mut self) -> &mut T {
debug_assert!(!self.beap.is_empty());
self.sift = true;
self.beap.data.first_mut().unwrap()
}
}
impl<'a, T: Ord> PeekMut<'a, T> {
pub fn pop(mut this: PeekMut<'a, T>) -> T {
let value = this.beap.pop().unwrap();
this.sift = false;
value
}
}
impl<T: Clone> Clone for Beap<T> {
fn clone(&self) -> Self {
Beap {
data: self.data.clone(),
height: self.height,
}
}
fn clone_from(&mut self, source: &Self) {
self.data.clone_from(&source.data);
self.height.clone_from(&source.height);
}
}
pub struct TailMut<'a, T: 'a + Ord> {
beap: &'a mut Beap<T>,
sift: bool,
pos: usize,
}
impl<T: Ord + fmt::Debug> fmt::Debug for TailMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("TailMut")
.field(&self.beap.data[self.pos])
.finish()
}
}
impl<T: Ord> Drop for TailMut<'_, T> {
fn drop(&mut self) {
if self.sift {
self.beap.repair(self.pos);
}
}
}
impl<T: Ord> Deref for TailMut<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.beap.data.get(self.pos).unwrap()
}
}
impl<T: Ord> DerefMut for TailMut<'_, T> {
fn deref_mut(&mut self) -> &mut T {
self.sift = true;
self.beap.data.get_mut(self.pos).unwrap()
}
}
impl<'a, T: Ord> TailMut<'a, T> {
pub fn pop(mut this: TailMut<'a, T>) -> T {
let value = this.beap.remove_index(this.pos).unwrap();
this.sift = false;
value
}
}
pub struct PosMut<'a, T: 'a + Ord> {
beap: &'a mut Beap<T>,
sift: bool,
pos: usize,
}
impl<T: Ord + fmt::Debug> fmt::Debug for PosMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("PosMut")
.field(&self.beap.data[self.pos])
.finish()
}
}
impl<T: Ord> Drop for PosMut<'_, T> {
fn drop(&mut self) {
if self.sift {
self.beap.repair(self.pos);
}
}
}
impl<T: Ord> Deref for PosMut<'_, T> {
type Target = T;
fn deref(&self) -> &T {
self.beap.data.get(self.pos).unwrap()
}
}
impl<T: Ord> DerefMut for PosMut<'_, T> {
fn deref_mut(&mut self) -> &mut T {
self.sift = true;
self.beap.data.get_mut(self.pos).unwrap()
}
}
impl<'a, T: Ord> PosMut<'a, T> {
pub fn remove(mut this: PosMut<'a, T>) -> T {
let value = this.beap.remove_index(this.pos).unwrap();
this.sift = true;
value
}
}
#[cfg(test)]
mod tests;