module Simulation.Aivika.Trans.DoubleLinkedList
(DoubleLinkedList,
listNull,
listCount,
newList,
listInsertFirst,
listAddLast,
listRemoveFirst,
listRemoveLast,
listFirst,
listLast) where
import Control.Monad
import Simulation.Aivika.Trans.Session
import Simulation.Aivika.Trans.ProtoRef
import Simulation.Aivika.Trans.Comp
data DoubleLinkedItem m a =
DoubleLinkedItem { itemVal :: a,
itemPrev :: ProtoRef m (Maybe (DoubleLinkedItem m a)),
itemNext :: ProtoRef m (Maybe (DoubleLinkedItem m a)) }
data DoubleLinkedList m a =
DoubleLinkedList { listSession :: Session m,
listHead :: ProtoRef m (Maybe (DoubleLinkedItem m a)),
listTail :: ProtoRef m (Maybe (DoubleLinkedItem m a)),
listSize :: ProtoRef m Int }
listNull :: ProtoRefMonad m => DoubleLinkedList m a -> m Bool
listNull x =
do head <- readProtoRef (listHead x)
case head of
Nothing -> return True
Just _ -> return False
listCount :: ProtoRefMonad m => DoubleLinkedList m a -> m Int
listCount x = readProtoRef (listSize x)
newList :: ProtoRefMonad m => Session m -> m (DoubleLinkedList m a)
newList s =
do head <- newProtoRef s Nothing
tail <- newProtoRef s Nothing
size <- newProtoRef s 0
return DoubleLinkedList { listSession = s,
listHead = head,
listTail = tail,
listSize = size }
listInsertFirst :: ProtoRefMonad m => DoubleLinkedList m a -> a -> m ()
listInsertFirst x v =
do let s = listSession x
size <- readProtoRef (listSize x)
writeProtoRef (listSize x) (size + 1)
head <- readProtoRef (listHead x)
case head of
Nothing ->
do prev <- newProtoRef s Nothing
next <- newProtoRef s Nothing
let item = Just DoubleLinkedItem { itemVal = v,
itemPrev = prev,
itemNext = next }
writeProtoRef (listHead x) item
writeProtoRef (listTail x) item
Just h ->
do prev <- newProtoRef s Nothing
next <- newProtoRef s head
let item = Just DoubleLinkedItem { itemVal = v,
itemPrev = prev,
itemNext = next }
writeProtoRef (itemPrev h) item
writeProtoRef (listHead x) item
listAddLast :: ProtoRefMonad m => DoubleLinkedList m a -> a -> m ()
listAddLast x v =
do let s = listSession x
size <- readProtoRef (listSize x)
writeProtoRef (listSize x) (size + 1)
tail <- readProtoRef (listTail x)
case tail of
Nothing ->
do prev <- newProtoRef s Nothing
next <- newProtoRef s Nothing
let item = Just DoubleLinkedItem { itemVal = v,
itemPrev = prev,
itemNext = next }
writeProtoRef (listHead x) item
writeProtoRef (listTail x) item
Just t ->
do prev <- newProtoRef s tail
next <- newProtoRef s Nothing
let item = Just DoubleLinkedItem { itemVal = v,
itemPrev = prev,
itemNext = next }
writeProtoRef (itemNext t) item
writeProtoRef (listTail x) item
listRemoveFirst :: ProtoRefMonad m => DoubleLinkedList m a -> m ()
listRemoveFirst x =
do head <- readProtoRef (listHead x)
case head of
Nothing ->
error "Empty list: listRemoveFirst"
Just h ->
do size <- readProtoRef (listSize x)
writeProtoRef (listSize x) (size 1)
head' <- readProtoRef (itemNext h)
case head' of
Nothing ->
do writeProtoRef (listHead x) Nothing
writeProtoRef (listTail x) Nothing
Just h' ->
do writeProtoRef (itemPrev h') Nothing
writeProtoRef (listHead x) head'
listRemoveLast :: ProtoRefMonad m => DoubleLinkedList m a -> m ()
listRemoveLast x =
do tail <- readProtoRef (listTail x)
case tail of
Nothing ->
error "Empty list: listRemoveLast"
Just t ->
do size <- readProtoRef (listSize x)
writeProtoRef (listSize x) (size 1)
tail' <- readProtoRef (itemPrev t)
case tail' of
Nothing ->
do writeProtoRef (listHead x) Nothing
writeProtoRef (listTail x) Nothing
Just t' ->
do writeProtoRef (itemNext t') Nothing
writeProtoRef (listTail x) tail'
listFirst :: ProtoRefMonad m => DoubleLinkedList m a -> m a
listFirst x =
do head <- readProtoRef (listHead x)
case head of
Nothing ->
error "Empty list: listFirst"
Just h ->
return $ itemVal h
listLast :: ProtoRefMonad m => DoubleLinkedList m a -> m a
listLast x =
do tail <- readProtoRef (listTail x)
case tail of
Nothing ->
error "Empty list: listLast"
Just t ->
return $ itemVal t