vector-conduit 0.2.1.0 → 0.3.0.0
raw patch · 4 files changed
+121/−119 lines, 4 filesdep ~conduitPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: conduit
API changes (from Hackage documentation)
- Data.Vector.Conduit: consumeMVector :: (PrimMonad m, Resource m, MVector v a) => Sink a m (v (PrimState m) a)
- Data.Vector.Conduit: consumeMVectorN :: (PrimMonad m, Resource m, MVector v a) => Int -> Sink a m (v (PrimState m) a)
- Data.Vector.Conduit: consumeVector :: (PrimMonad m, Resource m, Vector v a) => Sink a m (v a)
- Data.Vector.Conduit: consumeVectorN :: (PrimMonad m, Resource m, Vector v a) => Int -> Sink a m (v a)
- Data.Vector.Conduit: freezeConduit :: (PrimMonad m, Resource m, Vector v a) => Conduit (Mutable v (PrimState m) a) m (v a)
- Data.Vector.Conduit: sourceMVector :: (PrimMonad m, Resource m, MVector v a) => v (PrimState m) a -> Source m a
- Data.Vector.Conduit: sourceVector :: (Resource m, Vector v a) => v a -> Source m a
- Data.Vector.Conduit: thawConduit :: (PrimMonad m, Resource m, Vector v a) => Conduit (v a) m (Mutable v (PrimState m) a)
+ Data.Conduit.Vector: consumeMVector :: (PrimMonad m, MVector v a) => Sink a m (v (PrimState m) a)
+ Data.Conduit.Vector: consumeVector :: (PrimMonad m, Vector v a) => Sink a m (v a)
+ Data.Conduit.Vector: freezeConduit :: (PrimMonad m, Vector v a) => Conduit (Mutable v (PrimState m) a) m (v a)
+ Data.Conduit.Vector: sourceMVector :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Source m a
+ Data.Conduit.Vector: sourceVector :: (Monad m, Vector v a) => v a -> Source m a
+ Data.Conduit.Vector: takeMVector :: (PrimMonad m, MVector v a) => Int -> Sink a m (v (PrimState m) a)
+ Data.Conduit.Vector: takeVector :: (PrimMonad m, Vector v a) => Int -> Sink a m (v a)
+ Data.Conduit.Vector: thawConduit :: (PrimMonad m, Vector v a) => Conduit (v a) m (Mutable v (PrimState m) a)
Files
- Data/Conduit/Vector.hs +113/−0
- Data/Vector/Conduit.hs +0/−111
- test/Main.hs +4/−4
- vector-conduit.cabal +4/−4
+ Data/Conduit/Vector.hs view
@@ -0,0 +1,113 @@+module Data.Conduit.Vector+ (+ sourceVector,+ sourceMVector,+ consumeVector,+ consumeMVector,+ takeVector,+ takeMVector,+ thawConduit,+ freezeConduit+ )+where++import Control.Monad.Primitive+import Control.Monad.ST.Safe+import Control.Monad.Trans.Class+import Data.Conduit+import qualified Data.Conduit.List as L+import qualified Data.Vector.Generic as V+import qualified Data.Vector.Generic.Mutable as M++-- | Use an immutable vector as a source.+sourceVector :: (Monad m, V.Vector v a) => v a -> Source m a+sourceVector v = sourceState 0 f+ where f index | index == V.length v = return StateClosed+ | otherwise = return $ StateOpen (index + 1) (v V.! index)++-- | Use a mutable vector as a source in the ST or IO monad.+sourceMVector :: (PrimMonad m, M.MVector v a)+ => v (PrimState m) a+ -> Source m a+sourceMVector v = sourceState 0 f+ where f index | index == M.length v = return StateClosed+ | otherwise = do x <- M.read v index+ return $ StateOpen (index + 1) x++-- | Consumes all values from the stream and return as an immutable vector.+-- Due to the way it operates, it requires the ST monad at the minimum,+-- although it can also operate IO. This is due to its dependency on+-- a mutable vector.+consumeVector :: (PrimMonad 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 -> M.new 10+ Just vec -> return vec+ let len = M.length v'+ v'' <- if index >= len+ then M.grow v' len+ else return v'+ M.write v'' index x+ return $ StateProcessing (Just v'', index + 1)+ close (Nothing, index) = return $ V.fromList []+ close (Just v, index) = V.unsafeFreeze $ M.take index v++-- | Consumes the first n values from a source and returns as an immutable+-- vector.+takeVector :: (PrimMonad m, V.Vector v a)+ => Int -> Sink a m (v a)+takeVector n = sinkState (Nothing, 0) push close+ where push (v, index) x = do+ v' <- case v of+ Nothing -> M.new n+ Just vec -> return vec+ if index >= n+ then do v'' <- V.unsafeFreeze v'+ return $ StateDone Nothing v''+ else do M.write v' index x+ return $ StateProcessing (Just v', index + 1)+ close (Nothing, index) = return $ V.fromList []+ close (Just v, index) = V.unsafeFreeze v++-- | Consumes all values from the stream and returns as a mutable vector.+consumeMVector :: (PrimMonad m, M.MVector v a)+ => Sink a m (v (PrimState m) a)+consumeMVector = sinkState (Nothing, 0) push close+ where push (v, index) x = do v' <- case v of+ Nothing -> M.new 10+ Just vec -> return vec+ let len = M.length v'+ v'' <- if index >= len+ then M.grow v' len+ else return v'+ M.write v'' index x+ return $ StateProcessing (Just v'', index + 1)+ close (Nothing, index) = 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.+takeMVector :: (PrimMonad m, M.MVector v a)+ => Int -> Sink a m (v (PrimState m) a)+takeMVector n = sinkState (Nothing, 0) push close+ where push (v, index) x =+ do v' <- case v of+ Nothing -> M.new n+ Just vec -> return vec+ if index >= n+ then return $ StateDone Nothing v'+ else do M.write v' index x+ return $ StateProcessing (Just v', index + 1)+ close (Nothing, index) = M.new 0+ close (Just v, index) = return v++-- | Conduit which thaws immutable vectors into mutable vectors+thawConduit :: (PrimMonad 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, V.Vector v a)+ => Conduit (V.Mutable v (PrimState m) a) m (v a)+freezeConduit = L.mapM V.unsafeFreeze
− Data/Vector/Conduit.hs
@@ -1,111 +0,0 @@-{-# LANGUAGE Rank2Types, ImpredicativeTypes, FlexibleContexts #-}-module Data.Vector.Conduit- (- sourceVector,- sourceMVector,- consumeVector,- consumeVectorN,- consumeMVector,- consumeMVectorN,- thawConduit,- freezeConduit- )-where--import Control.Monad.Primitive-import Control.Monad.ST.Safe-import Control.Monad.Trans.Class-import Data.Conduit-import qualified Data.Conduit.List as L-import qualified Data.Vector.Generic as V-import qualified Data.Vector.Generic.Mutable as M---- | Use an immutable vector as a source.-sourceVector :: (Resource m, V.Vector v a) => v a -> Source m a-sourceVector v = sourceState 0 f- where f index | index == V.length v = return StateClosed- | otherwise = return $ StateOpen (index + 1) (v V.! index)---- | Use a mutable vector as a source in the ST or IO monad.-sourceMVector :: (PrimMonad m, Resource m, M.MVector v a)- => v (PrimState m) a- -> Source m a-sourceMVector v = sourceState 0 f- where f index | index == M.length v = return StateClosed- | otherwise = do x <- lift $ M.read v index- return $ StateOpen (index + 1) x---- | Consumes all values from the stream and return as an immutable vector.-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---- | 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- 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) = 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
@@ -3,8 +3,8 @@ import Control.Monad.ST import Data.Conduit+import Data.Conduit.Vector import qualified Data.Vector as V-import Data.Vector.Conduit import Test.QuickCheck import qualified Test.Framework as F import Test.Framework.Runners.Console (defaultMain)@@ -12,19 +12,19 @@ testInverse :: [Int] -> Bool testInverse l = runST $ do let v = V.fromList l- v' <- runResourceT $ sourceVector v $$ consumeVector+ v' <- sourceVector v $$ consumeVector return $ v == v' testInverse2 :: [Int] -> Bool testInverse2 l = runST $ do let v = V.fromList l- v' <- runResourceT $ sourceVector v $$ consumeMVector+ v' <- sourceVector v $$ consumeMVector 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+ v' <- sourceVector v $= thawConduit $= freezeConduit $$ consumeVector return $ v == v' tests :: [F.Test]
vector-conduit.cabal view
@@ -1,5 +1,5 @@ name: vector-conduit-version: 0.2.1.0+version: 0.3.0.0 synopsis: Conduit utilities for vectors description: Provides sources and sinks for vectors. license: GPL-3@@ -13,10 +13,10 @@ library exposed-modules:- Data.Vector.Conduit+ Data.Conduit.Vector build-depends: base >= 4.0 && < 5.0,- conduit >= 0.2 && < 0.3,+ conduit >= 0.3 && < 0.4, primitive >= 0.4 && < 0.5, transformers >= 0.2 && < 0.3, vector >= 0.9 && < 0.10@@ -28,7 +28,7 @@ build-depends: vector-conduit, base >= 4.0 && < 5.0,- conduit >= 0.2 && < 0.3,+ conduit >= 0.3 && < 0.4, test-framework >= 0.5 && < 0.6, test-framework-quickcheck2 >= 0.2 && < 0.3, QuickCheck >= 2.4 && < 2.5,