{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}

-- |
-- Module     : Simulation.Aivika.Distributed.Optimistic.QueueStrategy
-- Copyright  : Copyright (c) 2015-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 'DIO' computation.
--
module Simulation.Aivika.Distributed.Optimistic.QueueStrategy () where

import Control.Monad.Trans

import Simulation.Aivika.Trans
import qualified Simulation.Aivika.Trans.DoubleLinkedList as LL
import Simulation.Aivika.Distributed.Optimistic.Internal.DIO
import Simulation.Aivika.Distributed.Optimistic.Ref.Base

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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