vector-mmap 0.0.2 → 0.0.3
raw patch · 3 files changed
+65/−4 lines, 3 filesdep +QuickCheckdep +temporarydep +vector-mmapdep ~vectornew-uploader
Dependencies added: QuickCheck, temporary, vector-mmap
Dependency ranges changed: vector
Files
- Data/Vector/Storable/MMap.hs +24/−2
- test/TestSuite.hs +27/−0
- vector-mmap.cabal +14/−2
Data/Vector/Storable/MMap.hs view
@@ -1,8 +1,10 @@ {-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeFamilies #-} module Data.Vector.Storable.MMap ( System.IO.MMap.Mode(..), unsafeMMapMVector,- unsafeMMapVector+ unsafeMMapVector,+ writeMMapVector ) where import System.IO.MMap@@ -10,12 +12,17 @@ import qualified Data.Vector.Storable as I import qualified Data.Vector.Storable.Mutable as M++import qualified Data.Vector.Generic as G++ import Data.Int +import Control.Monad import Control.Monad.Primitive -- | Map a file into memory as a mutable vector.-unsafeMMapMVector :: forall s a. Storable a => FilePath -- ^ Path of the file to map+unsafeMMapMVector :: forall a. Storable a => FilePath -- ^ Path of the file to map -> Mode -- ^ Mapping mode -> Maybe (Int64, Int) -- ^ 'Nothing' to map entire file into memory, otherwise 'Just (fileOffset, elementCount)' -> IO (M.MVector (PrimState IO) a)@@ -36,3 +43,18 @@ Nothing -> Nothing Just (start, length) -> Just (start, length * sizeOf (undefined :: a)) return $ I.unsafeFromForeignPtr foreignPtr offset (size `div` sizeOf (undefined :: a))+++-- | Write a file from a vector+-- Be careful with existing files, parts behind the mapped range will stay+-- as they are before the write operation.+writeMMapVector :: forall v a. (Storable a, G.Vector v a) + => FilePath -- ^ Path of the file to map+ -> v a -- ^ Vector to write+ -> IO ()+writeMMapVector path src = do+ let len = G.length src+ target <- unsafeMMapMVector path ReadWriteEx (Just (0,len))+ I.copy target (G.convert src)++{-# INLINABLE writeMMapVector #-}
+ test/TestSuite.hs view
@@ -0,0 +1,27 @@+++++module Main where++import Test.QuickCheck+import Test.QuickCheck.Monadic++import qualified Data.Vector.Storable as V+import qualified Data.Vector.Storable.MMap as V++import System.IO.Temp+++prop_read_write :: [Double] -> Property+prop_read_write l = monadicIO $ do+ let v = V.fromList l++ v' <- run $ withSystemTempFile "vector-mmap-" $ \fn _ -> do+ _ <- V.writeMMapVector fn v+ V.unsafeMMapVector fn Nothing++ assert (v == v')+++main = quickCheck prop_read_write
vector-mmap.cabal view
@@ -1,5 +1,5 @@ Name: vector-mmap-Version: 0.0.2+Version: 0.0.3 License: BSD3 License-File: LICENSE Author: Daniel Peebles <pumpkingod@gmail.com>@@ -11,7 +11,7 @@ Description: Memory map immutable and mutable vectors. -Cabal-Version: >= 1.2+Cabal-Version: >= 1.8 Build-Type: Simple Library@@ -19,3 +19,15 @@ Data.Vector.Storable.MMap Build-Depends: base >= 2 && < 5, mmap >= 0.5.4, vector >= 0.5, primitive >= 0.2.1+++Test-Suite quickcheck+ Type: exitcode-stdio-1.0+ HS-Source-Dirs: test+ Main-is: TestSuite.hs+ build-depends: base >= 2 && < 5, vector-mmap, vector, QuickCheck, temporary+++Source-Repository head+ Type: git+ Location: https://github.com/copumpkin/vector-mmap