module Simulation.Aivika.RealTime.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.RealTime.Internal.RT
import Simulation.Aivika.RealTime.Comp
import qualified Simulation.Aivika.DoubleLinkedList as LL
import qualified Simulation.Aivika.PriorityQueue as PQ
import qualified Simulation.Aivika.Vector as V
instance (Monad m, MonadComp m, MonadIO m)
=> QueueStrategy (RT m) FCFS where
newtype StrategyQueue (RT m) FCFS a = FCFSQueue (LL.DoubleLinkedList a)
newStrategyQueue s =
fmap FCFSQueue $
liftIO LL.newList
strategyQueueNull (FCFSQueue q) =
liftIO $ LL.listNull q
instance (QueueStrategy (RT m) FCFS, MonadComp m, MonadIO m)
=> DequeueStrategy (RT m) FCFS where
strategyDequeue (FCFSQueue q) =
liftIO $
do i <- LL.listFirst q
LL.listRemoveFirst q
return i
instance (DequeueStrategy (RT m) FCFS, MonadComp m, MonadIO m)
=> EnqueueStrategy (RT m) FCFS where
strategyEnqueue (FCFSQueue q) i =
liftIO $ LL.listAddLast q i
instance (DequeueStrategy (RT m) FCFS, MonadComp m, MonadIO m)
=> DeletingQueueStrategy (RT 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)
=> QueueStrategy (RT m) LCFS where
newtype StrategyQueue (RT m) LCFS a = LCFSQueue (LL.DoubleLinkedList a)
newStrategyQueue s =
fmap LCFSQueue $
liftIO LL.newList
strategyQueueNull (LCFSQueue q) =
liftIO $ LL.listNull q
instance (QueueStrategy (RT m) LCFS, MonadComp m, MonadIO m)
=> DequeueStrategy (RT m) LCFS where
strategyDequeue (LCFSQueue q) =
liftIO $
do i <- LL.listFirst q
LL.listRemoveFirst q
return i
instance (DequeueStrategy (RT m) LCFS, MonadComp m, MonadIO m)
=> EnqueueStrategy (RT m) LCFS where
strategyEnqueue (LCFSQueue q) i =
liftIO $ LL.listInsertFirst q i
instance (DequeueStrategy (RT m) LCFS, MonadComp m, MonadIO m)
=> DeletingQueueStrategy (RT 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)
=> QueueStrategy (RT m) StaticPriorities where
newtype StrategyQueue (RT m) StaticPriorities a = StaticPriorityQueue (PQ.PriorityQueue a)
newStrategyQueue s =
fmap StaticPriorityQueue $
liftIO $ PQ.newQueue
strategyQueueNull (StaticPriorityQueue q) =
liftIO $ PQ.queueNull q
instance (QueueStrategy (RT m) StaticPriorities, MonadComp m, MonadIO m)
=> DequeueStrategy (RT m) StaticPriorities where
strategyDequeue (StaticPriorityQueue q) =
liftIO $
do (_, i) <- PQ.queueFront q
PQ.dequeue q
return i
instance (DequeueStrategy (RT m) StaticPriorities, MonadComp m, MonadIO m)
=> PriorityQueueStrategy (RT m) StaticPriorities Double where
strategyEnqueueWithPriority (StaticPriorityQueue q) p i =
liftIO $ PQ.enqueue q p i
instance (DequeueStrategy (RT m) StaticPriorities, MonadComp m, MonadIO m)
=> DeletingQueueStrategy (RT 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)
=> QueueStrategy (RT m) SIRO where
newtype StrategyQueue (RT 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 (RT m) SIRO, MonadComp m, MonadIO m)
=> DequeueStrategy (RT 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 (RT m) SIRO, MonadComp m, MonadIO m)
=> EnqueueStrategy (RT m) SIRO where
strategyEnqueue (SIROQueue q) i =
liftIO $ V.appendVector q i
instance (DequeueStrategy (RT m) SIRO, MonadComp m, MonadIO m)
=> DeletingQueueStrategy (RT m) SIRO where
strategyQueueDeleteBy (SIROQueue q) p =
liftIO $ V.vectorDeleteBy q p
strategyQueueContainsBy (SIROQueue q) p =
liftIO $ V.vectorContainsBy q p