module Data.Vector.Storable.Allocated where
import Control.DeepSeq
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Binary.Storable
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as MG
import qualified Data.Vector.Storable as S
import qualified Data.Vector.Storable.Mutable as MS
import Foreign
import System.IO
newtype Vector a = Vector { unV :: S.Vector a }
deriving (Eq, Ord, Read, Show, NFData)
newtype MVector s a = MVector { unMV :: MS.MVector s a }
deriving (NFData)
instance Storable a => MG.MVector MVector a where
basicLength = MG.basicLength . unMV
basicUnsafeSlice j m = MVector . MG.basicUnsafeSlice j m . unMV
basicOverlaps (MVector v) (MVector w) = MG.basicOverlaps v w
basicUnsafeNew = fmap MVector . callocVector
basicInitialize = MG.basicInitialize . unMV
basicUnsafeRead = MG.basicUnsafeRead . unMV
basicUnsafeWrite = MG.basicUnsafeWrite . unMV
basicUnsafeCopy (MVector v) (MVector w) = MG.basicUnsafeCopy v w
basicUnsafeMove (MVector v) (MVector w) = MG.basicUnsafeMove v w
basicSet = MG.basicSet . unMV
type instance G.Mutable Vector = MVector
instance Storable a => G.Vector Vector a where
basicUnsafeFreeze = fmap Vector . G.basicUnsafeFreeze . unMV
basicUnsafeThaw = fmap MVector . G.basicUnsafeThaw . unV
basicLength = G.basicLength . unV
basicUnsafeSlice j n = Vector . G.basicUnsafeSlice j n . unV
basicUnsafeIndexM = G.basicUnsafeIndexM . unV
basicUnsafeCopy (MVector mv) (Vector v) = G.basicUnsafeCopy mv v
elemseq = G.elemseq . unV
callocVector :: forall a m. (PrimMonad m, Storable a)
=> Int -> m (S.MVector (PrimState m) a)
callocVector n = unsafePrimToPrim $
S.MVector n <$> (newForeignPtr finalizerFree =<< callocArray n)
replicate :: (PrimMonad m, Storable a) => Int -> a -> m (MVector (PrimState m) a)
replicate = MG.replicate
read :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a
read = MG.read
write :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m ()
write = MG.write
modify :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
modify = MG.modify
unsafeRead :: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> m a
unsafeRead = MG.unsafeRead
unsafeWrite
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> Int -> a -> m ()
unsafeWrite = MG.unsafeWrite
unsafeModify
:: (PrimMonad m, Storable a) => MVector (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify = MG.unsafeModify
(!) :: Storable a => Vector a -> Int -> a
(!) = (G.!)
length :: Storable a => Vector a -> Int
length = G.length
generate :: Storable a => Int -> (Int -> a) -> Vector a
generate = G.generate
create :: Storable a => (forall s. ST s (MVector s a)) -> Vector a
create = G.create
map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b
map = G.map
ifilter :: Storable a => (Int -> a -> Bool) -> Vector a -> Vector a
ifilter = G.ifilter
fromList :: Storable a => [a] -> Vector a
fromList = G.fromList
getMVector :: forall a. Storable a => Handle -> MVector RealWorld a -> IO ()
getMVector h (MVector (S.MVector n ptr))
= withForeignPtr ptr $ \ptr -> hGetBuf h ptr n' >>= \m ->
when (m /= n') $ fail "Not enough bytes."
where
n' = n * sizeOf (undefined :: a)
instance Storable a => Binary (Vector a) where
put h (Vector v)
= S.unsafeWith v $ \ptr -> put h n >> hPutBuf h ptr (n * size)
where
n = S.length v
size = sizeOf (undefined :: a)
get h = get h >>= \n -> MG.new n >>= getMVector h >>. G.unsafeFreeze
where
(>>.) = liftM2 (>>)