use super::stable::StableUniquePrioContainer;
use crate::iter::StableHeapIterMax;
use std::{cmp::Reverse, hash::Hash};
pub struct StableUniquePrioContainerMax<T> {
heap: StableUniquePrioContainer<Reverse<T>>,
}
impl<T: Ord + Hash + Clone> StableUniquePrioContainerMax<T> {
pub fn new(capacity: usize) -> Self {
let heap = StableUniquePrioContainer::new(capacity);
StableUniquePrioContainerMax { heap }
}
pub fn new_allocated(capacity: usize, alloc_size: usize) -> Self {
let heap = StableUniquePrioContainer::new_allocated(capacity, alloc_size);
StableUniquePrioContainerMax { heap }
}
pub fn insert(&mut self, item: T) -> bool {
self.heap.insert(Reverse(item))
}
#[inline]
pub fn contains(&self, item: &T) -> bool {
self.heap
.container
.heap
.iter()
.any(|i| i.as_ref().0 == *item)
}
#[inline]
pub fn into_sorted_vec(self) -> Vec<T> {
self.into_iter().collect()
}
}
impl<T> StableUniquePrioContainerMax<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 StableUniquePrioContainerMax<T> {
type Item = T;
type IntoIter = StableHeapIterMax<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
StableHeapIterMax::new(self.heap.container.heap)
}
}
impl<T: Ord + Hash + Clone> Extend<T> for StableUniquePrioContainerMax<T> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for i in iter {
self.insert(i);
}
}
}