module Streamly.Internal.Data.Array.Foreign.Type
(
Array (..)
, asPtrUnsafe
, unsafeFreeze
, unsafeFreezeWithShrink
, unsafeThaw
, splice
, fromPtr
, fromForeignPtrUnsafe
, fromAddr#
, fromCString#
, fromList
, fromListN
, fromListRev
, fromListRevN
, fromStreamDN
, fromStreamD
, breakOn
, unsafeIndexIO
, unsafeIndex
, byteLength
, length
, foldl'
, foldr
, splitAt
, readRev
, toStreamD
, toStreamDRev
, toStreamK
, toStreamKRev
, toStream
, toStreamRev
, toList
, writeWith
, writeN
, writeNUnsafe
, MA.ArrayUnsafe (..)
, writeNAligned
, writeNAlignedUnmanaged
, write
, arraysOf
, bufferChunks
, flattenArrays
, flattenArraysRev
)
where
#include "ArrayMacros.h"
#include "inline.hs"
import Control.Exception (assert)
import Control.DeepSeq (NFData(..))
import Control.Monad.IO.Class (MonadIO(..))
import Data.Functor.Identity (Identity(..))
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#, build)
import GHC.Exts (IsList, IsString(..))
import GHC.ForeignPtr (ForeignPtr)
import GHC.IO (unsafePerformIO)
import GHC.Ptr (Ptr(..))
import Streamly.Internal.Data.Array.Foreign.Mut.Type
(ArrayContents, ReadUState(..), touch)
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Stream.Serial (SerialT(..))
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 GHC.Exts as Exts
import Streamly.Internal.System.IO (unsafeInlineIO, defaultChunkSize)
#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
{ forall a. Array a -> ArrayContents
arrContents ::
#ifndef USE_FOREIGN_PTR
{-# UNPACK #-}
#endif
!ArrayContents
, forall a. Array a -> Ptr a
arrStart :: {-# UNPACK #-} !(Ptr a)
, forall a. Array a -> Ptr a
aEnd :: {-# UNPACK #-} !(Ptr a)
}
foreign import ccall unsafe "string.h strlen" c_strlen
:: CString -> IO CSize
asPtrUnsafe :: MonadIO m => Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe :: forall (m :: * -> *) a b.
MonadIO m =>
Array a -> (Ptr a -> m b) -> m b
asPtrUnsafe Array{Ptr a
ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} Ptr a -> m b
f = do
b
r <- Ptr a -> m b
f Ptr a
arrStart
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
{-# INLINE unsafeFreeze #-}
unsafeFreeze :: MA.Array a -> Array a
unsafeFreeze :: forall a. Array a -> Array a
unsafeFreeze (MA.Array ArrayContents
ac Ptr a
as Ptr a
ae Ptr a
_) = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array ArrayContents
ac Ptr a
as Ptr a
ae
{-# INLINE unsafeFreezeWithShrink #-}
unsafeFreezeWithShrink :: Storable a => MA.Array a -> Array a
unsafeFreezeWithShrink :: forall a. Storable a => Array a -> Array a
unsafeFreezeWithShrink Array a
arr = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
MA.Array ArrayContents
ac Ptr a
as Ptr a
ae Ptr a
_ <- forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> m (Array a)
MA.rightSize Array a
arr
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array ArrayContents
ac Ptr a
as Ptr a
ae
{-# INLINE unsafeThaw #-}
unsafeThaw :: Array a -> MA.Array a
unsafeThaw :: forall a. Array a -> Array a
unsafeThaw (Array ArrayContents
ac Ptr a
as Ptr a
ae) = forall a. ArrayContents -> Ptr a -> Ptr a -> Ptr a -> Array a
MA.Array ArrayContents
ac Ptr a
as Ptr a
ae Ptr a
ae
{-# INLINE splice #-}
splice :: (MonadIO m, Storable a) => Array a -> Array a -> m (Array a)
splice :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
splice Array a
arr1 Array a
arr2 =
forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
MA.splice (forall a. Array a -> Array a
unsafeThaw Array a
arr1) (forall a. Array a -> Array a
unsafeThaw Array a
arr2)
{-# INLINE fromPtr #-}
fromPtr ::
#ifdef DEVBUILD
Storable a =>
#endif
Int -> Ptr a -> Array a
fromPtr :: forall a. Int -> Ptr a -> Array a
fromPtr Int
n Ptr a
ptr = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
let end :: Ptr b
end = Ptr a
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
n
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Array
{ arrContents :: ArrayContents
arrContents = ArrayContents
MA.nilArrayContents
, arrStart :: Ptr a
arrStart = Ptr a
ptr
, aEnd :: Ptr a
aEnd = forall {b}. Ptr b
end
}
{-# INLINE fromForeignPtrUnsafe #-}
fromForeignPtrUnsafe ::
#ifdef DEVBUILD
Storable a =>
#endif
ForeignPtr a -> Ptr a -> Array a
fromForeignPtrUnsafe :: forall a. ForeignPtr a -> Ptr a -> Array a
fromForeignPtrUnsafe ForeignPtr a
fp Ptr a
end = forall a. Array a -> Array a
unsafeFreeze forall a b. (a -> b) -> a -> b
$ forall a. ForeignPtr a -> Ptr a -> Ptr a -> Array a
MA.fromForeignPtrUnsafe ForeignPtr a
fp Ptr a
end Ptr a
end
{-# INLINE fromAddr# #-}
fromAddr# ::
#ifdef DEVBUILD
Storable a =>
#endif
Int -> Addr# -> Array a
fromAddr# :: forall a. Int -> Addr# -> Array a
fromAddr# Int
n Addr#
addr# = forall a. Int -> Ptr a -> Array a
fromPtr Int
n (forall a b. Ptr a -> Ptr b
castPtr forall a b. (a -> b) -> a -> b
$ 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 = forall a. Addr# -> Ptr a
Ptr Addr#
addr#
len :: CSize
len = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ CString -> IO CSize
c_strlen forall {b}. Ptr b
cstr
forall a. Int -> Ptr a -> Array a
fromPtr (forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
len) (forall a b. Ptr a -> Ptr b
castPtr forall {b}. Ptr b
cstr)
{-# INLINABLE fromListN #-}
fromListN :: Storable a => Int -> [a] -> Array a
fromListN :: forall a. Storable a => Int -> [a] -> Array a
fromListN Int
n [a]
xs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> [a] -> m (Array a)
MA.fromListN Int
n [a]
xs
{-# INLINABLE fromListRevN #-}
fromListRevN :: Int -> [a] -> Array a
fromListRevN :: forall a. Int -> [a] -> Array a
fromListRevN Int
_n [a]
_xs = forall a. HasCallStack => a
undefined
{-# INLINE fromList #-}
fromList :: Storable a => [a] -> Array a
fromList :: forall a. Storable a => [a] -> Array a
fromList [a]
xs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
[a] -> m (Array a)
MA.fromList [a]
xs
{-# INLINABLE fromListRev #-}
fromListRev :: [a] -> Array a
fromListRev :: forall a. [a] -> Array a
fromListRev [a]
_xs = forall a. HasCallStack => a
undefined
{-# INLINE_NORMAL fromStreamDN #-}
fromStreamDN :: forall m a. (MonadIO m, Storable a)
=> Int -> D.Stream m a -> m (Array a)
fromStreamDN :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> m (Array a)
fromStreamDN Int
limit Stream m a
str = forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m a -> m (Array a)
fromStreamD Stream m a
str = forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m a -> m (Stream m (Array a))
bufferChunks Stream m a
m = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr forall a (m :: * -> *). a -> Stream m a -> Stream m a
K.cons forall (m :: * -> *) a. Stream m a
K.nil forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> Stream m (Array a)
arraysOf Int
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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Stream m a -> Stream m (Array a)
arraysOf Int
n Stream m a
str = forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. Array a -> Array a
unsafeFreeze forall a b. (a -> b) -> a -> b
$ 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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m (Array a) -> Stream m a
flattenArrays = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m (Array a) -> Stream m a
MA.flattenArrays forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map 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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m (Array a) -> Stream m a
flattenArraysRev = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Stream m (Array a) -> Stream m a
MA.flattenArraysRev forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. Array a -> Array a
unsafeThaw
{-# INLINE breakOn #-}
breakOn :: MonadIO m
=> Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
breakOn :: forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
breakOn Word8
sep Array Word8
arr = do
(Array Word8
a, Maybe (Array Word8)
b) <- forall (m :: * -> *).
MonadIO m =>
Word8 -> Array Word8 -> m (Array Word8, Maybe (Array Word8))
MA.breakOn Word8
sep (forall a. Array a -> Array a
unsafeThaw Array Word8
arr)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Array a -> Array a
unsafeFreeze Array Word8
a, forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Array Word8)
b)
{-# INLINE_NORMAL unsafeIndexIO #-}
unsafeIndexIO :: forall a. Storable a => Int -> Array a -> IO a
unsafeIndexIO :: forall a. Storable a => Int -> Array a -> IO a
unsafeIndexIO Int
i Array a
arr = forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Array a -> m a
MA.getIndexUnsafe Int
i (forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL unsafeIndex #-}
unsafeIndex :: forall a. Storable a => Int -> Array a -> a
unsafeIndex :: forall a. Storable a => Int -> Array a -> a
unsafeIndex Int
i Array a
arr = let !r :: a
r = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Int -> Array a -> IO a
unsafeIndexIO Int
i Array a
arr in a
r
{-# INLINE byteLength #-}
byteLength :: Array a -> Int
byteLength :: forall a. Array a -> Int
byteLength = forall a. Array a -> Int
MA.byteLength forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> Array a
unsafeThaw
{-# INLINE length #-}
length :: forall a. Storable a => Array a -> Int
length :: forall a. Storable a => Array a -> Int
length Array a
arr = forall a. Storable a => Array a -> Int
MA.length (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 :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Unfold m (Array a) a
readRev = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {m :: * -> *} {a}.
(Monad m, Storable a) =>
ReadUState a -> m (Step (ReadUState a) a)
step forall {m :: * -> *} {a}. Monad m => Array a -> m (ReadUState a)
inject
where
inject :: Array a -> m (ReadUState a)
inject (Array ArrayContents
contents Ptr a
start Ptr a
end) =
let p :: Ptr b
p = PTR_PREV(end,a)
in forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. ArrayContents -> Ptr a -> Ptr a -> ReadUState a
ReadUState ArrayContents
contents Ptr a
start forall {b}. Ptr b
p
{-# INLINE_LATE step #-}
step :: ReadUState a -> m (Step (ReadUState a) a)
step (ReadUState ArrayContents
contents Ptr a
start Ptr a
p) | Ptr a
p forall a. Ord a => a -> a -> Bool
< Ptr a
start =
let x :: ()
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ ArrayContents -> IO ()
touch ArrayContents
contents
in ()
x seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step (ReadUState ArrayContents
contents Ptr a
start Ptr a
p) = do
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (forall a. ArrayContents -> Ptr a -> Ptr a -> ReadUState a
ReadUState ArrayContents
contents Ptr a
start (PTR_PREV(p,a)))
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: forall m a. (Monad m, Storable a) => Array a -> D.Stream m a
toStreamD :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamD Array{Ptr a
ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {p} {b}.
Monad m =>
p -> Ptr a -> m (Step (Ptr b) a)
step Ptr a
arrStart
where
{-# INLINE_LATE step #-}
step :: p -> Ptr a -> m (Step (Ptr b) a)
step p
_ Ptr a
p | forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
p forall a. Ord a => a -> a -> Bool
<= Ptr a
aEnd) (Ptr a
p forall a. Eq a => a -> a -> Bool
== Ptr a
aEnd) = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step p
_ Ptr a
p = do
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (PTR_NEXT(p,a))
{-# INLINE toStreamK #-}
toStreamK :: forall m a. Storable a => Array a -> K.Stream m a
toStreamK :: forall (m :: * -> *) a. Storable a => Array a -> Stream m a
toStreamK Array{Ptr a
ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = forall {m :: * -> *}. Ptr a -> Stream m a
go Ptr a
arrStart
where
go :: Ptr a -> Stream m a
go Ptr a
p | forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
p forall a. Ord a => a -> a -> Bool
<= Ptr a
aEnd) (Ptr a
p forall a. Eq a => a -> a -> Bool
== Ptr a
aEnd) = forall (m :: * -> *) a. Stream m a
K.nil
| Bool
otherwise =
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
in a
x forall a (m :: * -> *). a -> Stream m a -> Stream m a
`K.cons` Ptr a -> Stream m a
go (PTR_NEXT(p,a))
{-# INLINE_NORMAL toStreamDRev #-}
toStreamDRev :: forall m a. (Monad m, Storable a) => Array a -> D.Stream m a
toStreamDRev :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamDRev Array{Ptr a
ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream forall {m :: * -> *} {p} {b}.
Monad m =>
p -> Ptr a -> m (Step (Ptr b) a)
step (PTR_PREV(aEnd,a))
where
{-# INLINE_LATE step #-}
step :: p -> Ptr a -> m (Step (Ptr b) a)
step p
_ Ptr a
p | Ptr a
p forall a. Ord a => a -> a -> Bool
< Ptr a
arrStart = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
D.Stop
step p
_ Ptr a
p = do
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
D.Yield a
x (PTR_PREV(p,a))
{-# INLINE toStreamKRev #-}
toStreamKRev :: forall m a. Storable a => Array a -> K.Stream m a
toStreamKRev :: forall (m :: * -> *) a. Storable a => Array a -> Stream m a
toStreamKRev Array {Ptr a
ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = forall {m :: * -> *}. Ptr a -> Stream m a
go (PTR_PREV(aEnd,a))
where
go :: Ptr a -> Stream m a
go Ptr a
p | Ptr a
p forall a. Ord a => a -> a -> Bool
< Ptr a
arrStart = forall (m :: * -> *) a. Stream m a
K.nil
| Bool
otherwise =
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
in a
x forall a (m :: * -> *). a -> Stream m a -> Stream m a
`K.cons` Ptr a -> Stream m a
go (PTR_PREV(p,a))
{-# INLINE_EARLY toStream #-}
toStream :: (Monad m, Storable a) => Array a -> SerialT m a
toStream :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> SerialT m a
toStream = forall (m :: * -> *) a. Stream m a -> SerialT m a
SerialT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
D.toStreamK forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamD
{-# INLINE_EARLY toStreamRev #-}
toStreamRev :: (Monad m, Storable a) => Array a -> SerialT m a
toStreamRev :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> SerialT m a
toStreamRev = forall (m :: * -> *) a. Stream m a -> SerialT m a
SerialT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
D.toStreamK forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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' :: forall a b. Storable a => (b -> a -> b) -> b -> Array a -> b
foldl' b -> a -> b
f b
z Array a
arr = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
D.foldl' b -> a -> b
f b
z forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamD Array a
arr
{-# INLINE_NORMAL foldr #-}
foldr :: Storable a => (a -> b -> b) -> b -> Array a -> b
foldr :: forall a b. Storable a => (a -> b -> b) -> b -> Array a -> b
foldr a -> b -> b
f b
z Array a
arr = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr a -> b -> b
f b
z forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamD Array a
arr
splitAt :: forall a. Storable a => Int -> Array a -> (Array a, Array a)
splitAt :: forall a. Storable a => Int -> Array a -> (Array a, Array a)
splitAt Int
i Array a
arr = (forall a. Array a -> Array a
unsafeFreeze Array a
a, forall a. Array a -> Array a
unsafeFreeze Array a
b)
where
(Array a
a, Array a
b) = forall a. Storable a => Int -> Array a -> (Array a, Array a)
MA.splitAt Int
i (forall a. Array a -> Array a
unsafeThaw Array a
arr)
{-# INLINE_LATE toListFB #-}
toListFB :: forall a b. Storable a => (a -> b -> b) -> b -> Array a -> b
toListFB :: forall a b. Storable a => (a -> b -> b) -> b -> Array a -> b
toListFB a -> b -> b
c b
n Array{Ptr a
ArrayContents
aEnd :: Ptr a
arrStart :: Ptr a
arrContents :: ArrayContents
aEnd :: forall a. Array a -> Ptr a
arrStart :: forall a. Array a -> Ptr a
arrContents :: forall a. Array a -> ArrayContents
..} = Ptr a -> b
go Ptr a
arrStart
where
go :: Ptr a -> b
go Ptr a
p | forall a. HasCallStack => Bool -> a -> a
assert (Ptr a
p forall a. Ord a => a -> a -> Bool
<= Ptr a
aEnd) (Ptr a
p forall a. Eq a => a -> a -> Bool
== Ptr a
aEnd) = b
n
go Ptr a
p =
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ do
a
r <- forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
ArrayContents -> IO ()
touch ArrayContents
arrContents
forall (m :: * -> *) a. Monad m => a -> m a
return a
r
in a -> b -> b
c a
x (Ptr a -> b
go (PTR_NEXT(p,a)))
{-# INLINE toList #-}
toList :: Storable a => Array a -> [a]
toList :: forall a. Storable a => Array a -> [a]
toList Array a
s = forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\a -> b -> b
c b
n -> forall a b. Storable a => (a -> b -> b) -> b -> Array a -> b
toListFB a -> b -> b
c b
n Array a
s)
{-# INLINE_NORMAL writeN #-}
writeN :: forall m a. (MonadIO m, Storable a) => Int -> Fold m a (Array a)
writeN :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
writeN = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Array a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> Fold m a (Array a)
writeNAligned Int
alignSize = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Array a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Int -> Fold m a (Array a)
writeNAlignedUnmanaged Int
alignSize =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Array a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
writeNUnsafe Int
n = forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
MA.writeNUnsafe Int
n
{-# INLINE_NORMAL writeWith #-}
writeWith :: forall m a. (MonadIO m, Storable a)
=> Int -> Fold m a (Array a)
writeWith :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
writeWith Int
elemCount = forall a. Array a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Int -> Fold m a (Array a)
MA.writeWith Int
elemCount
{-# INLINE write #-}
write :: forall m a. (MonadIO m, Storable a) => Fold m a (Array a)
write :: forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Fold m a (Array a)
write = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Array a -> Array a
unsafeFreeze forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Fold m a (Array a)
MA.write
instance (Show a, Storable a) => Show (Array a) where
{-# INLINE showsPrec #-}
showsPrec :: Int -> Array a -> ShowS
showsPrec Int
_ = forall a. Show a => a -> ShowS
shows forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall a. Storable a => [a] -> Array a
fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Read a => ReadPrec a
readPrec
readListPrec :: ReadPrec [Array a]
readListPrec = forall a. Read a => ReadPrec [a]
readListPrecDefault
instance (a ~ Char) => IsString (Array a) where
{-# INLINE fromString #-}
fromString :: String -> Array a
fromString = 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 = forall a. Storable a => [a] -> Array a
fromList
{-# INLINE fromListN #-}
fromListN :: Int -> [Item (Array a)] -> Array a
fromListN = forall a. Storable a => Int -> [a] -> Array a
fromListN
{-# INLINE toList #-}
toList :: Array a -> [Item (Array a)]
toList = forall a. Storable a => Array a -> [a]
toList
instance (Storable a, Eq a) => Eq (Array a) where
{-# INLINE (==) #-}
Array a
arr1 == :: Array a -> Array a -> Bool
== Array a
arr2 = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$! forall a. Array a -> Array a
unsafeThaw Array a
arr1 forall (m :: * -> *) a. MonadIO m => Array a -> Array a -> m Bool
`MA.cmp` forall a. Array a -> Array a
unsafeThaw Array a
arr2
instance NFData (Array a) where
{-# INLINE rnf #-}
rnf :: Array a -> ()
rnf Array {} = ()
instance (Storable a, Ord a) => Ord (Array a) where
{-# INLINE compare #-}
compare :: Array a -> Array a -> Ordering
compare Array a
arr1 Array a
arr2 = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
D.cmpBy forall a. Ord a => a -> a -> Ordering
compare (forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamD Array a
arr1) (forall (m :: * -> *) a.
(Monad m, Storable a) =>
Array a -> Stream m a
toStreamD Array a
arr2)
{-# INLINE (<) #-}
Array a
x < :: Array a -> Array a -> Bool
< Array a
y = case 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 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 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 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 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 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{..} = D.Stream step arrStart
where
{-# INLINE_LATE step #-}
step _ p | p == aEnd = return D.Stop
step _ p = liftIO $ do
x <- peek p
touch arrContents
return $ D.Yield x (p `plusPtr` size)
#endif
instance Storable a => Semigroup (Array a) where
Array a
arr1 <> :: Array a -> Array a -> Array a
<> Array a
arr2 = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Array a -> Array a -> m (Array a)
splice Array a
arr1 Array a
arr2
nil ::
#ifdef DEVBUILD
Storable a =>
#endif
Array a
nil :: forall a. Array a
nil = forall a. ArrayContents -> Ptr a -> Ptr a -> Array a
Array ArrayContents
MA.nilArrayContents (forall a. Addr# -> Ptr a
Ptr Addr#
nullAddr#) (forall a. Addr# -> Ptr a
Ptr Addr#
nullAddr#)
instance Storable a => Monoid (Array a) where
mempty :: Array a
mempty = forall a. Array a
nil
mappend :: Array a -> Array a -> Array a
mappend = forall a. Semigroup a => a -> a -> a
(<>)