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