{-# LANGUAGE CPP, DeriveDataTypeable, MultiParamTypeClasses, FlexibleInstances, BangPatterns, TypeFamilies #-}
module Data.Vector.Mutable (
MVector(..), IOVector, STVector,
length, null,
slice, init, tail, take, drop, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
overlaps,
new, unsafeNew, replicate, replicateM, clone,
grow, unsafeGrow,
clear,
read, write, modify, swap,
unsafeRead, unsafeWrite, unsafeModify, unsafeSwap,
nextPermutation,
set, copy, move, unsafeCopy, unsafeMove
) where
import Control.Monad (when)
import qualified Data.Vector.Generic.Mutable as G
import Data.Primitive.Array
import Control.Monad.Primitive
import Prelude hiding ( length, null, replicate, reverse, read,
take, drop, splitAt, init, tail )
import Data.Typeable ( Typeable )
#include "vector.h"
data MVector s a = MVector {-# UNPACK #-} !Int
{-# UNPACK #-} !Int
{-# UNPACK #-} !(MutableArray s a)
deriving ( Typeable )
type IOVector = MVector RealWorld
type STVector s = MVector s
instance G.MVector MVector a where
{-# INLINE basicLength #-}
basicLength (MVector _ n _) = n
{-# INLINE basicUnsafeSlice #-}
basicUnsafeSlice j m (MVector i _ arr) = MVector (i+j) m arr
{-# INLINE basicOverlaps #-}
basicOverlaps (MVector i m arr1) (MVector j n arr2)
= sameMutableArray arr1 arr2
&& (between i j (j+n) || between j i (i+m))
where
between x y z = x >= y && x < z
{-# INLINE basicUnsafeNew #-}
basicUnsafeNew n
= do
arr <- newArray n uninitialised
return (MVector 0 n arr)
{-# INLINE basicInitialize #-}
basicInitialize _ = return ()
{-# INLINE basicUnsafeReplicate #-}
basicUnsafeReplicate n x
= do
arr <- newArray n x
return (MVector 0 n arr)
{-# INLINE basicUnsafeRead #-}
basicUnsafeRead (MVector i _ arr) j = readArray arr (i+j)
{-# INLINE basicUnsafeWrite #-}
basicUnsafeWrite (MVector i _ arr) j x = writeArray arr (i+j) x
{-# INLINE basicUnsafeCopy #-}
basicUnsafeCopy (MVector i n dst) (MVector j _ src)
= copyMutableArray dst i src j n
basicUnsafeMove dst@(MVector iDst n arrDst) src@(MVector iSrc _ arrSrc)
= case n of
0 -> return ()
1 -> readArray arrSrc iSrc >>= writeArray arrDst iDst
2 -> do
x <- readArray arrSrc iSrc
y <- readArray arrSrc (iSrc + 1)
writeArray arrDst iDst x
writeArray arrDst (iDst + 1) y
_
| overlaps dst src
-> case compare iDst iSrc of
LT -> moveBackwards arrDst iDst iSrc n
EQ -> return ()
GT | (iDst - iSrc) * 2 < n
-> moveForwardsLargeOverlap arrDst iDst iSrc n
| otherwise
-> moveForwardsSmallOverlap arrDst iDst iSrc n
| otherwise -> G.basicUnsafeCopy dst src
{-# INLINE basicClear #-}
basicClear v = G.set v uninitialised
{-# INLINE moveBackwards #-}
moveBackwards :: PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> Int -> m ()
moveBackwards !arr !dstOff !srcOff !len =
INTERNAL_CHECK(check) "moveBackwards" "not a backwards move" (dstOff < srcOff)
$ loopM len $ \ i -> readArray arr (srcOff + i) >>= writeArray arr (dstOff + i)
{-# INLINE moveForwardsSmallOverlap #-}
moveForwardsSmallOverlap :: PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> Int -> m ()
moveForwardsSmallOverlap !arr !dstOff !srcOff !len =
INTERNAL_CHECK(check) "moveForwardsSmallOverlap" "not a forward move" (dstOff > srcOff)
$ do
tmp <- newArray overlap uninitialised
loopM overlap $ \ i -> readArray arr (dstOff + i) >>= writeArray tmp i
loopM nonOverlap $ \ i -> readArray arr (srcOff + i) >>= writeArray arr (dstOff + i)
loopM overlap $ \ i -> readArray tmp i >>= writeArray arr (dstOff + nonOverlap + i)
where nonOverlap = dstOff - srcOff; overlap = len - nonOverlap
moveForwardsLargeOverlap :: PrimMonad m => MutableArray (PrimState m) a -> Int -> Int -> Int -> m ()
moveForwardsLargeOverlap !arr !dstOff !srcOff !len =
INTERNAL_CHECK(check) "moveForwardsLargeOverlap" "not a forward move" (dstOff > srcOff)
$ do
queue <- newArray nonOverlap uninitialised
loopM nonOverlap $ \ i -> readArray arr (srcOff + i) >>= writeArray queue i
let mov !i !qTop = when (i < dstOff + len) $ do
x <- readArray arr i
y <- readArray queue qTop
writeArray arr i y
writeArray queue qTop x
mov (i+1) (if qTop + 1 >= nonOverlap then 0 else qTop + 1)
mov dstOff 0
where nonOverlap = dstOff - srcOff
{-# INLINE loopM #-}
loopM :: Monad m => Int -> (Int -> m a) -> m ()
loopM !n k = let
go i = when (i < n) (k i >> go (i+1))
in go 0
uninitialised :: a
uninitialised = error "Data.Vector.Mutable: uninitialised element. If you are trying to compact a vector, use the 'force' function to remove uninitialised elements from the underlying array."
length :: MVector s a -> Int
{-# INLINE length #-}
length = G.length
null :: MVector s a -> Bool
{-# INLINE null #-}
null = G.null
slice :: Int
-> Int
-> MVector s a
-> MVector s a
{-# INLINE slice #-}
slice = G.slice
take :: Int -> MVector s a -> MVector s a
{-# INLINE take #-}
take = G.take
drop :: Int -> MVector s a -> MVector s a
{-# INLINE drop #-}
drop = G.drop
{-# INLINE splitAt #-}
splitAt :: Int -> MVector s a -> (MVector s a, MVector s a)
splitAt = G.splitAt
init :: MVector s a -> MVector s a
{-# INLINE init #-}
init = G.init
tail :: MVector s a -> MVector s a
{-# INLINE tail #-}
tail = G.tail
unsafeSlice :: Int
-> Int
-> MVector s a
-> MVector s a
{-# INLINE unsafeSlice #-}
unsafeSlice = G.unsafeSlice
unsafeTake :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake
unsafeDrop :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop
unsafeInit :: MVector s a -> MVector s a
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit
unsafeTail :: MVector s a -> MVector s a
{-# INLINE unsafeTail #-}
unsafeTail = G.unsafeTail
overlaps :: MVector s a -> MVector s a -> Bool
{-# INLINE overlaps #-}
overlaps = G.overlaps
new :: PrimMonad m => Int -> m (MVector (PrimState m) a)
{-# INLINE new #-}
new = G.new
unsafeNew :: PrimMonad m => Int -> m (MVector (PrimState m) a)
{-# INLINE unsafeNew #-}
unsafeNew = G.unsafeNew
replicate :: PrimMonad m => Int -> a -> m (MVector (PrimState m) a)
{-# INLINE replicate #-}
replicate = G.replicate
replicateM :: PrimMonad m => Int -> m a -> m (MVector (PrimState m) a)
{-# INLINE replicateM #-}
replicateM = G.replicateM
clone :: PrimMonad m => MVector (PrimState m) a -> m (MVector (PrimState m) a)
{-# INLINE clone #-}
clone = G.clone
grow :: PrimMonad m
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
{-# INLINE grow #-}
grow = G.grow
unsafeGrow :: PrimMonad m
=> MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
{-# INLINE unsafeGrow #-}
unsafeGrow = G.unsafeGrow
clear :: PrimMonad m => MVector (PrimState m) a -> m ()
{-# INLINE clear #-}
clear = G.clear
read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
{-# INLINE read #-}
read = G.read
write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m ()
{-# INLINE write #-}
write = G.write
modify :: PrimMonad m => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
{-# INLINE modify #-}
modify = G.modify
swap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m ()
{-# INLINE swap #-}
swap = G.swap
unsafeRead :: PrimMonad m => MVector (PrimState m) a -> Int -> m a
{-# INLINE unsafeRead #-}
unsafeRead = G.unsafeRead
unsafeWrite :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m ()
{-# INLINE unsafeWrite #-}
unsafeWrite = G.unsafeWrite
unsafeModify :: PrimMonad m => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
{-# INLINE unsafeModify #-}
unsafeModify = G.unsafeModify
unsafeSwap :: PrimMonad m => MVector (PrimState m) a -> Int -> Int -> m ()
{-# INLINE unsafeSwap #-}
unsafeSwap = G.unsafeSwap
set :: PrimMonad m => MVector (PrimState m) a -> a -> m ()
{-# INLINE set #-}
set = G.set
copy :: PrimMonad m => MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
{-# INLINE copy #-}
copy = G.copy
unsafeCopy :: PrimMonad m => MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
{-# INLINE unsafeCopy #-}
unsafeCopy = G.unsafeCopy
move :: PrimMonad m => MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
{-# INLINE move #-}
move = G.move
unsafeMove :: PrimMonad m => MVector (PrimState m) a
-> MVector (PrimState m) a
-> m ()
{-# INLINE unsafeMove #-}
unsafeMove = G.unsafeMove
nextPermutation :: (PrimMonad m,Ord e) => MVector (PrimState m) e -> m Bool
{-# INLINE nextPermutation #-}
nextPermutation = G.nextPermutation