module Game.LambdaHack.Utils.LQueue
( LQueue, newLQueue, nullLQueue, trimLQueue, tryReadLQueue, writeLQueue
) where
import Data.Maybe
type LQueue a = ([a], [a])
newLQueue :: LQueue a
newLQueue = ([], [])
nullLQueue :: LQueue a -> Bool
nullLQueue (rs, ws) = null rs && null ws
trimLQueue :: LQueue (Maybe a) -> LQueue (Maybe a)
trimLQueue (rs, ws) =
let trim (_, w:_) = ([w], [])
trim ([], []) = ([], [])
trim (rsj, []) = ([last rsj], [])
in trim (filter isJust rs, filter isJust ws)
tryReadLQueue :: LQueue a -> Maybe (a, LQueue a)
tryReadLQueue (r : rs, ws) = Just (r, (rs, ws))
tryReadLQueue ([], []) = Nothing
tryReadLQueue ([], ws) = tryReadLQueue (reverse ws, [])
writeLQueue :: LQueue a -> a -> LQueue a
writeLQueue (rs, ws) w = (rs, w : ws)