{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
module Simulation.Aivika.Lattice.QueueStrategy () where
import Control.Monad.Trans
import Simulation.Aivika.Trans
import qualified Simulation.Aivika.Trans.DoubleLinkedList as LL
import Simulation.Aivika.Lattice.Internal.LIO
import Simulation.Aivika.Lattice.Ref.Base
instance QueueStrategy LIO FCFS where
newtype StrategyQueue LIO FCFS a = FCFSQueueLIO (LL.DoubleLinkedList LIO a)
newStrategyQueue :: FCFS -> Simulation LIO (StrategyQueue LIO FCFS a)
newStrategyQueue FCFS
s = (DoubleLinkedList LIO a -> StrategyQueue LIO FCFS a)
-> Simulation LIO (DoubleLinkedList LIO a)
-> Simulation LIO (StrategyQueue LIO FCFS a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DoubleLinkedList LIO a -> StrategyQueue LIO FCFS a
forall a. DoubleLinkedList LIO a -> StrategyQueue LIO FCFS a
FCFSQueueLIO Simulation LIO (DoubleLinkedList LIO a)
forall (m :: * -> *) a.
MonadRef m =>
Simulation m (DoubleLinkedList m a)
LL.newList
strategyQueueNull :: StrategyQueue LIO FCFS a -> Event LIO Bool
strategyQueueNull (FCFSQueueLIO q) = DoubleLinkedList LIO a -> Event LIO Bool
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m Bool
LL.listNull DoubleLinkedList LIO a
q
instance DequeueStrategy LIO FCFS where
strategyDequeue :: StrategyQueue LIO FCFS a -> Event LIO a
strategyDequeue (FCFSQueueLIO q) =
do a
i <- DoubleLinkedList LIO a -> Event LIO a
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m a
LL.listFirst DoubleLinkedList LIO a
q
DoubleLinkedList LIO a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m ()
LL.listRemoveFirst DoubleLinkedList LIO a
q
a -> Event LIO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i
instance EnqueueStrategy LIO FCFS where
strategyEnqueue :: StrategyQueue LIO FCFS a -> a -> Event LIO ()
strategyEnqueue (FCFSQueueLIO q) a
i = DoubleLinkedList LIO a -> a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> a -> Event m ()
LL.listAddLast DoubleLinkedList LIO a
q a
i
instance QueueStrategy LIO LCFS where
newtype StrategyQueue LIO LCFS a = LCFSQueueLIO (LL.DoubleLinkedList LIO a)
newStrategyQueue :: LCFS -> Simulation LIO (StrategyQueue LIO LCFS a)
newStrategyQueue LCFS
s = (DoubleLinkedList LIO a -> StrategyQueue LIO LCFS a)
-> Simulation LIO (DoubleLinkedList LIO a)
-> Simulation LIO (StrategyQueue LIO LCFS a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DoubleLinkedList LIO a -> StrategyQueue LIO LCFS a
forall a. DoubleLinkedList LIO a -> StrategyQueue LIO LCFS a
LCFSQueueLIO Simulation LIO (DoubleLinkedList LIO a)
forall (m :: * -> *) a.
MonadRef m =>
Simulation m (DoubleLinkedList m a)
LL.newList
strategyQueueNull :: StrategyQueue LIO LCFS a -> Event LIO Bool
strategyQueueNull (LCFSQueueLIO q) = DoubleLinkedList LIO a -> Event LIO Bool
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m Bool
LL.listNull DoubleLinkedList LIO a
q
instance DequeueStrategy LIO LCFS where
strategyDequeue :: StrategyQueue LIO LCFS a -> Event LIO a
strategyDequeue (LCFSQueueLIO q) =
do a
i <- DoubleLinkedList LIO a -> Event LIO a
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m a
LL.listFirst DoubleLinkedList LIO a
q
DoubleLinkedList LIO a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m ()
LL.listRemoveFirst DoubleLinkedList LIO a
q
a -> Event LIO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i
instance EnqueueStrategy LIO LCFS where
strategyEnqueue :: StrategyQueue LIO LCFS a -> a -> Event LIO ()
strategyEnqueue (LCFSQueueLIO q) a
i = DoubleLinkedList LIO a -> a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> a -> Event m ()
LL.listInsertFirst DoubleLinkedList LIO a
q a
i