{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleInstances #-}

-- |
-- Module     : Simulation.Aivika.Branch.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 'BR' computation.
--
module Simulation.Aivika.Branch.QueueStrategy () where

import Control.Monad.Trans

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

import Simulation.Aivika.Branch.Internal.BR
import Simulation.Aivika.Branch.Ref.Base

-- | An implementation of the 'FCFS' queue strategy.
instance QueueStrategy (BR IO) FCFS where

  -- | A queue used by the 'FCFS' strategy.
  newtype StrategyQueue (BR IO) FCFS a = FCFSQueue (LL.DoubleLinkedList (BR IO) a)

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

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

-- | An implementation of the 'FCFS' queue strategy.
instance DequeueStrategy (BR IO) FCFS where

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

-- | An implementation of the 'FCFS' queue strategy.
instance EnqueueStrategy (BR IO) FCFS where

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

-- | An implementation of the 'LCFS' queue strategy.
instance QueueStrategy (BR IO) LCFS where

  -- | A queue used by the 'LCFS' strategy.
  newtype StrategyQueue (BR IO) LCFS a = LCFSQueue (LL.DoubleLinkedList (BR IO) a)

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

-- | An implementation of the 'LCFS' queue strategy.
instance DequeueStrategy (BR IO) LCFS where

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

-- | An implementation of the 'LCFS' queue strategy.
instance EnqueueStrategy (BR IO) LCFS where

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