{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}

-- |
-- Module     : Simulation.Aivika.Lattice.QueueStrategy
-- Copyright  : Copyright (c) 2016-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 7.10.3
--
-- This module defines queue strategies 'FCFS' and 'LCFS' for the 'LIO' computation.
--
module Simulation.Aivika.Lattice.QueueStrategy () where

import Control.Monad.Trans

import Simulation.Aivika.Trans
import qualified Simulation.Aivika.Trans.DoubleLinkedList as LL

import Simulation.Aivika.Lattice.Internal.LIO
import Simulation.Aivika.Lattice.Ref.Base

-- | An implementation of the 'FCFS' queue strategy.
instance QueueStrategy LIO FCFS where

  -- | A queue used by the 'FCFS' strategy.
  newtype StrategyQueue LIO FCFS a = FCFSQueueLIO (LL.DoubleLinkedList LIO a)

  newStrategyQueue :: FCFS -> Simulation LIO (StrategyQueue LIO FCFS a)
newStrategyQueue FCFS
s = (DoubleLinkedList LIO a -> StrategyQueue LIO FCFS a)
-> Simulation LIO (DoubleLinkedList LIO a)
-> Simulation LIO (StrategyQueue LIO FCFS a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DoubleLinkedList LIO a -> StrategyQueue LIO FCFS a
forall a. DoubleLinkedList LIO a -> StrategyQueue LIO FCFS a
FCFSQueueLIO Simulation LIO (DoubleLinkedList LIO a)
forall (m :: * -> *) a.
MonadRef m =>
Simulation m (DoubleLinkedList m a)
LL.newList

  strategyQueueNull :: StrategyQueue LIO FCFS a -> Event LIO Bool
strategyQueueNull (FCFSQueueLIO q) = DoubleLinkedList LIO a -> Event LIO Bool
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m Bool
LL.listNull DoubleLinkedList LIO a
q

-- | An implementation of the 'FCFS' queue strategy.
instance DequeueStrategy LIO FCFS where

  strategyDequeue :: StrategyQueue LIO FCFS a -> Event LIO a
strategyDequeue (FCFSQueueLIO q) =
    do a
i <- DoubleLinkedList LIO a -> Event LIO a
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m a
LL.listFirst DoubleLinkedList LIO a
q
       DoubleLinkedList LIO a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m ()
LL.listRemoveFirst DoubleLinkedList LIO a
q
       a -> Event LIO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i

-- | An implementation of the 'FCFS' queue strategy.
instance EnqueueStrategy LIO FCFS where

  strategyEnqueue :: StrategyQueue LIO FCFS a -> a -> Event LIO ()
strategyEnqueue (FCFSQueueLIO q) a
i = DoubleLinkedList LIO a -> a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> a -> Event m ()
LL.listAddLast DoubleLinkedList LIO a
q a
i

-- | An implementation of the 'LCFS' queue strategy.
instance QueueStrategy LIO LCFS where

  -- | A queue used by the 'LCFS' strategy.
  newtype StrategyQueue LIO LCFS a = LCFSQueueLIO (LL.DoubleLinkedList LIO a)

  newStrategyQueue :: LCFS -> Simulation LIO (StrategyQueue LIO LCFS a)
newStrategyQueue LCFS
s = (DoubleLinkedList LIO a -> StrategyQueue LIO LCFS a)
-> Simulation LIO (DoubleLinkedList LIO a)
-> Simulation LIO (StrategyQueue LIO LCFS a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DoubleLinkedList LIO a -> StrategyQueue LIO LCFS a
forall a. DoubleLinkedList LIO a -> StrategyQueue LIO LCFS a
LCFSQueueLIO Simulation LIO (DoubleLinkedList LIO a)
forall (m :: * -> *) a.
MonadRef m =>
Simulation m (DoubleLinkedList m a)
LL.newList
       
  strategyQueueNull :: StrategyQueue LIO LCFS a -> Event LIO Bool
strategyQueueNull (LCFSQueueLIO q) = DoubleLinkedList LIO a -> Event LIO Bool
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m Bool
LL.listNull DoubleLinkedList LIO a
q

-- | An implementation of the 'LCFS' queue strategy.
instance DequeueStrategy LIO LCFS where

  strategyDequeue :: StrategyQueue LIO LCFS a -> Event LIO a
strategyDequeue (LCFSQueueLIO q) =
    do a
i <- DoubleLinkedList LIO a -> Event LIO a
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m a
LL.listFirst DoubleLinkedList LIO a
q
       DoubleLinkedList LIO a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> Event m ()
LL.listRemoveFirst DoubleLinkedList LIO a
q
       a -> Event LIO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
i

-- | An implementation of the 'LCFS' queue strategy.
instance EnqueueStrategy LIO LCFS where

  strategyEnqueue :: StrategyQueue LIO LCFS a -> a -> Event LIO ()
strategyEnqueue (LCFSQueueLIO q) a
i = DoubleLinkedList LIO a -> a -> Event LIO ()
forall (m :: * -> *) a.
MonadRef m =>
DoubleLinkedList m a -> a -> Event m ()
LL.listInsertFirst DoubleLinkedList LIO a
q a
i