{-# LANGUAGE CPP #-}
module Streamly.Internal.Data.Array.Type
(
Array (..)
, asPtrUnsafe
, nil
, unsafeFreeze
, unsafeFreezeWithShrink
, unsafeThaw
, pin
, unpin
, isPinned
, splice
, fromList
, pinnedFromList
, fromListN
, pinnedFromListN
, fromListRev
, fromListRevN
, fromStreamDN
, fromStreamD
, fromPureStream
, fromByteStr#
, breakOn
, clone
, pinnedClone
, unsafeIndexIO
, getIndexUnsafe
, byteLength
, length
, foldl'
, foldr
, splitAt
, toStreamD
, toStreamDRev
, toStreamK
, toStreamKRev
, toStream
, toStreamRev
, read
, readRev
, readerRev
, toList
, writeWith
, writeN
, pinnedWriteN
, writeNUnsafe
, pinnedWriteNUnsafe
, MA.ArrayUnsafe (..)
, pinnedWriteNAligned
, write
, pinnedWrite
, unsafeMakePure
, chunksOf
, pinnedChunksOf
, bufferChunks
, flattenArrays
, flattenArraysRev
, unsafeIndex
)
where
#include "ArrayMacros.h"
#include "inline.hs"
import Control.Exception (assert)
import Control.Monad (replicateM)
import Control.Monad.IO.Class (MonadIO(..))
import Data.Functor.Identity (Identity(..))
import Data.Proxy (Proxy(..))
import Data.Word (Word8)
import GHC.Base (build)
import GHC.Exts (IsList, IsString(..), Addr#)
import GHC.IO (unsafePerformIO)
import GHC.Ptr (Ptr(..))
import Streamly.Internal.Data.MutArray.Type (MutArray(..), MutByteArray)
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.Stream.Type (Stream)
import Streamly.Internal.Data.Unbox (Unbox(..))
import Streamly.Internal.Data.Unfold.Type (Unfold(..))
import Text.Read (readPrec)
import Prelude hiding (Foldable(..), read, unlines, splitAt)
import qualified GHC.Exts as Exts
import qualified Streamly.Internal.Data.MutArray.Type as MA
import qualified Streamly.Internal.Data.Stream.Type as D
import qualified Streamly.Internal.Data.Stream.Generate as D
import qualified Streamly.Internal.Data.StreamK.Type as K
import qualified Streamly.Internal.Data.MutByteArray.Type as Unboxed
import qualified Streamly.Internal.Data.Unfold.Type as Unfold
import qualified Text.ParserCombinators.ReadPrec as ReadPrec
import Streamly.Internal.System.IO (unsafeInlineIO, defaultChunkSize)
#include "DocTestDataArray.hs"
data Array a =
#ifdef DEVBUILD
Unbox a =>
#endif
Array
{ forall a. Array a -> MutByteArray
arrContents :: {-# UNPACK #-} !MutByteArray
, forall a. Array a -> Int
arrStart :: {-# UNPACK #-} !Int
, forall a. Array a -> Int
arrEnd :: {-# UNPACK #-} !Int
}
{-# INLINE asPtrUnsafe #-}
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 a
arr = forall (m :: * -> *) a b.
MonadIO m =>
MutArray a -> (Ptr a -> m b) -> m b
MA.asPtrUnsafe (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE unsafeFreeze #-}
unsafeFreeze :: MutArray a -> Array a
unsafeFreeze :: forall a. MutArray a -> Array a
unsafeFreeze (MutArray MutByteArray
ac Int
as Int
ae Int
_) = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
ac Int
as Int
ae
{-# INLINE unsafeFreezeWithShrink #-}
unsafeFreezeWithShrink :: Unbox a => MutArray a -> Array a
unsafeFreezeWithShrink :: forall a. Unbox a => MutArray a -> Array a
unsafeFreezeWithShrink MutArray a
arr = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
MutArray MutByteArray
ac Int
as Int
ae Int
_ <- forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> m (MutArray a)
MA.rightSize MutArray a
arr
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
ac Int
as Int
ae
{-# INLINE unsafeThaw #-}
unsafeThaw :: Array a -> MutArray a
unsafeThaw :: forall a. Array a -> MutArray a
unsafeThaw (Array MutByteArray
ac Int
as Int
ae) = forall a. MutByteArray -> Int -> Int -> Int -> MutArray a
MutArray MutByteArray
ac Int
as Int
ae Int
ae
{-# INLINE pin #-}
pin :: Array a -> IO (Array a)
pin :: forall a. Array a -> IO (Array a)
pin = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MutArray a -> IO (MutArray a)
MA.pin forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> MutArray a
unsafeThaw
{-# INLINE unpin #-}
unpin :: Array a -> IO (Array a)
unpin :: forall a. Array a -> IO (Array a)
unpin = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. MutArray a -> IO (MutArray a)
MA.unpin forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> MutArray a
unsafeThaw
{-# INLINE isPinned #-}
isPinned :: Array a -> Bool
isPinned :: forall a. Array a -> Bool
isPinned = forall a. MutArray a -> Bool
MA.isPinned forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> MutArray a
unsafeThaw
{-# INLINE splice #-}
splice :: (MonadIO m, Unbox a) => Array a -> Array a -> m (Array a)
splice :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Array a -> Array a -> m (Array a)
splice Array a
arr1 Array a
arr2 =
forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
MutArray a -> MutArray a -> m (MutArray a)
MA.splice (forall a. Array a -> MutArray a
unsafeThaw Array a
arr1) (forall a. Array a -> MutArray a
unsafeThaw Array a
arr2)
{-# INLINABLE fromListN #-}
fromListN :: Unbox a => Int -> [a] -> Array a
fromListN :: forall a. Unbox 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. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> [a] -> m (MutArray a)
MA.fromListN Int
n [a]
xs
{-# INLINABLE pinnedFromListN #-}
pinnedFromListN :: Unbox a => Int -> [a] -> Array a
pinnedFromListN :: forall a. Unbox a => Int -> [a] -> Array a
pinnedFromListN Int
n [a]
xs =
forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> [a] -> m (MutArray a)
MA.pinnedFromListN Int
n [a]
xs
{-# INLINABLE fromListRevN #-}
fromListRevN :: Unbox a => Int -> [a] -> Array a
fromListRevN :: forall a. Unbox a => Int -> [a] -> Array a
fromListRevN Int
n [a]
xs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> [a] -> m (MutArray a)
MA.fromListRevN Int
n [a]
xs
{-# INLINE fromList #-}
fromList :: Unbox a => [a] -> Array a
fromList :: forall a. Unbox a => [a] -> Array a
fromList [a]
xs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[a] -> m (MutArray a)
MA.fromList [a]
xs
{-# INLINE pinnedFromList #-}
pinnedFromList :: Unbox a => [a] -> Array a
pinnedFromList :: forall a. Unbox a => [a] -> Array a
pinnedFromList [a]
xs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[a] -> m (MutArray a)
MA.pinnedFromList [a]
xs
{-# INLINABLE fromListRev #-}
fromListRev :: Unbox a => [a] -> Array a
fromListRev :: forall a. Unbox a => [a] -> Array a
fromListRev [a]
xs = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
[a] -> m (MutArray a)
MA.fromListRev [a]
xs
{-# INLINE_NORMAL fromStreamDN #-}
fromStreamDN :: forall m a. (MonadIO m, Unbox a)
=> Int -> D.Stream m a -> m (Array a)
fromStreamDN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> m (Array a)
fromStreamDN Int
limit Stream m a
str = forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> m (MutArray a)
MA.fromStreamDN Int
limit Stream m a
str
{-# INLINE_NORMAL fromStreamD #-}
fromStreamD :: forall m a. (MonadIO m, Unbox a)
=> D.Stream m a -> m (Array a)
fromStreamD :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (Array a)
fromStreamD Stream m a
str = forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (MutArray a)
MA.fromStreamD Stream m a
str
{-# INLINE clone #-}
clone ::
( MonadIO m
#ifdef DEVBUILD
, Unbox a
#endif
)
=> Array a -> m (Array a)
clone :: forall (m :: * -> *) a. MonadIO m => Array a -> m (Array a)
clone = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => MutArray a -> m (MutArray a)
MA.clone forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> MutArray a
unsafeThaw
{-# INLINE pinnedClone #-}
pinnedClone ::
( MonadIO m
#ifdef DEVBUILD
, Unbox a
#endif
)
=> Array a -> m (Array a)
pinnedClone :: forall (m :: * -> *) a. MonadIO m => Array a -> m (Array a)
pinnedClone = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => MutArray a -> m (MutArray a)
MA.pinnedClone forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> MutArray a
unsafeThaw
{-# INLINE bufferChunks #-}
bufferChunks :: (MonadIO m, Unbox a) =>
D.Stream m a -> m (K.StreamK m (Array a))
bufferChunks :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m a -> m (StreamK 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 -> StreamK m a -> StreamK m a
K.cons forall (m :: * -> *) a. StreamK m a
K.nil forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
chunksOf Int
defaultChunkSize Stream m a
m
{-# INLINE_NORMAL chunksOf #-}
chunksOf :: forall m a. (MonadIO m, Unbox a)
=> Int -> D.Stream m a -> D.Stream m (Array a)
chunksOf :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
chunksOf Int
n Stream m a
str = forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. MutArray a -> Array a
unsafeFreeze forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (MutArray a)
MA.chunksOf Int
n Stream m a
str
{-# INLINE_NORMAL pinnedChunksOf #-}
pinnedChunksOf :: forall m a. (MonadIO m, Unbox a)
=> Int -> D.Stream m a -> D.Stream m (Array a)
pinnedChunksOf :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (Array a)
pinnedChunksOf Int
n Stream m a
str = forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
D.map forall a. MutArray a -> Array a
unsafeFreeze forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Stream m a -> Stream m (MutArray a)
MA.pinnedChunksOf Int
n Stream m a
str
{-# INLINE_NORMAL flattenArrays #-}
flattenArrays :: forall m a. (MonadIO m, Unbox a)
=> D.Stream m (Array a) -> D.Stream m a
flattenArrays :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (Array a) -> Stream m a
flattenArrays = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (MutArray 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 -> MutArray a
unsafeThaw
{-# INLINE_NORMAL flattenArraysRev #-}
flattenArraysRev :: forall m a. (MonadIO m, Unbox a)
=> D.Stream m (Array a) -> D.Stream m a
flattenArraysRev :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (Array a) -> Stream m a
flattenArraysRev = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream m (MutArray 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 -> MutArray 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
(MutArray Word8
a, Maybe (MutArray Word8)
b) <- forall (m :: * -> *).
MonadIO m =>
Word8
-> MutArray Word8 -> m (MutArray Word8, Maybe (MutArray Word8))
MA.breakOn Word8
sep (forall a. Array a -> MutArray a
unsafeThaw Array Word8
arr)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. MutArray a -> Array a
unsafeFreeze MutArray Word8
a, forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (MutArray Word8)
b)
{-# INLINE_NORMAL unsafeIndexIO #-}
unsafeIndexIO :: forall a. Unbox a => Int -> Array a -> IO a
unsafeIndexIO :: forall a. Unbox a => Int -> Array a -> IO a
unsafeIndexIO Int
i Array a
arr = forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> MutArray a -> m a
MA.getIndexUnsafe Int
i (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL getIndexUnsafe #-}
getIndexUnsafe :: forall a. Unbox a => Int -> Array a -> a
getIndexUnsafe :: forall a. Unbox a => Int -> Array a -> a
getIndexUnsafe Int
i Array a
arr = let !r :: a
r = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> Array a -> IO a
unsafeIndexIO Int
i Array a
arr in a
r
{-# DEPRECATED unsafeIndex "Please use 'getIndexUnsafe' instead" #-}
{-# INLINE_NORMAL unsafeIndex #-}
unsafeIndex :: forall a. Unbox a => Int -> Array a -> a
unsafeIndex :: forall a. Unbox a => Int -> Array a -> a
unsafeIndex = forall a. Unbox a => Int -> Array a -> a
getIndexUnsafe
{-# INLINE byteLength #-}
byteLength :: Array a -> Int
byteLength :: forall a. Array a -> Int
byteLength = forall a. MutArray a -> Int
MA.byteLength forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> MutArray a
unsafeThaw
{-# INLINE length #-}
length :: Unbox a => Array a -> Int
length :: forall a. Unbox a => Array a -> Int
length Array a
arr = forall a. Unbox a => MutArray a -> Int
MA.length (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL readerRev #-}
readerRev :: forall m a. (Monad m, Unbox a) => Unfold m (Array a) a
readerRev :: forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
readerRev = forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap forall a. Array a -> MutArray a
unsafeThaw forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> Unfold m (MutArray a) a
MA.readerRevWith (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafeInlineIO)
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: forall m a. (Monad m, Unbox a) => Array a -> D.Stream m a
toStreamD :: forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
toStreamD Array a
arr = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> Stream m a
MA.toStreamDWith (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafeInlineIO) (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE toStreamK #-}
toStreamK :: forall m a. (Monad m, Unbox a) => Array a -> K.StreamK m a
toStreamK :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Array a -> StreamK m a
toStreamK Array a
arr = forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> StreamK m a
MA.toStreamKWith (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafeInlineIO) (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE_NORMAL toStreamDRev #-}
toStreamDRev :: forall m a. (Monad m, Unbox a) => Array a -> D.Stream m a
toStreamDRev :: forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
toStreamDRev Array a
arr =
forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> Stream m a
MA.toStreamDRevWith (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafeInlineIO) (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE toStreamKRev #-}
toStreamKRev :: forall m a. (Monad m, Unbox a) => Array a -> K.StreamK m a
toStreamKRev :: forall (m :: * -> *) a.
(Monad m, Unbox a) =>
Array a -> StreamK m a
toStreamKRev Array a
arr =
forall (m :: * -> *) a.
(Monad m, Unbox a) =>
(forall b. IO b -> m b) -> MutArray a -> StreamK m a
MA.toStreamKRevWith (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IO a -> a
unsafeInlineIO) (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE_EARLY read #-}
read :: (Monad m, Unbox a) => Array a -> Stream m a
read :: forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
read = forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
toStreamD
{-# DEPRECATED toStream "Please use 'read' instead." #-}
{-# INLINE_EARLY toStream #-}
toStream :: (Monad m, Unbox a) => Array a -> Stream m a
toStream :: forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
toStream = forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
read
{-# INLINE_EARLY readRev #-}
readRev :: (Monad m, Unbox a) => Array a -> Stream m a
readRev :: forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
readRev = forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
toStreamDRev
{-# DEPRECATED toStreamRev "Please use 'readRev' instead." #-}
{-# INLINE_EARLY toStreamRev #-}
toStreamRev :: (Monad m, Unbox a) => Array a -> Stream m a
toStreamRev :: forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
toStreamRev = forall (m :: * -> *) a. (Monad m, Unbox a) => Array a -> Stream m a
readRev
{-# INLINE_NORMAL foldl' #-}
foldl' :: forall a b. Unbox a => (b -> a -> b) -> b -> Array a -> b
foldl' :: forall a b. Unbox 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, Unbox a) => Array a -> Stream m a
toStreamD Array a
arr
{-# INLINE_NORMAL foldr #-}
foldr :: Unbox a => (a -> b -> b) -> b -> Array a -> b
foldr :: forall a b. Unbox 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, Unbox a) => Array a -> Stream m a
toStreamD Array a
arr
splitAt :: Unbox a => Int -> Array a -> (Array a, Array a)
splitAt :: forall a. Unbox a => Int -> Array a -> (Array a, Array a)
splitAt Int
i Array a
arr = (forall a. MutArray a -> Array a
unsafeFreeze MutArray a
a, forall a. MutArray a -> Array a
unsafeFreeze MutArray a
b)
where
(MutArray a
a, MutArray a
b) = forall a. Unbox a => Int -> MutArray a -> (MutArray a, MutArray a)
MA.splitAt Int
i (forall a. Array a -> MutArray a
unsafeThaw Array a
arr)
{-# INLINE_LATE toListFB #-}
toListFB :: forall a b. Unbox a => (a -> b -> b) -> b -> Array a -> b
toListFB :: forall a b. Unbox a => (a -> b -> b) -> b -> Array a -> b
toListFB a -> b -> b
c b
n Array{Int
MutByteArray
arrEnd :: Int
arrStart :: Int
arrContents :: MutByteArray
arrEnd :: forall a. Array a -> Int
arrStart :: forall a. Array a -> Int
arrContents :: forall a. Array a -> MutByteArray
..} = Int -> b
go Int
arrStart
where
go :: Int -> b
go Int
p | forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
p forall a. Ord a => a -> a -> Bool
<= Int
arrEnd) (Int
p forall a. Eq a => a -> a -> Bool
== Int
arrEnd) = b
n
go Int
p =
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => Int -> MutByteArray -> IO a
peekAt Int
p MutByteArray
arrContents
in a -> b -> b
c a
x (Int -> b
go (INDEX_NEXT(p,a)))
{-# INLINE toList #-}
toList :: Unbox a => Array a -> [a]
toList :: forall a. Unbox 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. Unbox 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, Unbox a) => Int -> Fold m a (Array a)
writeN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
writeN = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
MA.writeN
{-# INLINE_NORMAL pinnedWriteN #-}
pinnedWriteN :: forall m a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a)
pinnedWriteN :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
pinnedWriteN = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
MA.pinnedWriteN
{-# INLINE_NORMAL pinnedWriteNAligned #-}
pinnedWriteNAligned :: forall m a. (MonadIO m, Unbox a)
=> Int -> Int -> Fold m a (Array a)
pinnedWriteNAligned :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Int -> Fold m a (Array a)
pinnedWriteNAligned Int
alignSize = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Int -> Fold m a (MutArray a)
MA.pinnedWriteNAligned Int
alignSize
{-# INLINE_NORMAL writeNUnsafe #-}
writeNUnsafe :: forall m a. (MonadIO m, Unbox a)
=> Int -> Fold m a (Array a)
writeNUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
writeNUnsafe Int
n = forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
MA.writeNUnsafe Int
n
{-# INLINE_NORMAL pinnedWriteNUnsafe #-}
pinnedWriteNUnsafe :: forall m a. (MonadIO m, Unbox a)
=> Int -> Fold m a (Array a)
pinnedWriteNUnsafe :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
pinnedWriteNUnsafe Int
n = forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
MA.pinnedWriteNUnsafe Int
n
{-# INLINE_NORMAL writeWith #-}
writeWith :: forall m a. (MonadIO m, Unbox a)
=> Int -> Fold m a (Array a)
writeWith :: forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (Array a)
writeWith Int
elemCount = forall a. MutArray a -> Array a
unsafeFreeze forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Int -> Fold m a (MutArray a)
MA.writeWith Int
elemCount
{-# INLINE write #-}
write :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
write :: forall (m :: * -> *) a. (MonadIO m, Unbox a) => Fold m a (Array a)
write = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Fold m a (MutArray a)
MA.write
{-# INLINE pinnedWrite #-}
pinnedWrite :: forall m a. (MonadIO m, Unbox a) => Fold m a (Array a)
pinnedWrite :: forall (m :: * -> *) a. (MonadIO m, Unbox a) => Fold m a (Array a)
pinnedWrite = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. MutArray a -> Array a
unsafeFreeze forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Fold m a (MutArray a)
MA.pinnedWrite
{-# INLINE unsafeMakePure #-}
unsafeMakePure :: Monad m => Fold IO a b -> Fold m a b
unsafeMakePure :: forall (m :: * -> *) a b. Monad m => Fold IO a b -> Fold m a b
unsafeMakePure (Fold s -> a -> IO (Step s b)
step IO (Step s b)
initial s -> IO b
extract s -> IO b
final) =
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b))
-> m (Step s b) -> (s -> m b) -> (s -> m b) -> Fold m a b
Fold (\s
x a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. IO a -> a
unsafeInlineIO (s -> a -> IO (Step s b)
step s
x a
a))
(forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. IO a -> a
unsafePerformIO IO (Step s b)
initial)
(\s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ s -> IO b
extract s
s)
(\s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ s -> IO b
final s
s)
fromPureStream :: Unbox a => Stream Identity a -> Array a
fromPureStream :: forall a. Unbox a => Stream Identity a -> Array a
fromPureStream Stream Identity a
x = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. MutArray a -> Array a
unsafeFreeze) (forall (m :: * -> *) a.
(MonadIO m, Unbox a) =>
Stream Identity a -> m (MutArray a)
MA.fromPureStream Stream Identity a
x)
fromByteStr# :: Addr# -> Array Word8
fromByteStr# :: Addr# -> Array Word8
fromByteStr# Addr#
addr = forall a. Unbox a => Stream Identity a -> Array a
fromPureStream (forall (m :: * -> *). Monad m => Addr# -> Stream m Word8
D.fromByteStr# Addr#
addr)
instance (Show a, Unbox a) => Show (Array a) where
{-# INLINE show #-}
show :: Array a -> String
show Array a
arr = String
"fromList " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall a. Unbox a => Array a -> [a]
toList Array a
arr)
instance (Unbox a, Read a, Show a) => Read (Array a) where
{-# INLINE readPrec #-}
readPrec :: ReadPrec (Array a)
readPrec = do
String
fromListWord <- forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
9 ReadPrec Char
ReadPrec.get
if String
fromListWord forall a. Eq a => a -> a -> Bool
== String
"fromList "
then forall a. Unbox a => [a] -> Array a
fromList forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Read a => ReadPrec a
readPrec
else forall a. ReadPrec a
ReadPrec.pfail
instance (a ~ Char) => IsString (Array a) where
{-# INLINE fromString #-}
fromString :: String -> Array a
fromString = forall a. Unbox a => [a] -> Array a
fromList
instance Unbox a => IsList (Array a) where
type (Item (Array a)) = a
{-# INLINE fromList #-}
fromList :: [Item (Array a)] -> Array a
fromList = forall a. Unbox a => [a] -> Array a
fromList
{-# INLINE fromListN #-}
fromListN :: Int -> [Item (Array a)] -> Array a
fromListN = forall a. Unbox a => Int -> [a] -> Array a
fromListN
{-# INLINE toList #-}
toList :: Array a -> [Item (Array a)]
toList = forall a. Unbox a => Array a -> [a]
toList
instance (Unbox a, Eq a) => Eq (Array a) where
{-# INLINE (==) #-}
Array a
arr1 == :: Array a -> Array a -> Bool
== Array a
arr2 =
forall a. Eq a => a -> a -> Bool
(==) Ordering
EQ forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$! forall a. Array a -> MutArray a
unsafeThaw Array a
arr1 forall (m :: * -> *) a.
MonadIO m =>
MutArray a -> MutArray a -> m Ordering
`MA.cmp` forall a. Array a -> MutArray a
unsafeThaw Array a
arr2
instance (Unbox 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, Unbox a) => Array a -> Stream m a
toStreamD Array a
arr1) (forall (m :: * -> *) a. (Monad m, Unbox 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 == arrEnd = return D.Stop
step _ p = liftIO $ do
x <- peekAt p arrContents
return $ D.Yield x (p + size)
#endif
instance Unbox 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, Unbox a) =>
Array a -> Array a -> m (Array a)
splice Array a
arr1 Array a
arr2
nil ::
#ifdef DEVBUILD
Unbox a =>
#endif
Array a
nil :: forall a. Array a
nil = forall a. MutByteArray -> Int -> Int -> Array a
Array MutByteArray
Unboxed.nil Int
0 Int
0
instance Unbox 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
(<>)