module Control.Distributed.Process.Extras.Internal.Queue.PriorityQ where
import qualified Data.PriorityQueue.FingerTree as PQ
import Data.PriorityQueue.FingerTree (PQueue)
newtype PriorityQ k a = PriorityQ { q :: PQueue k a }
empty :: Ord k => PriorityQ k v
empty = PriorityQ $ PQ.empty
isEmpty :: Ord k => PriorityQ k v -> Bool
isEmpty = PQ.null . q
singleton :: Ord k => k -> a -> PriorityQ k a
singleton !k !v = PriorityQ $ PQ.singleton k v
enqueue :: Ord k => k -> v -> PriorityQ k v -> PriorityQ k v
enqueue !k !v p = PriorityQ (PQ.add k v $ q p)
dequeue :: Ord k => PriorityQ k v -> Maybe (v, PriorityQ k v)
dequeue p = maybe Nothing (\(v, pq') -> Just (v, pq')) $
case (PQ.minView (q p)) of
Nothing -> Nothing
Just (v, q') -> Just (v, PriorityQ $ q')
peek :: Ord k => PriorityQ k v -> Maybe v
peek p = maybe Nothing (\(v, _) -> Just v) $ dequeue p