use alloc::*;
use heap as slice;
use rel::ord::*;
use super::vec::Vec;
#[derive(Debug)]
pub struct Heap<T, Rel: TotalOrderRelation<T> = ::rel::Core, A: Alloc = crate::DefaultA> {
rel: Rel,
arity: usize,
data: Vec<T, A>,
}
impl<T, Rel: TotalOrderRelation<T>, A: Alloc> Heap<T, Rel, A> {
#[inline]
pub fn new_in(a: A, rel: Rel, arity: usize) -> Self {
Heap { rel, arity, data: Vec::new_in(a) }
}
#[inline]
pub fn with_capacity_in(a: A, rel: Rel, arity: usize, cap: usize) -> Option<Self> {
Vec::with_capacity_in(a, cap).map(|v| Self::from_vec(rel, arity, v))
}
#[inline]
pub fn arity (&self) -> usize { self.arity }
#[inline]
pub fn length (&self) -> usize { self.data.len() }
#[inline]
pub fn capacity(&self) -> usize { self.data.capacity() }
#[inline]
pub fn reserve(&mut self, n_more: usize) -> bool { self.data.reserve(n_more) }
#[inline]
pub fn from_vec(rel: Rel, arity: usize, mut v: Vec<T, A>) -> Self {
slice::build(arity, |a, b| rel.less(a, b), &mut v[..]);
Heap { rel, arity, data: v }
}
#[inline]
pub fn push(&mut self, x: T) -> Result<(), T> {
let (arity, (rel, data)) = (self.arity, self.components());
data.push(x).map(|()| slice::push(arity, |a, b| rel.less(a, b), &mut data[..]))
}
#[inline]
pub fn pop(&mut self) -> Option<T> {
if self.data.len() == 0 { None } else {
let (arity, (rel, data)) = (self.arity, self.components());
slice::pop(arity, |a, b| rel.less(a, b), &mut data[..]);
data.pop()
}
}
#[inline]
pub fn peek(&self) -> Option<&T> {
if self.data.len() == 0 { None } else { Some(&self.data[0]) }
}
#[inline]
pub fn peek_mut(&mut self) -> Option<&mut T> {
if self.data.len() == 0 { None } else { Some(&mut self.data[0]) }
}
#[inline]
pub fn push_pop(&mut self, x: T) -> T {
let (arity, (rel, data)) = (self.arity, self.components());
slice::replace_root(arity, |a, b| rel.less(a, b), &mut data[..], x)
}
#[inline]
pub fn into_vec(self) -> Vec<T, A> { self.data }
#[inline]
pub fn into_sorted_vec(mut self) -> Vec<T, A> {
{
let (arity, (rel, data)) = (self.arity, self.components());
slice::sort(arity, |a, b| rel.less(a, b), &mut data[..]);
}
self.data
}
fn components(&mut self) -> (&Rel, &mut Vec<T, A>) { (&self.rel, &mut self.data) }
#[inline]
pub unsafe fn alloc_mut(&mut self) -> &mut A { self.data.alloc_mut() }
}