+{-# OPTIONS_GHC -fno-bang-patterns #-}
+
-----------------------------------------------------------------------------
-- |
-- Module : Data.Array.Base
import Data.Ix ( Ix, range, index, rangeSize )
import Data.Int
import Data.Word
+import Foreign.C.Types
import Foreign.Ptr
import Foreign.StablePtr
import GHC.Stable ( StablePtr(..) )
import GHC.Int ( Int8(..), Int16(..), Int32(..), Int64(..) )
import GHC.Word ( Word8(..), Word16(..), Word32(..), Word64(..) )
+import GHC.IOBase ( IO(..) )
#endif
#ifdef __HUGS__
-----------------------------------------------------------------------------
-- Class of immutable arrays
--- | Class of array types with immutable bounds
--- (even if the array elements are mutable).
-class HasBounds a where
- -- | Extracts the bounds of an array
- bounds :: Ix i => a i e -> (i,i)
-
{- | Class of immutable array types.
An array type has the form @(a i e)@ where @a@ is the array type
parameterised over both @a@ and @e@, so that instances specialised to
certain element types can be defined.
-}
-class HasBounds a => IArray a e where
+class IArray a e where
+ -- | Extracts the bounds of an immutable array
+ bounds :: Ix i => a i e -> (i,i)
unsafeArray :: Ix i => (i,i) -> [(Int, e)] -> a i e
unsafeAt :: Ix i => a i e -> Int -> e
unsafeReplace :: Ix i => a i e -> [(Int, e)] -> a i e
{-# INLINE indices #-}
-- | Returns a list of all the valid indices in an array.
-indices :: (HasBounds a, Ix i) => a i e -> [i]
+indices :: (IArray a e, Ix i) => a i e -> [i]
indices arr = case bounds arr of (l,u) -> range (l,u)
{-# INLINE elems #-}
-----------------------------------------------------------------------------
-- Normal polymorphic arrays
-instance HasBounds Arr.Array where
+instance IArray Arr.Array e where
{-# INLINE bounds #-}
bounds = Arr.bounds
-
-instance IArray Arr.Array e where
{-# INLINE unsafeArray #-}
unsafeArray = Arr.unsafeArray
{-# INLINE unsafeAt #-}
INSTANCE_TYPEABLE2(UArray,uArrayTc,"UArray")
-instance HasBounds UArray where
- {-# INLINE bounds #-}
- bounds (UArray l u _) = (l,u)
-
{-# INLINE unsafeArrayUArray #-}
unsafeArrayUArray :: (MArray (STUArray s) e (ST s), Ix i)
=> (i,i) -> [(Int, e)] -> e -> ST s (UArray i e)
#endif
instance IArray UArray Bool where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies False)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Char where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies '\0')
{-# INLINE unsafeAt #-}
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Int where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Word where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray (Ptr a) where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies nullPtr)
{-# INLINE unsafeAt #-}
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray (FunPtr a) where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies nullFunPtr)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Float where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Double where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray (StablePtr a) where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies nullStablePtr)
#ifdef __GLASGOW_HASKELL__
#endif
instance IArray UArray Int8 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Int16 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Int32 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Int64 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Word8 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Word16 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Word32 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
instance IArray UArray Word64 where
+ {-# INLINE bounds #-}
+ bounds (UArray l u _) = (l,u)
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies 0)
#ifdef __GLASGOW_HASKELL__
{-# INLINE unsafeAccumArray #-}
unsafeAccumArray f init lu ies = runST (unsafeAccumArrayUArray f init lu ies)
-instance Ix ix => Eq (UArray ix Bool) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Char) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Int) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Word) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix (Ptr a)) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix (FunPtr a)) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Float) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Double) where
- (==) = eqUArray
-
-#ifdef __GLASGOW_HASKELL__
-instance Ix ix => Eq (UArray ix (StablePtr a)) where
- (==) = eqUArray
-#endif
-
-instance Ix ix => Eq (UArray ix Int8) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Int16) where
+instance (Ix ix, Eq e, IArray UArray e) => Eq (UArray ix e) where
(==) = eqUArray
-instance Ix ix => Eq (UArray ix Int32) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Int64) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Word8) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Word16) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Word32) where
- (==) = eqUArray
-
-instance Ix ix => Eq (UArray ix Word64) where
- (==) = eqUArray
-
-instance Ix ix => Ord (UArray ix Bool) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Char) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Int) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Word) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix (Ptr a)) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix (FunPtr a)) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Float) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Double) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Int8) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Int16) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Int32) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Int64) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Word8) where
+instance (Ix ix, Ord e, IArray UArray e) => Ord (UArray ix e) where
compare = cmpUArray
-instance Ix ix => Ord (UArray ix Word16) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Word32) where
- compare = cmpUArray
-
-instance Ix ix => Ord (UArray ix Word64) where
- compare = cmpUArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Bool) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Char) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Int) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Word) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Float) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Double) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Int8) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Int16) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Int32) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Int64) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Word8) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Word16) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Word32) where
- showsPrec = showsIArray
-
-instance (Ix ix, Show ix) => Show (UArray ix Word64) where
+instance (Ix ix, Show ix, Show e, IArray UArray e) => Show (UArray ix e) where
showsPrec = showsIArray
-----------------------------------------------------------------------------
same way as for 'IArray'), and also over the type of the monad, @m@,
in which the mutable array will be manipulated.
-}
-class (HasBounds a, Monad m) => MArray a e m where
+class (Monad m) => MArray a e m where
+
+ -- | Returns the bounds of the array
+ getBounds :: Ix i => a i e -> m (i,i)
-- | Builds a new array, with every element initialised to the supplied
-- value.
{-# INLINE readArray #-}
-- | Read an element from a mutable array
readArray :: (MArray a e m, Ix i) => a i e -> i -> m e
-readArray marr i = case bounds marr of
- (l,u) -> unsafeRead marr (index (l,u) i)
+readArray marr i = do
+ (l,u) <- getBounds marr
+ unsafeRead marr (index (l,u) i)
{-# INLINE writeArray #-}
-- | Write an element in a mutable array
writeArray :: (MArray a e m, Ix i) => a i e -> i -> e -> m ()
-writeArray marr i e = case bounds marr of
- (l,u) -> unsafeWrite marr (index (l,u) i) e
+writeArray marr i e = do
+ (l,u) <- getBounds marr
+ unsafeWrite marr (index (l,u) i) e
{-# INLINE getElems #-}
-- | Return a list of all the elements of a mutable array
getElems :: (MArray a e m, Ix i) => a i e -> m [e]
-getElems marr = case bounds marr of
- (l,u) -> sequence [unsafeRead marr i | i <- [0 .. rangeSize (l,u) - 1]]
+getElems marr = do
+ (l,u) <- getBounds marr
+ sequence [unsafeRead marr i | i <- [0 .. rangeSize (l,u) - 1]]
{-# INLINE getAssocs #-}
-- | Return a list of all the associations of a mutable array, in
-- index order.
getAssocs :: (MArray a e m, Ix i) => a i e -> m [(i, e)]
-getAssocs marr = case bounds marr of
- (l,u) -> sequence [do e <- unsafeRead marr (index (l,u) i); return (i,e)
- | i <- range (l,u)]
+getAssocs marr = do
+ (l,u) <- getBounds marr
+ sequence [ do e <- unsafeRead marr (index (l,u) i); return (i,e)
+ | i <- range (l,u)]
{-# INLINE mapArray #-}
-- | Constructs a new array derived from the original array by applying a
-- function to each of the elements.
mapArray :: (MArray a e' m, MArray a e m, Ix i) => (e' -> e) -> a i e' -> m (a i e)
-mapArray f marr = case bounds marr of
- (l,u) -> do
- marr' <- newArray_ (l,u)
- sequence_ [do
+mapArray f marr = do
+ (l,u) <- getBounds marr
+ marr' <- newArray_ (l,u)
+ sequence_ [do
e <- unsafeRead marr i
unsafeWrite marr' i (f e)
| i <- [0 .. rangeSize (l,u) - 1]]
- return marr'
+ return marr'
{-# INLINE mapIndices #-}
-- | Constructs a new array derived from the original array by applying a
-----------------------------------------------------------------------------
-- Polymorphic non-strict mutable arrays (ST monad)
-instance HasBounds (STArray s) where
- {-# INLINE bounds #-}
- bounds = ArrST.boundsSTArray
-
instance MArray (STArray s) e (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds arr = return $! ArrST.boundsSTArray arr
{-# INLINE newArray #-}
newArray = ArrST.newSTArray
{-# INLINE unsafeRead #-}
unsafeWrite = ArrST.unsafeWriteSTArray
instance MArray (STArray s) e (Lazy.ST s) where
+ {-# INLINE getBounds #-}
+ getBounds arr = strictToLazyST (return $! ArrST.boundsSTArray arr)
{-# INLINE newArray #-}
newArray (l,u) e = strictToLazyST (ArrST.newSTArray (l,u) e)
{-# INLINE unsafeRead #-}
INSTANCE_TYPEABLE3(STUArray,stUArrayTc,"STUArray")
-instance HasBounds (STUArray s) where
- {-# INLINE bounds #-}
- bounds (STUArray l u _) = (l,u)
-
#ifdef __GLASGOW_HASKELL__
instance MArray (STUArray s) Bool (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray #-}
newArray (l,u) init = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s3#, () #) }}}}
instance MArray (STUArray s) Char (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Int (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Word (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) (Ptr a) (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) (FunPtr a) (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Float (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Double (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) (StablePtr a) (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Int8 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Int16 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Int32 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Int64 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Word8 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Word16 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Word32 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
(# s2#, () #) }
instance MArray (STUArray s) Word64 (ST s) where
+ {-# INLINE getBounds #-}
+ getBounds (STUArray l u _) = return (l,u)
{-# INLINE newArray_ #-}
newArray_ (l,u) = ST $ \s1# ->
case rangeSize (l,u) of { I# n# ->
unsafeWriteMBArray :: Storable e => STUArray s i e -> Int -> e -> ST s ()
unsafeWriteMBArray (STUArray _ _ marr) = writeMutableByteArray marr
+getBoundsMBArray (STUArray l u _) = return (l,u)
+
instance MArray (STUArray s) Bool (ST s) where
+ getBounds = getBoundsMBArray
newArray_ (l,u) = do
marr <- newMutableByteArray (bOOL_SCALE (rangeSize (l,u)))
return (STUArray l u marr)
(if e then setBit (w::BitSet) bit else clearBit w bit)
instance MArray (STUArray s) Char (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Int (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Word (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) (Ptr a) (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) (FunPtr a) (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Float (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Double (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) (StablePtr a) (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Int8 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Int16 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Int32 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Int64 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Word8 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Word16 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Word32 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
instance MArray (STUArray s) Word64 (ST s) where
+ getBounds = getBoundsMBArray
newArray_ = newMBArray_
unsafeRead = unsafeReadMBArray
unsafeWrite = unsafeWriteMBArray
-- immutable array (any instance of 'IArray') by taking a complete
-- copy of it.
freeze :: (Ix i, MArray a e m, IArray b e) => a i e -> m (b i e)
-freeze marr = case bounds marr of
- (l,u) -> do
- ies <- sequence [do e <- unsafeRead marr i; return (i,e)
- | i <- [0 .. rangeSize (l,u) - 1]]
- return (unsafeArray (l,u) ies)
+freeze marr = do
+ (l,u) <- getBounds marr
+ ies <- sequence [do e <- unsafeRead marr i; return (i,e)
+ | i <- [0 .. rangeSize (l,u) - 1]]
+ return (unsafeArray (l,u) ies)
#ifdef __GLASGOW_HASKELL__
freezeSTUArray :: Ix i => STUArray s i e -> ST s (UArray i e)
freezeSTUArray (STUArray l u marr#) = ST $ \s1# ->
case sizeofMutableByteArray# marr# of { n# ->
case newByteArray# n# s1# of { (# s2#, marr'# #) ->
- case unsafeCoerce# memcpy marr'# marr# n# s2# of { (# s3#, () #) ->
+ case memcpy_freeze marr'# marr# (fromIntegral (I# n#)) of { IO m ->
+ case unsafeCoerce# m s2# of { (# s3#, _ #) ->
case unsafeFreezeByteArray# marr'# s3# of { (# s4#, arr# #) ->
- (# s4#, UArray l u arr# #) }}}}
+ (# s4#, UArray l u arr# #) }}}}}
+
+foreign import ccall unsafe "memcpy"
+ memcpy_freeze :: MutableByteArray# s -> MutableByteArray# s -> CSize
+ -> IO (Ptr a)
{-# RULES
"freeze/STArray" freeze = ArrST.freezeSTArray
thawSTUArray (UArray l u arr#) = ST $ \s1# ->
case sizeofByteArray# arr# of { n# ->
case newByteArray# n# s1# of { (# s2#, marr# #) ->
- case unsafeCoerce# memcpy marr# arr# n# s2# of { (# s3#, () #) ->
- (# s3#, STUArray l u marr# #) }}}
+ case memcpy_thaw marr# arr# (fromIntegral (I# n#)) of { IO m ->
+ case unsafeCoerce# m s2# of { (# s3#, _ #) ->
+ (# s3#, STUArray l u marr# #) }}}}
foreign import ccall unsafe "memcpy"
- memcpy :: MutableByteArray# RealWorld -> ByteArray# -> Int# -> IO ()
+ memcpy_thaw :: MutableByteArray# s -> ByteArray# -> CSize
+ -> IO (Ptr a)
{-# RULES
"thaw/STArray" thaw = ArrST.thawSTArray