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