module Data.Mutable.BRef
(
BRef
, IOBRef
, asBRef
, MutableRef (..)
) where
import Control.Monad (liftM)
import Data.Monoid (Monoid, mempty)
import Data.MonoTraversable (Element)
import Data.Mutable.Class
import Data.Sequences (IsSequence)
import qualified Data.Vector.Generic.Mutable as V
import qualified Data.Vector.Mutable as VB
newtype BRef s a = BRef (VB.MVector s a)
asBRef :: BRef s a -> BRef s a
asBRef x = x
type IOBRef = BRef (PrimState IO)
instance MutableContainer (BRef s a) where
type MCState (BRef s a) = s
instance MutableRef (BRef s a) where
type RefElement (BRef s a) = a
newRef = liftM BRef . V.replicate 1
readRef (BRef v) = V.unsafeRead v 0
writeRef (BRef v) = V.unsafeWrite v 0
modifyRef (BRef v) f = V.unsafeRead v 0 >>= V.unsafeWrite v 0 . f
modifyRef' = modifyRef
instance Monoid w => MutableCollection (BRef s w) where
type CollElement (BRef s w) = Element w
newColl = newRef mempty
instance IsSequence seq => MutablePushFront (BRef s seq) where
pushFront = pushFrontRef
instance IsSequence seq => MutablePushBack (BRef s seq) where
pushBack = pushBackRef
instance IsSequence seq => MutablePopFront (BRef s seq) where
popFront = popFrontRef
instance IsSequence seq => MutablePopBack (BRef s seq) where
popBack = popBackRef