#include "inline.hs"
module Streamly.Internal.Data.Array.Foreign.Type
(
Array (..)
, unsafeFreeze
, unsafeFreezeWithShrink
, unsafeThaw
, spliceTwo
, fromPtr
, fromAddr#
, fromCString#
, fromList
, fromListN
, fromStreamDN
, fromStreamD
, breakOn
, unsafeIndexIO
, unsafeIndex
, byteLength
, length
, foldl'
, foldr
, splitAt
, readRev
, toStreamD
, toStreamDRev
, toStreamK
, toStreamKRev
, toStream
, toStreamRev
, toList
, toArrayMinChunk
, writeN
, writeNUnsafe
, MA.ArrayUnsafe (..)
, writeNAligned
, writeNAlignedUnmanaged
, write
, writeAligned
, arraysOf
, bufferChunks
, flattenArrays
, flattenArraysRev
, MA.defaultChunkSize
, MA.mkChunkSize
, MA.mkChunkSizeKB
, MA.unsafeInlineIO
, MA.memcpy
, MA.memcmp
, MA.bytesToElemCount
)
where
import Control.DeepSeq (NFData(..))
import Control.Monad.IO.Class (MonadIO(..))
import Data.Word (Word8)
import Foreign.C.String (CString)
import Foreign.C.Types (CSize(..))
import Foreign.Ptr (plusPtr, castPtr)
import Foreign.Storable (Storable(..))
import GHC.Base (Addr#, nullAddr#)
import GHC.Exts (IsList, IsString(..))
import GHC.ForeignPtr (ForeignPtr(..), newForeignPtr_)
#ifdef DEVBUILD
import GHC.ForeignPtr (touchForeignPtr, unsafeForeignPtrToPtr)
#endif
import GHC.IO (unsafePerformIO)
import GHC.Ptr (Ptr(..))
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Text.Read (readPrec, readListPrec, readListPrecDefault)
import Prelude hiding (length, foldr, read, unlines, splitAt)
import qualified Streamly.Internal.Data.Array.Foreign.Mut.Type as MA
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
import qualified Streamly.Internal.Data.Unfold.Type as Unfold
import qualified GHC.Exts as Exts
#if __GLASGOW_HASKELL__ < 808
import Data.Semigroup (Semigroup(..))
#endif
#ifdef DEVBUILD
import qualified Data.Foldable as F
#endif
data Array a =
#ifdef DEVBUILD
Storable a =>
#endif
Array
{ Array a -> ForeignPtr a
aStart :: {-# UNPACK #-} !(ForeignPtr a)
, Array a -> Ptr a
aEnd :: {-# UNPACK #-} !(Ptr a)
}
foreign import ccall unsafe "string.h strlen" c_strlen
:: CString -> IO CSize
{-# INLINE unsafeFreeze #-}
unsafeFreeze :: MA.Array a -> Array a
unsafeFreeze :: Array a -> Array a
unsafeFreeze (MA.Array ForeignPtr a
as Ptr a
ae Ptr a
_) = ForeignPtr a -> Ptr a -> Array a
forall a. ForeignPtr a -> Ptr a -> Array a
Array ForeignPtr a
as Ptr a
ae
{-# INLINE unsafeFreezeWithShrink #-}
unsafeFreezeWithShrink :: Storable a => MA.Array a -> Array a
unsafeFreezeWithShrink :: Array a -> Array a
unsafeFreezeWithShrink Array a
arr = IO (Array a) -> Array a
forall a. IO a -> a
unsafePerformIO (IO (Array a) -> Array a) -> IO (Array a) -> Array a
forall a b. (a -> b) -> a -> b
$ do
MA.Array ForeignPtr a
as Ptr a
ae Ptr a
_ <- Array a -> IO (Array a)
forall a. Storable a => Array a -> IO (Array a)
MA.shrinkToFit Array a
arr
Array a -> IO (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> IO (Array a)) -> Array a -> IO (Array a)
forall a b. (a -> b) -> a -> b
$ ForeignPtr a -> Ptr a -> Array a
forall a. ForeignPtr a -> Ptr a -> Array a
Array ForeignPtr a
as Ptr a
ae
{-# INLINE unsafeThaw #-}
unsafeThaw :: Array a -> MA.Array a
unsafeThaw :: Array a -> Array a
unsafeThaw (Array ForeignPtr a
as Ptr a
ae) = ForeignPtr a -> Ptr a -> Ptr a -> Array a
forall a. ForeignPtr a -> Ptr a -> Ptr a -> Array a
MA.Array ForeignPtr a
as Ptr a
ae Ptr a
ae
{-# INLINE spliceTwo #-}
spliceTwo :: (MonadIO m, Storable a) => Array a -> Array a -> m (Array a)
spliceTwo :: Array a -> Array a -> m (Array a)
spliceTwo Array a
arr1 Array a
arr2 =
Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Array a -> Array a) -> m (Array a) -> m (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Array a -> Array a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
MA.spliceTwo (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr1) (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr2)
{-# INLINE fromPtr #-}
fromPtr ::
#ifdef DEVBUILD
Storable a =>
#endif
Int -> Ptr a -> Array a
fromPtr :: Int -> Ptr a -> Array a
fromPtr Int
n Ptr a
ptr = IO (Array a) -> Array a
forall a. IO a -> a
MA.unsafeInlineIO (IO (Array a) -> Array a) -> IO (Array a) -> Array a
forall a b. (a -> b) -> a -> b
$ do
ForeignPtr a
fptr <- Ptr a -> IO (ForeignPtr a)
forall a. Ptr a -> IO (ForeignPtr a)
newForeignPtr_ Ptr a
ptr
let end :: Ptr b
end = Ptr a
ptr Ptr a -> Int -> Ptr b
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
n
Array a -> IO (Array a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array a -> IO (Array a)) -> Array a -> IO (Array a)
forall a b. (a -> b) -> a -> b
$ Array :: forall a. ForeignPtr a -> Ptr a -> Array a
Array
{ aStart :: ForeignPtr a
aStart = ForeignPtr a
fptr
, aEnd :: Ptr a
aEnd = Ptr a
forall b. Ptr b
end
}
{-# INLINE fromAddr# #-}
fromAddr# ::
#ifdef DEVBUILD
Storable a =>
#endif
Int -> Addr# -> Array a
fromAddr# :: Int -> Addr# -> Array a
fromAddr# Int
n Addr#
addr# = Int -> Ptr a -> Array a
forall a. Int -> Ptr a -> Array a
fromPtr Int
n (Ptr Any -> Ptr a
forall a b. Ptr a -> Ptr b
castPtr (Ptr Any -> Ptr a) -> Ptr Any -> Ptr a
forall a b. (a -> b) -> a -> b
$ Addr# -> Ptr Any
forall a. Addr# -> Ptr a
Ptr Addr#
addr#)
{-# INLINE fromCString# #-}
fromCString# :: Addr# -> Array Word8
fromCString# :: Addr# -> Array Word8
fromCString# Addr#
addr# = do
let cstr :: Ptr a
cstr = Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
addr#
len :: CSize
len = IO CSize -> CSize
forall a. IO a -> a
MA.unsafeInlineIO (IO CSize -> CSize) -> IO CSize -> CSize
forall a b. (a -> b) -> a -> b
$ CString -> IO CSize
c_strlen CString
forall b. Ptr b
cstr
Int -> Ptr Word8 -> Array Word8
forall a. Int -> Ptr a -> Array a
fromPtr (CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
len) (Ptr Any -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr Any
forall b. Ptr b
cstr)
{-# INLINABLE fromListN #-}
fromListN :: Storable a => Int -> [a] -> Array a
fromListN :: Int -> [a] -> Array a
fromListN Int
n [a]
xs = Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Array a -> Array a) -> Array a -> Array a
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> Array a
forall a. Storable a => Int -> [a] -> Array a
MA.fromListN Int
n [a]
xs
{-# INLINABLE fromList #-}
fromList :: Storable a => [a] -> Array a
fromList :: [a] -> Array a
fromList [a]
xs = Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Array a -> Array a) -> Array a -> Array a
forall a b. (a -> b) -> a -> b
$ [a] -> Array a
forall a. Storable a => [a] -> Array a
MA.fromList [a]
xs
{-# INLINE_NORMAL fromStreamDN #-}
fromStreamDN :: forall m a. (MonadIO m, Storable a)
=> Int -> D.Stream m a -> m (Array a)
fromStreamDN :: Int -> Stream m a -> m (Array a)
fromStreamDN Int
limit Stream m a
str = Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Array a -> Array a) -> m (Array a) -> m (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Stream m a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> m (Array a)
MA.fromStreamDN Int
limit Stream m a
str
{-# INLINE_NORMAL fromStreamD #-}
fromStreamD :: forall m a. (MonadIO m, Storable a)
=> D.Stream m a -> m (Array a)
fromStreamD :: Stream m a -> m (Array a)
fromStreamD Stream m a
str = Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Array a -> Array a) -> m (Array a) -> m (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream m a -> m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m a -> m (Array a)
MA.fromStreamD Stream m a
str
{-# INLINE bufferChunks #-}
bufferChunks :: (MonadIO m, Storable a) =>
D.Stream m a -> m (K.Stream m (Array a))
bufferChunks :: Stream m a -> m (Stream m (Array a))
bufferChunks Stream m a
m = (Array a -> Stream m (Array a) -> Stream m (Array a))
-> Stream m (Array a)
-> Stream m (Array a)
-> m (Stream m (Array a))
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr Array a -> Stream m (Array a) -> Stream m (Array a)
forall (t :: (* -> *) -> * -> *) a (m :: * -> *).
IsStream t =>
a -> t m a -> t m a
K.cons Stream m (Array a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
IsStream t =>
t m a
K.nil (Stream m (Array a) -> m (Stream m (Array a)))
-> Stream m (Array a) -> m (Stream m (Array a))
forall a b. (a -> b) -> a -> b
$ Int -> Stream m a -> Stream m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> Stream m (Array a)
arraysOf Int
MA.defaultChunkSize Stream m a
m
{-# INLINE_NORMAL arraysOf #-}
arraysOf :: forall m a. (MonadIO m, Storable a)
=> Int -> D.Stream m a -> D.Stream m (Array a)
arraysOf :: Int -> Stream m a -> Stream m (Array a)
arraysOf Int
n Stream m a
str = (Array a -> Array a) -> Stream m (Array a) -> Stream m (Array a)
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Stream m (Array a) -> Stream m (Array a))
-> Stream m (Array a) -> Stream m (Array a)
forall a b. (a -> b) -> a -> b
$ Int -> Stream m a -> Stream m (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> Stream m (Array a)
MA.arraysOf Int
n Stream m a
str
{-# INLINE_NORMAL flattenArrays #-}
flattenArrays :: forall m a. (MonadIO m, Storable a)
=> D.Stream m (Array a) -> D.Stream m a
flattenArrays :: Stream m (Array a) -> Stream m a
flattenArrays = Stream m (Array a) -> Stream m a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m (Array a) -> Stream m a
MA.flattenArrays (Stream m (Array a) -> Stream m a)
-> (Stream m (Array a) -> Stream m (Array a))
-> Stream m (Array a)
-> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array a -> Array a) -> Stream m (Array a) -> Stream m (Array a)
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map Array a -> Array a
forall a. Array a -> Array a
unsafeThaw
{-# INLINE_NORMAL flattenArraysRev #-}
flattenArraysRev :: forall m a. (MonadIO m, Storable a)
=> D.Stream m (Array a) -> D.Stream m a
flattenArraysRev :: Stream m (Array a) -> Stream m a
flattenArraysRev = Stream m (Array a) -> Stream m a
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m (Array a) -> Stream m a
MA.flattenArraysRev (Stream m (Array a) -> Stream m a)
-> (Stream m (Array a) -> Stream m (Array a))
-> Stream m (Array a)
-> Stream m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Array a -> Array a) -> Stream m (Array a) -> Stream m (Array a)
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map Array a -> Array a
forall a. Array a -> Array a
unsafeThaw
{-# INLINE breakOn #-}
breakOn :: MonadIO m
=> Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
breakOn :: Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
breakOn Word8
sep Array Word8
arr = do
(Array Word8
a, Maybe (Array Word8)
b) <- Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
MA.breakOn Word8
sep (Array Word8 -> Array Word8
forall a. Array a -> Array a
unsafeThaw Array Word8
arr)
(Array Word8, Maybe (Array Word8))
-> m (Array Word8, Maybe (Array Word8))
forall (m :: * -> *) a. Monad m => a -> m a
return (Array Word8 -> Array Word8
forall a. Array a -> Array a
unsafeFreeze Array Word8
a, Array Word8 -> Array Word8
forall a. Array a -> Array a
unsafeFreeze (Array Word8 -> Array Word8)
-> Maybe (Array Word8) -> Maybe (Array Word8)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Array Word8)
b)
{-# INLINE_NORMAL unsafeIndexIO #-}
unsafeIndexIO :: forall a. Storable a => Array a -> Int -> IO a
unsafeIndexIO :: Array a -> Int -> IO a
unsafeIndexIO Array a
arr = Array a -> Int -> IO a
forall a. Storable a => Array a -> Int -> IO a
MA.unsafeIndexIO (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL unsafeIndex #-}
unsafeIndex :: forall a. Storable a => Array a -> Int -> a
unsafeIndex :: Array a -> Int -> a
unsafeIndex Array a
arr = Array a -> Int -> a
forall a. Storable a => Array a -> Int -> a
MA.unsafeIndex (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE byteLength #-}
byteLength :: Array a -> Int
byteLength :: Array a -> Int
byteLength = Array a -> Int
forall a. Array a -> Int
MA.byteLength (Array a -> Int) -> (Array a -> Array a) -> Array a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> Array a
forall a. Array a -> Array a
unsafeThaw
{-# INLINE length #-}
length :: forall a. Storable a => Array a -> Int
length :: Array a -> Int
length Array a
arr = Array a -> Int
forall a. Storable a => Array a -> Int
MA.length (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL readRev #-}
readRev :: forall m a. (Monad m, Storable a) => Unfold m (Array a) a
readRev :: Unfold m (Array a) a
readRev = (Array a -> Array a)
-> Unfold m (Array a) a -> Unfold m (Array a) a
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Unfold m (Array a) a
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
MA.readRev
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: forall m a. (Monad m, Storable a) => Array a -> D.Stream m a
toStreamD :: Array a -> Stream m a
toStreamD Array a
arr = Array a -> Stream m a
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
MA.toStreamD (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE toStreamK #-}
toStreamK :: forall t m a. (K.IsStream t, Storable a) => Array a -> t m a
toStreamK :: Array a -> t m a
toStreamK Array a
arr = Array a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Storable a) =>
Array a -> t m a
MA.toStreamK (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL toStreamDRev #-}
toStreamDRev :: forall m a. (Monad m, Storable a) => Array a -> D.Stream m a
toStreamDRev :: Array a -> Stream m a
toStreamDRev Array a
arr = Array a -> Stream m a
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
MA.toStreamDRev (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE toStreamKRev #-}
toStreamKRev :: forall t m a. (K.IsStream t, Storable a) => Array a -> t m a
toStreamKRev :: Array a -> t m a
toStreamKRev Array a
arr = Array a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Storable a) =>
Array a -> t m a
MA.toStreamKRev (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE_EARLY toStream #-}
toStream :: (Monad m, K.IsStream t, Storable a) => Array a -> t m a
toStream :: Array a -> t m a
toStream = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (Stream m a -> t m a)
-> (Array a -> Stream m a) -> Array a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> Stream m a
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamD
{-# INLINE_EARLY toStreamRev #-}
toStreamRev :: (Monad m, K.IsStream t, Storable a) => Array a -> t m a
toStreamRev :: Array a -> t m a
toStreamRev = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (Stream m a -> t m a)
-> (Array a -> Stream m a) -> Array a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> Stream m a
forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamDRev
{-# INLINE_NORMAL foldl' #-}
foldl' :: forall a b. Storable a => (b -> a -> b) -> b -> Array a -> b
foldl' :: (b -> a -> b) -> b -> Array a -> b
foldl' b -> a -> b
f b
z Array a
arr = (b -> a -> b) -> b -> Array a -> b
forall a b. Storable a => (b -> a -> b) -> b -> Array a -> b
MA.foldl' b -> a -> b
f b
z (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL foldr #-}
foldr :: Storable a => (a -> b -> b) -> b -> Array a -> b
foldr :: (a -> b -> b) -> b -> Array a -> b
foldr a -> b -> b
f b
z Array a
arr = (a -> b -> b) -> b -> Array a -> b
forall a b. Storable a => (a -> b -> b) -> b -> Array a -> b
MA.foldr a -> b -> b
f b
z (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
splitAt :: forall a. Storable a => Int -> Array a -> (Array a, Array a)
splitAt :: Int -> Array a -> (Array a, Array a)
splitAt Int
i Array a
arr = (Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze Array a
a, Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze Array a
b)
where
(Array a
a, Array a
b) = Int -> Array a -> (Array a, Array a)
forall a. Storable a => Int -> Array a -> (Array a, Array a)
MA.splitAt Int
i (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE toList #-}
toList :: Storable a => Array a -> [a]
toList :: Array a -> [a]
toList = Array a -> [a]
forall a. Storable a => Array a -> [a]
MA.toList (Array a -> [a]) -> (Array a -> Array a) -> Array a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> Array a
forall a. Array a -> Array a
unsafeThaw
{-# INLINE_NORMAL writeN #-}
writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
writeN :: Int -> Fold m a (Array a)
writeN = (Array a -> Array a) -> Fold m a (Array a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Fold m a (Array a) -> Fold m a (Array a))
-> (Int -> Fold m a (Array a)) -> Int -> Fold m a (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
MA.writeN
{-# INLINE_NORMAL writeNAligned #-}
writeNAligned :: forall m a. (MonadIO m, Storable a)
=> Int -> Int -> Fold m a (Array a)
writeNAligned :: Int -> Int -> Fold m a (Array a)
writeNAligned Int
alignSize = (Array a -> Array a) -> Fold m a (Array a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Fold m a (Array a) -> Fold m a (Array a))
-> (Int -> Fold m a (Array a)) -> Int -> Fold m a (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> Fold m a (Array a)
MA.writeNAligned Int
alignSize
{-# INLINE_NORMAL writeNAlignedUnmanaged #-}
writeNAlignedUnmanaged :: forall m a. (MonadIO m, Storable a)
=> Int -> Int -> Fold m a (Array a)
writeNAlignedUnmanaged :: Int -> Int -> Fold m a (Array a)
writeNAlignedUnmanaged Int
alignSize =
(Array a -> Array a) -> Fold m a (Array a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Fold m a (Array a) -> Fold m a (Array a))
-> (Int -> Fold m a (Array a)) -> Int -> Fold m a (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> Fold m a (Array a)
MA.writeNAlignedUnmanaged Int
alignSize
{-# INLINE_NORMAL writeNUnsafe #-}
writeNUnsafe :: forall m a. (MonadIO m, Storable a)
=> Int -> Fold m a (Array a)
writeNUnsafe :: Int -> Fold m a (Array a)
writeNUnsafe Int
n = Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Array a -> Array a) -> Fold m a (Array a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
MA.writeNUnsafe Int
n
{-# INLINE_NORMAL toArrayMinChunk #-}
toArrayMinChunk :: forall m a. (MonadIO m, Storable a)
=> Int -> Int -> Fold m a (Array a)
toArrayMinChunk :: Int -> Int -> Fold m a (Array a)
toArrayMinChunk Int
alignSize Int
elemCount =
Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Array a -> Array a) -> Fold m a (Array a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> Fold m a (Array a)
MA.toArrayMinChunk Int
alignSize Int
elemCount
{-# INLINE write #-}
write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
write :: Fold m a (Array a)
write = (Array a -> Array a) -> Fold m a (Array a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Fold m a (Array a)
MA.write
{-# INLINE writeAligned #-}
writeAligned :: forall m a. (MonadIO m, Storable a)
=> Int -> Fold m a (Array a)
writeAligned :: Int -> Fold m a (Array a)
writeAligned = (Array a -> Array a) -> Fold m a (Array a) -> Fold m a (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Array a -> Array a
forall a. Array a -> Array a
unsafeFreeze (Fold m a (Array a) -> Fold m a (Array a))
-> (Int -> Fold m a (Array a)) -> Int -> Fold m a (Array a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Fold m a (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
MA.writeAligned
instance (Show a, Storable a) => Show (Array a) where
{-# INLINE showsPrec #-}
showsPrec :: Int -> Array a -> ShowS
showsPrec Int
_ = [a] -> ShowS
forall a. Show a => a -> ShowS
shows ([a] -> ShowS) -> (Array a -> [a]) -> Array a -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array a -> [a]
forall a. Storable a => Array a -> [a]
toList
instance (Storable a, Read a, Show a) => Read (Array a) where
{-# INLINE readPrec #-}
readPrec :: ReadPrec (Array a)
readPrec = [a] -> Array a
forall a. Storable a => [a] -> Array a
fromList ([a] -> Array a) -> ReadPrec [a] -> ReadPrec (Array a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadPrec [a]
forall a. Read a => ReadPrec a
readPrec
readListPrec :: ReadPrec [Array a]
readListPrec = ReadPrec [Array a]
forall a. Read a => ReadPrec [a]
readListPrecDefault
instance (a ~ Char) => IsString (Array a) where
{-# INLINE fromString #-}
fromString :: String -> Array a
fromString = String -> Array a
forall a. Storable a => [a] -> Array a
fromList
instance Storable a => IsList (Array a) where
type (Item (Array a)) = a
{-# INLINE fromList #-}
fromList :: [Item (Array a)] -> Array a
fromList = [Item (Array a)] -> Array a
forall a. Storable a => [a] -> Array a
fromList
{-# INLINE fromListN #-}
fromListN :: Int -> [Item (Array a)] -> Array a
fromListN = Int -> [Item (Array a)] -> Array a
forall a. Storable a => Int -> [a] -> Array a
fromListN
{-# INLINE toList #-}
toList :: Array a -> [Item (Array a)]
toList = Array a -> [Item (Array a)]
forall a. Storable a => Array a -> [a]
toList
instance (Storable a, Eq a) => Eq (Array a) where
{-# INLINE (==) #-}
Array a
a == :: Array a -> Array a -> Bool
== Array a
b = Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
a Array a -> Array a -> Bool
forall a. Eq a => a -> a -> Bool
== Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
b
instance (Storable a, NFData a) => NFData (Array a) where
{-# INLINE rnf #-}
rnf :: Array a -> ()
rnf = (() -> a -> ()) -> () -> Array a -> ()
forall a b. Storable a => (b -> a -> b) -> b -> Array a -> b
foldl' (\()
_ a
x -> a -> ()
forall a. NFData a => a -> ()
rnf a
x) ()
instance (Storable a, Ord a) => Ord (Array a) where
{-# INLINE compare #-}
compare :: Array a -> Array a -> Ordering
compare Array a
a Array a
b = Array a -> Array a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
a) (Array a -> Array a
forall a. Array a -> Array a
unsafeThaw Array a
b)
{-# INLINE (<) #-}
Array a
x < :: Array a -> Array a -> Bool
< Array a
y = case Array a -> Array a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Array a
x Array a
y of { Ordering
LT -> Bool
True; Ordering
_ -> Bool
False }
{-# INLINE (<=) #-}
Array a
x <= :: Array a -> Array a -> Bool
<= Array a
y = case Array a -> Array a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Array a
x Array a
y of { Ordering
GT -> Bool
False; Ordering
_ -> Bool
True }
{-# INLINE (>) #-}
Array a
x > :: Array a -> Array a -> Bool
> Array a
y = case Array a -> Array a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Array a
x Array a
y of { Ordering
GT -> Bool
True; Ordering
_ -> Bool
False }
{-# INLINE (>=) #-}
Array a
x >= :: Array a -> Array a -> Bool
>= Array a
y = case Array a -> Array a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Array a
x Array a
y of { Ordering
LT -> Bool
False; Ordering
_ -> Bool
True }
{-# INLINE max #-}
max :: Array a -> Array a -> Array a
max Array a
x Array a
y = if Array a
x Array a -> Array a -> Bool
forall a. Ord a => a -> a -> Bool
<= Array a
y then Array a
y else Array a
x
{-# INLINE min #-}
min :: Array a -> Array a -> Array a
min Array a
x Array a
y = if Array a
x Array a -> Array a -> Bool
forall a. Ord a => a -> a -> Bool
<= Array a
y then Array a
x else Array a
y
#ifdef DEVBUILD
{-# INLINE_NORMAL toStreamD_ #-}
toStreamD_ :: forall m a. MonadIO m => Int -> Array a -> D.Stream m a
toStreamD_ size Array{..} =
let p = unsafeForeignPtrToPtr aStart
in D.Stream step p
where
{-# INLINE_LATE step #-}
step _ p | p == aEnd = return D.Stop
step _ p = do
x <- liftIO $ do
r <- peek p
touchForeignPtr aStart
return r
return $ D.Yield x (p `plusPtr` size)
{-# INLINE_NORMAL _foldr #-}
_foldr :: forall a b. (a -> b -> b) -> b -> Array a -> b
_foldr f z arr@Array {..} =
let !n = sizeOf (undefined :: a)
in unsafePerformIO $ D.foldr f z $ toStreamD_ n arr
instance Foldable Array where
foldr = _foldr
#endif
instance Storable a => Semigroup (Array a) where
Array a
arr1 <> :: Array a -> Array a -> Array a
<> Array a
arr2 = IO (Array a) -> Array a
forall a. IO a -> a
unsafePerformIO (IO (Array a) -> Array a) -> IO (Array a) -> Array a
forall a b. (a -> b) -> a -> b
$ Array a -> Array a -> IO (Array a)
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
spliceTwo Array a
arr1 Array a
arr2
nullForeignPtr :: ForeignPtr a
nullForeignPtr :: ForeignPtr a
nullForeignPtr = Addr# -> ForeignPtrContents -> ForeignPtr a
forall a. Addr# -> ForeignPtrContents -> ForeignPtr a
ForeignPtr Addr#
nullAddr# (String -> ForeignPtrContents
forall a. HasCallStack => String -> a
error String
"nullForeignPtr")
nil ::
#ifdef DEVBUILD
Storable a =>
#endif
Array a
nil :: Array a
nil = ForeignPtr a -> Ptr a -> Array a
forall a. ForeignPtr a -> Ptr a -> Array a
Array ForeignPtr a
forall a. ForeignPtr a
nullForeignPtr (Addr# -> Ptr a
forall a. Addr# -> Ptr a
Ptr Addr#
nullAddr#)
instance Storable a => Monoid (Array a) where
mempty :: Array a
mempty = Array a
forall a. Array a
nil
mappend :: Array a -> Array a -> Array a
mappend = Array a -> Array a -> Array a
forall a. Semigroup a => a -> a -> a
(<>)