use super::StablePrioContainer;
use crate::iter::StableHeapIterMax;
use std::cmp::Reverse;
pub struct StablePrioContainerMax<T> {
heap: StablePrioContainer<Reverse<T>>,
}
impl<T: Ord> StablePrioContainerMax<T> {
pub fn new(capacity: usize) -> Self {
let heap = StablePrioContainer::new(capacity);
StablePrioContainerMax { heap }
}
pub fn new_allocated(capacity: usize, alloc_size: usize) -> Self {
let heap = StablePrioContainer::new_allocated(capacity, alloc_size);
StablePrioContainerMax { heap }
}
pub fn insert(&mut self, item: T) -> bool {
self.heap.insert(Reverse(item))
}
#[inline]
pub fn contains(&self, item: &T) -> bool {
self.heap.heap.iter().any(|i| i.0 == *item)
}
#[inline]
pub fn into_sorted_vec(self) -> Vec<T> {
self.into_iter().collect()
}
}
impl<T> StablePrioContainerMax<T> {
#[inline]
pub fn len(&self) -> usize {
self.heap.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn capacity(&self) -> usize {
self.heap.capacity()
}
#[inline]
pub fn total_pushed(&self) -> usize {
self.heap.total_pushed
}
}
impl<T: Ord> IntoIterator for StablePrioContainerMax<T> {
type Item = T;
type IntoIter = StableHeapIterMax<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
StableHeapIterMax::new(self.heap.heap)
}
}
impl<T: Ord> Extend<T> for StablePrioContainerMax<T> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for i in iter {
self.insert(i);
}
}
}