{-
--------------------------------------------------------------------------------
--
-- Copyright (C) 2008 Martin Sulzmann, Edmund Lam. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-}

module Actor.QList where

import IO
import Monad
import Data.IORef

import Actor.SList

-- a QList is a queue (represented as a list) of elements pointing to SList elements

data QItem a = QNode { valQ :: IORef (Item a)
                     , nextQ :: IORef (QItem a) }
            | QNull
            | QHead { nextQ :: IORef (QItem a) }

data QList a = QList { headQ :: IORef (IORef (QItem a)), 
                       tailQ :: IORef (IORef (QItem a)) }

-- we assume a static head pointer, pointing to the first node which must be Head
-- tail points to the last node which must be Null

type QIterator a = IORef (IORef (QItem a))


-- basic api

-- we create a new Qlist
newQList :: IO (QList a)
newQList = 
   do null <- newIORef QNull
      hd <- newIORef (QHead null)
      hdPtr <- newIORef hd
      tailPtr <- newIORef null
      return (QList {headQ = hdPtr, tailQ = tailPtr})


-- we add a new node, by overwriting the null tail node
-- we only need to adjust tail but not head because
-- of the static Head
-- we return the location of the newly added node
enQueue :: QList a -> IORef (Item a) -> IO ()
enQueue (QList {tailQ = tailPtrPtr}) x =
   do null <- newIORef QNull
      tailPtr <- readIORef tailPtrPtr
      writeIORef tailPtr (QNode {valQ = x, nextQ = null})
      writeIORef tailPtrPtr null


-- the iterator always points to the PREVIOUS node,
-- recall that there's a static dummy new Head
newQIterator :: QList a -> IO (QIterator a)
newQIterator (QList {headQ = hd}) =
  do hdPtr <- readIORef hd
     it <- newIORef hdPtr
     return it



-- we iterate through the list and return the first "not deleted" node
-- we delink deleted nodes
-- there's no need to adjust head, tail
-- cause head has a static Head and
-- tail points to Null
iterateQList :: QIterator a -> IO (Maybe (IORef (Item a)))
iterateQList itPtrPtr = 
  let go prevPtr =
        do prevNode <- readIORef prevPtr
           let curPtr = nextQ prevNode -- the prev node has always a next field
                                      -- recall the static dummy head node
           curNode <- readIORef curPtr
           case curNode of
             QNull -> return Nothing -- reached end of the list
             QNode { valQ = itemPtr, nextQ = nextNode } ->
                do  Node { deleted = del } <- readIORef itemPtr   -- check element in store
                    if not del                                    -- if deleted
                     then -- node available
                       do writeIORef itPtrPtr curPtr  -- adjust iterator
                          return (Just itemPtr) 
                     else -- delete curNode by setting the next of prevNode to next of curNode
                     do case prevNode of
                          QHead {} -> writeIORef prevPtr (QHead {nextQ = nextNode})
                          QNode {} -> writeIORef prevPtr 
                                       (QNode {valQ = valQ prevNode, nextQ = nextNode})
                        go prevPtr 
  in do startPtr <- readIORef itPtrPtr
        go startPtr