-- |
-- Module     : Simulation.Aivika.Queue
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- This module defines a queue that can use the specified strategies. So, having only
-- the 'FCFS', 'LCFS', 'SIRO' and 'StaticPriorities' strategies, you can build
-- 4 x 4 x 4 = 64 different types of the queue, each of them will have its own
-- behaviour.
--
module Simulation.Aivika.Queue
       (-- * Queue Types
        FCFSQueue,
        LCFSQueue,
        SIROQueue,
        PriorityQueue,
        Queue,
        -- * Creating Queue
        newFCFSQueue,
        newLCFSQueue,
        newSIROQueue,
        newPriorityQueue,
        newQueue,
        -- * Queue Properties and Activities
        enqueueStrategy,
        enqueueStoringStrategy,
        dequeueStrategy,
        queueNull,
        queueFull,
        queueMaxCount,
        queueCount,
        queueCountStats,
        enqueueCount,
        enqueueLostCount,
        enqueueStoreCount,
        dequeueCount,
        dequeueExtractCount,
        queueLoadFactor,
        enqueueRate,
        enqueueStoreRate,
        dequeueRate,
        dequeueExtractRate,
        queueWaitTime,
        queueTotalWaitTime,
        enqueueWaitTime,
        dequeueWaitTime,
        queueRate,
        -- * Dequeuing and Enqueuing
        dequeue,
        dequeueWithOutputPriority,
        tryDequeue,
        enqueue,
        enqueueWithInputPriority,
        enqueueWithStoringPriority,
        enqueueWithInputStoringPriorities,
        tryEnqueue,
        tryEnqueueWithStoringPriority,
        enqueueOrLost,
        enqueueOrLost_,
        enqueueWithStoringPriorityOrLost,
        enqueueWithStoringPriorityOrLost_,
        queueDelete,
        queueDelete_,
        queueDeleteBy,
        queueDeleteBy_,
        queueContains,
        queueContainsBy,
        clearQueue,
        -- * Statistics Reset
        resetQueue,
        -- * Awaiting
        waitWhileFullQueue,
        -- * Summary
        queueSummary,
        -- * Derived Signals for Properties
        queueNullChanged,
        queueNullChanged_,
        queueFullChanged,
        queueFullChanged_,
        queueCountChanged,
        queueCountChanged_,
        enqueueCountChanged,
        enqueueCountChanged_,
        enqueueLostCountChanged,
        enqueueLostCountChanged_,
        enqueueStoreCountChanged,
        enqueueStoreCountChanged_,
        dequeueCountChanged,
        dequeueCountChanged_,
        dequeueExtractCountChanged,
        dequeueExtractCountChanged_,
        queueLoadFactorChanged,
        queueLoadFactorChanged_,
        queueWaitTimeChanged,
        queueWaitTimeChanged_,
        queueTotalWaitTimeChanged,
        queueTotalWaitTimeChanged_,
        enqueueWaitTimeChanged,
        enqueueWaitTimeChanged_,
        dequeueWaitTimeChanged,
        dequeueWaitTimeChanged_,
        queueRateChanged,
        queueRateChanged_,
        -- * Basic Signals
        enqueueInitiated,
        enqueueStored,
        enqueueLost,
        dequeueRequested,
        dequeueExtracted,
        -- * Overall Signal
        queueChanged_) where

import Data.IORef
import Data.Monoid
import Data.Maybe

import Control.Monad
import Control.Monad.Trans

import Simulation.Aivika.Internal.Specs
import Simulation.Aivika.Internal.Simulation
import Simulation.Aivika.Internal.Dynamics
import Simulation.Aivika.Internal.Event
import Simulation.Aivika.Internal.Process
import Simulation.Aivika.Signal
import Simulation.Aivika.Resource.Base
import Simulation.Aivika.QueueStrategy
import Simulation.Aivika.Statistics

import qualified Simulation.Aivika.DoubleLinkedList as DLL 
import qualified Simulation.Aivika.Vector as V
import qualified Simulation.Aivika.PriorityQueue as PQ

-- | A type synonym for the ordinary FIFO queue also known as the FCFS
-- (First Come - First Serviced) queue.
type FCFSQueue a = Queue FCFS FCFS FCFS a

-- | A type synonym for the ordinary LIFO queue also known as the LCFS
-- (Last Come - First Serviced) queue.
type LCFSQueue a = Queue FCFS LCFS FCFS a

-- | A type synonym for the SIRO (Serviced in Random Order) queue.
type SIROQueue a = Queue FCFS SIRO FCFS a

-- | A type synonym for the queue with static priorities applied when
-- storing the elements in the queue.
type PriorityQueue a = Queue FCFS StaticPriorities FCFS a

-- | Represents a queue using the specified strategies for enqueueing (input), @si@,
-- internal storing (in memory), @sm@, and dequeueing (output), @so@, where @a@ denotes
-- the type of items stored in the queue.
data Queue si sm so a =
  Queue { Queue si sm so a -> Int
queueMaxCount :: Int,
          -- ^ The queue capacity.
          Queue si sm so a -> si
enqueueStrategy :: si,
          -- ^ The strategy applied to the enqueueing (input) processes when the queue is full.
          Queue si sm so a -> sm
enqueueStoringStrategy :: sm,
          -- ^ The strategy applied when storing (in memory) items in the queue.
          Queue si sm so a -> so
dequeueStrategy :: so,
          -- ^ The strategy applied to the dequeueing (output) processes when the queue is empty.
          Queue si sm so a -> Resource si
enqueueRes :: Resource si,
          Queue si sm so a -> StrategyQueue sm (QueueItem a)
queueStore :: StrategyQueue sm (QueueItem a),
          Queue si sm so a -> Resource so
dequeueRes :: Resource so,
          Queue si sm so a -> IORef Int
queueCountRef :: IORef Int,
          Queue si sm so a -> IORef (TimingStats Int)
queueCountStatsRef :: IORef (TimingStats Int),
          Queue si sm so a -> IORef Int
enqueueCountRef :: IORef Int,
          Queue si sm so a -> IORef Int
enqueueLostCountRef :: IORef Int,
          Queue si sm so a -> IORef Int
enqueueStoreCountRef :: IORef Int,
          Queue si sm so a -> IORef Int
dequeueCountRef :: IORef Int,
          Queue si sm so a -> IORef Int
dequeueExtractCountRef :: IORef Int,
          Queue si sm so a -> IORef (SamplingStats Double)
queueWaitTimeRef :: IORef (SamplingStats Double),
          Queue si sm so a -> IORef (SamplingStats Double)
queueTotalWaitTimeRef :: IORef (SamplingStats Double),
          Queue si sm so a -> IORef (SamplingStats Double)
enqueueWaitTimeRef :: IORef (SamplingStats Double),
          Queue si sm so a -> IORef (SamplingStats Double)
dequeueWaitTimeRef :: IORef (SamplingStats Double),
          Queue si sm so a -> SignalSource a
enqueueInitiatedSource :: SignalSource a,
          Queue si sm so a -> SignalSource a
enqueueLostSource :: SignalSource a,
          Queue si sm so a -> SignalSource a
enqueueStoredSource :: SignalSource a,
          Queue si sm so a -> SignalSource ()
dequeueRequestedSource :: SignalSource (),
          Queue si sm so a -> SignalSource a
dequeueExtractedSource :: SignalSource a }

-- | Stores the item and a time of its enqueuing. 
data QueueItem a =
  QueueItem { QueueItem a -> a
itemValue :: a,
              -- ^ Return the item value.
              QueueItem a -> Double
itemInputTime :: Double,
              -- ^ Return the time of enqueuing the item.
              QueueItem a -> Double
itemStoringTime :: Double
              -- ^ Return the time of storing in the queue, or
              -- @itemInputTime@ before the actual storing when
              -- the item was just enqueued.
            }
  
-- | Create a new FCFS queue with the specified capacity.  
newFCFSQueue :: Int -> Event (FCFSQueue a)  
newFCFSQueue :: Int -> Event (FCFSQueue a)
newFCFSQueue = FCFS -> FCFS -> FCFS -> Int -> Event (FCFSQueue a)
forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Event (Queue si sm so a)
newQueue FCFS
FCFS FCFS
FCFS FCFS
FCFS
  
-- | Create a new LCFS queue with the specified capacity.  
newLCFSQueue :: Int -> Event (LCFSQueue a)  
newLCFSQueue :: Int -> Event (LCFSQueue a)
newLCFSQueue = FCFS -> LCFS -> FCFS -> Int -> Event (LCFSQueue a)
forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Event (Queue si sm so a)
newQueue FCFS
FCFS LCFS
LCFS FCFS
FCFS
  
-- | Create a new SIRO queue with the specified capacity.  
newSIROQueue :: Int -> Event (SIROQueue a)  
newSIROQueue :: Int -> Event (SIROQueue a)
newSIROQueue = FCFS -> SIRO -> FCFS -> Int -> Event (SIROQueue a)
forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Event (Queue si sm so a)
newQueue FCFS
FCFS SIRO
SIRO FCFS
FCFS
  
-- | Create a new priority queue with the specified capacity.  
newPriorityQueue :: Int -> Event (PriorityQueue a)  
newPriorityQueue :: Int -> Event (PriorityQueue a)
newPriorityQueue = FCFS -> StaticPriorities -> FCFS -> Int -> Event (PriorityQueue a)
forall si sm so a.
(QueueStrategy si, QueueStrategy sm, QueueStrategy so) =>
si -> sm -> so -> Int -> Event (Queue si sm so a)
newQueue FCFS
FCFS StaticPriorities
StaticPriorities FCFS
FCFS
  
-- | Create a new queue with the specified strategies and capacity.  
newQueue :: (QueueStrategy si,
             QueueStrategy sm,
             QueueStrategy so) =>
            si
            -- ^ the strategy applied to the enqueueing (input) processes when the queue is full
            -> sm
            -- ^ the strategy applied when storing items in the queue
            -> so
            -- ^ the strategy applied to the dequeueing (output) processes when the queue is empty
            -> Int
            -- ^ the queue capacity
            -> Event (Queue si sm so a)  
newQueue :: si -> sm -> so -> Int -> Event (Queue si sm so a)
newQueue si
si sm
sm so
so Int
count =
  do Double
t  <- Dynamics Double -> Event Double
forall (m :: * -> *) a. DynamicsLift m => Dynamics a -> m a
liftDynamics Dynamics Double
time
     IORef Int
i  <- IO (IORef Int) -> Event (IORef Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef Int) -> Event (IORef Int))
-> IO (IORef Int) -> Event (IORef Int)
forall a b. (a -> b) -> a -> b
$ Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
     IORef (TimingStats Int)
is <- IO (IORef (TimingStats Int)) -> Event (IORef (TimingStats Int))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef (TimingStats Int)) -> Event (IORef (TimingStats Int)))
-> IO (IORef (TimingStats Int)) -> Event (IORef (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ TimingStats Int -> IO (IORef (TimingStats Int))
forall a. a -> IO (IORef a)
newIORef (TimingStats Int -> IO (IORef (TimingStats Int)))
-> TimingStats Int -> IO (IORef (TimingStats Int))
forall a b. (a -> b) -> a -> b
$ Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
0
     IORef Int
ci <- IO (IORef Int) -> Event (IORef Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef Int) -> Event (IORef Int))
-> IO (IORef Int) -> Event (IORef Int)
forall a b. (a -> b) -> a -> b
$ Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
     IORef Int
cl <- IO (IORef Int) -> Event (IORef Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef Int) -> Event (IORef Int))
-> IO (IORef Int) -> Event (IORef Int)
forall a b. (a -> b) -> a -> b
$ Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
     IORef Int
cm <- IO (IORef Int) -> Event (IORef Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef Int) -> Event (IORef Int))
-> IO (IORef Int) -> Event (IORef Int)
forall a b. (a -> b) -> a -> b
$ Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
     IORef Int
cr <- IO (IORef Int) -> Event (IORef Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef Int) -> Event (IORef Int))
-> IO (IORef Int) -> Event (IORef Int)
forall a b. (a -> b) -> a -> b
$ Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
     IORef Int
co <- IO (IORef Int) -> Event (IORef Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef Int) -> Event (IORef Int))
-> IO (IORef Int) -> Event (IORef Int)
forall a b. (a -> b) -> a -> b
$ Int -> IO (IORef Int)
forall a. a -> IO (IORef a)
newIORef Int
0
     Resource si
ri <- Simulation (Resource si) -> Event (Resource si)
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (Resource si) -> Event (Resource si))
-> Simulation (Resource si) -> Event (Resource si)
forall a b. (a -> b) -> a -> b
$ si -> Int -> Maybe Int -> Simulation (Resource si)
forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Simulation (Resource s)
newResourceWithMaxCount si
si Int
count (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
count)
     StrategyQueue sm (QueueItem a)
qm <- Simulation (StrategyQueue sm (QueueItem a))
-> Event (StrategyQueue sm (QueueItem a))
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (StrategyQueue sm (QueueItem a))
 -> Event (StrategyQueue sm (QueueItem a)))
-> Simulation (StrategyQueue sm (QueueItem a))
-> Event (StrategyQueue sm (QueueItem a))
forall a b. (a -> b) -> a -> b
$ sm -> Simulation (StrategyQueue sm (QueueItem a))
forall s i. QueueStrategy s => s -> Simulation (StrategyQueue s i)
newStrategyQueue sm
sm
     Resource so
ro <- Simulation (Resource so) -> Event (Resource so)
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (Resource so) -> Event (Resource so))
-> Simulation (Resource so) -> Event (Resource so)
forall a b. (a -> b) -> a -> b
$ so -> Int -> Maybe Int -> Simulation (Resource so)
forall s.
QueueStrategy s =>
s -> Int -> Maybe Int -> Simulation (Resource s)
newResourceWithMaxCount so
so Int
0 (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
count)
     IORef (SamplingStats Double)
w  <- IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef (SamplingStats Double))
 -> Event (IORef (SamplingStats Double)))
-> IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> IO (IORef (SamplingStats Double))
forall a. a -> IO (IORef a)
newIORef SamplingStats Double
forall a. Monoid a => a
mempty
     IORef (SamplingStats Double)
wt <- IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef (SamplingStats Double))
 -> Event (IORef (SamplingStats Double)))
-> IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> IO (IORef (SamplingStats Double))
forall a. a -> IO (IORef a)
newIORef SamplingStats Double
forall a. Monoid a => a
mempty
     IORef (SamplingStats Double)
wi <- IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef (SamplingStats Double))
 -> Event (IORef (SamplingStats Double)))
-> IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> IO (IORef (SamplingStats Double))
forall a. a -> IO (IORef a)
newIORef SamplingStats Double
forall a. Monoid a => a
mempty
     IORef (SamplingStats Double)
wo <- IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (IORef (SamplingStats Double))
 -> Event (IORef (SamplingStats Double)))
-> IO (IORef (SamplingStats Double))
-> Event (IORef (SamplingStats Double))
forall a b. (a -> b) -> a -> b
$ SamplingStats Double -> IO (IORef (SamplingStats Double))
forall a. a -> IO (IORef a)
newIORef SamplingStats Double
forall a. Monoid a => a
mempty 
     SignalSource a
s1 <- Simulation (SignalSource a) -> Event (SignalSource a)
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (SignalSource a) -> Event (SignalSource a))
-> Simulation (SignalSource a) -> Event (SignalSource a)
forall a b. (a -> b) -> a -> b
$ Simulation (SignalSource a)
forall a. Simulation (SignalSource a)
newSignalSource
     SignalSource a
s2 <- Simulation (SignalSource a) -> Event (SignalSource a)
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (SignalSource a) -> Event (SignalSource a))
-> Simulation (SignalSource a) -> Event (SignalSource a)
forall a b. (a -> b) -> a -> b
$ Simulation (SignalSource a)
forall a. Simulation (SignalSource a)
newSignalSource
     SignalSource a
s3 <- Simulation (SignalSource a) -> Event (SignalSource a)
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (SignalSource a) -> Event (SignalSource a))
-> Simulation (SignalSource a) -> Event (SignalSource a)
forall a b. (a -> b) -> a -> b
$ Simulation (SignalSource a)
forall a. Simulation (SignalSource a)
newSignalSource
     SignalSource ()
s4 <- Simulation (SignalSource ()) -> Event (SignalSource ())
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (SignalSource ()) -> Event (SignalSource ()))
-> Simulation (SignalSource ()) -> Event (SignalSource ())
forall a b. (a -> b) -> a -> b
$ Simulation (SignalSource ())
forall a. Simulation (SignalSource a)
newSignalSource
     SignalSource a
s5 <- Simulation (SignalSource a) -> Event (SignalSource a)
forall (m :: * -> *) a. SimulationLift m => Simulation a -> m a
liftSimulation (Simulation (SignalSource a) -> Event (SignalSource a))
-> Simulation (SignalSource a) -> Event (SignalSource a)
forall a b. (a -> b) -> a -> b
$ Simulation (SignalSource a)
forall a. Simulation (SignalSource a)
newSignalSource
     Queue si sm so a -> Event (Queue si sm so a)
forall (m :: * -> *) a. Monad m => a -> m a
return Queue :: forall si sm so a.
Int
-> si
-> sm
-> so
-> Resource si
-> StrategyQueue sm (QueueItem a)
-> Resource so
-> IORef Int
-> IORef (TimingStats Int)
-> IORef Int
-> IORef Int
-> IORef Int
-> IORef Int
-> IORef Int
-> IORef (SamplingStats Double)
-> IORef (SamplingStats Double)
-> IORef (SamplingStats Double)
-> IORef (SamplingStats Double)
-> SignalSource a
-> SignalSource a
-> SignalSource a
-> SignalSource ()
-> SignalSource a
-> Queue si sm so a
Queue { queueMaxCount :: Int
queueMaxCount = Int
count,
                    enqueueStrategy :: si
enqueueStrategy = si
si,
                    enqueueStoringStrategy :: sm
enqueueStoringStrategy = sm
sm,
                    dequeueStrategy :: so
dequeueStrategy = so
so,
                    enqueueRes :: Resource si
enqueueRes = Resource si
ri,
                    queueStore :: StrategyQueue sm (QueueItem a)
queueStore = StrategyQueue sm (QueueItem a)
qm,
                    dequeueRes :: Resource so
dequeueRes = Resource so
ro,
                    queueCountRef :: IORef Int
queueCountRef = IORef Int
i,
                    queueCountStatsRef :: IORef (TimingStats Int)
queueCountStatsRef = IORef (TimingStats Int)
is,
                    enqueueCountRef :: IORef Int
enqueueCountRef = IORef Int
ci,
                    enqueueLostCountRef :: IORef Int
enqueueLostCountRef = IORef Int
cl,
                    enqueueStoreCountRef :: IORef Int
enqueueStoreCountRef = IORef Int
cm,
                    dequeueCountRef :: IORef Int
dequeueCountRef = IORef Int
cr,
                    dequeueExtractCountRef :: IORef Int
dequeueExtractCountRef = IORef Int
co,
                    queueWaitTimeRef :: IORef (SamplingStats Double)
queueWaitTimeRef = IORef (SamplingStats Double)
w,
                    queueTotalWaitTimeRef :: IORef (SamplingStats Double)
queueTotalWaitTimeRef = IORef (SamplingStats Double)
wt,
                    enqueueWaitTimeRef :: IORef (SamplingStats Double)
enqueueWaitTimeRef = IORef (SamplingStats Double)
wi,
                    dequeueWaitTimeRef :: IORef (SamplingStats Double)
dequeueWaitTimeRef = IORef (SamplingStats Double)
wo,
                    enqueueInitiatedSource :: SignalSource a
enqueueInitiatedSource = SignalSource a
s1,
                    enqueueLostSource :: SignalSource a
enqueueLostSource = SignalSource a
s2,
                    enqueueStoredSource :: SignalSource a
enqueueStoredSource = SignalSource a
s3,
                    dequeueRequestedSource :: SignalSource ()
dequeueRequestedSource = SignalSource ()
s4,
                    dequeueExtractedSource :: SignalSource a
dequeueExtractedSource = SignalSource a
s5 }
  
-- | Test whether the queue is empty.
--
-- See also 'queueNullChanged' and 'queueNullChanged_'.
queueNull :: Queue si sm so a -> Event Bool
queueNull :: Queue si sm so a -> Event Bool
queueNull Queue si sm so a
q =
  (Point -> IO Bool) -> Event Bool
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Bool) -> Event Bool)
-> (Point -> IO Bool) -> Event Bool
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
n <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
     Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0)
  
-- | Signal when the 'queueNull' property value has changed.
queueNullChanged :: Queue si sm so a -> Signal Bool
queueNullChanged :: Queue si sm so a -> Signal Bool
queueNullChanged Queue si sm so a
q =
  (() -> Event Bool) -> Signal () -> Signal Bool
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Bool -> () -> Event Bool
forall a b. a -> b -> a
const (Event Bool -> () -> Event Bool) -> Event Bool -> () -> Event Bool
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Bool
forall si sm so a. Queue si sm so a -> Event Bool
queueNull Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueNullChanged_ Queue si sm so a
q)
  
-- | Signal when the 'queueNull' property value has changed.
queueNullChanged_ :: Queue si sm so a -> Signal ()
queueNullChanged_ :: Queue si sm so a -> Signal ()
queueNullChanged_ = Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueCountChanged_

-- | Test whether the queue is full.
--
-- See also 'queueFullChanged' and 'queueFullChanged_'.
queueFull :: Queue si sm so a -> Event Bool
queueFull :: Queue si sm so a -> Event Bool
queueFull Queue si sm so a
q =
  (Point -> IO Bool) -> Event Bool
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Bool) -> Event Bool)
-> (Point -> IO Bool) -> Event Bool
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
n <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
     Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Queue si sm so a -> Int
forall si sm so a. Queue si sm so a -> Int
queueMaxCount Queue si sm so a
q)
  
-- | Signal when the 'queueFull' property value has changed.
queueFullChanged :: Queue si sm so a -> Signal Bool
queueFullChanged :: Queue si sm so a -> Signal Bool
queueFullChanged Queue si sm so a
q =
  (() -> Event Bool) -> Signal () -> Signal Bool
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Bool -> () -> Event Bool
forall a b. a -> b -> a
const (Event Bool -> () -> Event Bool) -> Event Bool -> () -> Event Bool
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Bool
forall si sm so a. Queue si sm so a -> Event Bool
queueFull Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueFullChanged_ Queue si sm so a
q)
  
-- | Signal when the 'queueFull' property value has changed.
queueFullChanged_ :: Queue si sm so a -> Signal ()
queueFullChanged_ :: Queue si sm so a -> Signal ()
queueFullChanged_ = Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueCountChanged_

-- | Return the current queue size.
--
-- See also 'queueCountStats', 'queueCountChanged' and 'queueCountChanged_'.
queueCount :: Queue si sm so a -> Event Int
queueCount :: Queue si sm so a -> Event Int
queueCount Queue si sm so a
q =
  (Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)

-- | Return the queue size statistics.
queueCountStats :: Queue si sm so a -> Event (TimingStats Int)
queueCountStats :: Queue si sm so a -> Event (TimingStats Int)
queueCountStats Queue si sm so a
q =
  (Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (TimingStats Int)) -> Event (TimingStats Int))
-> (Point -> IO (TimingStats Int)) -> Event (TimingStats Int)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (TimingStats Int) -> IO (TimingStats Int)
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef (TimingStats Int)
forall si sm so a. Queue si sm so a -> IORef (TimingStats Int)
queueCountStatsRef Queue si sm so a
q)
  
-- | Signal when the 'queueCount' property value has changed.
queueCountChanged :: Queue si sm so a -> Signal Int
queueCountChanged :: Queue si sm so a -> Signal Int
queueCountChanged Queue si sm so a
q =
  (() -> Event Int) -> Signal () -> Signal Int
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Int -> () -> Event Int
forall a b. a -> b -> a
const (Event Int -> () -> Event Int) -> Event Int -> () -> Event Int
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
queueCount Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueCountChanged_ Queue si sm so a
q)
  
-- | Signal when the 'queueCount' property value has changed.
queueCountChanged_ :: Queue si sm so a -> Signal ()
queueCountChanged_ :: Queue si sm so a -> Signal ()
queueCountChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueStored Queue si sm so a
q) Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)

-- | Return the total number of input items that were enqueued.
--
-- See also 'enqueueCountChanged' and 'enqueueCountChanged_'.
enqueueCount :: Queue si sm so a -> Event Int
enqueueCount :: Queue si sm so a -> Event Int
enqueueCount Queue si sm so a
q =
  (Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueCountRef Queue si sm so a
q)
  
-- | Signal when the 'enqueueCount' property value has changed.
enqueueCountChanged :: Queue si sm so a -> Signal Int
enqueueCountChanged :: Queue si sm so a -> Signal Int
enqueueCountChanged Queue si sm so a
q =
  (() -> Event Int) -> Signal () -> Signal Int
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Int -> () -> Event Int
forall a b. a -> b -> a
const (Event Int -> () -> Event Int) -> Event Int -> () -> Event Int
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
enqueueCount Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
enqueueCountChanged_ Queue si sm so a
q)
  
-- | Signal when the 'enqueueCount' property value has changed.
enqueueCountChanged_ :: Queue si sm so a -> Signal ()
enqueueCountChanged_ :: Queue si sm so a -> Signal ()
enqueueCountChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueInitiated Queue si sm so a
q)
  
-- | Return the number of lost items.
--
-- See also 'enqueueLostCountChanged' and 'enqueueLostCountChanged_'.
enqueueLostCount :: Queue si sm so a -> Event Int
enqueueLostCount :: Queue si sm so a -> Event Int
enqueueLostCount Queue si sm so a
q =
  (Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueLostCountRef Queue si sm so a
q)
  
-- | Signal when the 'enqueueLostCount' property value has changed.
enqueueLostCountChanged :: Queue si sm so a -> Signal Int
enqueueLostCountChanged :: Queue si sm so a -> Signal Int
enqueueLostCountChanged Queue si sm so a
q =
  (() -> Event Int) -> Signal () -> Signal Int
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Int -> () -> Event Int
forall a b. a -> b -> a
const (Event Int -> () -> Event Int) -> Event Int -> () -> Event Int
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
enqueueLostCount Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
enqueueLostCountChanged_ Queue si sm so a
q)
  
-- | Signal when the 'enqueueLostCount' property value has changed.
enqueueLostCountChanged_ :: Queue si sm so a -> Signal ()
enqueueLostCountChanged_ :: Queue si sm so a -> Signal ()
enqueueLostCountChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueLost Queue si sm so a
q)
      
-- | Return the total number of input items that were stored.
--
-- See also 'enqueueStoreCountChanged' and 'enqueueStoreCountChanged_'.
enqueueStoreCount :: Queue si sm so a -> Event Int
enqueueStoreCount :: Queue si sm so a -> Event Int
enqueueStoreCount Queue si sm so a
q =
  (Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueStoreCountRef Queue si sm so a
q)
  
-- | Signal when the 'enqueueStoreCount' property value has changed.
enqueueStoreCountChanged :: Queue si sm so a -> Signal Int
enqueueStoreCountChanged :: Queue si sm so a -> Signal Int
enqueueStoreCountChanged Queue si sm so a
q =
  (() -> Event Int) -> Signal () -> Signal Int
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Int -> () -> Event Int
forall a b. a -> b -> a
const (Event Int -> () -> Event Int) -> Event Int -> () -> Event Int
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
enqueueStoreCount Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
enqueueStoreCountChanged_ Queue si sm so a
q)
  
-- | Signal when the 'enqueueStoreCount' property value has changed.
enqueueStoreCountChanged_ :: Queue si sm so a -> Signal ()
enqueueStoreCountChanged_ :: Queue si sm so a -> Signal ()
enqueueStoreCountChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueStored Queue si sm so a
q)
      
-- | Return the total number of requests for dequeueing the items,
-- not taking into account the failed attempts to dequeue immediately
-- without suspension.
--
-- See also 'dequeueCountChanged' and 'dequeueCountChanged_'.
dequeueCount :: Queue si sm so a -> Event Int
dequeueCount :: Queue si sm so a -> Event Int
dequeueCount Queue si sm so a
q =
  (Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueCountRef Queue si sm so a
q)
      
-- | Signal when the 'dequeueCount' property value has changed.
dequeueCountChanged :: Queue si sm so a -> Signal Int
dequeueCountChanged :: Queue si sm so a -> Signal Int
dequeueCountChanged Queue si sm so a
q =
  (() -> Event Int) -> Signal () -> Signal Int
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Int -> () -> Event Int
forall a b. a -> b -> a
const (Event Int -> () -> Event Int) -> Event Int -> () -> Event Int
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
dequeueCount Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
dequeueCountChanged_ Queue si sm so a
q)
  
-- | Signal when the 'dequeueCount' property value has changed.
dequeueCountChanged_ :: Queue si sm so a -> Signal ()
dequeueCountChanged_ :: Queue si sm so a -> Signal ()
dequeueCountChanged_ Queue si sm so a
q =
  (() -> ()) -> Signal () -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> () -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
dequeueRequested Queue si sm so a
q)
      
-- | Return the total number of output items that were actually dequeued.
--
-- See also 'dequeueExtractCountChanged' and 'dequeueExtractCountChanged_'.
dequeueExtractCount :: Queue si sm so a -> Event Int
dequeueExtractCount :: Queue si sm so a -> Event Int
dequeueExtractCount Queue si sm so a
q =
  (Point -> IO Int) -> Event Int
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Int) -> Event Int) -> (Point -> IO Int) -> Event Int
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueExtractCountRef Queue si sm so a
q)
      
-- | Signal when the 'dequeueExtractCount' property value has changed.
dequeueExtractCountChanged :: Queue si sm so a -> Signal Int
dequeueExtractCountChanged :: Queue si sm so a -> Signal Int
dequeueExtractCountChanged Queue si sm so a
q =
  (() -> Event Int) -> Signal () -> Signal Int
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Int -> () -> Event Int
forall a b. a -> b -> a
const (Event Int -> () -> Event Int) -> Event Int -> () -> Event Int
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
dequeueExtractCount Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
dequeueExtractCountChanged_ Queue si sm so a
q)
  
-- | Signal when the 'dequeueExtractCount' property value has changed.
dequeueExtractCountChanged_ :: Queue si sm so a -> Signal ()
dequeueExtractCountChanged_ :: Queue si sm so a -> Signal ()
dequeueExtractCountChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)

-- | Return the load factor: the queue size divided by its maximum size.
--
-- See also 'queueLoadFactorChanged' and 'queueLoadFactorChanged_'.
queueLoadFactor :: Queue si sm so a -> Event Double
queueLoadFactor :: Queue si sm so a -> Event Double
queueLoadFactor Queue si sm so a
q =
  (Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
x <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
     let y :: Int
y = Queue si sm so a -> Int
forall si sm so a. Queue si sm so a -> Int
queueMaxCount Queue si sm so a
q
     Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
y)
      
-- | Signal when the 'queueLoadFactor' property value has changed.
queueLoadFactorChanged :: Queue si sm so a -> Signal Double
queueLoadFactorChanged :: Queue si sm so a -> Signal Double
queueLoadFactorChanged Queue si sm so a
q =
  (() -> Event Double) -> Signal () -> Signal Double
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Double -> () -> Event Double
forall a b. a -> b -> a
const (Event Double -> () -> Event Double)
-> Event Double -> () -> Event Double
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
queueLoadFactor Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueLoadFactorChanged_ Queue si sm so a
q)
  
-- | Signal when the 'queueLoadFactor' property value has changed.
queueLoadFactorChanged_ :: Queue si sm so a -> Signal ()
queueLoadFactorChanged_ :: Queue si sm so a -> Signal ()
queueLoadFactorChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueStored Queue si sm so a
q) Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)
      
-- | Return the rate of the input items that were enqueued: how many items
-- per time.
enqueueRate :: Queue si sm so a -> Event Double
enqueueRate :: Queue si sm so a -> Event Double
enqueueRate Queue si sm so a
q =
  (Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
x <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueCountRef Queue si sm so a
q)
     let t0 :: Double
t0 = Specs -> Double
spcStartTime (Specs -> Double) -> Specs -> Double
forall a b. (a -> b) -> a -> b
$ Point -> Specs
pointSpecs Point
p
         t :: Double
t  = Point -> Double
pointTime Point
p
     Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
      
-- | Return the rate of the items that were stored: how many items
-- per time.
enqueueStoreRate :: Queue si sm so a -> Event Double
enqueueStoreRate :: Queue si sm so a -> Event Double
enqueueStoreRate Queue si sm so a
q =
  (Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
x <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueStoreCountRef Queue si sm so a
q)
     let t0 :: Double
t0 = Specs -> Double
spcStartTime (Specs -> Double) -> Specs -> Double
forall a b. (a -> b) -> a -> b
$ Point -> Specs
pointSpecs Point
p
         t :: Double
t  = Point -> Double
pointTime Point
p
     Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
      
-- | Return the rate of the requests for dequeueing the items: how many requests
-- per time. It does not include the failed attempts to dequeue immediately
-- without suspension.
dequeueRate :: Queue si sm so a -> Event Double
dequeueRate :: Queue si sm so a -> Event Double
dequeueRate Queue si sm so a
q =
  (Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
x <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueCountRef Queue si sm so a
q)
     let t0 :: Double
t0 = Specs -> Double
spcStartTime (Specs -> Double) -> Specs -> Double
forall a b. (a -> b) -> a -> b
$ Point -> Specs
pointSpecs Point
p
         t :: Double
t  = Point -> Double
pointTime Point
p
     Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
      
-- | Return the rate of the output items that were actually dequeued: how many items
-- per time.
dequeueExtractRate :: Queue si sm so a -> Event Double
dequeueExtractRate :: Queue si sm so a -> Event Double
dequeueExtractRate Queue si sm so a
q =
  (Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
x <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueExtractCountRef Queue si sm so a
q)
     let t0 :: Double
t0 = Specs -> Double
spcStartTime (Specs -> Double) -> Specs -> Double
forall a b. (a -> b) -> a -> b
$ Point -> Specs
pointSpecs Point
p
         t :: Double
t  = Point -> Double
pointTime Point
p
     Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0))
      
-- | Return the wait time from the time at which the item was stored in the queue to
-- the time at which it was dequeued.
--
-- See also 'queueWaitTimeChanged' and 'queueWaitTimeChanged_'.
queueWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
queueWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
queueWaitTime Queue si sm so a
q =
  (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (SamplingStats Double))
 -> Event (SamplingStats Double))
-> (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (SamplingStats Double) -> IO (SamplingStats Double)
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
queueWaitTimeRef Queue si sm so a
q)
      
-- | Signal when the 'queueWaitTime' property value has changed.
queueWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
queueWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
queueWaitTimeChanged Queue si sm so a
q =
  (() -> Event (SamplingStats Double))
-> Signal () -> Signal (SamplingStats Double)
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event (SamplingStats Double) -> () -> Event (SamplingStats Double)
forall a b. a -> b -> a
const (Event (SamplingStats Double)
 -> () -> Event (SamplingStats Double))
-> Event (SamplingStats Double)
-> ()
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
queueWaitTime Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueWaitTimeChanged_ Queue si sm so a
q)
  
-- | Signal when the 'queueWaitTime' property value has changed.
queueWaitTimeChanged_ :: Queue si sm so a -> Signal ()
queueWaitTimeChanged_ :: Queue si sm so a -> Signal ()
queueWaitTimeChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)
      
-- | Return the total wait time from the time at which the enqueueing operation
-- was initiated to the time at which the item was dequeued.
--
-- In some sense, @queueTotalWaitTime == enqueueWaitTime + queueWaitTime@.
--
-- See also 'queueTotalWaitTimeChanged' and 'queueTotalWaitTimeChanged_'.
queueTotalWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
queueTotalWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
queueTotalWaitTime Queue si sm so a
q =
  (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (SamplingStats Double))
 -> Event (SamplingStats Double))
-> (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (SamplingStats Double) -> IO (SamplingStats Double)
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
queueTotalWaitTimeRef Queue si sm so a
q)
      
-- | Signal when the 'queueTotalWaitTime' property value has changed.
queueTotalWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
queueTotalWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
queueTotalWaitTimeChanged Queue si sm so a
q =
  (() -> Event (SamplingStats Double))
-> Signal () -> Signal (SamplingStats Double)
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event (SamplingStats Double) -> () -> Event (SamplingStats Double)
forall a b. a -> b -> a
const (Event (SamplingStats Double)
 -> () -> Event (SamplingStats Double))
-> Event (SamplingStats Double)
-> ()
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
queueTotalWaitTime Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueTotalWaitTimeChanged_ Queue si sm so a
q)
  
-- | Signal when the 'queueTotalWaitTime' property value has changed.
queueTotalWaitTimeChanged_ :: Queue si sm so a -> Signal ()
queueTotalWaitTimeChanged_ :: Queue si sm so a -> Signal ()
queueTotalWaitTimeChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)
      
-- | Return the enqueue wait time from the time at which the enqueueing operation
-- was initiated to the time at which the item was stored in the queue.
--
-- See also 'enqueueWaitTimeChanged' and 'enqueueWaitTimeChanged_'.
enqueueWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
enqueueWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
enqueueWaitTime Queue si sm so a
q =
  (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (SamplingStats Double))
 -> Event (SamplingStats Double))
-> (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (SamplingStats Double) -> IO (SamplingStats Double)
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
enqueueWaitTimeRef Queue si sm so a
q)
      
-- | Signal when the 'enqueueWaitTime' property value has changed.
enqueueWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
enqueueWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
enqueueWaitTimeChanged Queue si sm so a
q =
  (() -> Event (SamplingStats Double))
-> Signal () -> Signal (SamplingStats Double)
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event (SamplingStats Double) -> () -> Event (SamplingStats Double)
forall a b. a -> b -> a
const (Event (SamplingStats Double)
 -> () -> Event (SamplingStats Double))
-> Event (SamplingStats Double)
-> ()
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
enqueueWaitTime Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
enqueueWaitTimeChanged_ Queue si sm so a
q)
  
-- | Signal when the 'enqueueWaitTime' property value has changed.
enqueueWaitTimeChanged_ :: Queue si sm so a -> Signal ()
enqueueWaitTimeChanged_ :: Queue si sm so a -> Signal ()
enqueueWaitTimeChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueStored Queue si sm so a
q)
      
-- | Return the dequeue wait time from the time at which the item was requested
-- for dequeueing to the time at which it was actually dequeued.
--
-- See also 'dequeueWaitTimeChanged' and 'dequeueWaitTimeChanged_'.
dequeueWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
dequeueWaitTime :: Queue si sm so a -> Event (SamplingStats Double)
dequeueWaitTime Queue si sm so a
q =
  (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (SamplingStats Double))
 -> Event (SamplingStats Double))
-> (Point -> IO (SamplingStats Double))
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ \Point
p -> IORef (SamplingStats Double) -> IO (SamplingStats Double)
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
dequeueWaitTimeRef Queue si sm so a
q)
      
-- | Signal when the 'dequeueWaitTime' property value has changed.
dequeueWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
dequeueWaitTimeChanged :: Queue si sm so a -> Signal (SamplingStats Double)
dequeueWaitTimeChanged Queue si sm so a
q =
  (() -> Event (SamplingStats Double))
-> Signal () -> Signal (SamplingStats Double)
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event (SamplingStats Double) -> () -> Event (SamplingStats Double)
forall a b. a -> b -> a
const (Event (SamplingStats Double)
 -> () -> Event (SamplingStats Double))
-> Event (SamplingStats Double)
-> ()
-> Event (SamplingStats Double)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
dequeueWaitTime Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
dequeueWaitTimeChanged_ Queue si sm so a
q)
  
-- | Signal when the 'dequeueWaitTime' property value has changed.
dequeueWaitTimeChanged_ :: Queue si sm so a -> Signal ()
dequeueWaitTimeChanged_ :: Queue si sm so a -> Signal ()
dequeueWaitTimeChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)

-- | Return a long-term average queue rate calculated as
-- the average queue size divided by the average wait time.
--
-- This value may be less than the actual arrival rate as the queue is
-- finite and new arrivals may be locked while the queue remains full.
--
-- See also 'queueRateChanged' and 'queueRateChanged_'.
queueRate :: Queue si sm so a -> Event Double
queueRate :: Queue si sm so a -> Event Double
queueRate Queue si sm so a
q =
  (Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do TimingStats Int
x <- IORef (TimingStats Int) -> IO (TimingStats Int)
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef (TimingStats Int)
forall si sm so a. Queue si sm so a -> IORef (TimingStats Int)
queueCountStatsRef Queue si sm so a
q)
     SamplingStats Double
y <- IORef (SamplingStats Double) -> IO (SamplingStats Double)
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
queueWaitTimeRef Queue si sm so a
q)
     Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (TimingStats Int -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsMean TimingStats Int
x Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ SamplingStats Double -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats Double
y) 
      
-- | Signal when the 'queueRate' property value has changed.
queueRateChanged :: Queue si sm so a -> Signal Double
queueRateChanged :: Queue si sm so a -> Signal Double
queueRateChanged Queue si sm so a
q =
  (() -> Event Double) -> Signal () -> Signal Double
forall a b. (a -> Event b) -> Signal a -> Signal b
mapSignalM (Event Double -> () -> Event Double
forall a b. a -> b -> a
const (Event Double -> () -> Event Double)
-> Event Double -> () -> Event Double
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
queueRate Queue si sm so a
q) (Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
queueRateChanged_ Queue si sm so a
q)
      
-- | Signal when the 'queueRate' property value has changed.
queueRateChanged_ :: Queue si sm so a -> Signal ()
queueRateChanged_ :: Queue si sm so a -> Signal ()
queueRateChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueStored Queue si sm so a
q) Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)

-- | Dequeue suspending the process if the queue is empty.
dequeue :: (DequeueStrategy si,
            DequeueStrategy sm,
            EnqueueStrategy so)
           => Queue si sm so a
           -- ^ the queue
           -> Process a
           -- ^ the dequeued value
dequeue :: Queue si sm so a -> Process a
dequeue Queue si sm so a
q =
  do Double
t <- Event Double -> Process Double
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event Double -> Process Double) -> Event Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
dequeueRequest Queue si sm so a
q
     Resource so -> Process ()
forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource (Queue si sm so a -> Resource so
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
     Event a -> Process a
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event a -> Process a) -> Event a -> Process a
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Double -> Event a
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Double -> Event a
dequeueExtract Queue si sm so a
q Double
t
  
-- | Dequeue with the output priority suspending the process if the queue is empty.
dequeueWithOutputPriority :: (DequeueStrategy si,
                              DequeueStrategy sm,
                              PriorityQueueStrategy so po)
                             => Queue si sm so a
                             -- ^ the queue
                             -> po
                             -- ^ the priority for output
                             -> Process a
                             -- ^ the dequeued value
dequeueWithOutputPriority :: Queue si sm so a -> po -> Process a
dequeueWithOutputPriority Queue si sm so a
q po
po =
  do Double
t <- Event Double -> Process Double
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event Double -> Process Double) -> Event Double -> Process Double
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
dequeueRequest Queue si sm so a
q
     Resource so -> po -> Process ()
forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority (Queue si sm so a -> Resource so
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q) po
po
     Event a -> Process a
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event a -> Process a) -> Event a -> Process a
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Double -> Event a
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Double -> Event a
dequeueExtract Queue si sm so a
q Double
t
  
-- | Try to dequeue immediately.
tryDequeue :: (DequeueStrategy si,
               DequeueStrategy sm)
              => Queue si sm so a
              -- ^ the queue
              -> Event (Maybe a)
              -- ^ the dequeued value of 'Nothing'
tryDequeue :: Queue si sm so a -> Event (Maybe a)
tryDequeue Queue si sm so a
q =
  do Bool
x <- Resource so -> Event Bool
forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (Queue si sm so a -> Resource so
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
     if Bool
x 
       then do Double
t <- Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
dequeueRequest Queue si sm so a
q
               (a -> Maybe a) -> Event a -> Event (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (Event a -> Event (Maybe a)) -> Event a -> Event (Maybe a)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Double -> Event a
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Double -> Event a
dequeueExtract Queue si sm so a
q Double
t
       else Maybe a -> Event (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing

-- | Remove the item from the queue and return a flag indicating
-- whether the item was found and actually removed.
queueDelete :: (Eq a,
                DequeueStrategy si,
                DeletingQueueStrategy sm,
                DequeueStrategy so)
               => Queue si sm so a
               -- ^ the queue
               -> a
               -- ^ the item to remove from the queue
               -> Event Bool
               -- ^ whether the item was found and removed
queueDelete :: Queue si sm so a -> a -> Event Bool
queueDelete Queue si sm so a
q a
a = (Maybe a -> Bool) -> Event (Maybe a) -> Event Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe a -> Bool
forall a. Maybe a -> Bool
isJust (Event (Maybe a) -> Event Bool) -> Event (Maybe a) -> Event Bool
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
 DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a)

-- | Remove the specified item from the queue.
queueDelete_ :: (Eq a,
                 DequeueStrategy si,
                 DeletingQueueStrategy sm,
                 DequeueStrategy so)
                => Queue si sm so a
                -- ^ the queue
                -> a
                -- ^ the item to remove from the queue
                -> Event ()
queueDelete_ :: Queue si sm so a -> a -> Event ()
queueDelete_ Queue si sm so a
q a
a = (Maybe a -> ()) -> Event (Maybe a) -> Event ()
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> Maybe a -> ()
forall a b. a -> b -> a
const ()) (Event (Maybe a) -> Event ()) -> Event (Maybe a) -> Event ()
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
 DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a)

-- | Remove an item satisfying the specified predicate and return the item if found.
queueDeleteBy :: (DequeueStrategy si,
                  DeletingQueueStrategy sm,
                  DequeueStrategy so)
                 => Queue si sm so a
                 -- ^ the queue
                 -> (a -> Bool)
                 -- ^ the predicate
                 -> Event (Maybe a)
queueDeleteBy :: Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q a -> Bool
pred =
  do Bool
x <- Resource so -> Event Bool
forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (Queue si sm so a -> Resource so
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
     if Bool
x
       then do Maybe (QueueItem a)
i <- StrategyQueue sm (QueueItem a)
-> (QueueItem a -> Bool) -> Event (Maybe (QueueItem a))
forall s i.
DeletingQueueStrategy s =>
StrategyQueue s i -> (i -> Bool) -> Event (Maybe i)
strategyQueueDeleteBy (Queue si sm so a -> StrategyQueue sm (QueueItem a)
forall si sm so a.
Queue si sm so a -> StrategyQueue sm (QueueItem a)
queueStore Queue si sm so a
q) (a -> Bool
pred (a -> Bool) -> (QueueItem a -> a) -> QueueItem a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QueueItem a -> a
forall a. QueueItem a -> a
itemValue)
               case Maybe (QueueItem a)
i of
                 Maybe (QueueItem a)
Nothing ->
                   do Resource so -> Event ()
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (Queue si sm so a -> Resource so
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
                      Maybe a -> Event (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
                 Just QueueItem a
i ->
                   do Double
t <- Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
dequeueRequest Queue si sm so a
q
                      (a -> Maybe a) -> Event a -> Event (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just (Event a -> Event (Maybe a)) -> Event a -> Event (Maybe a)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> Double -> QueueItem a -> Event a
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Double -> QueueItem a -> Event a
dequeuePostExtract Queue si sm so a
q Double
t QueueItem a
i
       else Maybe a -> Event (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
               
-- | Remove an item satisfying the specified predicate.
queueDeleteBy_ :: (DequeueStrategy si,
                   DeletingQueueStrategy sm,
                   DequeueStrategy so)
                  => Queue si sm so a
                  -- ^ the queue
                  -> (a -> Bool)
                  -- ^ the predicate
                  -> Event ()
queueDeleteBy_ :: Queue si sm so a -> (a -> Bool) -> Event ()
queueDeleteBy_ Queue si sm so a
q a -> Bool
pred = (Maybe a -> ()) -> Event (Maybe a) -> Event ()
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (() -> Maybe a -> ()
forall a b. a -> b -> a
const ()) (Event (Maybe a) -> Event ()) -> Event (Maybe a) -> Event ()
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
forall si sm so a.
(DequeueStrategy si, DeletingQueueStrategy sm,
 DequeueStrategy so) =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueDeleteBy Queue si sm so a
q a -> Bool
pred

-- | Detect whether the item is contained in the queue.
queueContains :: (Eq a,
                  DeletingQueueStrategy sm)
                 => Queue si sm so a
                 -- ^ the queue
                 -> a
                 -- ^ the item to search the queue for
                 -> Event Bool
                 -- ^ whether the item was found
queueContains :: Queue si sm so a -> a -> Event Bool
queueContains Queue si sm so a
q a
a = (Maybe a -> Bool) -> Event (Maybe a) -> Event Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe a -> Bool
forall a. Maybe a -> Bool
isJust (Event (Maybe a) -> Event Bool) -> Event (Maybe a) -> Event Bool
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
forall sm si so a.
DeletingQueueStrategy sm =>
Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueContainsBy Queue si sm so a
q (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
a)

-- | Detect whether an item satisfying the specified predicate is contained in the queue.
queueContainsBy :: DeletingQueueStrategy sm
                   => Queue si sm so a
                   -- ^ the queue
                   -> (a -> Bool)
                   -- ^ the predicate
                   -> Event (Maybe a)
                   -- ^ the item if it was found
queueContainsBy :: Queue si sm so a -> (a -> Bool) -> Event (Maybe a)
queueContainsBy Queue si sm so a
q a -> Bool
pred =
  do Maybe (QueueItem a)
x <- StrategyQueue sm (QueueItem a)
-> (QueueItem a -> Bool) -> Event (Maybe (QueueItem a))
forall s i.
DeletingQueueStrategy s =>
StrategyQueue s i -> (i -> Bool) -> Event (Maybe i)
strategyQueueContainsBy (Queue si sm so a -> StrategyQueue sm (QueueItem a)
forall si sm so a.
Queue si sm so a -> StrategyQueue sm (QueueItem a)
queueStore Queue si sm so a
q) (a -> Bool
pred (a -> Bool) -> (QueueItem a -> a) -> QueueItem a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QueueItem a -> a
forall a. QueueItem a -> a
itemValue)
     case Maybe (QueueItem a)
x of
       Maybe (QueueItem a)
Nothing -> Maybe a -> Event (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
       Just QueueItem a
i  -> Maybe a -> Event (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> Event (Maybe a)) -> Maybe a -> Event (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just (QueueItem a -> a
forall a. QueueItem a -> a
itemValue QueueItem a
i)

-- | Clear the queue immediately.
clearQueue :: (DequeueStrategy si,
               DequeueStrategy sm)
              => Queue si sm so a
              -- ^ the queue
              -> Event ()
clearQueue :: Queue si sm so a -> Event ()
clearQueue Queue si sm so a
q =
  do Maybe a
x <- Queue si sm so a -> Event (Maybe a)
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event (Maybe a)
tryDequeue Queue si sm so a
q
     case Maybe a
x of
       Maybe a
Nothing -> () -> Event ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
       Just a
a  -> Queue si sm so a -> Event ()
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Event ()
clearQueue Queue si sm so a
q
              
-- | Enqueue the item suspending the process if the queue is full.  
enqueue :: (EnqueueStrategy si,
            EnqueueStrategy sm,
            DequeueStrategy so)
           => Queue si sm so a
           -- ^ the queue
           -> a
           -- ^ the item to enqueue
           -> Process ()
enqueue :: Queue si sm so a -> a -> Process ()
enqueue Queue si sm so a
q a
a =
  do QueueItem a
i <- Event (QueueItem a) -> Process (QueueItem a)
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event (QueueItem a) -> Process (QueueItem a))
-> Event (QueueItem a) -> Process (QueueItem a)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a
     Resource si -> Process ()
forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
     Event () -> Process ()
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event () -> Process ()) -> Event () -> Process ()
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> QueueItem a -> Event ()
forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> QueueItem a -> Event ()
enqueueStore Queue si sm so a
q QueueItem a
i
     
-- | Enqueue with the input priority the item suspending the process if the queue is full.  
enqueueWithInputPriority :: (PriorityQueueStrategy si pi,
                             EnqueueStrategy sm,
                             DequeueStrategy so)
                            => Queue si sm so a
                            -- ^ the queue
                            -> pi
                            -- ^ the priority for input
                            -> a
                            -- ^ the item to enqueue
                            -> Process ()
enqueueWithInputPriority :: Queue si sm so a -> pi -> a -> Process ()
enqueueWithInputPriority Queue si sm so a
q pi
pi a
a =
  do QueueItem a
i <- Event (QueueItem a) -> Process (QueueItem a)
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event (QueueItem a) -> Process (QueueItem a))
-> Event (QueueItem a) -> Process (QueueItem a)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a
     Resource si -> pi -> Process ()
forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q) pi
pi
     Event () -> Process ()
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event () -> Process ()) -> Event () -> Process ()
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> QueueItem a -> Event ()
forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> QueueItem a -> Event ()
enqueueStore Queue si sm so a
q QueueItem a
i
     
-- | Enqueue with the storing priority the item suspending the process if the queue is full.  
enqueueWithStoringPriority :: (EnqueueStrategy si,
                               PriorityQueueStrategy sm pm,
                               DequeueStrategy so)
                              => Queue si sm so a
                              -- ^ the queue
                              -> pm
                              -- ^ the priority for storing
                              -> a
                              -- ^ the item to enqueue
                              -> Process ()
enqueueWithStoringPriority :: Queue si sm so a -> pm -> a -> Process ()
enqueueWithStoringPriority Queue si sm so a
q pm
pm a
a =
  do QueueItem a
i <- Event (QueueItem a) -> Process (QueueItem a)
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event (QueueItem a) -> Process (QueueItem a))
-> Event (QueueItem a) -> Process (QueueItem a)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a
     Resource si -> Process ()
forall s. EnqueueStrategy s => Resource s -> Process ()
requestResource (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
     Event () -> Process ()
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event () -> Process ()) -> Event () -> Process ()
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> pm -> QueueItem a -> Event ()
forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> QueueItem a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm QueueItem a
i
     
-- | Enqueue with the input and storing priorities the item suspending the process if the queue is full.  
enqueueWithInputStoringPriorities :: (PriorityQueueStrategy si pi,
                                      PriorityQueueStrategy sm pm,
                                      DequeueStrategy so)
                                     => Queue si sm so a
                                     -- ^ the queue
                                     -> pi
                                     -- ^ the priority for input
                                     -> pm
                                     -- ^ the priority for storing
                                     -> a
                                     -- ^ the item to enqueue
                                     -> Process ()
enqueueWithInputStoringPriorities :: Queue si sm so a -> pi -> pm -> a -> Process ()
enqueueWithInputStoringPriorities Queue si sm so a
q pi
pi pm
pm a
a =
  do QueueItem a
i <- Event (QueueItem a) -> Process (QueueItem a)
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event (QueueItem a) -> Process (QueueItem a))
-> Event (QueueItem a) -> Process (QueueItem a)
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a
     Resource si -> pi -> Process ()
forall s p.
PriorityQueueStrategy s p =>
Resource s -> p -> Process ()
requestResourceWithPriority (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q) pi
pi
     Event () -> Process ()
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Event () -> Process ()) -> Event () -> Process ()
forall a b. (a -> b) -> a -> b
$ Queue si sm so a -> pm -> QueueItem a -> Event ()
forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> QueueItem a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm QueueItem a
i
     
-- | Try to enqueue the item. Return 'False' in the monad if the queue is full.
tryEnqueue :: (EnqueueStrategy sm,
               DequeueStrategy so)
              => Queue si sm so a
              -- ^ the queue
              -> a
              -- ^ the item which we try to enqueue
              -> Event Bool
tryEnqueue :: Queue si sm so a -> a -> Event Bool
tryEnqueue Queue si sm so a
q a
a =
  do Bool
x <- Resource si -> Event Bool
forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
     if Bool
x 
       then do Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a Event (QueueItem a) -> (QueueItem a -> Event ()) -> Event ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Queue si sm so a -> QueueItem a -> Event ()
forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> QueueItem a -> Event ()
enqueueStore Queue si sm so a
q
               Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
       else Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

-- | Try to enqueue with the storing priority the item. Return 'False' in
-- the monad if the queue is full.
tryEnqueueWithStoringPriority :: (PriorityQueueStrategy sm pm,
                                  DequeueStrategy so)
                                 => Queue si sm so a
                                 -- ^ the queue
                                 -> pm
                                 -- ^ the priority for storing
                                 -> a
                                 -- ^ the item which we try to enqueue
                                 -> Event Bool
tryEnqueueWithStoringPriority :: Queue si sm so a -> pm -> a -> Event Bool
tryEnqueueWithStoringPriority Queue si sm so a
q pm
pm a
a =
  do Bool
x <- Resource si -> Event Bool
forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
     if Bool
x 
       then do Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a Event (QueueItem a) -> (QueueItem a -> Event ()) -> Event ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Queue si sm so a -> pm -> QueueItem a -> Event ()
forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> QueueItem a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm
               Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
       else Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

-- | Try to enqueue the item. If the queue is full then the item will be lost
-- and 'False' will be returned.
enqueueOrLost :: (EnqueueStrategy sm,
                  DequeueStrategy so)
                 => Queue si sm so a
                 -- ^ the queue
                 -> a
                 -- ^ the item which we try to enqueue
                 -> Event Bool
enqueueOrLost :: Queue si sm so a -> a -> Event Bool
enqueueOrLost Queue si sm so a
q a
a =
  do Bool
x <- Resource si -> Event Bool
forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
     if Bool
x
       then do Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a Event (QueueItem a) -> (QueueItem a -> Event ()) -> Event ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Queue si sm so a -> QueueItem a -> Event ()
forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> QueueItem a -> Event ()
enqueueStore Queue si sm so a
q
               Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
       else do Queue si sm so a -> a -> Event ()
forall si sm so a. Queue si sm so a -> a -> Event ()
enqueueDeny Queue si sm so a
q a
a
               Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

-- | Try to enqueue with the storing priority the item. If the queue is full
-- then the item will be lost and 'False' will be returned.
enqueueWithStoringPriorityOrLost :: (PriorityQueueStrategy sm pm,
                                     DequeueStrategy so)
                                    => Queue si sm so a
                                    -- ^ the queue
                                    -> pm
                                    -- ^ the priority for storing
                                    -> a
                                    -- ^ the item which we try to enqueue
                                    -> Event Bool
enqueueWithStoringPriorityOrLost :: Queue si sm so a -> pm -> a -> Event Bool
enqueueWithStoringPriorityOrLost Queue si sm so a
q pm
pm a
a =
  do Bool
x <- Resource si -> Event Bool
forall s. Resource s -> Event Bool
tryRequestResourceWithinEvent (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
     if Bool
x
       then do Queue si sm so a -> a -> Event (QueueItem a)
forall si sm so a. Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a Event (QueueItem a) -> (QueueItem a -> Event ()) -> Event ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Queue si sm so a -> pm -> QueueItem a -> Event ()
forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> QueueItem a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm
               Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
       else do Queue si sm so a -> a -> Event ()
forall si sm so a. Queue si sm so a -> a -> Event ()
enqueueDeny Queue si sm so a
q a
a
               Bool -> Event Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

-- | Try to enqueue the item. If the queue is full then the item will be lost.
enqueueOrLost_ :: (EnqueueStrategy sm,
                   DequeueStrategy so)
                  => Queue si sm so a
                  -- ^ the queue
                  -> a
                  -- ^ the item which we try to enqueue
                  -> Event ()
enqueueOrLost_ :: Queue si sm so a -> a -> Event ()
enqueueOrLost_ Queue si sm so a
q a
a =
  do Bool
x <- Queue si sm so a -> a -> Event Bool
forall sm so si a.
(EnqueueStrategy sm, DequeueStrategy so) =>
Queue si sm so a -> a -> Event Bool
enqueueOrLost Queue si sm so a
q a
a
     () -> Event ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Try to enqueue with the storing priority the item. If the queue is full
-- then the item will be lost.
enqueueWithStoringPriorityOrLost_ :: (PriorityQueueStrategy sm pm,
                                      DequeueStrategy so)
                                     => Queue si sm so a
                                     -- ^ the queue
                                     -> pm
                                     -- ^ the priority for storing
                                     -> a
                                     -- ^ the item which we try to enqueue
                                     -> Event ()
enqueueWithStoringPriorityOrLost_ :: Queue si sm so a -> pm -> a -> Event ()
enqueueWithStoringPriorityOrLost_ Queue si sm so a
q pm
pm a
a =
  do Bool
x <- Queue si sm so a -> pm -> a -> Event Bool
forall sm pm so si a.
(PriorityQueueStrategy sm pm, DequeueStrategy so) =>
Queue si sm so a -> pm -> a -> Event Bool
enqueueWithStoringPriorityOrLost Queue si sm so a
q pm
pm a
a
     () -> Event ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Return a signal that notifies when the enqueuing operation is initiated.
enqueueInitiated :: Queue si sm so a -> Signal a
enqueueInitiated :: Queue si sm so a -> Signal a
enqueueInitiated Queue si sm so a
q = SignalSource a -> Signal a
forall a. SignalSource a -> Signal a
publishSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
enqueueInitiatedSource Queue si sm so a
q)

-- | Return a signal that notifies when the enqueuing operation is completed
-- and the item is stored in the internal memory of the queue.
enqueueStored :: Queue si sm so a -> Signal a
enqueueStored :: Queue si sm so a -> Signal a
enqueueStored Queue si sm so a
q = SignalSource a -> Signal a
forall a. SignalSource a -> Signal a
publishSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
enqueueStoredSource Queue si sm so a
q)

-- | Return a signal which notifies that the item was lost when 
-- attempting to add it to the full queue with help of
-- 'enqueueOrLost', 'enqueueOrLost_' or similar functions that imply
-- that the element can be lost. All their names are ending with @OrLost@
-- or @OrLost_@.
--
-- In other cases the enqueued items are not lost but the corresponded process
-- can suspend until the internal queue storage is freed. Although there is one
-- exception from this rule. If the process trying to enqueue a new element was
-- suspended but then canceled through 'cancelProcess' from the outside then
-- the item will not be added.
enqueueLost :: Queue si sm so a -> Signal a
enqueueLost :: Queue si sm so a -> Signal a
enqueueLost Queue si sm so a
q = SignalSource a -> Signal a
forall a. SignalSource a -> Signal a
publishSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
enqueueLostSource Queue si sm so a
q)

-- | Return a signal that notifies when the dequeuing operation was requested.
dequeueRequested :: Queue si sm so a -> Signal ()
dequeueRequested :: Queue si sm so a -> Signal ()
dequeueRequested Queue si sm so a
q = SignalSource () -> Signal ()
forall a. SignalSource a -> Signal a
publishSignal (Queue si sm so a -> SignalSource ()
forall si sm so a. Queue si sm so a -> SignalSource ()
dequeueRequestedSource Queue si sm so a
q)

-- | Return a signal that notifies when the item was extracted from the internal
-- storage of the queue and prepared for immediate receiving by the dequeuing process.
dequeueExtracted :: Queue si sm so a -> Signal a
dequeueExtracted :: Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q = SignalSource a -> Signal a
forall a. SignalSource a -> Signal a
publishSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
dequeueExtractedSource Queue si sm so a
q)

-- | Initiate the process of enqueuing the item.
enqueueInitiate :: Queue si sm so a
                   -- ^ the queue
                   -> a
                   -- ^ the item to be enqueued
                   -> Event (QueueItem a)
enqueueInitiate :: Queue si sm so a -> a -> Event (QueueItem a)
enqueueInitiate Queue si sm so a
q a
a =
  (Point -> IO (QueueItem a)) -> Event (QueueItem a)
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO (QueueItem a)) -> Event (QueueItem a))
-> (Point -> IO (QueueItem a)) -> Event (QueueItem a)
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let t :: Double
t = Point -> Double
pointTime Point
p
     IORef Int -> (Int -> Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueCountRef Queue si sm so a
q) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       SignalSource a -> a -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
enqueueInitiatedSource Queue si sm so a
q) a
a
     QueueItem a -> IO (QueueItem a)
forall (m :: * -> *) a. Monad m => a -> m a
return QueueItem :: forall a. a -> Double -> Double -> QueueItem a
QueueItem { itemValue :: a
itemValue = a
a,
                        itemInputTime :: Double
itemInputTime = Double
t,
                        itemStoringTime :: Double
itemStoringTime = Double
t  -- it will be updated soon
                      }

-- | Store the item.
enqueueStore :: (EnqueueStrategy sm,
                 DequeueStrategy so)
                => Queue si sm so a
                -- ^ the queue
                -> QueueItem a
                -- ^ the item to be stored
                -> Event ()
enqueueStore :: Queue si sm so a -> QueueItem a -> Event ()
enqueueStore Queue si sm so a
q QueueItem a
i =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let i' :: QueueItem a
i' = QueueItem a
i { itemStoringTime :: Double
itemStoringTime = Point -> Double
pointTime Point
p }  -- now we have the actual time of storing
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       StrategyQueue sm (QueueItem a) -> QueueItem a -> Event ()
forall s i. EnqueueStrategy s => StrategyQueue s i -> i -> Event ()
strategyEnqueue (Queue si sm so a -> StrategyQueue sm (QueueItem a)
forall si sm so a.
Queue si sm so a -> StrategyQueue sm (QueueItem a)
queueStore Queue si sm so a
q) QueueItem a
i'
     Int
c <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
     let c' :: Int
c' = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
         t :: Double
t  = Point -> Double
pointTime Point
p 
     Int
c' Int -> IO () -> IO ()
`seq` IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q) Int
c'
     IORef (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef (TimingStats Int)
forall si sm so a. Queue si sm so a -> IORef (TimingStats Int)
queueCountStatsRef Queue si sm so a
q) (Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     IORef Int -> (Int -> Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueStoreCountRef Queue si sm so a
q) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       Queue si sm so a -> QueueItem a -> Event ()
forall si sm so a. Queue si sm so a -> QueueItem a -> Event ()
enqueueStat Queue si sm so a
q QueueItem a
i'
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       Resource so -> Event ()
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (Queue si sm so a -> Resource so
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       SignalSource a -> a -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
enqueueStoredSource Queue si sm so a
q) (QueueItem a -> a
forall a. QueueItem a -> a
itemValue QueueItem a
i')

-- | Store with the priority the item.
enqueueStoreWithPriority :: (PriorityQueueStrategy sm pm,
                             DequeueStrategy so)
                            => Queue si sm so a
                            -- ^ the queue
                            -> pm
                            -- ^ the priority for storing
                            -> QueueItem a
                            -- ^ the item to be enqueued
                            -> Event ()
enqueueStoreWithPriority :: Queue si sm so a -> pm -> QueueItem a -> Event ()
enqueueStoreWithPriority Queue si sm so a
q pm
pm QueueItem a
i =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let i' :: QueueItem a
i' = QueueItem a
i { itemStoringTime :: Double
itemStoringTime = Point -> Double
pointTime Point
p }  -- now we have the actual time of storing
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       StrategyQueue sm (QueueItem a) -> pm -> QueueItem a -> Event ()
forall s p i.
PriorityQueueStrategy s p =>
StrategyQueue s i -> p -> i -> Event ()
strategyEnqueueWithPriority (Queue si sm so a -> StrategyQueue sm (QueueItem a)
forall si sm so a.
Queue si sm so a -> StrategyQueue sm (QueueItem a)
queueStore Queue si sm so a
q) pm
pm QueueItem a
i'
     Int
c <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
     let c' :: Int
c' = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
         t :: Double
t  = Point -> Double
pointTime Point
p
     Int
c' Int -> IO () -> IO ()
`seq` IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q) Int
c'
     IORef (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef (TimingStats Int)
forall si sm so a. Queue si sm so a -> IORef (TimingStats Int)
queueCountStatsRef Queue si sm so a
q) (Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     IORef Int -> (Int -> Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueStoreCountRef Queue si sm so a
q) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       Queue si sm so a -> QueueItem a -> Event ()
forall si sm so a. Queue si sm so a -> QueueItem a -> Event ()
enqueueStat Queue si sm so a
q QueueItem a
i'
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       Resource so -> Event ()
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (Queue si sm so a -> Resource so
forall si sm so a. Queue si sm so a -> Resource so
dequeueRes Queue si sm so a
q)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       SignalSource a -> a -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
enqueueStoredSource Queue si sm so a
q) (QueueItem a -> a
forall a. QueueItem a -> a
itemValue QueueItem a
i')

-- | Deny the enqueuing.
enqueueDeny :: Queue si sm so a
               -- ^ the queue
               -> a
               -- ^ the item to be denied
               -> Event ()
enqueueDeny :: Queue si sm so a -> a -> Event ()
enqueueDeny Queue si sm so a
q a
a =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do IORef Int -> (Int -> Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueLostCountRef Queue si sm so a
q) ((Int -> Int) -> IO ()) -> (Int -> Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Num a => a -> a -> a
(+) Int
1
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       SignalSource a -> a -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
enqueueLostSource Queue si sm so a
q) a
a

-- | Update the statistics for the input wait time of the enqueuing operation.
enqueueStat :: Queue si sm so a
               -- ^ the queue
               -> QueueItem a
               -- ^ the item and its input time
               -> Event ()
               -- ^ the action of updating the statistics
enqueueStat :: Queue si sm so a -> QueueItem a -> Event ()
enqueueStat Queue si sm so a
q QueueItem a
i =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let t0 :: Double
t0 = QueueItem a -> Double
forall a. QueueItem a -> Double
itemInputTime QueueItem a
i
         t1 :: Double
t1 = QueueItem a -> Double
forall a. QueueItem a -> Double
itemStoringTime QueueItem a
i
     IORef (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
enqueueWaitTimeRef Queue si sm so a
q) ((SamplingStats Double -> SamplingStats Double) -> IO ())
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a b. (a -> b) -> a -> b
$
       Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)

-- | Accept the dequeuing request and return the current simulation time.
dequeueRequest :: Queue si sm so a
                 -- ^ the queue
                 -> Event Double
                 -- ^ the current time
dequeueRequest :: Queue si sm so a -> Event Double
dequeueRequest Queue si sm so a
q =
  (Point -> IO Double) -> Event Double
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO Double) -> Event Double)
-> (Point -> IO Double) -> Event Double
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do IORef Int -> (Int -> Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueCountRef Queue si sm so a
q) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       SignalSource () -> () -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Queue si sm so a -> SignalSource ()
forall si sm so a. Queue si sm so a -> SignalSource ()
dequeueRequestedSource Queue si sm so a
q) ()
     Double -> IO Double
forall (m :: * -> *) a. Monad m => a -> m a
return (Double -> IO Double) -> Double -> IO Double
forall a b. (a -> b) -> a -> b
$ Point -> Double
pointTime Point
p 

-- | Extract an item for the dequeuing request.  
dequeueExtract :: (DequeueStrategy si,
                   DequeueStrategy sm)
                  => Queue si sm so a
                  -- ^ the queue
                  -> Double
                  -- ^ the time of the dequeuing request
                  -> Event a
                  -- ^ the dequeued value
dequeueExtract :: Queue si sm so a -> Double -> Event a
dequeueExtract Queue si sm so a
q Double
t' =
  (Point -> IO a) -> Event a
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO a) -> Event a) -> (Point -> IO a) -> Event a
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do QueueItem a
i <- Point -> Event (QueueItem a) -> IO (QueueItem a)
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event (QueueItem a) -> IO (QueueItem a))
-> Event (QueueItem a) -> IO (QueueItem a)
forall a b. (a -> b) -> a -> b
$
          StrategyQueue sm (QueueItem a) -> Event (QueueItem a)
forall s i. DequeueStrategy s => StrategyQueue s i -> Event i
strategyDequeue (Queue si sm so a -> StrategyQueue sm (QueueItem a)
forall si sm so a.
Queue si sm so a -> StrategyQueue sm (QueueItem a)
queueStore Queue si sm so a
q)
     Point -> Event a -> IO a
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event a -> IO a) -> Event a -> IO a
forall a b. (a -> b) -> a -> b
$
       Queue si sm so a -> Double -> QueueItem a -> Event a
forall si sm so a.
(DequeueStrategy si, DequeueStrategy sm) =>
Queue si sm so a -> Double -> QueueItem a -> Event a
dequeuePostExtract Queue si sm so a
q Double
t' QueueItem a
i

-- | A post action after extracting the item by the dequeuing request.  
dequeuePostExtract :: (DequeueStrategy si,
                       DequeueStrategy sm)
                      => Queue si sm so a
                      -- ^ the queue
                      -> Double
                      -- ^ the time of the dequeuing request
                      -> QueueItem a
                      -- ^ the item to dequeue
                      -> Event a
                      -- ^ the dequeued value
dequeuePostExtract :: Queue si sm so a -> Double -> QueueItem a -> Event a
dequeuePostExtract Queue si sm so a
q Double
t' QueueItem a
i =
  (Point -> IO a) -> Event a
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO a) -> Event a) -> (Point -> IO a) -> Event a
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do Int
c <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
     let c' :: Int
c' = Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
         t :: Double
t  = Point -> Double
pointTime Point
p
     Int
c' Int -> IO () -> IO ()
`seq` IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q) Int
c'
     IORef (TimingStats Int)
-> (TimingStats Int -> TimingStats Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef (TimingStats Int)
forall si sm so a. Queue si sm so a -> IORef (TimingStats Int)
queueCountStatsRef Queue si sm so a
q) (Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t Int
c')
     IORef Int -> (Int -> Int) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueExtractCountRef Queue si sm so a
q) (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       Queue si sm so a -> Double -> QueueItem a -> Event ()
forall si sm so a.
Queue si sm so a -> Double -> QueueItem a -> Event ()
dequeueStat Queue si sm so a
q Double
t' QueueItem a
i
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       Resource si -> Event ()
forall s. DequeueStrategy s => Resource s -> Event ()
releaseResourceWithinEvent (Queue si sm so a -> Resource si
forall si sm so a. Queue si sm so a -> Resource si
enqueueRes Queue si sm so a
q)
     Point -> Event () -> IO ()
forall a. Point -> Event a -> IO a
invokeEvent Point
p (Event () -> IO ()) -> Event () -> IO ()
forall a b. (a -> b) -> a -> b
$
       SignalSource a -> a -> Event ()
forall a. SignalSource a -> a -> Event ()
triggerSignal (Queue si sm so a -> SignalSource a
forall si sm so a. Queue si sm so a -> SignalSource a
dequeueExtractedSource Queue si sm so a
q) (QueueItem a -> a
forall a. QueueItem a -> a
itemValue QueueItem a
i)
     a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> IO a) -> a -> IO a
forall a b. (a -> b) -> a -> b
$ QueueItem a -> a
forall a. QueueItem a -> a
itemValue QueueItem a
i

-- | Update the statistics for the output wait time of the dequeuing operation
-- and the wait time of storing in the queue.
dequeueStat :: Queue si sm so a
               -- ^ the queue
               -> Double
               -- ^ the time of the dequeuing request
               -> QueueItem a
               -- ^ the item and its input time
               -> Event ()
               -- ^ the action of updating the statistics
dequeueStat :: Queue si sm so a -> Double -> QueueItem a -> Event ()
dequeueStat Queue si sm so a
q Double
t' QueueItem a
i =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let t0 :: Double
t0 = QueueItem a -> Double
forall a. QueueItem a -> Double
itemInputTime QueueItem a
i
         t1 :: Double
t1 = QueueItem a -> Double
forall a. QueueItem a -> Double
itemStoringTime QueueItem a
i
         t :: Double
t  = Point -> Double
pointTime Point
p
     IORef (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
dequeueWaitTimeRef Queue si sm so a
q) ((SamplingStats Double -> SamplingStats Double) -> IO ())
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a b. (a -> b) -> a -> b
$
       Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t')
     IORef (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
queueTotalWaitTimeRef Queue si sm so a
q) ((SamplingStats Double -> SamplingStats Double) -> IO ())
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a b. (a -> b) -> a -> b
$
       Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
     IORef (SamplingStats Double)
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef' (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
queueWaitTimeRef Queue si sm so a
q) ((SamplingStats Double -> SamplingStats Double) -> IO ())
-> (SamplingStats Double -> SamplingStats Double) -> IO ()
forall a b. (a -> b) -> a -> b
$
       Double -> SamplingStats Double -> SamplingStats Double
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t1)

-- | Wait while the queue is full.
waitWhileFullQueue :: Queue si sm so a -> Process ()
waitWhileFullQueue :: Queue si sm so a -> Process ()
waitWhileFullQueue Queue si sm so a
q =
  do Bool
x <- Event Bool -> Process Bool
forall (m :: * -> *) a. EventLift m => Event a -> m a
liftEvent (Queue si sm so a -> Event Bool
forall si sm so a. Queue si sm so a -> Event Bool
queueFull Queue si sm so a
q)
     Bool -> Process () -> Process ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
x (Process () -> Process ()) -> Process () -> Process ()
forall a b. (a -> b) -> a -> b
$
       do Signal a -> Process a
forall a. Signal a -> Process a
processAwait (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)
          Queue si sm so a -> Process ()
forall si sm so a. Queue si sm so a -> Process ()
waitWhileFullQueue Queue si sm so a
q

-- | Signal whenever any property of the queue changes.
--
-- The property must have the corresponded signal. There are also characteristics
-- similar to the properties but that have no signals. As a rule, such characteristics
-- already depend on the simulation time and therefore they may change at any
-- time point.
queueChanged_ :: Queue si sm so a -> Signal ()
queueChanged_ :: Queue si sm so a -> Signal ()
queueChanged_ Queue si sm so a
q =
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueInitiated Queue si sm so a
q) Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueStored Queue si sm so a
q) Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
enqueueLost Queue si sm so a
q) Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
  Queue si sm so a -> Signal ()
forall si sm so a. Queue si sm so a -> Signal ()
dequeueRequested Queue si sm so a
q Signal () -> Signal () -> Signal ()
forall a. Semigroup a => a -> a -> a
<>
  (a -> ()) -> Signal a -> Signal ()
forall a b. (a -> b) -> Signal a -> Signal b
mapSignal (() -> a -> ()
forall a b. a -> b -> a
const ()) (Queue si sm so a -> Signal a
forall si sm so a. Queue si sm so a -> Signal a
dequeueExtracted Queue si sm so a
q)

-- | Return the summary for the queue with desciption of its
-- properties and activities using the specified indent.
queueSummary :: (Show si, Show sm, Show so) => Queue si sm so a -> Int -> Event ShowS
queueSummary :: Queue si sm so a -> Int -> Event ShowS
queueSummary Queue si sm so a
q Int
indent =
  do let si :: si
si = Queue si sm so a -> si
forall si sm so a. Queue si sm so a -> si
enqueueStrategy Queue si sm so a
q
         sm :: sm
sm = Queue si sm so a -> sm
forall si sm so a. Queue si sm so a -> sm
enqueueStoringStrategy Queue si sm so a
q
         so :: so
so = Queue si sm so a -> so
forall si sm so a. Queue si sm so a -> so
dequeueStrategy Queue si sm so a
q
     Bool
null <- Queue si sm so a -> Event Bool
forall si sm so a. Queue si sm so a -> Event Bool
queueNull Queue si sm so a
q
     Bool
full <- Queue si sm so a -> Event Bool
forall si sm so a. Queue si sm so a -> Event Bool
queueFull Queue si sm so a
q
     let maxCount :: Int
maxCount = Queue si sm so a -> Int
forall si sm so a. Queue si sm so a -> Int
queueMaxCount Queue si sm so a
q
     Int
count <- Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
queueCount Queue si sm so a
q
     TimingStats Int
countStats <- Queue si sm so a -> Event (TimingStats Int)
forall si sm so a. Queue si sm so a -> Event (TimingStats Int)
queueCountStats Queue si sm so a
q
     Int
enqueueCount <- Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
enqueueCount Queue si sm so a
q
     Int
enqueueLostCount <- Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
enqueueLostCount Queue si sm so a
q
     Int
enqueueStoreCount <- Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
enqueueStoreCount Queue si sm so a
q
     Int
dequeueCount <- Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
dequeueCount Queue si sm so a
q
     Int
dequeueExtractCount <- Queue si sm so a -> Event Int
forall si sm so a. Queue si sm so a -> Event Int
dequeueExtractCount Queue si sm so a
q
     Double
loadFactor <- Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
queueLoadFactor Queue si sm so a
q
     Double
enqueueRate <- Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
enqueueRate Queue si sm so a
q
     Double
enqueueStoreRate <- Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
enqueueStoreRate Queue si sm so a
q
     Double
dequeueRate <- Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
dequeueRate Queue si sm so a
q
     Double
dequeueExtractRate <- Queue si sm so a -> Event Double
forall si sm so a. Queue si sm so a -> Event Double
dequeueExtractRate Queue si sm so a
q
     SamplingStats Double
waitTime <- Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
queueWaitTime Queue si sm so a
q
     SamplingStats Double
totalWaitTime <- Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
queueTotalWaitTime Queue si sm so a
q
     SamplingStats Double
enqueueWaitTime <- Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
enqueueWaitTime Queue si sm so a
q
     SamplingStats Double
dequeueWaitTime <- Queue si sm so a -> Event (SamplingStats Double)
forall si sm so a. Queue si sm so a -> Event (SamplingStats Double)
dequeueWaitTime Queue si sm so a
q
     let tab :: [Char]
tab = Int -> Char -> [Char]
forall a. Int -> a -> [a]
replicate Int
indent Char
' '
     ShowS -> Event ShowS
forall (m :: * -> *) a. Monad m => a -> m a
return (ShowS -> Event ShowS) -> ShowS -> Event ShowS
forall a b. (a -> b) -> a -> b
$
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueueing (input) strategy = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       si -> ShowS
forall a. Show a => a -> ShowS
shows si
si ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the storing (memory) strategy = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       sm -> ShowS
forall a. Show a => a -> ShowS
shows sm
sm ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeueing (output) strategy = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       so -> ShowS
forall a. Show a => a -> ShowS
shows so
so ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"empty? = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Bool -> ShowS
forall a. Show a => a -> ShowS
shows Bool
null ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"full? = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Bool -> ShowS
forall a. Show a => a -> ShowS
shows Bool
full ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"max. capacity = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
maxCount ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"size = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
count ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the size statistics = \n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       TimingStats Int -> Int -> ShowS
forall a. (Show a, TimingData a) => TimingStats a -> Int -> ShowS
timingStatsSummary TimingStats Int
countStats (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue count (number of the input items that were enqueued) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
enqueueCount ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue lost count (number of the lost items) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
enqueueLostCount ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue store count (number of the input items that were stored) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
enqueueStoreCount ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue count (number of requests for dequeueing an item) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
dequeueCount ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue extract count (number of the output items that were dequeued) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
dequeueExtractCount ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the load factor (size / max. capacity) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
loadFactor ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue rate (how many input items were enqueued per time) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
enqueueRate ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue store rate (how many input items were stored per time) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
enqueueStoreRate ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue rate (how many requests for dequeueing per time) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
dequeueRate ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue extract rate (how many output items were dequeued per time) = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       Double -> ShowS
forall a. Show a => a -> ShowS
shows Double
dequeueExtractRate ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the wait time (when was stored -> when was dequeued) = \n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       SamplingStats Double -> Int -> ShowS
forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
waitTime (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the total wait time (when the enqueueing was initiated -> when was dequeued) = \n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       SamplingStats Double -> Int -> ShowS
forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
totalWaitTime (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the enqueue wait time (when the enqueueing was initiated -> when was stored) = \n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       SamplingStats Double -> Int -> ShowS
forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
enqueueWaitTime (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"\n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       [Char] -> ShowS
showString [Char]
"the dequeue wait time (when was requested for dequeueing -> when was dequeued) = \n\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
       SamplingStats Double -> Int -> ShowS
forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats Double
dequeueWaitTime (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
indent)

-- | Reset the statistics.
resetQueue :: Queue si sm so a -> Event ()
resetQueue :: Queue si sm so a -> Event ()
resetQueue Queue si sm so a
q =
  (Point -> IO ()) -> Event ()
forall a. (Point -> IO a) -> Event a
Event ((Point -> IO ()) -> Event ()) -> (Point -> IO ()) -> Event ()
forall a b. (a -> b) -> a -> b
$ \Point
p ->
  do let t :: Double
t = Point -> Double
pointTime Point
p
     Int
queueCount <- IORef Int -> IO Int
forall a. IORef a -> IO a
readIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
queueCountRef Queue si sm so a
q)
     IORef (TimingStats Int) -> TimingStats Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef (TimingStats Int)
forall si sm so a. Queue si sm so a -> IORef (TimingStats Int)
queueCountStatsRef Queue si sm so a
q) (TimingStats Int -> IO ()) -> TimingStats Int -> IO ()
forall a b. (a -> b) -> a -> b
$
       Double -> Int -> TimingStats Int
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t Int
queueCount
     IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueCountRef Queue si sm so a
q) Int
0
     IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueLostCountRef Queue si sm so a
q) Int
0
     IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
enqueueStoreCountRef Queue si sm so a
q) Int
0
     IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueCountRef Queue si sm so a
q) Int
0
     IORef Int -> Int -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef Int
forall si sm so a. Queue si sm so a -> IORef Int
dequeueExtractCountRef Queue si sm so a
q) Int
0
     IORef (SamplingStats Double) -> SamplingStats Double -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
queueWaitTimeRef Queue si sm so a
q) SamplingStats Double
forall a. Monoid a => a
mempty
     IORef (SamplingStats Double) -> SamplingStats Double -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
queueTotalWaitTimeRef Queue si sm so a
q) SamplingStats Double
forall a. Monoid a => a
mempty
     IORef (SamplingStats Double) -> SamplingStats Double -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
enqueueWaitTimeRef Queue si sm so a
q) SamplingStats Double
forall a. Monoid a => a
mempty
     IORef (SamplingStats Double) -> SamplingStats Double -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef (Queue si sm so a -> IORef (SamplingStats Double)
forall si sm so a. Queue si sm so a -> IORef (SamplingStats Double)
dequeueWaitTimeRef Queue si sm so a
q) SamplingStats Double
forall a. Monoid a => a
mempty