Commit cae572e8 authored by Hans-Peter Deifel's avatar Hans-Peter Deifel
Browse files

Add `toList` and `clear` functions to BlockQueue

These are both useful for testing.
parent 5eae355f
......@@ -15,11 +15,14 @@ module Data.BlockQueue
, enqueue
, dequeue
, elem
, clear
, toList
) where
import Prelude hiding (null, elem)
import Control.Monad.ST
import qualified Data.Foldable as Foldable
import Data.Primitive.MutVar
import Data.Sequence (Seq)
......@@ -68,7 +71,7 @@ enqueue q block = unlessM (elem block q) $ do
modifyMutVar (q^.queue) $ \s -> s Seq.|> block
UnboxedV.write (q^.presence) (fromBlock block) True
-- | Read and delete the first element in the queue
-- | Read and delete the first element in the queue.
--
-- Returns 'Nothing' and doesn't modify the queue if it was empty.
--
......@@ -81,8 +84,23 @@ dequeue q = Seq.viewl <$> readMutVar (q^.queue) >>= \case
writeMutVar (q^.queue) rest
return (Just x)
-- | Tests whether an element is in the queue
-- | Tests whether an element is in the queue.
--
-- Runtime: O(1)
elem :: Block -> BlockQueue s -> ST s Bool
elem (Block block) !q = UnboxedV.read (q^.presence) block
-- | Removes all items from the queue.
--
-- Runtime: O(max size of queue)
clear :: BlockQueue s -> ST s ()
clear q = do
writeMutVar (q^.queue) Seq.empty
UnboxedV.set (q^.presence) False
-- | Return a list of currently queued items in the order in which they were
-- added to the queue.
--
-- Runtime: O(n)
toList :: BlockQueue s -> ST s [Block]
toList q = Foldable.toList <$> readMutVar (q^.queue)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment