Skip to content

Commit 5a74209

Browse files
committed
Move safe function and {IO,ST}Vector to safe modules
Exception: - {IO,ST}Vector are left in D.V.Storable.Mutable.Unsafe since IOVector is used there - {IO,ST}Vector are left in D.V.Unsafe.Unsafe. They were previously defined in D.V.Unsafe.Base. Now Base simply reexport Unsafe module
1 parent 8cbf15a commit 5a74209

10 files changed

Lines changed: 157 additions & 179 deletions

File tree

vector/src/Data/Vector.hs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ module Data.Vector (
178178
) where
179179

180180
import Control.Applicative (Applicative)
181+
import Data.Primitive.Array
182+
import qualified Data.Traversable as Traversable
181183
import Data.Vector.Mutable.Unsafe ( MVector )
182184
import Data.Vector.Unsafe
183185
import qualified Data.Vector.Generic as G
@@ -187,11 +189,36 @@ import Control.Monad.Primitive
187189

188190
import Prelude
189191
( Eq, Ord, Num, Enum, Monoid, Monad, Bool, Ordering(..), Int, Maybe, Either
190-
, id, (==))
192+
, id, (==), (&&), otherwise)
191193

192194

193-
import qualified Data.Traversable as Traversable
194195

196+
-- | /O(1)/ Convert an array to a vector.
197+
--
198+
-- @since 0.12.2.0
199+
fromArray :: Array a -> Vector a
200+
{-# INLINE fromArray #-}
201+
fromArray arr = Vector 0 (sizeofArray arr) arr
202+
203+
-- | /O(n)/ Convert a vector to an array.
204+
--
205+
-- @since 0.12.2.0
206+
toArray :: Vector a -> Array a
207+
{-# INLINE toArray #-}
208+
toArray (Vector offset len arr)
209+
| offset == 0 && len == sizeofArray arr = arr
210+
| otherwise = cloneArray arr offset len
211+
212+
-- | /O(1)/ Extract the underlying `Array`, offset where vector starts and the
213+
-- total number of elements in the vector. Below property always holds:
214+
--
215+
-- > let (array, offset, len) = toArraySlice v
216+
-- > v === unsafeFromArraySlice len offset array
217+
--
218+
-- @since 0.13.0.0
219+
toArraySlice :: Vector a -> (Array a, Int, Int)
220+
{-# INLINE toArraySlice #-}
221+
toArraySlice (Vector offset len arr) = (arr, offset, len)
195222

196223

197224
-- Length information

vector/src/Data/Vector/Mutable.hs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ module Data.Vector.Mutable (
7474
) where
7575

7676
import qualified Data.Vector.Generic.Mutable as G
77-
import Data.Vector.Mutable.Unsafe (MVector,IOVector,STVector,toMutableArray,fromMutableArray)
77+
import Data.Vector.Mutable.Unsafe (MVector)
7878
import qualified Data.Vector.Mutable.Unsafe as U
7979
import Data.Primitive.Array
8080
import Control.Monad.Primitive
8181

82-
import Prelude( Ord, Bool, Ordering(..), Int, Maybe )
82+
import Prelude( Ord, Bool, Ordering(..), Int, Maybe, (<$>) )
8383

8484
#include "vector.h"
8585

@@ -88,6 +88,29 @@ pattern MVector i j arr = U.MVector i j arr
8888
{-# COMPLETE MVector #-}
8989
{-# DEPRECATED MVector "Use MVector exported from \"Data.Vector.Mutable.Unsafe\"" #-}
9090

91+
type IOVector = MVector RealWorld
92+
type STVector s = MVector s
93+
94+
95+
-- Conversions - Arrays
96+
-- -----------------------------
97+
98+
-- | /O(n)/ Make a copy of a mutable array to a new mutable vector.
99+
--
100+
-- @since 0.12.2.0
101+
fromMutableArray :: PrimMonad m => MutableArray (PrimState m) a -> m (MVector (PrimState m) a)
102+
{-# INLINE fromMutableArray #-}
103+
fromMutableArray marr =
104+
let size = sizeofMutableArray marr
105+
in MVector 0 size <$> cloneMutableArray marr 0 size
106+
107+
-- | /O(n)/ Make a copy of a mutable vector into a new mutable array.
108+
--
109+
-- @since 0.12.2.0
110+
toMutableArray :: PrimMonad m => MVector (PrimState m) a -> m (MutableArray (PrimState m) a)
111+
{-# INLINE toMutableArray #-}
112+
toMutableArray (MVector offset size marr) = cloneMutableArray marr offset size
113+
91114

92115
-- Length information
93116
-- ------------------

vector/src/Data/Vector/Mutable/Unsafe.hs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,13 @@
1313
-- generally unsafe and may violate memory safety
1414
module Data.Vector.Mutable.Unsafe
1515
( MVector(..)
16-
, IOVector
17-
, STVector
18-
-- * Array conversions
19-
, toMutableArray
20-
, fromMutableArray
2116
) where
2217

23-
import Control.Monad (when, liftM)
18+
import Control.Monad (when)
2419
import Control.Monad.ST (ST)
2520
import qualified Data.Vector.Generic.Mutable as G
2621
import Data.Vector.Internal.Check
2722
import Data.Primitive.Array
28-
import Control.Monad.Primitive
2923

3024
import Prelude
3125
( Monad, Ordering(..), Int
@@ -45,9 +39,6 @@ data MVector s a = MVector { _offset :: {-# UNPACK #-} !Int
4539
-- ^ Underlying array
4640
}
4741

48-
type IOVector = MVector RealWorld
49-
type STVector s = MVector s
50-
5142

5243
-- NOTE: This seems unsafe, see http://trac.haskell.org/vector/ticket/54
5344
{-
@@ -165,23 +156,3 @@ loopM !n k = let
165156

166157
uninitialised :: a
167158
uninitialised = error "Data.Vector.Mutable: uninitialised element. If you are trying to compact a vector, use the 'Data.Vector.force' function to remove uninitialised elements from the underlying array."
168-
169-
170-
-- Conversions - Arrays
171-
-- -----------------------------
172-
173-
-- | /O(n)/ Make a copy of a mutable array to a new mutable vector.
174-
--
175-
-- @since 0.12.2.0
176-
fromMutableArray :: PrimMonad m => MutableArray (PrimState m) a -> m (MVector (PrimState m) a)
177-
{-# INLINE fromMutableArray #-}
178-
fromMutableArray marr =
179-
let size = sizeofMutableArray marr
180-
in MVector 0 size `liftM` cloneMutableArray marr 0 size
181-
182-
-- | /O(n)/ Make a copy of a mutable vector into a new mutable array.
183-
--
184-
-- @since 0.12.2.0
185-
toMutableArray :: PrimMonad m => MVector (PrimState m) a -> m (MutableArray (PrimState m) a)
186-
{-# INLINE toMutableArray #-}
187-
toMutableArray (MVector offset size marr) = cloneMutableArray marr offset size

vector/src/Data/Vector/Primitive/Mutable.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import qualified Data.Vector.Generic.Mutable as G
7575
import Data.Primitive ( Prim )
7676
import Data.Primitive.ByteArray
7777
import Data.Vector.Primitive.Mutable.Unsafe
78-
(MVector,IOVector,STVector,unsafeCoerceMVector,unsafeCast)
78+
(MVector,unsafeCoerceMVector,unsafeCast)
7979
import qualified Data.Vector.Primitive.Mutable.Unsafe as U
8080
import Control.Monad.Primitive
8181

@@ -89,6 +89,10 @@ pattern MVector i j arr = U.MVector i j arr
8989
{-# COMPLETE MVector #-}
9090
{-# DEPRECATED MVector "Use MVector exported from \"Data.Vector.Primitive.Mutable.Unsafe\"" #-}
9191

92+
type IOVector = MVector RealWorld
93+
type STVector s = MVector s
94+
95+
9296
-- Length information
9397
-- ------------------
9498

vector/src/Data/Vector/Primitive/Mutable/Unsafe.hs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
-- generally unsafe and may violate memory safety.
1414
module Data.Vector.Primitive.Mutable.Unsafe
1515
( MVector(..)
16-
, IOVector
17-
, STVector
1816
, unsafeCoerceMVector
1917
, unsafeCast
2018
) where
@@ -23,7 +21,6 @@ import qualified Data.Vector.Generic.Mutable as MG
2321
import Data.Primitive.ByteArray
2422
import Data.Primitive ( Prim, sizeOf )
2523
import Data.Word ( Word8 )
26-
import Control.Monad.Primitive
2724
import Control.Monad ( liftM )
2825
import GHC.Stack (HasCallStack)
2926

@@ -50,9 +47,6 @@ data MVector s a = MVector {-# UNPACK #-} !Int -- ^ offset
5047
{-# UNPACK #-} !Int -- ^ length
5148
{-# UNPACK #-} !(MutableByteArray s) -- ^ underlying mutable byte array
5249

53-
type IOVector = MVector RealWorld
54-
type STVector s = MVector s
55-
5650
-- | /O(1)/ Unsafely coerce a mutable vector from one element type to another,
5751
-- representationally equal type. The operation just changes the type of the
5852
-- underlying pointer and does not modify the elements.

vector/src/Data/Vector/Strict.hs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,63 @@ module Data.Vector.Strict (
179179

180180
import Control.Applicative (Applicative)
181181
import Control.Monad.Primitive
182+
import Control.DeepSeq (NFData1(liftRnf))
183+
import Data.Primitive.Array
182184
import Data.Vector.Strict.Mutable.Unsafe ( MVector(..) )
183185
import Data.Vector.Strict.Unsafe
184186
import qualified Data.Vector.Generic as G
187+
import qualified Data.Vector as V
185188
import qualified Data.Traversable as Traversable
186189

187190
import Control.Monad.ST ( ST )
188191

189192
import Prelude
190193
( Eq(..), Ord(..), Num, Enum, Monoid, Monad, Bool, Ordering(..), Int, Maybe, Either
191-
, id)
194+
, id, ($), seq)
195+
196+
-- Conversions - Lazy vectors
197+
-- -----------------------------
198+
199+
-- | /O(1)/ Convert strict array to lazy array
200+
toLazy :: Vector a -> V.Vector a
201+
toLazy (Vector v) = v
202+
203+
-- | /O(n)/ Convert lazy array to strict array. This function reduces
204+
-- each element of vector to WHNF.
205+
fromLazy :: V.Vector a -> Vector a
206+
fromLazy vec = liftRnf (`seq` ()) v `seq` v where v = Vector vec
207+
208+
209+
-- Conversions - Arrays
210+
-- -----------------------------
211+
212+
-- | /O(n)/ Convert an array to a vector and reduce each element to WHNF.
213+
--
214+
-- @since 0.13.2.0
215+
fromArray :: Array a -> Vector a
216+
{-# INLINE fromArray #-}
217+
fromArray arr = liftRnf (`seq` ()) vec `seq` vec
218+
where
219+
vec = Vector $ V.fromArray arr
220+
221+
-- | /O(n)/ Convert a vector to an array.
222+
--
223+
-- @since 0.13.2.0
224+
toArray :: Vector a -> Array a
225+
{-# INLINE toArray #-}
226+
toArray (Vector v) = V.toArray v
227+
228+
-- | /O(1)/ Extract the underlying `Array`, offset where vector starts and the
229+
-- total number of elements in the vector. Below property always holds:
230+
--
231+
-- > let (array, offset, len) = toArraySlice v
232+
-- > v === unsafeFromArraySlice len offset array
233+
--
234+
-- @since 0.13.2.0
235+
toArraySlice :: Vector a -> (Array a, Int, Int)
236+
{-# INLINE toArraySlice #-}
237+
toArraySlice (Vector v) = V.toArraySlice v
238+
192239

193240
-- Length information
194241
-- ------------------

vector/src/Data/Vector/Strict/Mutable.hs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ module Data.Vector.Strict.Mutable (
7878
PrimMonad, PrimState, RealWorld
7979
) where
8080

81+
import Data.Primitive.Array
8182
import qualified Data.Vector.Generic.Mutable as G
8283
import qualified Data.Vector.Mutable as MV
83-
import Data.Vector.Strict.Mutable.Unsafe
84-
(MVector,IOVector,STVector,toLazy,fromLazy,toMutableArray,fromMutableArray)
84+
import Data.Vector.Strict.Mutable.Unsafe (MVector)
8585
import qualified Data.Vector.Strict.Mutable.Unsafe as U
8686
import Control.Monad.Primitive
8787

88-
import Prelude ( Ord, Bool, Int, Maybe, Ordering(..))
88+
import Prelude ( Ord, Bool, Int, Maybe, Ordering(..), Monad(..), (<$>), ($))
8989

9090
#include "vector.h"
9191

@@ -94,6 +94,49 @@ pattern MVector v = U.MVector v
9494
{-# COMPLETE MVector #-}
9595
{-# DEPRECATED MVector "Use MVector constructor exported from \"Data.Vector.Strict.Unsafe\"" #-}
9696

97+
type IOVector = MVector RealWorld
98+
type STVector s = MVector s
99+
100+
101+
-- Conversions - Lazy vectors
102+
-- -----------------------------
103+
104+
-- | /O(1)/ Convert strict mutable vector to lazy mutable
105+
-- vector. Vectors will share mutable buffer
106+
toLazy :: MVector s a -> MV.MVector s a
107+
{-# INLINE toLazy #-}
108+
toLazy (MVector vec) = vec
109+
110+
-- | /O(n)/ Convert lazy mutable vector to strict mutable
111+
-- vector. Vectors will share mutable buffer. This function evaluates
112+
-- vector elements to WHNF.
113+
fromLazy :: PrimMonad m => MV.MVector (PrimState m) a -> m (MVector (PrimState m) a)
114+
fromLazy mvec = stToPrim $ do
115+
G.foldM' (\_ !_ -> return ()) () mvec
116+
return $ MVector mvec
117+
118+
119+
-- Conversions - Arrays
120+
-- -----------------------------
121+
122+
-- | /O(n)/ Make a copy of a mutable array to a new mutable
123+
-- vector. All elements of a vector are evaluated to WHNF
124+
--
125+
-- @since 0.13.2.0
126+
fromMutableArray :: PrimMonad m => MutableArray (PrimState m) a -> m (MVector (PrimState m) a)
127+
{-# INLINE fromMutableArray #-}
128+
fromMutableArray marr = stToPrim $ do
129+
mvec <- MVector <$> MV.fromMutableArray marr
130+
G.foldM' (\_ !_ -> return ()) () mvec
131+
return mvec
132+
133+
-- | /O(n)/ Make a copy of a mutable vector into a new mutable array.
134+
--
135+
-- @since 0.13.2.0
136+
toMutableArray :: PrimMonad m => MVector (PrimState m) a -> m (MutableArray (PrimState m) a)
137+
{-# INLINE toMutableArray #-}
138+
toMutableArray (MVector v) = MV.toMutableArray v
139+
97140

98141
-- Length information
99142
-- ------------------

0 commit comments

Comments
 (0)