@@ -6,13 +6,12 @@
6{-# LANGUAGE TypeFamilies #-}
6{-# LANGUAGE TypeFamilies #-}
7{-# LANGUAGE UnboxedTuples #-}
7{-# LANGUAGE UnboxedTuples #-}
11−-- Module : Data.Primitive.PrimArray
10+-- Module : Streamly.Internal.Data.Prim.Array.Types
12-- Copyright : (c) Roman Leshchinskiy 2009-2012
11-- Copyright : (c) Roman Leshchinskiy 2009-2012
13-- License : BSD-style
12-- License : BSD-style
15−-- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
14+-- Maintainer : streamly@composewell.com
16-- Portability : non-portable
15-- Portability : non-portable
18-- Arrays of unboxed primitive types. The function provided by this module
17-- Arrays of unboxed primitive types. The function provided by this module
@@ -25,16 +24,14 @@
25-- terms of elements, not bytes.
24-- terms of elements, not bytes.
27-- @since 0.6.4.0
26-- @since 0.6.4.0
28−module Data.Primitive.PrimArray
27+module Streamly.Internal.Data.Prim.Array.Types
29 ( -- * Types
28 ( -- * Types
30 PrimArray(..)
29 PrimArray(..)
31 , MutablePrimArray(..)
30 , MutablePrimArray(..)
32 -- * Allocation
31 -- * Allocation
33 , newPrimArray
32 , newPrimArray
34 , resizeMutablePrimArray
33 , resizeMutablePrimArray
35−#if __GLASGOW_HASKELL__ >= 710
36 , shrinkMutablePrimArray
34 , shrinkMutablePrimArray
38 -- * Element Access
35 -- * Element Access
39 , readPrimArray
36 , readPrimArray
40 , writePrimArray
37 , writePrimArray
@@ -45,10 +42,8 @@ module Data.Primitive.PrimArray
45 -- * Block Operations
42 -- * Block Operations
46 , copyPrimArray
43 , copyPrimArray
47 , copyMutablePrimArray
44 , copyMutablePrimArray
48−#if __GLASGOW_HASKELL__ >= 708
49 , copyPrimArrayToPtr
45 , copyPrimArrayToPtr
50 , copyMutablePrimArrayToPtr
46 , copyMutablePrimArrayToPtr
52 , setPrimArray
47 , setPrimArray
53 -- * Information
48 -- * Information
54 , sameMutablePrimArray
49 , sameMutablePrimArray
@@ -94,11 +89,12 @@ module Data.Primitive.PrimArray
96import GHC.Exts
91import GHC.Exts
97−import GHC.Base ( Int(..) )
92+
98−import Data.Primitive.Internal.Compat (isTrue#)
99import Data.Primitive.Types
93import Data.Primitive.Types
100import Data.Primitive.ByteArray (ByteArray(..))
94import Data.Primitive.ByteArray (ByteArray(..))
95+#if !MIN_VERSION_base(4,11,0)
101import Data.Monoid (Monoid(..),(<>))
96import Data.Monoid (Monoid(..),(<>))
102import Control.Applicative
98import Control.Applicative
103import Control.Monad.Primitive
99import Control.Monad.Primitive
104import Control.Monad.ST
100import Control.Monad.ST
@@ -106,12 +102,10 @@ import qualified Data.List as L
106import qualified Data.Primitive.ByteArray as PB
102import qualified Data.Primitive.ByteArray as PB
107import qualified Data.Primitive.Types as PT
103import qualified Data.Primitive.Types as PT
109−#if MIN_VERSION_base(4,7,0)
105+#if MIN_VERSION_base(4,9,0) && !MIN_VERSION_base(4,11,0)
110−import GHC.Exts (IsList(..))
106+import Data.Semigroup (Semigroup)
113#if MIN_VERSION_base(4,9,0)
108#if MIN_VERSION_base(4,9,0)
114−import Data.Semigroup (Semigroup)
115import qualified Data.Semigroup as SG
109import qualified Data.Semigroup as SG
@@ -134,12 +128,7 @@ data MutablePrimArray s a = MutablePrimArray (MutableByteArray# s)
134sameByteArray :: ByteArray# -> ByteArray# -> Bool
128sameByteArray :: ByteArray# -> ByteArray# -> Bool
135sameByteArray ba1 ba2 =
129sameByteArray ba1 ba2 =
136 case reallyUnsafePtrEquality# (unsafeCoerce# ba1 :: ()) (unsafeCoerce# ba2 :: ()) of
130 case reallyUnsafePtrEquality# (unsafeCoerce# ba1 :: ()) (unsafeCoerce# ba2 :: ()) of
137−#if __GLASGOW_HASKELL__ >= 708
138 r -> isTrue# r
131 r -> isTrue# r
144-- | @since 0.6.4.0
133-- | @since 0.6.4.0
145instance (Eq a, Prim a) => Eq (PrimArray a) where
134instance (Eq a, Prim a) => Eq (PrimArray a) where
@@ -174,14 +163,12 @@ instance (Ord a, Prim a) => Ord (PrimArray a) where
174 | otherwise = compare sz1 sz2
163 | otherwise = compare sz1 sz2
175 {-# INLINE compare #-}
164 {-# INLINE compare #-}
177−#if MIN_VERSION_base(4,7,0)
178-- | @since 0.6.4.0
166-- | @since 0.6.4.0
179instance Prim a => IsList (PrimArray a) where
167instance Prim a => IsList (PrimArray a) where
180 type Item (PrimArray a) = a
168 type Item (PrimArray a) = a
181 fromList = primArrayFromList
169 fromList = primArrayFromList
182 fromListN = primArrayFromListN
170 fromListN = primArrayFromListN
183 toList = primArrayToList
171 toList = primArrayToList
186-- | @since 0.6.4.0
173-- | @since 0.6.4.0
187instance (Show a, Prim a) => Show (PrimArray a) where
174instance (Show a, Prim a) => Show (PrimArray a) where
@@ -271,20 +258,13 @@ resizeMutablePrimArray :: forall m a. (PrimMonad m, Prim a)
271 -> Int -- ^ new size
258 -> Int -- ^ new size
272 -> m (MutablePrimArray (PrimState m) a)
259 -> m (MutablePrimArray (PrimState m) a)
273{-# INLINE resizeMutablePrimArray #-}
260{-# INLINE resizeMutablePrimArray #-}
274−#if __GLASGOW_HASKELL__ >= 710
275resizeMutablePrimArray (MutablePrimArray arr#) (I# n#)
261resizeMutablePrimArray (MutablePrimArray arr#) (I# n#)
276 = primitive (\s# -> case resizeMutableByteArray# arr# (n# *# sizeOf# (undefined :: a)) s# of
262 = primitive (\s# -> case resizeMutableByteArray# arr# (n# *# sizeOf# (undefined :: a)) s# of
277 (# s'#, arr'# #) -> (# s'#, MutablePrimArray arr'# #))
263 (# s'#, arr'# #) -> (# s'#, MutablePrimArray arr'# #))
279−resizeMutablePrimArray arr n
280− = do arr' <- newPrimArray n
281− copyMutablePrimArray arr' 0 arr 0 (min (sizeofMutablePrimArray arr) n)
285-- Although it is possible to shim resizeMutableByteArray for old GHCs, this
265-- Although it is possible to shim resizeMutableByteArray for old GHCs, this
286-- is not the case with shrinkMutablePrimArray.
266-- is not the case with shrinkMutablePrimArray.
287−#if __GLASGOW_HASKELL__ >= 710
267+
288-- | Shrink a mutable primitive array. The new size is given in elements.
268-- | Shrink a mutable primitive array. The new size is given in elements.
289-- It must be smaller than the old size. The array will be resized in place.
269-- It must be smaller than the old size. The array will be resized in place.
290-- This function is only available when compiling with GHC 7.10 or newer.
270-- This function is only available when compiling with GHC 7.10 or newer.
@@ -295,7 +275,6 @@ shrinkMutablePrimArray :: forall m a. (PrimMonad m, Prim a)
295{-# INLINE shrinkMutablePrimArray #-}
275{-# INLINE shrinkMutablePrimArray #-}
296shrinkMutablePrimArray (MutablePrimArray arr#) (I# n#)
276shrinkMutablePrimArray (MutablePrimArray arr#) (I# n#)
297 = primitive_ (shrinkMutableByteArray# arr# (n# *# sizeOf# (undefined :: a)))
277 = primitive_ (shrinkMutableByteArray# arr# (n# *# sizeOf# (undefined :: a)))
300readPrimArray :: (Prim a, PrimMonad m) => MutablePrimArray (PrimState m) a -> Int -> m a
279readPrimArray :: (Prim a, PrimMonad m) => MutablePrimArray (PrimState m) a -> Int -> m a
301{-# INLINE readPrimArray #-}
280{-# INLINE readPrimArray #-}
@@ -353,7 +332,6 @@ copyPrimArray (MutablePrimArray dst#) (I# doff#) (PrimArray src#) (I# soff#) (I#
353 (n# *# (sizeOf# (undefined :: a)))
332 (n# *# (sizeOf# (undefined :: a)))
356−#if __GLASGOW_HASKELL__ >= 708
357-- | Copy a slice of an immutable primitive array to an address.
335-- | Copy a slice of an immutable primitive array to an address.
358-- The offset and length are given in elements of type @a@.
336-- The offset and length are given in elements of type @a@.
359-- This function assumes that the 'Prim' instance of @a@
337-- This function assumes that the 'Prim' instance of @a@
@@ -389,7 +367,6 @@ copyMutablePrimArrayToPtr (Ptr addr#) (MutablePrimArray mba#) (I# soff#) (I# n#)
389 let s'# = copyMutableByteArrayToAddr# mba# (soff# *# siz#) addr# (n# *# siz#) s#
367 let s'# = copyMutableByteArrayToAddr# mba# (soff# *# siz#) addr# (n# *# siz#) s#
390 in (# s'#, () #))
368 in (# s'#, () #))
391 where siz# = sizeOf# (undefined :: a)
369 where siz# = sizeOf# (undefined :: a)
394-- | Fill a slice of a mutable primitive array with a value.
371-- | Fill a slice of a mutable primitive array with a value.
395setPrimArray
372setPrimArray