{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
module Simulation.Aivika.IO.QueueStrategy () where
import Control.Monad.Trans
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Parameter
import Simulation.Aivika.Trans.Parameter.Random
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.QueueStrategy
import Simulation.Aivika.IO.Comp
import qualified Simulation.Aivika.DoubleLinkedList as LL
import qualified Simulation.Aivika.PriorityQueue as PQ
import qualified Simulation.Aivika.Vector as V
instance QueueStrategy IO FCFS where
{-# SPECIALISE instance QueueStrategy IO FCFS #-}
newtype StrategyQueue IO FCFS a = FCFSQueue (LL.DoubleLinkedList a)
{-# INLINABLE newStrategyQueue #-}
newStrategyQueue :: FCFS -> Simulation IO (StrategyQueue IO FCFS a)
newStrategyQueue FCFS
s =
(DoubleLinkedList a -> StrategyQueue IO FCFS a)
-> Simulation IO (DoubleLinkedList a)
-> Simulation IO (StrategyQueue IO FCFS a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DoubleLinkedList a -> StrategyQueue IO FCFS a
forall a. DoubleLinkedList a -> StrategyQueue IO FCFS a
FCFSQueue (Simulation IO (DoubleLinkedList a)
-> Simulation IO (StrategyQueue IO FCFS a))
-> Simulation IO (DoubleLinkedList a)
-> Simulation IO (StrategyQueue IO FCFS a)
forall a b. (a -> b) -> a -> b
$
IO (DoubleLinkedList a) -> Simulation IO (DoubleLinkedList a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (DoubleLinkedList a)
forall a. IO (DoubleLinkedList a)
LL.newList
{-# INLINABLE strategyQueueNull #-}
strategyQueueNull :: StrategyQueue IO FCFS a -> Event IO Bool
strategyQueueNull (FCFSQueue q) =
IO Bool -> Event IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> Event IO Bool) -> IO Bool -> Event IO Bool
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> IO Bool
forall a. DoubleLinkedList a -> IO Bool
LL.listNull DoubleLinkedList a
q
instance DequeueStrategy IO FCFS where
{-# SPECIALISE instance DequeueStrategy IO FCFS #-}
{-# INLINABLE strategyDequeue #-}
strategyDequeue :: StrategyQueue IO FCFS a -> Event IO a
strategyDequeue (FCFSQueue q) =
IO a -> Event IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> Event IO a) -> IO a -> Event IO a
forall a b. (a -> b) -> a -> b
$
do a
i <- DoubleLinkedList a -> IO a
forall a. DoubleLinkedList a -> IO a
LL.listFirst DoubleLinkedList a
q
DoubleLinkedList a -> IO ()
forall a. DoubleLinkedList a -> IO ()
LL.listRemoveFirst DoubleLinkedList a
q
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i
instance EnqueueStrategy IO FCFS where
{-# SPECIALISE instance EnqueueStrategy IO FCFS #-}
{-# INLINABLE strategyEnqueue #-}
strategyEnqueue :: StrategyQueue IO FCFS a -> a -> Event IO ()
strategyEnqueue (FCFSQueue q) a
i =
IO () -> Event IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Event IO ()) -> IO () -> Event IO ()
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> a -> IO ()
forall a. DoubleLinkedList a -> a -> IO ()
LL.listAddLast DoubleLinkedList a
q a
i
instance DeletingQueueStrategy IO FCFS where
{-# SPECIALISE instance DeletingQueueStrategy IO FCFS #-}
{-# INLINABLE strategyQueueDeleteBy #-}
strategyQueueDeleteBy :: StrategyQueue IO FCFS a -> (a -> Bool) -> Event IO (Maybe a)
strategyQueueDeleteBy (FCFSQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
forall a. DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
LL.listRemoveBy DoubleLinkedList a
q a -> Bool
p
{-# INLINABLE strategyQueueContainsBy #-}
strategyQueueContainsBy :: StrategyQueue IO FCFS a -> (a -> Bool) -> Event IO (Maybe a)
strategyQueueContainsBy (FCFSQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
forall a. DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
LL.listContainsBy DoubleLinkedList a
q a -> Bool
p
instance QueueStrategy IO LCFS where
{-# SPECIALISE instance QueueStrategy IO LCFS #-}
newtype StrategyQueue IO LCFS a = LCFSQueue (LL.DoubleLinkedList a)
{-# INLINABLE newStrategyQueue #-}
newStrategyQueue :: LCFS -> Simulation IO (StrategyQueue IO LCFS a)
newStrategyQueue LCFS
s =
(DoubleLinkedList a -> StrategyQueue IO LCFS a)
-> Simulation IO (DoubleLinkedList a)
-> Simulation IO (StrategyQueue IO LCFS a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DoubleLinkedList a -> StrategyQueue IO LCFS a
forall a. DoubleLinkedList a -> StrategyQueue IO LCFS a
LCFSQueue (Simulation IO (DoubleLinkedList a)
-> Simulation IO (StrategyQueue IO LCFS a))
-> Simulation IO (DoubleLinkedList a)
-> Simulation IO (StrategyQueue IO LCFS a)
forall a b. (a -> b) -> a -> b
$
IO (DoubleLinkedList a) -> Simulation IO (DoubleLinkedList a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (DoubleLinkedList a)
forall a. IO (DoubleLinkedList a)
LL.newList
{-# INLINABLE strategyQueueNull #-}
strategyQueueNull :: StrategyQueue IO LCFS a -> Event IO Bool
strategyQueueNull (LCFSQueue q) =
IO Bool -> Event IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> Event IO Bool) -> IO Bool -> Event IO Bool
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> IO Bool
forall a. DoubleLinkedList a -> IO Bool
LL.listNull DoubleLinkedList a
q
instance DequeueStrategy IO LCFS where
{-# SPECIALISE instance DequeueStrategy IO LCFS #-}
{-# INLINABLE strategyDequeue #-}
strategyDequeue :: StrategyQueue IO LCFS a -> Event IO a
strategyDequeue (LCFSQueue q) =
IO a -> Event IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> Event IO a) -> IO a -> Event IO a
forall a b. (a -> b) -> a -> b
$
do a
i <- DoubleLinkedList a -> IO a
forall a. DoubleLinkedList a -> IO a
LL.listFirst DoubleLinkedList a
q
DoubleLinkedList a -> IO ()
forall a. DoubleLinkedList a -> IO ()
LL.listRemoveFirst DoubleLinkedList a
q
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i
instance EnqueueStrategy IO LCFS where
{-# SPECIALISE instance EnqueueStrategy IO LCFS #-}
{-# INLINABLE strategyEnqueue #-}
strategyEnqueue :: StrategyQueue IO LCFS a -> a -> Event IO ()
strategyEnqueue (LCFSQueue q) a
i =
IO () -> Event IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Event IO ()) -> IO () -> Event IO ()
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> a -> IO ()
forall a. DoubleLinkedList a -> a -> IO ()
LL.listInsertFirst DoubleLinkedList a
q a
i
instance DeletingQueueStrategy IO LCFS where
{-# SPECIALISE instance DeletingQueueStrategy IO LCFS #-}
{-# INLINABLE strategyQueueDeleteBy #-}
strategyQueueDeleteBy :: StrategyQueue IO LCFS a -> (a -> Bool) -> Event IO (Maybe a)
strategyQueueDeleteBy (LCFSQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
forall a. DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
LL.listRemoveBy DoubleLinkedList a
q a -> Bool
p
{-# INLINABLE strategyQueueContainsBy #-}
strategyQueueContainsBy :: StrategyQueue IO LCFS a -> (a -> Bool) -> Event IO (Maybe a)
strategyQueueContainsBy (LCFSQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
forall a. DoubleLinkedList a -> (a -> Bool) -> IO (Maybe a)
LL.listContainsBy DoubleLinkedList a
q a -> Bool
p
instance QueueStrategy IO StaticPriorities where
{-# SPECIALISE instance QueueStrategy IO StaticPriorities #-}
newtype StrategyQueue IO StaticPriorities a = StaticPriorityQueue (PQ.PriorityQueue a)
{-# INLINABLE newStrategyQueue #-}
newStrategyQueue :: StaticPriorities
-> Simulation IO (StrategyQueue IO StaticPriorities a)
newStrategyQueue StaticPriorities
s =
(PriorityQueue a -> StrategyQueue IO StaticPriorities a)
-> Simulation IO (PriorityQueue a)
-> Simulation IO (StrategyQueue IO StaticPriorities a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PriorityQueue a -> StrategyQueue IO StaticPriorities a
forall a. PriorityQueue a -> StrategyQueue IO StaticPriorities a
StaticPriorityQueue (Simulation IO (PriorityQueue a)
-> Simulation IO (StrategyQueue IO StaticPriorities a))
-> Simulation IO (PriorityQueue a)
-> Simulation IO (StrategyQueue IO StaticPriorities a)
forall a b. (a -> b) -> a -> b
$
IO (PriorityQueue a) -> Simulation IO (PriorityQueue a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (PriorityQueue a) -> Simulation IO (PriorityQueue a))
-> IO (PriorityQueue a) -> Simulation IO (PriorityQueue a)
forall a b. (a -> b) -> a -> b
$ IO (PriorityQueue a)
forall a. IO (PriorityQueue a)
PQ.newQueue
{-# INLINABLE strategyQueueNull #-}
strategyQueueNull :: StrategyQueue IO StaticPriorities a -> Event IO Bool
strategyQueueNull (StaticPriorityQueue q) =
IO Bool -> Event IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> Event IO Bool) -> IO Bool -> Event IO Bool
forall a b. (a -> b) -> a -> b
$ PriorityQueue a -> IO Bool
forall a. PriorityQueue a -> IO Bool
PQ.queueNull PriorityQueue a
q
instance DequeueStrategy IO StaticPriorities where
{-# SPECIALISE instance DequeueStrategy IO StaticPriorities #-}
{-# INLINABLE strategyDequeue #-}
strategyDequeue :: StrategyQueue IO StaticPriorities a -> Event IO a
strategyDequeue (StaticPriorityQueue q) =
IO a -> Event IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> Event IO a) -> IO a -> Event IO a
forall a b. (a -> b) -> a -> b
$
do (Double
_, a
i) <- PriorityQueue a -> IO (Double, a)
forall a. PriorityQueue a -> IO (Double, a)
PQ.queueFront PriorityQueue a
q
PriorityQueue a -> IO ()
forall a. PriorityQueue a -> IO ()
PQ.dequeue PriorityQueue a
q
a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i
instance PriorityQueueStrategy IO StaticPriorities Double where
{-# SPECIALISE instance PriorityQueueStrategy IO StaticPriorities Double #-}
{-# INLINABLE strategyEnqueueWithPriority #-}
strategyEnqueueWithPriority :: StrategyQueue IO StaticPriorities a -> Double -> a -> Event IO ()
strategyEnqueueWithPriority (StaticPriorityQueue q) Double
p a
i =
IO () -> Event IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Event IO ()) -> IO () -> Event IO ()
forall a b. (a -> b) -> a -> b
$ PriorityQueue a -> Double -> a -> IO ()
forall a. PriorityQueue a -> Double -> a -> IO ()
PQ.enqueue PriorityQueue a
q Double
p a
i
instance DeletingQueueStrategy IO StaticPriorities where
{-# SPECIALISE instance DeletingQueueStrategy IO StaticPriorities #-}
{-# INLINABLE strategyQueueDeleteBy #-}
strategyQueueDeleteBy :: StrategyQueue IO StaticPriorities a
-> (a -> Bool) -> Event IO (Maybe a)
strategyQueueDeleteBy (StaticPriorityQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ PriorityQueue a -> (a -> Bool) -> IO (Maybe a)
forall a. PriorityQueue a -> (a -> Bool) -> IO (Maybe a)
PQ.queueDeleteBy PriorityQueue a
q a -> Bool
p
{-# INLINABLE strategyQueueContainsBy #-}
strategyQueueContainsBy :: StrategyQueue IO StaticPriorities a
-> (a -> Bool) -> Event IO (Maybe a)
strategyQueueContainsBy (StaticPriorityQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ PriorityQueue a -> (a -> Bool) -> IO (Maybe a)
forall a. PriorityQueue a -> (a -> Bool) -> IO (Maybe a)
PQ.queueContainsBy PriorityQueue a
q a -> Bool
p
instance QueueStrategy IO SIRO where
{-# SPECIALISE instance QueueStrategy IO SIRO #-}
newtype StrategyQueue IO SIRO a = SIROQueue (V.Vector a)
{-# INLINABLE newStrategyQueue #-}
newStrategyQueue :: SIRO -> Simulation IO (StrategyQueue IO SIRO a)
newStrategyQueue SIRO
s =
(Vector a -> StrategyQueue IO SIRO a)
-> Simulation IO (Vector a)
-> Simulation IO (StrategyQueue IO SIRO a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Vector a -> StrategyQueue IO SIRO a
forall a. Vector a -> StrategyQueue IO SIRO a
SIROQueue (Simulation IO (Vector a)
-> Simulation IO (StrategyQueue IO SIRO a))
-> Simulation IO (Vector a)
-> Simulation IO (StrategyQueue IO SIRO a)
forall a b. (a -> b) -> a -> b
$
IO (Vector a) -> Simulation IO (Vector a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Vector a) -> Simulation IO (Vector a))
-> IO (Vector a) -> Simulation IO (Vector a)
forall a b. (a -> b) -> a -> b
$ IO (Vector a)
forall a. IO (Vector a)
V.newVector
{-# INLINABLE strategyQueueNull #-}
strategyQueueNull :: StrategyQueue IO SIRO a -> Event IO Bool
strategyQueueNull (SIROQueue q) =
IO Bool -> Event IO Bool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> Event IO Bool) -> IO Bool -> Event IO Bool
forall a b. (a -> b) -> a -> b
$
do Int
n <- Vector a -> IO Int
forall a. Vector a -> IO Int
V.vectorCount Vector 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)
instance DequeueStrategy IO SIRO where
{-# SPECIALISE instance DequeueStrategy IO SIRO #-}
{-# INLINABLE strategyDequeue #-}
strategyDequeue :: StrategyQueue IO SIRO a -> Event IO a
strategyDequeue (SIROQueue q) =
do Int
n <- IO Int -> Event IO Int
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int -> Event IO Int) -> IO Int -> Event IO Int
forall a b. (a -> b) -> a -> b
$ Vector a -> IO Int
forall a. Vector a -> IO Int
V.vectorCount Vector a
q
Int
i <- Parameter IO Int -> Event IO Int
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
ParameterLift t m =>
Parameter m a -> t m a
liftParameter (Parameter IO Int -> Event IO Int)
-> Parameter IO Int -> Event IO Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Parameter IO Int
forall (m :: * -> *). MonadComp m => Int -> Int -> Parameter m Int
randomUniformInt Int
0 (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
a
x <- IO a -> Event IO a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> Event IO a) -> IO a -> Event IO a
forall a b. (a -> b) -> a -> b
$ Vector a -> Int -> IO a
forall a. Vector a -> Int -> IO a
V.readVector Vector a
q Int
i
IO () -> Event IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Event IO ()) -> IO () -> Event IO ()
forall a b. (a -> b) -> a -> b
$ Vector a -> Int -> IO ()
forall a. Vector a -> Int -> IO ()
V.vectorDeleteAt Vector a
q Int
i
a -> Event IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
instance EnqueueStrategy IO SIRO where
{-# SPECIALISE instance EnqueueStrategy IO SIRO #-}
{-# INLINABLE strategyEnqueue #-}
strategyEnqueue :: StrategyQueue IO SIRO a -> a -> Event IO ()
strategyEnqueue (SIROQueue q) a
i =
IO () -> Event IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Event IO ()) -> IO () -> Event IO ()
forall a b. (a -> b) -> a -> b
$ Vector a -> a -> IO ()
forall a. Vector a -> a -> IO ()
V.appendVector Vector a
q a
i
instance DeletingQueueStrategy IO SIRO where
{-# SPECIALISE instance DeletingQueueStrategy IO SIRO #-}
{-# INLINABLE strategyQueueDeleteBy #-}
strategyQueueDeleteBy :: StrategyQueue IO SIRO a -> (a -> Bool) -> Event IO (Maybe a)
strategyQueueDeleteBy (SIROQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ Vector a -> (a -> Bool) -> IO (Maybe a)
forall a. Vector a -> (a -> Bool) -> IO (Maybe a)
V.vectorDeleteBy Vector a
q a -> Bool
p
{-# INLINABLE strategyQueueContainsBy #-}
strategyQueueContainsBy :: StrategyQueue IO SIRO a -> (a -> Bool) -> Event IO (Maybe a)
strategyQueueContainsBy (SIROQueue q) a -> Bool
p =
IO (Maybe a) -> Event IO (Maybe a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe a) -> Event IO (Maybe a))
-> IO (Maybe a) -> Event IO (Maybe a)
forall a b. (a -> b) -> a -> b
$ Vector a -> (a -> Bool) -> IO (Maybe a)
forall a. Vector a -> (a -> Bool) -> IO (Maybe a)
V.vectorContainsBy Vector a
q a -> Bool
p