use std::cmp::Reverse;
use std::collections::BinaryHeap;
use super::{Outcome, Stm, TRef};
#[derive(Clone)]
pub struct TPriorityQueue<A> {
inner: TRef<BinaryHeap<Reverse<A>>>,
}
impl<A> TPriorityQueue<A>
where
A: Clone + Ord + Send + Sync + 'static,
{
pub fn empty() -> Stm<TPriorityQueue<A>, ()> {
TPriorityQueue::from_values(Vec::new())
}
pub fn from_values(items: impl IntoIterator<Item = A>) -> Stm<TPriorityQueue<A>, ()> {
TRef::make(items.into_iter().map(Reverse).collect::<BinaryHeap<_>>())
.map(|inner| TPriorityQueue { inner })
}
pub fn offer<E: Send + 'static>(&self, value: A) -> Stm<(), E> {
self.offer_all(vec![value])
}
pub fn offer_all<E: Send + 'static>(&self, values: Vec<A>) -> Stm<(), E> {
let q = self.clone();
Stm::from_fn(move |txn| {
let mut heap = q.inner.read_txn(txn);
heap.extend(values.iter().cloned().map(Reverse));
q.inner.set_txn(txn, heap);
Outcome::Done(())
})
}
pub fn take<E: Send + 'static>(&self) -> Stm<A, E> {
let q = self.clone();
Stm::from_fn(move |txn| {
let mut heap = q.inner.read_txn(txn);
match heap.pop() {
Some(Reverse(x)) => {
q.inner.set_txn(txn, heap);
Outcome::Done(x)
}
None => Outcome::Retry,
}
})
}
pub fn take_option<E: Send + 'static>(&self) -> Stm<Option<A>, E> {
self
.take()
.map(Some)
.or_else(Stm::from_fn(|_| Outcome::Done(None)))
}
pub fn take_all<E: Send + 'static>(&self) -> Stm<Vec<A>, E> {
let q = self.clone();
Stm::from_fn(move |txn| {
let heap = q.inner.read_txn(txn);
if heap.is_empty() {
return Outcome::Done(Vec::new());
}
q.inner.set_txn(txn, BinaryHeap::new());
Outcome::Done(
heap
.into_sorted_vec()
.into_iter()
.rev()
.map(|Reverse(x)| x)
.collect(),
)
})
}
pub fn peek<E: Send + 'static>(&self) -> Stm<A, E> {
let q = self.clone();
Stm::from_fn(move |txn| match q.inner.read_txn(txn).peek() {
Some(Reverse(x)) => Outcome::Done(x.clone()),
None => Outcome::Retry,
})
}
pub fn size<E: Send + 'static>(&self) -> Stm<usize, E> {
let q = self.clone();
Stm::from_fn(move |txn| Outcome::Done(q.inner.read_txn(txn).len()))
}
pub fn is_empty<E: Send + 'static>(&self) -> Stm<bool, E> {
self.size().map(|n| n == 0)
}
}