Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions vector/src/Data/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,24 @@ module Data.Vector (
toList, Data.Vector.fromList, Data.Vector.fromListN,

-- ** Arrays
toArray, fromArray, toArraySlice, unsafeFromArraySlice,
toArray, fromArray, toArraySlice,

-- ** Other vector types
G.convert,

-- ** Mutable vectors
freeze, thaw, copy, unsafeFreeze, unsafeThaw, unsafeCopy
freeze, thaw, copy, unsafeFreeze, unsafeThaw, unsafeCopy,

-- * Deprecated
unsafeFromArraySlice
) where

import Control.Applicative (Applicative)
import Data.Primitive.Array
import qualified Data.Traversable as Traversable
import Data.Vector.Mutable.Unsafe ( MVector )
import Data.Vector.Unsafe
import Data.Vector.Unsafe ( Vector(..) )
import qualified Data.Vector.Unsafe as U
import qualified Data.Vector.Generic as G

import Control.Monad.ST ( ST )
Expand Down Expand Up @@ -2113,6 +2117,19 @@ copy :: PrimMonad m => MVector (PrimState m) a -> Vector a -> m ()
{-# INLINE copy #-}
copy = G.copy

-- Deprecated
-- ----------

-- | Unsafe functions are defined in "Data.Vector.Unsafe"
unsafeFromArraySlice ::
Array a -- ^ Immutable boxed array.
-> Int -- ^ Offset
-> Int -- ^ Length
-> Vector a
{-# INLINE unsafeFromArraySlice #-}
unsafeFromArraySlice = U.unsafeFromArraySlice
{-# DEPRECATED unsafeFromArraySlice "Use 'Data.Vector.Unsafe.unsafeCast'" #-}

-- $setup
-- >>> :set -Wno-type-defaults
-- >>> import Prelude (Char, String, Bool(True, False), min, max, fst, even, undefined, Ord(..), ($), (<>), Num(..))
17 changes: 16 additions & 1 deletion vector/src/Data/Vector/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,18 @@ module Data.Vector.Primitive (
) where

import Control.Applicative (Applicative)
import Data.Coerce (Coercible)
import qualified Data.Vector.Generic as G
import Data.Vector.Primitive.Unsafe (Vector,unsafeCoerceVector,unsafeCast)
import Data.Vector.Primitive.Unsafe (Vector)
import qualified Data.Vector.Primitive.Unsafe as U
import Data.Vector.Primitive.Mutable (MVector)
import Data.Vector.Primitive.Pattern

import Data.Primitive ( Prim )

import Control.Monad.ST ( ST )
import Control.Monad.Primitive
import GHC.Stack (HasCallStack)

import Prelude
( Eq, Ord, Num, Enum, Monoid, Traversable, Monad, Bool, Ordering(..), Int, Maybe, Either
Expand Down Expand Up @@ -1904,5 +1907,17 @@ copy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
{-# INLINE copy #-}
copy = G.copy

-- | Unsafe functions are defined in "Data.Vector.Primitive.Unsafe"
unsafeCoerceVector :: Coercible a b => Vector a -> Vector b
unsafeCoerceVector = U.unsafeCoerceVector
{-# DEPRECATED unsafeCoerceVector "Use 'Data.Vector.Primitive.Unsafe.unsafeCoerceVector'" #-}

-- | Unsafe functions are defined in "Data.Vector.Primitive.Unsafe"
unsafeCast :: forall a b. (HasCallStack, Prim a, Prim b) => Vector a -> Vector b
unsafeCast = U.unsafeCast
{-# INLINE unsafeCast #-}
{-# DEPRECATED unsafeCast "Use 'Data.Vector.Primitive.Unsafe.unsafeCast'" #-}


-- $setup
-- >>> import Prelude (($), min, even, max, succ, id, Ord(..), Num(..), undefined)
17 changes: 15 additions & 2 deletions vector/src/Data/Vector/Primitive/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ module Data.Vector.Primitive.Mutable (
Prim, PrimMonad, PrimState, RealWorld
) where

import Data.Coerce (Coercible)
import qualified Data.Vector.Generic.Mutable as G
import Data.Primitive ( Prim )
import Data.Vector.Primitive.Mutable.Unsafe
(MVector,unsafeCoerceMVector,unsafeCast)
import Data.Vector.Primitive.Mutable.Unsafe (MVector)
import qualified Data.Vector.Primitive.Mutable.Unsafe as U
import Data.Vector.Primitive.Pattern
import Control.Monad.Primitive

import GHC.Stack (HasCallStack)
import Prelude ( Ord, Bool, Int, Maybe, Ordering(..) )

#include "vector.h"
Expand Down Expand Up @@ -666,5 +668,16 @@ ifoldrM' :: (PrimMonad m, Prim a) => (Int -> a -> b -> m b) -> b -> MVector (Pri
{-# INLINE ifoldrM' #-}
ifoldrM' = G.ifoldrM'

-- | Unsafe functions are defined in "Data.Vector.Primitive.Mutable.Unsafe"
unsafeCoerceMVector :: Coercible a b => MVector s a -> MVector s b
unsafeCoerceMVector = U.unsafeCoerceMVector
{-# DEPRECATED unsafeCoerceMVector "Use 'Data.Vector.Primitive.Mutable.Unsafe.unsafeCoerceMVector'" #-}

-- | Unsafe functions are defined in "Data.Vector.Primitive.Mutable.Unsafe"
unsafeCast :: forall a b s. (HasCallStack, Prim a, Prim b) => MVector s a -> MVector s b
unsafeCast = U.unsafeCast
{-# INLINE unsafeCast #-}
{-# DEPRECATED unsafeCast "Use 'Data.Vector.Primitive.Mutable.Unsafe.unsafeCast'" #-}

-- $setup
-- >>> import Prelude (($), Num(..))
94 changes: 64 additions & 30 deletions vector/src/Data/Vector/Storable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,35 @@ module Data.Vector.Storable (
toList, fromList, fromListN,

-- ** Other vector types
G.convert, unsafeCast,
unsafeCoerceVector,
G.convert,

-- ** Mutable vectors
freeze, thaw, copy, unsafeFreeze, unsafeThaw, unsafeCopy,

-- * Raw pointers
-- * Re-exports
Storable,
-- * Deprecated
unsafeCast, unsafeCoerceVector,
unsafeFromForeignPtr, unsafeFromForeignPtr0,
unsafeToForeignPtr, unsafeToForeignPtr0,
unsafeWith,

-- * Re-exports
Storable
unsafeWith
) where

import Control.Applicative (Applicative)
import Data.Coerce
import qualified Data.Vector.Generic as G
import Data.Vector.Storable.Mutable ( MVector, pattern MVector )
import Data.Vector.Storable.Unsafe
import Data.Vector.Storable.Unsafe (Vector(..))
import qualified Data.Vector.Storable.Unsafe as U

import Control.Monad.ST ( ST )
import Control.Monad.Primitive
import Foreign.Storable
import Foreign.ForeignPtr
import Foreign.ForeignPtr (ForeignPtr)
import Foreign.Ptr (Ptr)
import Prelude
( Eq, Ord, Num, Enum, Monoid, Traversable, Monad, Bool, Ordering(..), Int, Maybe, Either
, undefined, div
, (*), (==), (&&))
( Eq,Ord,Num,Enum,Monoid,Traversable,Monad,Bool,Ordering(..),Int,Maybe,Either,IO
, (==), (&&))

#include "vector.h"

Expand Down Expand Up @@ -1885,21 +1886,6 @@ iforA_ :: (Applicative f, Storable a)
iforA_ = G.iforA_


-- Conversions - Unsafe casts
-- --------------------------

-- | /O(1)/ Unsafely cast a vector from one element type to another.
-- This operation just changes the type of the underlying pointer and does not
-- modify the elements.
--
-- The resulting vector contains as many elements as can fit into the
-- underlying memory block.
unsafeCast :: forall a b. (Storable a, Storable b) => Vector a -> Vector b
{-# INLINE unsafeCast #-}
unsafeCast (UnsafeVector n fp)
= UnsafeVector ((n * sizeOf (undefined :: a)) `div` sizeOf (undefined :: b))
(castForeignPtr fp)

-- Conversions - Mutable vectors
-- -----------------------------

Expand Down Expand Up @@ -1963,9 +1949,57 @@ copy :: (Storable a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
{-# INLINE copy #-}
copy = G.copy

-- Conversions - Raw pointers
-- --------------------------
-- Deprecated
-- ----------

-- | Unsafe functions are defined in "Data.Vector.Storable.Unsafe"
unsafeCoerceVector :: Coercible a b => Vector a -> Vector b
unsafeCoerceVector = U.unsafeCoerceVector
{-# DEPRECATED unsafeCoerceVector "Use 'Data.Vector.Storable.Unsafe.unsafeCoerceVector'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Unsafe"
unsafeCast :: forall a b. (Storable a, Storable b) => Vector a -> Vector b
{-# INLINE unsafeCast #-}
unsafeCast = U.unsafeCast
{-# DEPRECATED unsafeCast "Use 'Data.Vector.Storable.Unsafe.unsafeCast'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Unsafe"
unsafeFromForeignPtr :: Storable a
=> ForeignPtr a -- ^ pointer
-> Int -- ^ offset
-> Int -- ^ length
-> Vector a
{-# INLINE unsafeFromForeignPtr #-}
unsafeFromForeignPtr = U.unsafeFromForeignPtr
{-# DEPRECATED unsafeFromForeignPtr "Use 'Data.Vector.Storable.Unsafe.unsafeFromForeignPtr'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Unsafe"
unsafeFromForeignPtr0 :: ForeignPtr a -- ^ pointer
-> Int -- ^ length
-> Vector a
{-# INLINE unsafeFromForeignPtr0 #-}
unsafeFromForeignPtr0 = U.unsafeFromForeignPtr0
{-# DEPRECATED unsafeFromForeignPtr0 "Use 'Data.Vector.Storable.Unsafe.unsafeFromForeignPtr0'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Unsafe"
unsafeToForeignPtr :: Vector a -> (ForeignPtr a, Int, Int)
{-# INLINE unsafeToForeignPtr #-}
unsafeToForeignPtr = U.unsafeToForeignPtr
{-# DEPRECATED unsafeToForeignPtr "Use 'Data.Vector.Storable.Unsafe.unsafeToForeignPtr'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Unsafe"
unsafeToForeignPtr0 :: Vector a -> (ForeignPtr a, Int)
{-# INLINE unsafeToForeignPtr0 #-}
unsafeToForeignPtr0 = U.unsafeToForeignPtr0
{-# DEPRECATED unsafeToForeignPtr0 "Use 'Data.Vector.Storable.Unsafe.unsafeToForeignPtr0'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Unsafe"
unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO b
{-# INLINE unsafeWith #-}
unsafeWith = U.unsafeWith
{-# DEPRECATED unsafeWith "Use 'Data.Vector.Storable.Unsafe.unsafeWith'" #-}



-- $setup
-- >>> import Prelude (Bool(..), Double, ($), (+), (/), succ, even, min, max, id, Ord(..))
-- >>> import Prelude (Bool(..), Double, ($), (+), (/), succ, even, min, max, id, Ord(..), undefined)
75 changes: 65 additions & 10 deletions vector/src/Data/Vector/Storable/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,29 @@ module Data.Vector.Storable.Mutable(
-- ** Filling and copying
set, copy, move, unsafeCopy, unsafeMove,

-- * Unsafe conversions
unsafeCast,
unsafeCoerceMVector,
-- * Re-exports
Storable, PrimMonad, PrimState, RealWorld,

-- * Raw pointers
-- * Deprecated
unsafeCast, unsafeCoerceMVector,
unsafeFromForeignPtr, unsafeFromForeignPtr0,
unsafeToForeignPtr, unsafeToForeignPtr0,
unsafeWith,
-- * Re-exports
Storable, PrimMonad, PrimState, RealWorld
unsafeWith
) where

import Data.Coerce
import qualified Data.Vector.Generic.Mutable as G
import Data.Vector.Storable.Mutable.Unsafe(
MVector,IOVector,STVector,unsafeCast,unsafeWith,unsafeCoerceMVector,
unsafeToForeignPtr,unsafeToForeignPtr0,unsafeFromForeignPtr,unsafeFromForeignPtr0)
MVector,IOVector,STVector)
import qualified Data.Vector.Storable.Mutable.Unsafe as U
import Data.Vector.Storable.Pattern
import Foreign.Storable
import Foreign.ForeignPtr (ForeignPtr)
import Foreign.Ptr (Ptr)

import Control.Monad.Primitive

import Prelude (Int, Ord, Bool, Maybe, Ordering(..) )
import Prelude (IO, Int, Ord, Bool, Maybe, Ordering(..) )

#include "vector.h"

Expand Down Expand Up @@ -675,5 +676,59 @@ ifoldrM' :: (PrimMonad m, Storable a) => (Int -> a -> b -> m b) -> b -> MVector
{-# INLINE ifoldrM' #-}
ifoldrM' = G.ifoldrM'


-- Deprecated
-- ----------

-- | Unsafe functions are defined in "Data.Vector.Storable.Mutable.Unsafe"
unsafeCast :: forall a b s.
(Storable a, Storable b) => MVector s a -> MVector s b
{-# INLINE unsafeCast #-}
unsafeCast = U.unsafeCast
{-# DEPRECATED unsafeCast "Use 'Data.Vector.Storable.Unsafe.unsafeCast'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Mutable.Unsafe"
unsafeCoerceMVector :: Coercible a b => MVector s a -> MVector s b
unsafeCoerceMVector = U.unsafeCoerceMVector
{-# DEPRECATED unsafeCoerceMVector "Use 'Data.Vector.Storable.Unsafe.unsafeCoerceMVector'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Mutable.Unsafe"
unsafeFromForeignPtr :: Storable a
=> ForeignPtr a -- ^ pointer
-> Int -- ^ offset
-> Int -- ^ length
-> MVector s a
{-# INLINE_FUSED unsafeFromForeignPtr #-}
unsafeFromForeignPtr = U.unsafeFromForeignPtr
{-# DEPRECATED unsafeFromForeignPtr "Use 'Data.Vector.Storable.Unsafe.unsafeFromForeignPtr'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Mutable.Unsafe"
unsafeFromForeignPtr0 :: ForeignPtr a -- ^ pointer
-> Int -- ^ length
-> MVector s a
{-# INLINE unsafeFromForeignPtr0 #-}
unsafeFromForeignPtr0 = U.unsafeFromForeignPtr0
{-# DEPRECATED unsafeFromForeignPtr0 "Use 'Data.Vector.Storable.Unsafe.unsafeFromForeignPtr0'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Mutable.Unsafe"
unsafeToForeignPtr :: MVector s a -> (ForeignPtr a, Int, Int)
{-# INLINE unsafeToForeignPtr #-}
unsafeToForeignPtr = U.unsafeToForeignPtr
{-# DEPRECATED unsafeToForeignPtr "Use 'Data.Vector.Storable.Unsafe.unsafeToForeignPtr'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Mutable.Unsafe"
unsafeToForeignPtr0 :: MVector s a -> (ForeignPtr a, Int)
{-# INLINE unsafeToForeignPtr0 #-}
unsafeToForeignPtr0 = U.unsafeToForeignPtr0
{-# DEPRECATED unsafeToForeignPtr0 "Use 'Data.Vector.Storable.Unsafe.unsafeToForeignPtr0'" #-}

-- | Unsafe functions are defined in "Data.Vector.Storable.Mutable.Unsafe"
unsafeWith :: Storable a => IOVector a -> (Ptr a -> IO b) -> IO b
{-# INLINE unsafeWith #-}
unsafeWith = U.unsafeWith
{-# DEPRECATED unsafeWith "Use 'Data.Vector.Storable.Unsafe.unsafeWith'" #-}



-- $setup
-- >>> import Prelude (($), Num(..))
17 changes: 15 additions & 2 deletions vector/src/Data/Vector/Storable/Unsafe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
module Data.Vector.Storable.Unsafe
( Vector(..)
, unsafeCoerceVector
, unsafeCast
-- * Raw pointers
, unsafeFromForeignPtr, unsafeFromForeignPtr0
, unsafeToForeignPtr, unsafeToForeignPtr0
Expand All @@ -36,8 +37,8 @@ import Control.DeepSeq ( NFData(rnf), NFData1(liftRnf))
import Control.Monad.Primitive

import Prelude
( Eq, Ord, Monoid, Read, Show, Ordering(..), Int, IO
, compare, mempty, mappend, mconcat, showsPrec, return, seq
( Eq, Ord, Monoid, Read, Show, Ordering(..), Int, IO, Num(..)
, compare, mempty, mappend, mconcat, showsPrec, return, seq, undefined, div
, (<), (<=), (>), (>=), (==), (/=), (.), ($) )

import Data.Data ( Data(..) )
Expand All @@ -62,6 +63,18 @@ type role Vector nominal
unsafeCoerceVector :: Coercible a b => Vector a -> Vector b
unsafeCoerceVector = unsafeCoerce

-- | /O(1)/ Unsafely cast a vector from one element type to another.
-- This operation just changes the type of the underlying pointer and does not
-- modify the elements.
--
-- The resulting vector contains as many elements as can fit into the
-- underlying memory block.
unsafeCast :: forall a b. (Storable a, Storable b) => Vector a -> Vector b
{-# INLINE unsafeCast #-}
unsafeCast (UnsafeVector n fp)
= UnsafeVector ((n * sizeOf (undefined :: a)) `div` sizeOf (undefined :: b))
(castForeignPtr fp)

-- | 'Storable'-based vectors.
data Vector a = UnsafeVector
{ unsafeSize :: !Int
Expand Down
Loading
Loading