vector-conduit 0.2.0.0 → 0.2.1.0
raw patch · 3 files changed
+73/−11 lines, 3 filesdep −dlist
Dependencies removed: dlist
Files
- Data/Vector/Conduit.hs +64/−8
- test/Main.hs +8/−1
- vector-conduit.cabal +1/−2
Data/Vector/Conduit.hs view
@@ -4,7 +4,11 @@ sourceVector, sourceMVector, consumeVector,- consumeMVector+ consumeVectorN,+ consumeMVector,+ consumeMVectorN,+ thawConduit,+ freezeConduit ) where @@ -12,7 +16,7 @@ import Control.Monad.ST.Safe import Control.Monad.Trans.Class import Data.Conduit-import qualified Data.DList as D+import qualified Data.Conduit.List as L import qualified Data.Vector.Generic as V import qualified Data.Vector.Generic.Mutable as M @@ -32,13 +36,39 @@ return $ StateOpen (index + 1) x -- | Consumes all values from the stream and return as an immutable vector.--- Works by creating a DList.-consumeVector :: (Resource m, V.Vector v a) => Sink a m (v a)-consumeVector = sinkState D.empty push close- where push xs x = return . StateProcessing $ D.snoc xs x- close xs = return . V.fromList . D.toList $ xs+consumeVector :: (PrimMonad m, Resource m, V.Vector v a)+ => Sink a m (v a)+consumeVector = sinkState (Nothing, 0) push close+ where push (v, index) x = do v' <- case v of+ Nothing -> lift $ M.new 10+ Just vec -> return vec+ let len = M.length v'+ v'' <- if index >= len+ then lift $ M.grow v' len+ else return v'+ lift $ M.write v'' index x+ return $ StateProcessing (Just v'', index + 1)+ close (Nothing, index) = return $ V.fromList []+ close (Just v, index) = lift . V.unsafeFreeze $ M.take index v --- | Consume all values from the stream and return as a mutable vector.+-- | Consumes the first n values from a source and returns as an immutable+-- vector.+consumeVectorN :: (PrimMonad m, Resource m, V.Vector v a)+ => Int -> Sink a m (v a)+consumeVectorN n = sinkState (Nothing, 0) push close+ where push (v, index) x = do+ v' <- case v of+ Nothing -> lift $ M.new n+ Just vec -> return vec+ if index >= n+ then do v'' <- lift $ V.unsafeFreeze v'+ return $ StateDone Nothing v''+ else do lift $ M.write v' index x+ return $ StateProcessing (Just v', index + 1)+ close (Nothing, index) = return $ V.fromList []+ close (Just v, index) = lift $ V.unsafeFreeze v++-- | Consumes all values from the stream and returns as a mutable vector. consumeMVector :: (PrimMonad m, Resource m, M.MVector v a) => Sink a m (v (PrimState m) a) consumeMVector = sinkState (Nothing, 0) push close@@ -53,3 +83,29 @@ return $ StateProcessing (Just v'', index + 1) close (Nothing, index) = lift $ M.new 0 close (Just v, index) = return $ M.take index v++-- | Consumes the first n values from the stream and returns as a+-- mutable vector.+consumeMVectorN :: (PrimMonad m, Resource m, M.MVector v a)+ => Int -> Sink a m (v (PrimState m) a)+consumeMVectorN n = sinkState (Nothing, 0) push close+ where push (v, index) x =+ do v' <- case v of+ Nothing -> lift $ M.new n+ Just vec -> return vec+ if index >= n+ then return $ StateDone Nothing v'+ else do lift $ M.write v' index x+ return $ StateProcessing (Just v', index + 1)+ close (Nothing, index) = lift $ M.new 0+ close (Just v, index) = return v++-- | Conduit which thaws immutable vectors into mutable vectors+thawConduit :: (PrimMonad m, Resource m, V.Vector v a)+ => Conduit (v a) m (V.Mutable v (PrimState m) a)+thawConduit = L.mapM V.unsafeThaw++-- | Conduit which freezes mutable vectors into immutable vectors+freezeConduit :: (PrimMonad m, Resource m, V.Vector v a)+ => Conduit (V.Mutable v (PrimState m) a) m (v a)+freezeConduit = L.mapM V.unsafeFreeze
test/Main.hs view
@@ -21,9 +21,16 @@ v'' <- V.unsafeFreeze v' return $ v == v'' +testInverse3 :: [[Int]]-> Bool+testInverse3 ls = runST $ do let vs = map V.fromList ls+ let v = V.fromList vs+ v' <- runResourceT $ sourceVector v $= thawConduit $= freezeConduit $$ consumeVector+ return $ v == v'+ tests :: [F.Test] tests = [testProperty "Inverse" testInverse,- testProperty "Invers2" testInverse2]+ testProperty "Inverse2" testInverse2,+ testProperty "Inverse3" testInverse3] main :: IO () main = defaultMain tests
vector-conduit.cabal view
@@ -1,5 +1,5 @@ name: vector-conduit-version: 0.2.0.0+version: 0.2.1.0 synopsis: Conduit utilities for vectors description: Provides sources and sinks for vectors. license: GPL-3@@ -17,7 +17,6 @@ build-depends: base >= 4.0 && < 5.0, conduit >= 0.2 && < 0.3,- dlist >= 0.5 && < 0.6, primitive >= 0.4 && < 0.5, transformers >= 0.2 && < 0.3, vector >= 0.9 && < 0.10