From: Roman Leshchinskiy Date: Mon, 24 May 2010 05:57:27 +0000 (+0000) Subject: Rename unsafeFreeze to basicUnsafeFreeze and add unsafeFreeze as a free function X-Git-Tag: 0_7~16 X-Git-Url: http://git.haskell.org/darcs-mirrors/vector.git/commitdiff_plain/9ce0bad45c8fe2a764f3b9d9ffc42fd0e291104f Rename unsafeFreeze to basicUnsafeFreeze and add unsafeFreeze as a free function --- diff --git a/Data/Vector.hs b/Data/Vector.hs index 3b7a125..4ddee95 100644 --- a/Data/Vector.hs +++ b/Data/Vector.hs @@ -140,7 +140,7 @@ module Data.Vector ( G.convert, -- ** Mutable vectors - thaw, copy, unsafeCopy + unsafeFreeze, thaw, copy, unsafeCopy ) where import qualified Data.Vector.Generic as G @@ -192,8 +192,8 @@ instance Data a => Data (Vector a) where type instance G.Mutable Vector = MVector instance G.Vector Vector a where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MVector i n marr) + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MVector i n marr) = Vector i n `liftM` unsafeFreezeArray marr {-# INLINE basicLength #-} @@ -1297,6 +1297,12 @@ fromListN = G.fromListN -- Conversions - Mutable vectors -- ----------------------------- +-- | /O(1)/ Unsafe convert a mutable vector to an immutable one without +-- copying. The mutable vector may not be used after this operation. +unsafeFreeze :: PrimMonad m => MVector (PrimState m) a -> m (Vector a) +{-# INLINE unsafeFreeze #-} +unsafeFreeze = G.unsafeFreeze + -- | /O(n)/ Yield a mutable copy of the immutable vector. thaw :: PrimMonad m => Vector a -> m (MVector (PrimState m) a) {-# INLINE thaw #-} diff --git a/Data/Vector/Generic.hs b/Data/Vector/Generic.hs index c1360d9..dfe41d0 100644 --- a/Data/Vector/Generic.hs +++ b/Data/Vector/Generic.hs @@ -130,7 +130,7 @@ module Data.Vector.Generic ( convert, -- ** Mutable vectors - thaw, copy, unsafeCopy, + unsafeFreeze, thaw, copy, unsafeCopy, -- * Fusion support @@ -1584,6 +1584,13 @@ convert = unstream . stream -- Conversions - Mutable vectors -- ----------------------------- +-- | /O(1)/ Unsafe convert a mutable vector to an immutable one without +-- copying. The mutable vector may not be used after this operation. +unsafeFreeze + :: (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> m (v a) +{-# INLINE unsafeFreeze #-} +unsafeFreeze = basicUnsafeFreeze + -- | /O(n)/ Yield a mutable copy of the immutable vector. thaw :: (PrimMonad m, Vector v a) => v a -> m (Mutable v (PrimState m) a) {-# INLINE_STREAM thaw #-} diff --git a/Data/Vector/Generic/Base.hs b/Data/Vector/Generic/Base.hs index 13ffe9b..545f7ce 100644 --- a/Data/Vector/Generic/Base.hs +++ b/Data/Vector/Generic/Base.hs @@ -49,7 +49,7 @@ class MVector (Mutable v) a => Vector v a where -- Unsafely convert a mutable vector to its immutable version -- without copying. The mutable vector may not be used after -- this operation. - unsafeFreeze :: PrimMonad m => Mutable v (PrimState m) a -> m (v a) + basicUnsafeFreeze :: PrimMonad m => Mutable v (PrimState m) a -> m (v a) -- | /Assumed complexity: O(1)/ -- diff --git a/Data/Vector/Primitive.hs b/Data/Vector/Primitive.hs index 27a1cbe..b9fbe11 100644 --- a/Data/Vector/Primitive.hs +++ b/Data/Vector/Primitive.hs @@ -129,7 +129,7 @@ module Data.Vector.Primitive ( G.convert, -- ** Mutable vectors - thaw, copy, unsafeCopy + unsafeFreeze, thaw, copy, unsafeCopy ) where import qualified Data.Vector.Generic as G @@ -183,8 +183,8 @@ instance (Data a, Prim a) => Data (Vector a) where type instance G.Mutable Vector = MVector instance Prim a => G.Vector Vector a where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MVector i n marr) + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MVector i n marr) = Vector i n `liftM` unsafeFreezeByteArray marr {-# INLINE basicLength #-} @@ -1210,6 +1210,12 @@ fromListN = G.fromListN -- Conversions - Mutable vectors -- ----------------------------- +-- | /O(1)/ Unsafe convert a mutable vector to an immutable one without +-- copying. The mutable vector may not be used after this operation. +unsafeFreeze :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) +{-# INLINE unsafeFreeze #-} +unsafeFreeze = G.unsafeFreeze + -- | /O(n)/ Yield a mutable copy of the immutable vector. thaw :: (Prim a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) {-# INLINE thaw #-} diff --git a/Data/Vector/Storable.hs b/Data/Vector/Storable.hs index 681d491..577f99a 100644 --- a/Data/Vector/Storable.hs +++ b/Data/Vector/Storable.hs @@ -126,7 +126,7 @@ module Data.Vector.Storable ( G.convert, -- ** Mutable vectors - thaw, copy, unsafeCopy, + unsafeFreeze, thaw, copy, unsafeCopy, -- * Raw pointers unsafeFromForeignPtr, unsafeToForeignPtr, unsafeWith @@ -190,8 +190,8 @@ instance (Data a, Storable a) => Data (Vector a) where type instance G.Mutable Vector = MVector instance Storable a => G.Vector Vector a where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MVector p n fp) = return \$ Vector p n fp + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MVector p n fp) = return \$ Vector p n fp {-# INLINE basicLength #-} basicLength (Vector _ n _) = n @@ -1230,6 +1230,13 @@ fromListN = G.fromListN -- Conversions - Mutable vectors -- ----------------------------- +-- | /O(1)/ Unsafe convert a mutable vector to an immutable one without +-- copying. The mutable vector may not be used after this operation. +unsafeFreeze + :: (Storable a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) +{-# INLINE unsafeFreeze #-} +unsafeFreeze = G.unsafeFreeze + -- | /O(n)/ Yield a mutable copy of the immutable vector. thaw :: (Storable a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) {-# INLINE thaw #-} diff --git a/Data/Vector/Unboxed.hs b/Data/Vector/Unboxed.hs index 10904ca..a89ef8a 100644 --- a/Data/Vector/Unboxed.hs +++ b/Data/Vector/Unboxed.hs @@ -153,7 +153,7 @@ module Data.Vector.Unboxed ( G.convert, -- ** Mutable vectors - thaw, copy, unsafeCopy + unsafeFreeze, thaw, copy, unsafeCopy ) where import Data.Vector.Unboxed.Base @@ -1242,6 +1242,12 @@ fromListN = G.fromListN -- Conversions - Mutable vectors -- ----------------------------- +-- | /O(1)/ Unsafe convert a mutable vector to an immutable one without +-- copying. The mutable vector may not be used after this operation. +unsafeFreeze :: (Unbox a, PrimMonad m) => MVector (PrimState m) a -> m (Vector a) +{-# INLINE unsafeFreeze #-} +unsafeFreeze = G.unsafeFreeze + -- | /O(n)/ Yield a mutable copy of the immutable vector. thaw :: (Unbox a, PrimMonad m) => Vector a -> m (MVector (PrimState m) a) {-# INLINE thaw #-} diff --git a/Data/Vector/Unboxed/Base.hs b/Data/Vector/Unboxed/Base.hs index a2c1369..7d4fbb7 100644 --- a/Data/Vector/Unboxed/Base.hs +++ b/Data/Vector/Unboxed/Base.hs @@ -106,8 +106,8 @@ instance M.MVector MVector () where basicUnsafeGrow (MV_Unit n) m = return \$ MV_Unit (n+m) instance G.Vector Vector () where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MV_Unit n) = return \$ V_Unit n + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MV_Unit n) = return \$ V_Unit n {-# INLINE basicLength #-} basicLength (V_Unit n) = n @@ -156,12 +156,12 @@ instance M.MVector MVector ty where { \ #define primVector(ty,con,mcon) \ instance G.Vector Vector ty where { \ - {-# INLINE unsafeFreeze #-} \ + {-# INLINE basicUnsafeFreeze #-} \ ; {-# INLINE basicLength #-} \ ; {-# INLINE basicUnsafeSlice #-} \ ; {-# INLINE basicUnsafeIndexM #-} \ ; {-# INLINE elemseq #-} \ -; unsafeFreeze (mcon v) = con `liftM` G.unsafeFreeze v \ +; basicUnsafeFreeze (mcon v) = con `liftM` G.basicUnsafeFreeze v \ ; basicLength (con v) = G.basicLength v \ ; basicUnsafeSlice i n (con v) = con \$ G.basicUnsafeSlice i n v \ ; basicUnsafeIndexM (con v) i = G.basicUnsafeIndexM v i \ @@ -293,12 +293,12 @@ instance M.MVector MVector Bool where basicUnsafeGrow (MV_Bool v) n = MV_Bool `liftM` M.basicUnsafeGrow v n instance G.Vector Vector Bool where - {-# INLINE unsafeFreeze #-} + {-# INLINE basicUnsafeFreeze #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} {-# INLINE basicUnsafeIndexM #-} {-# INLINE elemseq #-} - unsafeFreeze (MV_Bool v) = V_Bool `liftM` G.unsafeFreeze v + basicUnsafeFreeze (MV_Bool v) = V_Bool `liftM` G.basicUnsafeFreeze v basicLength (V_Bool v) = G.basicLength v basicUnsafeSlice i n (V_Bool v) = V_Bool \$ G.basicUnsafeSlice i n v basicUnsafeIndexM (V_Bool v) i = toBool `liftM` G.basicUnsafeIndexM v i @@ -339,12 +339,12 @@ instance (RealFloat a, Unbox a) => M.MVector MVector (Complex a) where basicUnsafeGrow (MV_Complex v) n = MV_Complex `liftM` M.basicUnsafeGrow v n instance (RealFloat a, Unbox a) => G.Vector Vector (Complex a) where - {-# INLINE unsafeFreeze #-} + {-# INLINE basicUnsafeFreeze #-} {-# INLINE basicLength #-} {-# INLINE basicUnsafeSlice #-} {-# INLINE basicUnsafeIndexM #-} {-# INLINE elemseq #-} - unsafeFreeze (MV_Complex v) = V_Complex `liftM` G.unsafeFreeze v + basicUnsafeFreeze (MV_Complex v) = V_Complex `liftM` G.basicUnsafeFreeze v basicLength (V_Complex v) = G.basicLength v basicUnsafeSlice i n (V_Complex v) = V_Complex \$ G.basicUnsafeSlice i n v basicUnsafeIndexM (V_Complex v) i diff --git a/internal/GenUnboxTuple.hs b/internal/GenUnboxTuple.hs index f2ff2ca..08cb1ce 100644 --- a/internal/GenUnboxTuple.hs +++ b/internal/GenUnboxTuple.hs @@ -214,7 +214,7 @@ generate n = ,("basicUnsafeCopy", gen_unsafeCopy "MV" qM) ,("basicUnsafeGrow", gen_unsafeGrow)] - methods_Vector = [("unsafeFreeze", gen_unsafeFreeze) + methods_Vector = [("basicUnsafeFreeze", gen_unsafeFreeze) ,("basicLength", gen_length "V") ,("basicUnsafeSlice", gen_unsafeSlice "G" "V") ,("basicUnsafeIndexM", gen_basicUnsafeIndexM) diff --git a/internal/unbox-tuple-instances b/internal/unbox-tuple-instances index 29e0138..2fbfcc9 100644 --- a/internal/unbox-tuple-instances +++ b/internal/unbox-tuple-instances @@ -62,11 +62,11 @@ instance (Unbox a, Unbox b) => M.MVector MVector (a, b) where bs' <- M.basicUnsafeGrow bs m_ return \$ MV_2 (m_+n_) as' bs' instance (Unbox a, Unbox b) => G.Vector Vector (a, b) where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MV_2 n_ as bs) + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MV_2 n_ as bs) = do - as' <- G.unsafeFreeze as - bs' <- G.unsafeFreeze bs + as' <- G.basicUnsafeFreeze as + bs' <- G.basicUnsafeFreeze bs return \$ V_2 n_ as' bs' {-# INLINE basicLength #-} basicLength (V_2 n_ as bs) = n_ @@ -199,12 +199,12 @@ instance (Unbox a, instance (Unbox a, Unbox b, Unbox c) => G.Vector Vector (a, b, c) where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MV_3 n_ as bs cs) + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MV_3 n_ as bs cs) = do - as' <- G.unsafeFreeze as - bs' <- G.unsafeFreeze bs - cs' <- G.unsafeFreeze cs + as' <- G.basicUnsafeFreeze as + bs' <- G.basicUnsafeFreeze bs + cs' <- G.basicUnsafeFreeze cs return \$ V_3 n_ as' bs' cs' {-# INLINE basicLength #-} basicLength (V_3 n_ as bs cs) = n_ @@ -370,13 +370,13 @@ instance (Unbox a, Unbox b, Unbox c, Unbox d) => G.Vector Vector (a, b, c, d) where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MV_4 n_ as bs cs ds) + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MV_4 n_ as bs cs ds) = do - as' <- G.unsafeFreeze as - bs' <- G.unsafeFreeze bs - cs' <- G.unsafeFreeze cs - ds' <- G.unsafeFreeze ds + as' <- G.basicUnsafeFreeze as + bs' <- G.basicUnsafeFreeze bs + cs' <- G.basicUnsafeFreeze cs + ds' <- G.basicUnsafeFreeze ds return \$ V_4 n_ as' bs' cs' ds' {-# INLINE basicLength #-} basicLength (V_4 n_ as bs cs ds) = n_ @@ -585,14 +585,14 @@ instance (Unbox a, Unbox c, Unbox d, Unbox e) => G.Vector Vector (a, b, c, d, e) where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MV_5 n_ as bs cs ds es) - = do - as' <- G.unsafeFreeze as - bs' <- G.unsafeFreeze bs - cs' <- G.unsafeFreeze cs - ds' <- G.unsafeFreeze ds - es' <- G.unsafeFreeze es + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MV_5 n_ as bs cs ds es) + = do + as' <- G.basicUnsafeFreeze as + bs' <- G.basicUnsafeFreeze bs + cs' <- G.basicUnsafeFreeze cs + ds' <- G.basicUnsafeFreeze ds + es' <- G.basicUnsafeFreeze es return \$ V_5 n_ as' bs' cs' ds' es' {-# INLINE basicLength #-} basicLength (V_5 n_ as bs cs ds es) = n_ @@ -852,15 +852,15 @@ instance (Unbox a, Unbox d, Unbox e, Unbox f) => G.Vector Vector (a, b, c, d, e, f) where - {-# INLINE unsafeFreeze #-} - unsafeFreeze (MV_6 n_ as bs cs ds es fs) - = do - as' <- G.unsafeFreeze as - bs' <- G.unsafeFreeze bs - cs' <- G.unsafeFreeze cs - ds' <- G.unsafeFreeze ds - es' <- G.unsafeFreeze es - fs' <- G.unsafeFreeze fs + {-# INLINE basicUnsafeFreeze #-} + basicUnsafeFreeze (MV_6 n_ as bs cs ds es fs) + = do + as' <- G.basicUnsafeFreeze as + bs' <- G.basicUnsafeFreeze bs + cs' <- G.basicUnsafeFreeze cs + ds' <- G.basicUnsafeFreeze ds + es' <- G.basicUnsafeFreeze es + fs' <- G.basicUnsafeFreeze fs return \$ V_6 n_ as' bs' cs' ds' es' fs' {-# INLINE basicLength #-} basicLength (V_6 n_ as bs cs ds es fs) = n_