module Simulation.Aivika.QueueStrategy
(
QueueStrategy(..),
DequeueStrategy(..),
EnqueueStrategy(..),
PriorityQueueStrategy(..),
FCFS(..),
LCFS(..),
SIRO(..),
StaticPriorities(..)) where
import System.Random
import Control.Monad.Trans
import Simulation.Aivika.Simulation
import Simulation.Aivika.Event
import Simulation.Aivika.DoubleLinkedList
import qualified Simulation.Aivika.PriorityQueue as PQ
import qualified Simulation.Aivika.Vector as V
class QueueStrategy s q | s -> q where
newStrategyQueue :: s
-> Simulation (q i)
strategyQueueNull :: s
-> q i
-> Event Bool
class QueueStrategy s q => DequeueStrategy s q | s -> q where
strategyDequeue :: s
-> q i
-> Event i
class DequeueStrategy s q => EnqueueStrategy s q | s -> q where
strategyEnqueue :: s
-> q i
-> i
-> Event ()
class DequeueStrategy s q => PriorityQueueStrategy s q p | s -> q, s -> p where
strategyEnqueueWithPriority :: s
-> q i
-> p
-> i
-> Event ()
data FCFS = FCFS deriving (Eq, Ord, Show)
data LCFS = LCFS deriving (Eq, Ord, Show)
data SIRO = SIRO deriving (Eq, Ord, Show)
data StaticPriorities = StaticPriorities deriving (Eq, Ord, Show)
instance QueueStrategy FCFS DoubleLinkedList where
newStrategyQueue s = liftIO newList
strategyQueueNull s q = liftIO $ listNull q
instance DequeueStrategy FCFS DoubleLinkedList where
strategyDequeue s q =
liftIO $
do i <- listFirst q
listRemoveFirst q
return i
instance EnqueueStrategy FCFS DoubleLinkedList where
strategyEnqueue s q i = liftIO $ listAddLast q i
instance QueueStrategy LCFS DoubleLinkedList where
newStrategyQueue s = liftIO newList
strategyQueueNull s q = liftIO $ listNull q
instance DequeueStrategy LCFS DoubleLinkedList where
strategyDequeue s q =
liftIO $
do i <- listFirst q
listRemoveFirst q
return i
instance EnqueueStrategy LCFS DoubleLinkedList where
strategyEnqueue s q i = liftIO $ listInsertFirst q i
instance QueueStrategy StaticPriorities PQ.PriorityQueue where
newStrategyQueue s = liftIO PQ.newQueue
strategyQueueNull s q = liftIO $ PQ.queueNull q
instance DequeueStrategy StaticPriorities PQ.PriorityQueue where
strategyDequeue s q =
liftIO $
do (_, i) <- PQ.queueFront q
PQ.dequeue q
return i
instance PriorityQueueStrategy StaticPriorities PQ.PriorityQueue Double where
strategyEnqueueWithPriority s q p i = liftIO $ PQ.enqueue q p i
instance QueueStrategy SIRO V.Vector where
newStrategyQueue s = liftIO V.newVector
strategyQueueNull s q =
liftIO $
do n <- V.vectorCount q
return (n == 0)
instance DequeueStrategy SIRO V.Vector where
strategyDequeue s q =
liftIO $
do n <- V.vectorCount q
i <- getStdRandom (randomR (0, n 1))
x <- V.readVector q i
V.vectorDeleteAt q i
return x
instance EnqueueStrategy SIRO V.Vector where
strategyEnqueue s q i = liftIO $ V.appendVector q i