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.Trans.Template
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 (MonadComp m, MonadIO m, MonadTemplate m)
=> QueueStrategy m FCFS where
newtype StrategyQueue m FCFS a = FCFSQueue (LL.DoubleLinkedList a)
newStrategyQueue s =
fmap FCFSQueue $
liftIO LL.newList
strategyQueueNull (FCFSQueue q) =
liftIO $ LL.listNull q
instance (QueueStrategy m FCFS, MonadComp m, MonadIO m, MonadTemplate m)
=> DequeueStrategy m FCFS where
strategyDequeue (FCFSQueue q) =
liftIO $
do i <- LL.listFirst q
LL.listRemoveFirst q
return i
instance (DequeueStrategy m FCFS, MonadComp m, MonadIO m, MonadTemplate m)
=> EnqueueStrategy m FCFS where
strategyEnqueue (FCFSQueue q) i =
liftIO $ LL.listAddLast q i
instance (DequeueStrategy m FCFS, MonadComp m, MonadIO m, MonadTemplate m)
=> DeletingQueueStrategy m FCFS where
strategyQueueDeleteBy (FCFSQueue q) p =
liftIO $ LL.listRemoveBy q p
strategyQueueContainsBy (FCFSQueue q) p =
liftIO $ LL.listContainsBy q p
instance (MonadComp m, MonadIO m, MonadTemplate m)
=> QueueStrategy m LCFS where
newtype StrategyQueue m LCFS a = LCFSQueue (LL.DoubleLinkedList a)
newStrategyQueue s =
fmap LCFSQueue $
liftIO LL.newList
strategyQueueNull (LCFSQueue q) =
liftIO $ LL.listNull q
instance (QueueStrategy m LCFS, MonadComp m, MonadIO m, MonadTemplate m)
=> DequeueStrategy m LCFS where
strategyDequeue (LCFSQueue q) =
liftIO $
do i <- LL.listFirst q
LL.listRemoveFirst q
return i
instance (DequeueStrategy m LCFS, MonadComp m, MonadIO m, MonadTemplate m)
=> EnqueueStrategy m LCFS where
strategyEnqueue (LCFSQueue q) i =
liftIO $ LL.listInsertFirst q i
instance (DequeueStrategy m LCFS, MonadComp m, MonadIO m, MonadTemplate m)
=> DeletingQueueStrategy m LCFS where
strategyQueueDeleteBy (LCFSQueue q) p =
liftIO $ LL.listRemoveBy q p
strategyQueueContainsBy (LCFSQueue q) p =
liftIO $ LL.listContainsBy q p
instance (MonadComp m, MonadIO m, MonadTemplate m)
=> QueueStrategy m StaticPriorities where
newtype StrategyQueue m StaticPriorities a = StaticPriorityQueue (PQ.PriorityQueue a)
newStrategyQueue s =
fmap StaticPriorityQueue $
liftIO $ PQ.newQueue
strategyQueueNull (StaticPriorityQueue q) =
liftIO $ PQ.queueNull q
instance (QueueStrategy m StaticPriorities, MonadComp m, MonadIO m, MonadTemplate m)
=> DequeueStrategy m StaticPriorities where
strategyDequeue (StaticPriorityQueue q) =
liftIO $
do (_, i) <- PQ.queueFront q
PQ.dequeue q
return i
instance (DequeueStrategy m StaticPriorities, MonadComp m, MonadIO m, MonadTemplate m)
=> PriorityQueueStrategy m StaticPriorities Double where
strategyEnqueueWithPriority (StaticPriorityQueue q) p i =
liftIO $ PQ.enqueue q p i
instance (DequeueStrategy m StaticPriorities, MonadComp m, MonadIO m, MonadTemplate m)
=> DeletingQueueStrategy m StaticPriorities where
strategyQueueDeleteBy (StaticPriorityQueue q) p =
liftIO $ PQ.queueDeleteBy q p
strategyQueueContainsBy (StaticPriorityQueue q) p =
liftIO $ PQ.queueContainsBy q p
instance (MonadComp m, MonadIO m, MonadTemplate m)
=> QueueStrategy m SIRO where
newtype StrategyQueue m SIRO a = SIROQueue (V.Vector a)
newStrategyQueue s =
fmap SIROQueue $
liftIO $ V.newVector
strategyQueueNull (SIROQueue q) =
liftIO $
do n <- V.vectorCount q
return (n == 0)
instance (QueueStrategy m SIRO, MonadComp m, MonadIO m, MonadTemplate m)
=> DequeueStrategy m SIRO where
strategyDequeue (SIROQueue q) =
do n <- liftIO $ V.vectorCount q
i <- liftParameter $ randomUniformInt 0 (n 1)
x <- liftIO $ V.readVector q i
liftIO $ V.vectorDeleteAt q i
return x
instance (DequeueStrategy m SIRO, MonadComp m, MonadIO m, MonadTemplate m)
=> EnqueueStrategy m SIRO where
strategyEnqueue (SIROQueue q) i =
liftIO $ V.appendVector q i
instance (DequeueStrategy m SIRO, MonadComp m, MonadIO m, MonadTemplate m)
=> DeletingQueueStrategy m SIRO where
strategyQueueDeleteBy (SIROQueue q) p =
liftIO $ V.vectorDeleteBy q p
strategyQueueContainsBy (SIROQueue q) p =
liftIO $ V.vectorContainsBy q p