vector-sized 1.4.1.0 → 1.4.2
raw patch · 8 files changed
+134/−21 lines, 8 files
Files
- changelog.md +5/−0
- default.nix +1/−1
- src/Data/Vector/Generic/Sized.hs +12/−5
- src/Data/Vector/Sized.hs +8/−1
- src/Data/Vector/Storable/Sized.hs +8/−1
- src/Data/Vector/Unboxed/Mutable/Sized.hs +90/−10
- src/Data/Vector/Unboxed/Sized.hs +8/−1
- vector-sized.cabal +2/−2
changelog.md view
@@ -2,6 +2,11 @@ ## WIP +## [1.4.2] - 2020-08-20++- Add `instance Unbox a, KnownNat n) => Unbox (Vector n a)`+- Add `zipVectorsUnsafe`+ ## [1.4.1.0] - 2020-05-04 - GHC 8.10 compatibility
default.nix view
@@ -1,5 +1,5 @@ { pkgs ? import <nixpkgs> {}-, compiler ? "ghc882"+, compiler ? "ghc8102" , hoogle ? true }:
src/Data/Vector/Generic/Sized.hs view
@@ -242,6 +242,7 @@ , withSized , fromSized , withVectorUnsafe+ , zipVectorsUnsafe ) where import Data.Vector.Generic.Sized.Internal@@ -473,7 +474,7 @@ _last f vector = snoc (init vector) <$> f (last vector) {-# inline _last #-} --- | /O(1)/ Safe indexing in a monad. +-- | /O(1)/ Safe indexing in a monad. -- -- The monad allows operations to be strict in the vector when necessary. -- Suppose vector copying is implemented like this:@@ -1038,10 +1039,10 @@ -- | /O(n*m)/ Map a function over a vector and concatenate the results. The -- function is required to always return a vector of the same length.-concatMap :: (VG.Vector v a, VG.Vector v' b) +concatMap :: (VG.Vector v a, VG.Vector v' b) => (a -> Vector v' m b) -> Vector v n a -> Vector v' (n*m) b concatMap f (Vector v) = Vector . VG.concat . fmap (fromSized . f) . VG.toList $ v-{-# inline concatMap #-} +{-# inline concatMap #-} -- -- ** Monadic mapping@@ -1799,6 +1800,12 @@ withVectorUnsafe f (Vector v) = Vector (f v) {-# inline withVectorUnsafe #-} +-- | Apply a function on two unsized vectors to sized vectors. The function must+-- preserve the size of the vectors, this is not checked.+zipVectorsUnsafe :: (u a -> v b -> w c) -> Vector u n a -> Vector v n b -> Vector w n c+zipVectorsUnsafe f (Vector u) (Vector v) = Vector (f u v)+{-# inline zipVectorsUnsafe #-}+ -- | Internal existential wrapper used for implementing 'SomeSized' -- pattern synonym data SV_ v a = forall n. KnownNat n => SV_ (Vector v n a)@@ -1854,7 +1861,7 @@ -- @ -- -- Remember that the final type of the result of the do block ('()', here)--- must not depend on @n@. However, the +-- must not depend on @n@. However, the -- -- Also useful in ghci, where you can pattern match to get sized vectors -- from unsized vectors.@@ -1929,5 +1936,5 @@ atanh = map atanh instance (VG.Vector v a, Binary a, KnownNat n) => Binary (Vector v n a) where- get = replicateM Data.Binary.get + get = replicateM Data.Binary.get put = mapM_ put
src/Data/Vector/Sized.hs view
@@ -235,6 +235,7 @@ , withSized , fromSized , withVectorUnsafe+ , zipVectorsUnsafe ) where import qualified Data.Vector.Generic.Sized as V@@ -1530,6 +1531,12 @@ withVectorUnsafe = V.withVectorUnsafe {-# inline withVectorUnsafe #-} +-- | Apply a function on two unsized vectors to sized vectors. The function must+-- preserve the size of the vectors, this is not checked.+zipVectorsUnsafe :: (VU.Vector a -> VU.Vector b -> VU.Vector c) -> Vector n a -> Vector n b -> Vector n c+zipVectorsUnsafe = V.zipVectorsUnsafe+{-# inline zipVectorsUnsafe #-}+ -- | Pattern synonym that lets you treat an unsized vector as if it -- "contained" a sized vector. If you pattern match on an unsized vector, -- its contents will be the /sized/ vector counterpart.@@ -1579,7 +1586,7 @@ -- @ -- -- Remember that the final type of the result of the do block ('()', here)--- must not depend on @n@. However, the +-- must not depend on @n@. However, the -- -- Also useful in ghci, where you can pattern match to get sized vectors -- from unsized vectors.
src/Data/Vector/Storable/Sized.hs view
@@ -236,6 +236,7 @@ , withSized , fromSized , withVectorUnsafe+ , zipVectorsUnsafe ) where import qualified Data.Vector.Generic.Sized as V@@ -1605,6 +1606,12 @@ withVectorUnsafe = V.withVectorUnsafe {-# inline withVectorUnsafe #-} +-- | Apply a function on two unsized vectors to sized vectors. The function must+-- preserve the size of the vectors, this is not checked.+zipVectorsUnsafe :: (VS.Vector a -> VS.Vector b -> VS.Vector c) -> Vector n a -> Vector n b -> Vector n c+zipVectorsUnsafe = V.zipVectorsUnsafe+{-# inline zipVectorsUnsafe #-}+ -- | Pattern synonym that lets you treat an unsized vector as if it -- "contained" a sized vector. If you pattern match on an unsized vector, -- its contents will be the /sized/ vector counterpart.@@ -1654,7 +1661,7 @@ -- @ -- -- Remember that the final type of the result of the do block ('()', here)--- must not depend on @n@. However, the +-- must not depend on @n@. However, the -- -- Also useful in ghci, where you can pattern match to get sized vectors -- from unsized vectors.
src/Data/Vector/Unboxed/Mutable/Sized.hs view
@@ -1,8 +1,13 @@-{-# LANGUAGE CPP #-}-{-# LANGUAGE DataKinds #-}-{-# LANGUAGE FlexibleContexts #-}-{-# LANGUAGE RankNTypes #-}-{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE CPP #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE PolyKinds #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeOperators #-} {-| This module re-exports the functionality in 'Data.Vector.Generic.Mutable.Sized'@@ -82,8 +87,13 @@ , Unbox ) where +import Data.Vector.Generic.Sized.Internal (Vector(..))+import qualified Data.Vector.Generic as V+import qualified Data.Vector.Generic.Mutable as VM+import qualified Data.Vector.Generic.Sized as VG import qualified Data.Vector.Generic.Mutable.Sized as VGM-import qualified Data.Vector.Unboxed.Mutable as VSM+import qualified Data.Vector.Unboxed as VU+import qualified Data.Vector.Unboxed.Mutable as VUM import Data.Vector.Unboxed (Unbox) import GHC.TypeLits import Data.Finite@@ -95,7 +105,7 @@ -- | 'Data.Vector.Generic.Mutable.Sized.Vector' specialized to use -- 'Data.Vector.Unbox.Mutable'.-type MVector = VGM.MVector VSM.MVector+type MVector = VGM.MVector VUM.MVector -- * Accessors @@ -439,7 +449,7 @@ -- to it are also reflected in the given -- 'Data.Vector.Unbox.Mutable.MVector'. toSized :: forall n a s. (KnownNat n, Unbox a)- => VSM.MVector s a -> Maybe (MVector n s a)+ => VUM.MVector s a -> Maybe (MVector n s a) toSized = VGM.toSized {-# inline toSized #-} @@ -457,7 +467,7 @@ -- to it are also reflected in the given -- 'Data.Vector.Unbox.Mutable.MVector'. withSized :: forall s a r. Unbox a- => VSM.MVector s a -> (forall n. KnownNat n => MVector n s a -> r) -> r+ => VUM.MVector s a -> (forall n. KnownNat n => MVector n s a -> r) -> r withSized = VGM.withSized {-# inline withSized #-} @@ -468,8 +478,78 @@ -- 'Data.Vector.Unbox.Mutable.MVector' is a reference to the exact same -- vector in memory as the given one, and any modifications to it are also -- reflected in the given 'MVector'.-fromSized :: MVector n s a -> VSM.MVector s a+fromSized :: MVector n s a -> VUM.MVector s a fromSized = VGM.fromSized {-# inline fromSized #-} +-- | This instance allows to define sized matrices and tensors+-- backed by continuous memory segments, which reduces memory allocations+-- and relaxes pressure on garbage collector.+instance (Unbox a, KnownNat n) => Unbox (VG.Vector VU.Vector n a)++newtype instance VUM.MVector s (VG.Vector VU.Vector n a) = MV_Sized (VUM.MVector s a)+newtype instance VU.Vector (VG.Vector VU.Vector n a) = V_Sized (VU.Vector a)++instance (Unbox a, KnownNat n) => VM.MVector VUM.MVector (VG.Vector VU.Vector n a) where+ basicLength vs@(MV_Sized v) = VM.basicLength v `quot` intLenM vs+ {-# inline basicLength #-}++ basicUnsafeSlice i n vs@(MV_Sized v) = MV_Sized (VM.basicUnsafeSlice (i * intLenM vs) (n * intLenM vs) v)+ {-# inline basicUnsafeSlice #-}++ basicOverlaps (MV_Sized v1) (MV_Sized v2) = VM.basicOverlaps v1 v2+ {-# inline basicOverlaps #-}++ basicUnsafeNew n = MV_Sized <$> VM.basicUnsafeNew (n * fromIntegral (natVal (Proxy :: Proxy n)))+ {-# inline basicUnsafeNew #-}++ basicInitialize (MV_Sized v) = VM.basicInitialize v+ {-# inline basicInitialize #-}++ basicClear (MV_Sized v) = VM.basicClear v+ {-# inline basicClear #-}++ basicUnsafeCopy (MV_Sized v1) (MV_Sized v2) = VM.basicUnsafeCopy v1 v2+ {-# inline basicUnsafeCopy #-}++ basicUnsafeMove (MV_Sized v1) (MV_Sized v2) = VM.basicUnsafeMove v1 v2+ {-# inline basicUnsafeMove #-}++ basicUnsafeGrow vs@(MV_Sized v) n = MV_Sized <$> VM.basicUnsafeGrow v (n * intLenM vs)+ {-# inline basicUnsafeGrow #-}++ basicUnsafeRead vs@(MV_Sized v) i = Vector <$> V.freeze (VM.basicUnsafeSlice (i * intLenM vs) (intLenM vs) v)+ {-# inline basicUnsafeRead #-}++ basicUnsafeWrite vs@(MV_Sized v) i (Vector x) = V.basicUnsafeCopy (VM.basicUnsafeSlice (i * intLenM vs) (intLenM vs) v) x+ {-# inline basicUnsafeWrite #-}+++intLenM :: forall s n a. KnownNat n => VUM.MVector s (VG.Vector VU.Vector n a) -> Int+intLenM _ = fromIntegral (natVal (Proxy :: Proxy n))++instance (Unbox a, KnownNat n) => V.Vector VU.Vector (VG.Vector VU.Vector n a) where+ basicUnsafeFreeze (MV_Sized v) = V_Sized <$> V.basicUnsafeFreeze v+ {-# inline basicUnsafeFreeze #-}++ basicUnsafeThaw (V_Sized v) = MV_Sized <$> V.basicUnsafeThaw v+ {-# inline basicUnsafeThaw #-}++ basicLength vs@(V_Sized v) = V.basicLength v `quot` intLen vs+ {-# inline basicLength #-}++ basicUnsafeSlice i n vs@(V_Sized v) = V_Sized (V.basicUnsafeSlice (i * intLen vs) (n * intLen vs) v)+ {-# inline basicUnsafeSlice #-}++ basicUnsafeCopy (MV_Sized mv) (V_Sized v) = V.basicUnsafeCopy mv v+ {-# inline basicUnsafeCopy #-}++ elemseq _ = seq+ {-# inline elemseq #-}++ basicUnsafeIndexM vs@(V_Sized v) i = pure $! Vector (V.basicUnsafeSlice (i * intLen vs) (intLen vs) v)+ {-# inline basicUnsafeIndexM #-}++intLen :: forall n a. KnownNat n => VU.Vector (VG.Vector VU.Vector n a) -> Int+intLen _ = fromIntegral (natVal (Proxy :: Proxy n))
src/Data/Vector/Unboxed/Sized.hs view
@@ -236,6 +236,7 @@ , withSized , fromSized , withVectorUnsafe+ , zipVectorsUnsafe -- ** Unbox , Unbox ) where@@ -1603,6 +1604,12 @@ withVectorUnsafe = V.withVectorUnsafe {-# inline withVectorUnsafe #-} +-- | Apply a function on two unsized vectors to sized vectors. The function must+-- preserve the size of the vectors, this is not checked.+zipVectorsUnsafe :: (VU.Vector a -> VU.Vector b -> VU.Vector c) -> Vector n a -> Vector n b -> Vector n c+zipVectorsUnsafe = V.zipVectorsUnsafe+{-# inline zipVectorsUnsafe #-}+ -- | Pattern synonym that lets you treat an unsized vector as if it -- "contained" a sized vector. If you pattern match on an unsized vector, -- its contents will be the /sized/ vector counterpart.@@ -1652,7 +1659,7 @@ -- @ -- -- Remember that the final type of the result of the do block ('()', here)--- must not depend on @n@. However, the +-- must not depend on @n@. However, the -- -- Also useful in ghci, where you can pattern match to get sized vectors -- from unsized vectors.
vector-sized.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 556e7c11321b9c778c723cb20757a86fc9f11c06d89cc2af90365b2a0dce55c0+-- hash: ee1654a35ad22a0b6764be7fe44e3e0c7b29dbf714956529471c549c06bfa011 name: vector-sized-version: 1.4.1.0+version: 1.4.2 synopsis: Size tagged vectors description: Please see README.md category: Data