{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
-- |
-- Module      : Data.Massiv.Array.Manifest.Boxed
-- Copyright   : (c) Alexey Kuleshevich 2018-2022
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Massiv.Array.Manifest.Boxed
  ( B(..)
  , BL(..)
  , BN(..)
  , N
  , pattern N
  , Array(..)
  , MArray(..)
  , wrapLazyArray
  , unwrapLazyArray
  , unwrapNormalForm
  , evalNormalForm
  , unwrapArray
  , evalArray
  , toLazyArray
  , evalLazyArray
  , forceLazyArray
  , unwrapMutableArray
  , unwrapMutableLazyArray
  , evalMutableArray
  , unwrapNormalFormArray
  , evalNormalFormArray
  , unwrapNormalFormMutableArray
  , evalNormalFormMutableArray
  , toBoxedVector
  , toBoxedMVector
  , fromBoxedVector
  , fromBoxedMVector
  , evalBoxedVector
  , evalBoxedMVector
  , evalNormalBoxedVector
  , evalNormalBoxedMVector
  , coerceBoxedArray
  , coerceNormalBoxedArray
  , seqArray
  , deepseqArray
  ) where

import Control.DeepSeq (NFData(..), deepseq)
import Control.Exception
import Control.Monad ((>=>))
import Control.Monad.Primitive
import qualified Data.Foldable as F (Foldable(..))
import Data.Massiv.Array.Delayed.Pull (D)
import Data.Massiv.Array.Delayed.Push (DL)
import Data.Massiv.Array.Delayed.Stream (DS)
import Data.Massiv.Array.Manifest.Internal (compute, computeAs)
import Data.Massiv.Array.Manifest.List as L
import Data.Massiv.Array.Mutable
import Data.Massiv.Array.Ops.Fold
import Data.Massiv.Array.Ops.Fold.Internal
import Data.Massiv.Array.Ops.Map (traverseA)
import Data.Massiv.Core.Common
import Data.Massiv.Core.List
import Data.Massiv.Core.Operations
import Data.Massiv.Vector.Stream as S (isteps, steps)
import qualified Data.Primitive.Array as A
import qualified Data.Vector as VB
import qualified Data.Vector.Mutable as MVB
import GHC.Exts as GHC
import Prelude hiding (mapM, replicate)
import System.IO.Unsafe (unsafePerformIO)
#if !MIN_VERSION_vector(0,13,0)
import Unsafe.Coerce (unsafeCoerce)
#endif

#include "massiv.h"

----------------
-- Boxed Lazy --
----------------

-- | Array representation for Boxed elements. This data structure is lazy with
-- respect to its elements.
--
-- ====__Example__
--
-- Memoized version of a factorial that relies on laziness. Note that
-- computing memoized factorial of a million would likely overflow memory.
--
-- >>> import Data.Massiv.Array as A
-- >>> :{
-- mkMemoFactorial :: Int -> (Int -> Integer)
-- mkMemoFactorial n =
--   let arr = makeVectorR BL Seq (Sz1 n) fact
--       fact i | i == 0 = 1
--              | otherwise = (arr ! (i - 1)) * toInteger i
--   in (arr !)
-- :}
--
-- >>> let fact = mkMemoFactorial 1000001
-- >>> fact 50
-- 30414093201713378043612608166064768844377641568960512000000000000
-- >>> length $ show $ fact 5000
-- 16326
--
data BL = BL deriving Ix1 -> BL -> ShowS
[BL] -> ShowS
BL -> String
forall a.
(Ix1 -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BL] -> ShowS
$cshowList :: [BL] -> ShowS
show :: BL -> String
$cshow :: BL -> String
showsPrec :: Ix1 -> BL -> ShowS
$cshowsPrec :: Ix1 -> BL -> ShowS
Show

data instance Array BL ix e = BLArray { forall ix e. Array BL ix e -> Comp
blComp   :: !Comp
                                      , forall ix e. Array BL ix e -> Sz ix
blSize   :: !(Sz ix)
                                      , forall ix e. Array BL ix e -> Ix1
blOffset :: {-# UNPACK #-} !Int
                                      , forall ix e. Array BL ix e -> Array e
blData   :: {-# UNPACK #-} !(A.Array e)
                                      }
data instance MArray s BL ix e =
  MBLArray !(Sz ix) {-# UNPACK #-} !Int {-# UNPACK #-} !(A.MutableArray s e)

instance (Ragged L ix e, Show e) => Show (Array BL ix e) where
  showsPrec :: Ix1 -> Array BL ix e -> ShowS
showsPrec = forall r r' ix e.
(Ragged L ix e, Load r ix e, Load r' ix e, Source r' e, Show e) =>
(Array r ix e -> Array r' ix e) -> Ix1 -> Array r ix e -> ShowS
showsArrayPrec forall a. a -> a
id
  showList :: [Array BL ix e] -> ShowS
showList = forall arr. Show arr => [arr] -> ShowS
showArrayList

instance (Ragged L ix e, Show e) => Show (Array DL ix e) where
  showsPrec :: Ix1 -> Array DL ix e -> ShowS
showsPrec = forall r r' ix e.
(Ragged L ix e, Load r ix e, Load r' ix e, Source r' e, Show e) =>
(Array r ix e -> Array r' ix e) -> Ix1 -> Array r ix e -> ShowS
showsArrayPrec (forall r e r' ix.
(Manifest r e, Load r' ix e) =>
r -> Array r' ix e -> Array r ix e
computeAs BL
BL)
  showList :: [Array DL ix e] -> ShowS
showList = forall arr. Show arr => [arr] -> ShowS
showArrayList

instance Show e => Show (Array DS Ix1 e) where
  showsPrec :: Ix1 -> Array DS Ix1 e -> ShowS
showsPrec = forall r r' ix e.
(Ragged L ix e, Load r ix e, Load r' ix e, Source r' e, Show e) =>
(Array r ix e -> Array r' ix e) -> Ix1 -> Array r ix e -> ShowS
showsArrayPrec (forall r e r' ix.
(Manifest r e, Load r' ix e) =>
r -> Array r' ix e -> Array r ix e
computeAs BL
BL)
  showList :: [Array DS Ix1 e] -> ShowS
showList = forall arr. Show arr => [arr] -> ShowS
showArrayList


instance (Index ix, NFData e) => NFData (Array BL ix e) where
  rnf :: Array BL ix e -> ()
rnf = (forall a ix t. (NFData a, Index ix) => Array BL ix a -> t -> t
`deepseqArray` ())
  {-# INLINE rnf #-}

instance (Index ix, Eq e) => Eq (Array BL ix e) where
  == :: Array BL ix e -> Array BL ix e -> Bool
(==) = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Bool) -> Array r1 ix e1 -> Array r2 ix e2 -> Bool
eqArrays forall a. Eq a => a -> a -> Bool
(==)
  {-# INLINE (==) #-}

instance (Index ix, Ord e) => Ord (Array BL ix e) where
  compare :: Array BL ix e -> Array BL ix e -> Ordering
compare = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Ordering)
-> Array r1 ix e1 -> Array r2 ix e2 -> Ordering
compareArrays forall a. Ord a => a -> a -> Ordering
compare
  {-# INLINE compare #-}

instance Strategy BL where
  setComp :: forall ix e. Comp -> Array BL ix e -> Array BL ix e
setComp Comp
c Array BL ix e
arr = Array BL ix e
arr { blComp :: Comp
blComp = Comp
c }
  {-# INLINE setComp #-}
  getComp :: forall ix e. Array BL ix e -> Comp
getComp = forall ix e. Array BL ix e -> Comp
blComp
  {-# INLINE getComp #-}
  repr :: BL
repr = BL
BL


instance Source BL e where
  unsafeLinearIndex :: forall ix. Index ix => Array BL ix e -> Ix1 -> e
unsafeLinearIndex (BLArray Comp
_ Sz ix
_sz Ix1
o Array e
a) Ix1
i =
    forall a ix e.
String -> (a -> Sz ix) -> (a -> ix -> e) -> a -> ix -> e
indexAssert String
"BL.unsafeLinearIndex" (forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> Ix1
A.sizeofArray) forall a. Array a -> Ix1 -> a
A.indexArray Array e
a (Ix1
i forall a. Num a => a -> a -> a
+ Ix1
o)
  {-# INLINE unsafeLinearIndex #-}

  unsafeOuterSlice :: forall ix.
(Index ix, Index (Lower ix)) =>
Array BL ix e -> Sz (Lower ix) -> Ix1 -> Array BL (Lower ix) e
unsafeOuterSlice (BLArray Comp
c Sz ix
_ Ix1
o Array e
a) Sz (Lower ix)
szL Ix1
i = forall ix e. Comp -> Sz ix -> Ix1 -> Array e -> Array BL ix e
BLArray Comp
c Sz (Lower ix)
szL (Ix1
i forall a. Num a => a -> a -> a
* forall ix. Index ix => Sz ix -> Ix1
totalElem Sz (Lower ix)
szL forall a. Num a => a -> a -> a
+ Ix1
o) Array e
a
  {-# INLINE unsafeOuterSlice #-}

  unsafeLinearSlice :: forall ix.
Index ix =>
Ix1 -> Sz1 -> Array BL ix e -> Array BL Ix1 e
unsafeLinearSlice Ix1
i Sz1
k (BLArray Comp
c Sz ix
_ Ix1
o Array e
a) = forall ix e. Comp -> Sz ix -> Ix1 -> Array e -> Array BL ix e
BLArray Comp
c Sz1
k (Ix1
o forall a. Num a => a -> a -> a
+ Ix1
i) Array e
a
  {-# INLINE unsafeLinearSlice #-}

instance Manifest BL e where

  unsafeLinearIndexM :: forall ix. Index ix => Array BL ix e -> Ix1 -> e
unsafeLinearIndexM (BLArray Comp
_ Sz ix
_sz Ix1
o Array e
a) Ix1
i =
    forall a ix e.
String -> (a -> Sz ix) -> (a -> ix -> e) -> a -> ix -> e
indexAssert String
"BL.unsafeLinearIndexM" (forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Array a -> Ix1
A.sizeofArray) forall a. Array a -> Ix1 -> a
A.indexArray Array e
a (Ix1
i forall a. Num a => a -> a -> a
+ Ix1
o)
  {-# INLINE unsafeLinearIndexM #-}

  sizeOfMArray :: forall ix s. Index ix => MArray s BL ix e -> Sz ix
sizeOfMArray (MBLArray Sz ix
sz Ix1
_ MutableArray s e
_) = Sz ix
sz
  {-# INLINE sizeOfMArray #-}

  unsafeResizeMArray :: forall ix' ix s.
(Index ix', Index ix) =>
Sz ix' -> MArray s BL ix e -> MArray s BL ix' e
unsafeResizeMArray Sz ix'
sz (MBLArray Sz ix
_ Ix1
off MutableArray s e
marr) = forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray Sz ix'
sz Ix1
off MutableArray s e
marr
  {-# INLINE unsafeResizeMArray #-}

  unsafeLinearSliceMArray :: forall ix s.
Index ix =>
Ix1 -> Sz1 -> MArray s BL ix e -> MVector s BL e
unsafeLinearSliceMArray Ix1
i Sz1
k (MBLArray Sz ix
_ Ix1
o MutableArray s e
a) = forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray Sz1
k (Ix1
i forall a. Num a => a -> a -> a
+ Ix1
o) MutableArray s e
a
  {-# INLINE unsafeLinearSliceMArray #-}

  unsafeThaw :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Array BL ix e -> m (MArray (PrimState m) BL ix e)
unsafeThaw (BLArray Comp
_ Sz ix
sz Ix1
o Array e
a) = forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray Sz ix
sz Ix1
o forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
PrimMonad m =>
Array a -> m (MutableArray (PrimState m) a)
A.unsafeThawArray Array e
a
  {-# INLINE unsafeThaw #-}

  unsafeFreeze :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) BL ix e -> m (Array BL ix e)
unsafeFreeze Comp
comp (MBLArray Sz ix
sz Ix1
o MutableArray (PrimState m) e
ma) = forall ix e. Comp -> Sz ix -> Ix1 -> Array e -> Array BL ix e
BLArray Comp
comp Sz ix
sz Ix1
o forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> m (Array a)
A.unsafeFreezeArray MutableArray (PrimState m) e
ma
  {-# INLINE unsafeFreeze #-}

  unsafeNew :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) BL ix e)
unsafeNew Sz ix
sz = forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray Sz ix
sz Ix1
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
PrimMonad m =>
Ix1 -> a -> m (MutableArray (PrimState m) a)
A.newArray (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz) forall a. a
uninitialized
  {-# INLINE unsafeNew #-}

  initialize :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) BL ix e -> m ()
initialize MArray (PrimState m) BL ix e
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
  {-# INLINE initialize #-}

  newMArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) BL ix e)
newMArray Sz ix
sz e
e = forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray Sz ix
sz Ix1
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
PrimMonad m =>
Ix1 -> a -> m (MutableArray (PrimState m) a)
A.newArray (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz) e
e
  {-# INLINE newMArray #-}

  unsafeLinearRead :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) BL ix e -> Ix1 -> m e
unsafeLinearRead (MBLArray Sz ix
_ Ix1
o MutableArray (PrimState m) e
ma) Ix1
i =
    forall a ix e.
String -> (a -> Sz ix) -> (a -> ix -> e) -> a -> ix -> e
indexAssert String
"B.unsafeLinearRead" (forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. MutableArray s a -> Ix1
A.sizeofMutableArray) forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Ix1 -> m a
A.readArray MutableArray (PrimState m) e
ma (Ix1
i forall a. Num a => a -> a -> a
+ Ix1
o)
  {-# INLINE unsafeLinearRead #-}

  unsafeLinearWrite :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) BL ix e -> Ix1 -> e -> m ()
unsafeLinearWrite (MBLArray Sz ix
_sz Ix1
o MutableArray (PrimState m) e
ma) Ix1
i =
    forall a ix e.
String -> (a -> Sz ix) -> (a -> ix -> e) -> a -> ix -> e
indexAssert String
"B.unsafeLinearWrite" (forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. MutableArray s a -> Ix1
A.sizeofMutableArray) forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Ix1 -> a -> m ()
A.writeArray MutableArray (PrimState m) e
ma (Ix1
i forall a. Num a => a -> a -> a
+ Ix1
o)
  {-# INLINE unsafeLinearWrite #-}

instance Size BL where
  size :: forall ix e. Array BL ix e -> Sz ix
size = forall ix e. Array BL ix e -> Sz ix
blSize
  {-# INLINE size #-}
  unsafeResize :: forall ix ix' e.
(Index ix, Index ix') =>
Sz ix' -> Array BL ix e -> Array BL ix' e
unsafeResize !Sz ix'
sz !Array BL ix e
arr = Array BL ix e
arr { blSize :: Sz ix'
blSize = Sz ix'
sz }
  {-# INLINE unsafeResize #-}


instance Index ix => Shape BL ix where
  maxLinearSize :: forall e. Array BL ix e -> Maybe Sz1
maxLinearSize = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e. (Index ix, Size r) => Array r ix e -> Ix1
elemsCount
  {-# INLINE maxLinearSize #-}

instance Index ix => Load BL ix e where
  makeArray :: Comp -> Sz ix -> (ix -> e) -> Array BL ix e
makeArray Comp
comp Sz ix
sz ix -> e
f = forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
comp Sz ix
sz ix -> e
f :: Array D ix e)
  {-# INLINE makeArray #-}

  makeArrayLinear :: Comp -> Sz ix -> (Ix1 -> e) -> Array BL ix e
makeArrayLinear !Comp
comp !Sz ix
sz Ix1 -> e
f = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (Array r ix e)
generateArrayLinear Comp
comp Sz ix
sz (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix1 -> e
f)
  {-# INLINE makeArrayLinear #-}

  replicate :: Comp -> Sz ix -> e -> Array BL ix e
replicate Comp
comp Sz ix
sz e
e = forall a. (forall s. ST s a) -> a
runST (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) r ix e)
newMArray Sz ix
sz e
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp)
  {-# INLINE replicate #-}

  iterArrayLinearST_ :: forall s.
Scheduler s () -> Array BL ix e -> (Ix1 -> e -> ST s ()) -> ST s ()
iterArrayLinearST_ !Scheduler s ()
scheduler !Array BL ix e
arr =
    forall s (m :: * -> *) b.
MonadPrimBase s m =>
Scheduler s () -> Ix1 -> (Ix1 -> b) -> (Ix1 -> b -> m ()) -> m ()
splitLinearlyWith_ Scheduler s ()
scheduler (forall ix r e. (Index ix, Size r) => Array r ix e -> Ix1
elemsCount Array BL ix e
arr) (forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex Array BL ix e
arr)
  {-# INLINE iterArrayLinearST_ #-}

instance Index ix => StrideLoad BL ix e

instance Index ix => Stream BL ix e where
  toStream :: Array BL ix e -> Steps Id e
toStream = forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m e
S.steps
  {-# INLINE toStream #-}
  toStreamIx :: Array BL ix e -> Steps Id (ix, e)
toStreamIx = forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m (ix, e)
S.isteps
  {-# INLINE toStreamIx #-}


-- | Row-major sequential folding over a Boxed array.
instance Index ix => Foldable (Array BL ix) where
  fold :: forall m. Monoid m => Array BL ix m -> m
fold = forall e ix r.
(Monoid e, Index ix, Source r e) =>
Array r ix e -> e
fold
  {-# INLINE fold #-}
  foldMap :: forall m a. Monoid m => (a -> m) -> Array BL ix a -> m
foldMap = forall ix r e m.
(Index ix, Source r e, Monoid m) =>
(e -> m) -> Array r ix e -> m
foldMono
  {-# INLINE foldMap #-}
  foldl :: forall b a. (b -> a -> b) -> b -> Array BL ix a -> b
foldl = forall ix r e a.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> Array r ix e -> a
lazyFoldlS
  {-# INLINE foldl #-}
  foldl' :: forall b a. (b -> a -> b) -> b -> Array BL ix a -> b
foldl' = forall ix r e a.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> Array r ix e -> a
foldlS
  {-# INLINE foldl' #-}
  foldr :: forall a b. (a -> b -> b) -> b -> Array BL ix a -> b
foldr = forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrFB
  {-# INLINE foldr #-}
  foldr' :: forall a b. (a -> b -> b) -> b -> Array BL ix a -> b
foldr' = forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrS
  {-# INLINE foldr' #-}
  null :: forall a. Array BL ix a -> Bool
null (BLArray Comp
_ Sz ix
sz Ix1
_ Array a
_) = forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz forall a. Eq a => a -> a -> Bool
== Ix1
0
  {-# INLINE null #-}
  length :: forall a. Array BL ix a -> Ix1
length = forall ix. Index ix => Sz ix -> Ix1
totalElem forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Size r => Array r ix e -> Sz ix
size
  {-# INLINE length #-}
  toList :: forall a. Array BL ix a -> [a]
toList Array BL ix a
arr = forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\ a -> b -> b
c b
n -> forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrFB a -> b -> b
c b
n Array BL ix a
arr)
  {-# INLINE toList #-}


instance Index ix => Functor (Array BL ix) where
  fmap :: forall a b. (a -> b) -> Array BL ix a -> Array BL ix b
fmap a -> b
f Array BL ix a
arr = forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Ix1 -> e) -> Array r ix e
makeArrayLinear (forall ix e. Array BL ix e -> Comp
blComp Array BL ix a
arr) (forall ix e. Array BL ix e -> Sz ix
blSize Array BL ix a
arr) (a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex Array BL ix a
arr)
  {-# INLINE fmap #-}
  <$ :: forall a b. a -> Array BL ix b -> Array BL ix a
(<$) a
e Array BL ix b
arr = forall r ix e. Load r ix e => Comp -> Sz ix -> e -> Array r ix e
replicate (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array BL ix b
arr) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array BL ix b
arr) a
e
  {-# INLINE (<$) #-}

instance Index ix => Traversable (Array BL ix) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Array BL ix a -> f (Array BL ix b)
traverse = forall r ix e r' a (f :: * -> *).
(Source r' a, Manifest r e, Index ix, Applicative f) =>
(a -> f e) -> Array r' ix a -> f (Array r ix e)
traverseA
  {-# INLINE traverse #-}

instance (IsList (Array L ix e), Ragged L ix e) => IsList (Array BL ix e) where
  type Item (Array BL ix e) = Item (Array L ix e)
  fromList :: [Item (Array BL ix e)] -> Array BL ix e
fromList = forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
L.fromLists' Comp
Seq
  {-# INLINE fromList #-}
  toList :: Array BL ix e -> [Item (Array BL ix e)]
toList = forall l. IsList l => l -> [Item l]
GHC.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r.
(Ragged L ix e, Shape r ix, Source r e) =>
Array r ix e -> Array L ix e
toListArray
  {-# INLINE toList #-}

instance Num e => FoldNumeric BL e where
  unsafeDotProduct :: forall ix. Index ix => Array BL ix e -> Array BL ix e -> e
unsafeDotProduct = forall e ix r.
(Num e, Index ix, Source r e) =>
Array r ix e -> Array r ix e -> e
defaultUnsafeDotProduct
  {-# INLINE unsafeDotProduct #-}
  powerSumArray :: forall ix. Index ix => Array BL ix e -> Ix1 -> e
powerSumArray = forall ix r e.
(Index ix, Source r e, Num e) =>
Array r ix e -> Ix1 -> e
defaultPowerSumArray
  {-# INLINE powerSumArray #-}
  foldArray :: forall ix. Index ix => (e -> e -> e) -> e -> Array BL ix e -> e
foldArray = forall ix r e.
(Index ix, Source r e) =>
(e -> e -> e) -> e -> Array r ix e -> e
defaultFoldArray
  {-# INLINE foldArray #-}

instance Num e => Numeric BL e where
  unsafeLiftArray :: forall ix. Index ix => (e -> e) -> Array BL ix e -> Array BL ix e
unsafeLiftArray = forall r ix e.
(Load r ix e, Source r e) =>
(e -> e) -> Array r ix e -> Array r ix e
defaultUnsafeLiftArray
  {-# INLINE unsafeLiftArray #-}
  unsafeLiftArray2 :: forall ix.
Index ix =>
(e -> e -> e) -> Array BL ix e -> Array BL ix e -> Array BL ix e
unsafeLiftArray2 = forall r ix e.
(Load r ix e, Source r e) =>
(e -> e -> e) -> Array r ix e -> Array r ix e -> Array r ix e
defaultUnsafeLiftArray2
  {-# INLINE unsafeLiftArray2 #-}



------------------
-- Boxed Strict --
------------------

-- | Array representation for Boxed elements. Its elements are strict to Weak
-- Head Normal Form (WHNF) only.
data B = B deriving Ix1 -> B -> ShowS
[B] -> ShowS
B -> String
forall a.
(Ix1 -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [B] -> ShowS
$cshowList :: [B] -> ShowS
show :: B -> String
$cshow :: B -> String
showsPrec :: Ix1 -> B -> ShowS
$cshowsPrec :: Ix1 -> B -> ShowS
Show

newtype instance Array B ix e = BArray (Array BL ix e)

newtype instance MArray s B ix e = MBArray (MArray s BL ix e)

instance (Ragged L ix e, Show e) => Show (Array B ix e) where
  showsPrec :: Ix1 -> Array B ix e -> ShowS
showsPrec = forall r r' ix e.
(Ragged L ix e, Load r ix e, Load r' ix e, Source r' e, Show e) =>
(Array r ix e -> Array r' ix e) -> Ix1 -> Array r ix e -> ShowS
showsArrayPrec forall a. a -> a
id
  showList :: [Array B ix e] -> ShowS
showList = forall arr. Show arr => [arr] -> ShowS
showArrayList

instance (Index ix, NFData e) => NFData (Array B ix e) where
  rnf :: Array B ix e -> ()
rnf = (forall a ix t. (NFData a, Index ix) => Array BL ix a -> t -> t
`deepseqArray` ()) forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE rnf #-}

instance (Index ix, Eq e) => Eq (Array B ix e) where
  == :: Array B ix e -> Array B ix e -> Bool
(==) = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Bool) -> Array r1 ix e1 -> Array r2 ix e2 -> Bool
eqArrays forall a. Eq a => a -> a -> Bool
(==)
  {-# INLINE (==) #-}

instance (Index ix, Ord e) => Ord (Array B ix e) where
  compare :: Array B ix e -> Array B ix e -> Ordering
compare = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Ordering)
-> Array r1 ix e1 -> Array r2 ix e2 -> Ordering
compareArrays forall a. Ord a => a -> a -> Ordering
compare
  {-# INLINE compare #-}


instance Source B e where
  unsafeLinearIndex :: forall ix. Index ix => Array B ix e -> Ix1 -> e
unsafeLinearIndex Array B ix e
arr = forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex (forall ix e. Array B ix e -> Array BL ix e
toLazyArray Array B ix e
arr)
  {-# INLINE unsafeLinearIndex #-}

  unsafeLinearSlice :: forall ix. Index ix => Ix1 -> Sz1 -> Array B ix e -> Array B Ix1 e
unsafeLinearSlice Ix1
i Sz1
k Array B ix e
arr = coerce :: forall a b. Coercible a b => a -> b
coerce (forall r e ix.
(Source r e, Index ix) =>
Ix1 -> Sz1 -> Array r ix e -> Array r Ix1 e
unsafeLinearSlice Ix1
i Sz1
k (forall ix e. Array B ix e -> Array BL ix e
toLazyArray Array B ix e
arr))
  {-# INLINE unsafeLinearSlice #-}

  unsafeOuterSlice :: forall ix.
(Index ix, Index (Lower ix)) =>
Array B ix e -> Sz (Lower ix) -> Ix1 -> Array B (Lower ix) e
unsafeOuterSlice Array B ix e
arr Sz (Lower ix)
i = coerce :: forall a b. Coercible a b => a -> b
coerce (forall r e ix.
(Source r e, Index ix, Index (Lower ix)) =>
Array r ix e -> Sz (Lower ix) -> Ix1 -> Array r (Lower ix) e
unsafeOuterSlice (forall ix e. Array B ix e -> Array BL ix e
toLazyArray Array B ix e
arr) Sz (Lower ix)
i)
  {-# INLINE unsafeOuterSlice #-}

instance Strategy B where
  getComp :: forall ix e. Array B ix e -> Comp
getComp = forall ix e. Array BL ix e -> Comp
blComp forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE getComp #-}
  setComp :: forall ix e. Comp -> Array B ix e -> Array B ix e
setComp Comp
c Array B ix e
arr = forall ix e. Array BL ix e -> Array B ix e
coerceBoxedArray (coerce :: forall a b. Coercible a b => a -> b
coerce Array B ix e
arr) { blComp :: Comp
blComp = Comp
c }
  {-# INLINE setComp #-}
  repr :: B
repr = B
B


instance Index ix => Shape B ix where
  maxLinearSize :: forall e. Array B ix e -> Maybe Sz1
maxLinearSize = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e. (Index ix, Size r) => Array r ix e -> Ix1
elemsCount
  {-# INLINE maxLinearSize #-}

instance Size B where
  size :: forall ix e. Array B ix e -> Sz ix
size = forall ix e. Array BL ix e -> Sz ix
blSize forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE size #-}
  unsafeResize :: forall ix ix' e.
(Index ix, Index ix') =>
Sz ix' -> Array B ix e -> Array B ix' e
unsafeResize Sz ix'
sz = coerce :: forall a b. Coercible a b => a -> b
coerce (\Array BL ix e
arr -> Array BL ix e
arr { blSize :: Sz ix'
blSize = Sz ix'
sz })
  {-# INLINE unsafeResize #-}


instance Manifest B e where

  unsafeLinearIndexM :: forall ix. Index ix => Array B ix e -> Ix1 -> e
unsafeLinearIndexM = coerce :: forall a b. Coercible a b => a -> b
coerce forall r e ix. (Manifest r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndexM
  {-# INLINE unsafeLinearIndexM #-}

  sizeOfMArray :: forall ix s. Index ix => MArray s B ix e -> Sz ix
sizeOfMArray = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE sizeOfMArray #-}

  unsafeResizeMArray :: forall ix' ix s.
(Index ix', Index ix) =>
Sz ix' -> MArray s B ix e -> MArray s B ix' e
unsafeResizeMArray Sz ix'
sz = forall s ix e. MArray s BL ix e -> MArray s B ix e
MBArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r e ix' ix s.
(Manifest r e, Index ix', Index ix) =>
Sz ix' -> MArray s r ix e -> MArray s r ix' e
unsafeResizeMArray Sz ix'
sz forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE unsafeResizeMArray #-}

  unsafeLinearSliceMArray :: forall ix s.
Index ix =>
Ix1 -> Sz1 -> MArray s B ix e -> MVector s B e
unsafeLinearSliceMArray Ix1
i Sz1
k = forall s ix e. MArray s BL ix e -> MArray s B ix e
MBArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r e ix s.
(Manifest r e, Index ix) =>
Ix1 -> Sz1 -> MArray s r ix e -> MVector s r e
unsafeLinearSliceMArray Ix1
i Sz1
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE unsafeLinearSliceMArray #-}

  unsafeThaw :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Array B ix e -> m (MArray (PrimState m) B ix e)
unsafeThaw Array B ix e
arr = forall s ix e. MArray s BL ix e -> MArray s B ix e
MBArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e -> m (MArray (PrimState m) r ix e)
unsafeThaw (coerce :: forall a b. Coercible a b => a -> b
coerce Array B ix e
arr)
  {-# INLINE unsafeThaw #-}

  unsafeFreeze :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) B ix e -> m (Array B ix e)
unsafeFreeze Comp
comp MArray (PrimState m) B ix e
marr = forall ix e. Array BL ix e -> Array B ix e
BArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp (coerce :: forall a b. Coercible a b => a -> b
coerce MArray (PrimState m) B ix e
marr)
  {-# INLINE unsafeFreeze #-}

  unsafeNew :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) B ix e)
unsafeNew Sz ix
sz = forall s ix e. MArray s BL ix e -> MArray s B ix e
MBArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
  {-# INLINE unsafeNew #-}

  initialize :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) B ix e -> m ()
initialize MArray (PrimState m) B ix e
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
  {-# INLINE initialize #-}

  newMArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) B ix e)
newMArray Sz ix
sz !e
e = forall s ix e. MArray s BL ix e -> MArray s B ix e
MBArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) r ix e)
newMArray Sz ix
sz e
e
  {-# INLINE newMArray #-}

  unsafeLinearRead :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) B ix e -> Ix1 -> m e
unsafeLinearRead MArray (PrimState m) B ix e
ma = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> m e
unsafeLinearRead (coerce :: forall a b. Coercible a b => a -> b
coerce MArray (PrimState m) B ix e
ma)
  {-# INLINE unsafeLinearRead #-}

  unsafeLinearWrite :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) B ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) B ix e
ma Ix1
i e
e = e
e seq :: forall a b. a -> b -> b
`seq` forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite (coerce :: forall a b. Coercible a b => a -> b
coerce MArray (PrimState m) B ix e
ma) Ix1
i e
e
  {-# INLINE unsafeLinearWrite #-}

instance Index ix => Load B ix e where
  makeArray :: Comp -> Sz ix -> (ix -> e) -> Array B ix e
makeArray Comp
comp Sz ix
sz ix -> e
f = forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
comp Sz ix
sz ix -> e
f :: Array D ix e)
  {-# INLINE makeArray #-}

  makeArrayLinear :: Comp -> Sz ix -> (Ix1 -> e) -> Array B ix e
makeArrayLinear !Comp
comp !Sz ix
sz Ix1 -> e
f = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (Array r ix e)
generateArrayLinear Comp
comp Sz ix
sz (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix1 -> e
f)
  {-# INLINE makeArrayLinear #-}

  replicate :: Comp -> Sz ix -> e -> Array B ix e
replicate Comp
comp Sz ix
sz e
e = forall a. (forall s. ST s a) -> a
runST (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) r ix e)
newMArray Sz ix
sz e
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp)
  {-# INLINE replicate #-}

  iterArrayLinearST_ :: forall s.
Scheduler s () -> Array B ix e -> (Ix1 -> e -> ST s ()) -> ST s ()
iterArrayLinearST_ Scheduler s ()
scheduler = coerce :: forall a b. Coercible a b => a -> b
coerce (forall r ix e s.
Load r ix e =>
Scheduler s () -> Array r ix e -> (Ix1 -> e -> ST s ()) -> ST s ()
iterArrayLinearST_ Scheduler s ()
scheduler)
  {-# INLINE iterArrayLinearST_ #-}

instance Index ix => StrideLoad B ix e

instance Index ix => Stream B ix e where
  toStream :: Array B ix e -> Steps Id e
toStream = forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m e
S.steps
  {-# INLINE toStream #-}
  toStreamIx :: Array B ix e -> Steps Id (ix, e)
toStreamIx = forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m (ix, e)
S.isteps
  {-# INLINE toStreamIx #-}


-- | Row-major sequential folding over a Boxed array.
instance Index ix => Foldable (Array B ix) where
  fold :: forall m. Monoid m => Array B ix m -> m
fold = forall e ix r.
(Monoid e, Index ix, Source r e) =>
Array r ix e -> e
fold
  {-# INLINE fold #-}
  foldMap :: forall m a. Monoid m => (a -> m) -> Array B ix a -> m
foldMap = forall ix r e m.
(Index ix, Source r e, Monoid m) =>
(e -> m) -> Array r ix e -> m
foldMono
  {-# INLINE foldMap #-}
  foldl :: forall b a. (b -> a -> b) -> b -> Array B ix a -> b
foldl = forall ix r e a.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> Array r ix e -> a
lazyFoldlS
  {-# INLINE foldl #-}
  foldl' :: forall b a. (b -> a -> b) -> b -> Array B ix a -> b
foldl' = forall ix r e a.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> Array r ix e -> a
foldlS
  {-# INLINE foldl' #-}
  foldr :: forall a b. (a -> b -> b) -> b -> Array B ix a -> b
foldr = forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrFB
  {-# INLINE foldr #-}
  foldr' :: forall a b. (a -> b -> b) -> b -> Array B ix a -> b
foldr' = forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrS
  {-# INLINE foldr' #-}
  null :: forall a. Array B ix a -> Bool
null Array B ix a
arr = forall ix. Index ix => Sz ix -> Ix1
totalElem (forall r ix e. Size r => Array r ix e -> Sz ix
size Array B ix a
arr) forall a. Eq a => a -> a -> Bool
== Ix1
0
  {-# INLINE null #-}
  length :: forall a. Array B ix a -> Ix1
length = forall ix. Index ix => Sz ix -> Ix1
totalElem forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Size r => Array r ix e -> Sz ix
size
  {-# INLINE length #-}
  toList :: forall a. Array B ix a -> [a]
toList Array B ix a
arr = forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\ a -> b -> b
c b
n -> forall ix r e b.
(Index ix, Source r e) =>
(e -> b -> b) -> b -> Array r ix e -> b
foldrFB a -> b -> b
c b
n Array B ix a
arr)
  {-# INLINE toList #-}


instance Index ix => Functor (Array B ix) where
  fmap :: forall a b. (a -> b) -> Array B ix a -> Array B ix b
fmap a -> b
f Array B ix a
arr = forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Ix1 -> e) -> Array r ix e
makeArrayLinear (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array B ix a
arr) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array B ix a
arr) (a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex Array B ix a
arr)
  {-# INLINE fmap #-}
  <$ :: forall a b. a -> Array B ix b -> Array B ix a
(<$) !a
e Array B ix b
arr = forall r ix e. Load r ix e => Comp -> Sz ix -> e -> Array r ix e
replicate (forall r ix e. Strategy r => Array r ix e -> Comp
getComp Array B ix b
arr) (forall r ix e. Size r => Array r ix e -> Sz ix
size Array B ix b
arr) a
e
  {-# INLINE (<$) #-}

instance Index ix => Traversable (Array B ix) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Array B ix a -> f (Array B ix b)
traverse = forall r ix e r' a (f :: * -> *).
(Source r' a, Manifest r e, Index ix, Applicative f) =>
(a -> f e) -> Array r' ix a -> f (Array r ix e)
traverseA
  {-# INLINE traverse #-}

instance (IsList (Array L ix e), Ragged L ix e) => IsList (Array B ix e) where
  type Item (Array B ix e) = Item (Array L ix e)
  fromList :: [Item (Array B ix e)] -> Array B ix e
fromList = forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
L.fromLists' Comp
Seq
  {-# INLINE fromList #-}
  toList :: Array B ix e -> [Item (Array B ix e)]
toList = forall l. IsList l => l -> [Item l]
GHC.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r.
(Ragged L ix e, Shape r ix, Source r e) =>
Array r ix e -> Array L ix e
toListArray
  {-# INLINE toList #-}

instance Num e => FoldNumeric B e where
  unsafeDotProduct :: forall ix. Index ix => Array B ix e -> Array B ix e -> e
unsafeDotProduct = forall e ix r.
(Num e, Index ix, Source r e) =>
Array r ix e -> Array r ix e -> e
defaultUnsafeDotProduct
  {-# INLINE unsafeDotProduct #-}
  powerSumArray :: forall ix. Index ix => Array B ix e -> Ix1 -> e
powerSumArray = forall ix r e.
(Index ix, Source r e, Num e) =>
Array r ix e -> Ix1 -> e
defaultPowerSumArray
  {-# INLINE powerSumArray #-}
  foldArray :: forall ix. Index ix => (e -> e -> e) -> e -> Array B ix e -> e
foldArray = forall ix r e.
(Index ix, Source r e) =>
(e -> e -> e) -> e -> Array r ix e -> e
defaultFoldArray
  {-# INLINE foldArray #-}

instance Num e => Numeric B e where
  unsafeLiftArray :: forall ix. Index ix => (e -> e) -> Array B ix e -> Array B ix e
unsafeLiftArray = forall r ix e.
(Load r ix e, Source r e) =>
(e -> e) -> Array r ix e -> Array r ix e
defaultUnsafeLiftArray
  {-# INLINE unsafeLiftArray #-}
  unsafeLiftArray2 :: forall ix.
Index ix =>
(e -> e -> e) -> Array B ix e -> Array B ix e -> Array B ix e
unsafeLiftArray2 = forall r ix e.
(Load r ix e, Source r e) =>
(e -> e -> e) -> Array r ix e -> Array r ix e -> Array r ix e
defaultUnsafeLiftArray2
  {-# INLINE unsafeLiftArray2 #-}

-----------------------
-- Boxed Normal Form --
-----------------------

  -- | Array representation for Boxed elements. Its elements are always in Normal
-- Form (NF), therefore `NFData` instance is required.
data BN = BN deriving Ix1 -> BN -> ShowS
[BN] -> ShowS
BN -> String
forall a.
(Ix1 -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BN] -> ShowS
$cshowList :: [BN] -> ShowS
show :: BN -> String
$cshow :: BN -> String
showsPrec :: Ix1 -> BN -> ShowS
$cshowsPrec :: Ix1 -> BN -> ShowS
Show

-- | Type and pattern `N` have been added for backwards compatibility and will be replaced
-- in the future in favor of `BN`.
--
-- /Deprecated/ - since 1.0.0
type N = BN
pattern N :: N
pattern $bN :: BN
$mN :: forall {r}. BN -> ((# #) -> r) -> ((# #) -> r) -> r
N = BN
{-# COMPLETE N #-}
{-# DEPRECATED N "In favor of more consistently named `BN`" #-}

newtype instance Array BN ix e = BNArray (Array BL ix e)
newtype instance MArray s BN ix e = MBNArray (MArray s BL ix e)

instance (Ragged L ix e, Show e, NFData e) => Show (Array BN ix e) where
  showsPrec :: Ix1 -> Array BN ix e -> ShowS
showsPrec = forall r r' ix e.
(Ragged L ix e, Load r ix e, Load r' ix e, Source r' e, Show e) =>
(Array r ix e -> Array r' ix e) -> Ix1 -> Array r ix e -> ShowS
showsArrayPrec coerce :: forall a b. Coercible a b => a -> b
coerce
  showList :: [Array BN ix e] -> ShowS
showList = forall arr. Show arr => [arr] -> ShowS
showArrayList

-- | /O(1)/ - `BN` is already in normal form
instance NFData (Array BN ix e) where
  rnf :: Array BN ix e -> ()
rnf = (seq :: forall a b. a -> b -> b
`seq` ())
  {-# INLINE rnf #-}

instance (Index ix, NFData e, Eq e) => Eq (Array BN ix e) where
  == :: Array BN ix e -> Array BN ix e -> Bool
(==) = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Bool) -> Array r1 ix e1 -> Array r2 ix e2 -> Bool
eqArrays forall a. Eq a => a -> a -> Bool
(==)
  {-# INLINE (==) #-}

instance (Index ix, NFData e, Ord e) => Ord (Array BN ix e) where
  compare :: Array BN ix e -> Array BN ix e -> Ordering
compare = forall ix r1 e1 r2 e2.
(Index ix, Source r1 e1, Source r2 e2) =>
(e1 -> e2 -> Ordering)
-> Array r1 ix e1 -> Array r2 ix e2 -> Ordering
compareArrays forall a. Ord a => a -> a -> Ordering
compare
  {-# INLINE compare #-}

instance Strategy BN where
  setComp :: forall ix e. Comp -> Array BN ix e -> Array BN ix e
setComp Comp
c = coerce :: forall a b. Coercible a b => a -> b
coerce (forall r ix e. Strategy r => Comp -> Array r ix e -> Array r ix e
setComp Comp
c)
  {-# INLINE setComp #-}
  getComp :: forall ix e. Array BN ix e -> Comp
getComp = forall ix e. Array BL ix e -> Comp
blComp forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE getComp #-}
  repr :: BN
repr = BN
BN

instance NFData e => Source BN e where
  unsafeLinearIndex :: forall ix. Index ix => Array BN ix e -> Ix1 -> e
unsafeLinearIndex (BNArray Array BL ix e
arr) = forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex Array BL ix e
arr
  {-# INLINE unsafeLinearIndex #-}
  unsafeLinearSlice :: forall ix.
Index ix =>
Ix1 -> Sz1 -> Array BN ix e -> Array BN Ix1 e
unsafeLinearSlice Ix1
i Sz1
k (BNArray Array BL ix e
a) = coerce :: forall a b. Coercible a b => a -> b
coerce (forall r e ix.
(Source r e, Index ix) =>
Ix1 -> Sz1 -> Array r ix e -> Array r Ix1 e
unsafeLinearSlice Ix1
i Sz1
k Array BL ix e
a)
  {-# INLINE unsafeLinearSlice #-}
  unsafeOuterSlice :: forall ix.
(Index ix, Index (Lower ix)) =>
Array BN ix e -> Sz (Lower ix) -> Ix1 -> Array BN (Lower ix) e
unsafeOuterSlice (BNArray Array BL ix e
a) Sz (Lower ix)
i = coerce :: forall a b. Coercible a b => a -> b
coerce (forall r e ix.
(Source r e, Index ix, Index (Lower ix)) =>
Array r ix e -> Sz (Lower ix) -> Ix1 -> Array r (Lower ix) e
unsafeOuterSlice Array BL ix e
a Sz (Lower ix)
i)
  {-# INLINE unsafeOuterSlice #-}


instance Index ix => Shape BN ix where
  maxLinearSize :: forall e. Array BN ix e -> Maybe Sz1
maxLinearSize = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix. ix -> Sz ix
SafeSz forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix r e. (Index ix, Size r) => Array r ix e -> Ix1
elemsCount
  {-# INLINE maxLinearSize #-}

instance Size BN where
  size :: forall ix e. Array BN ix e -> Sz ix
size = forall ix e. Array BL ix e -> Sz ix
blSize forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE size #-}

  unsafeResize :: forall ix ix' e.
(Index ix, Index ix') =>
Sz ix' -> Array BN ix e -> Array BN ix' e
unsafeResize !Sz ix'
sz = coerce :: forall a b. Coercible a b => a -> b
coerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix ix' e.
(Size r, Index ix, Index ix') =>
Sz ix' -> Array r ix e -> Array r ix' e
unsafeResize Sz ix'
sz forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE unsafeResize #-}

instance NFData e => Manifest BN e where
  unsafeLinearIndexM :: forall ix. Index ix => Array BN ix e -> Ix1 -> e
unsafeLinearIndexM Array BN ix e
arr = forall r e ix. (Manifest r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndexM (coerce :: forall a b. Coercible a b => a -> b
coerce Array BN ix e
arr)
  {-# INLINE unsafeLinearIndexM #-}

  sizeOfMArray :: forall ix s. Index ix => MArray s BN ix e -> Sz ix
sizeOfMArray = forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE sizeOfMArray #-}

  unsafeResizeMArray :: forall ix' ix s.
(Index ix', Index ix) =>
Sz ix' -> MArray s BN ix e -> MArray s BN ix' e
unsafeResizeMArray Sz ix'
sz = coerce :: forall a b. Coercible a b => a -> b
coerce forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r e ix' ix s.
(Manifest r e, Index ix', Index ix) =>
Sz ix' -> MArray s r ix e -> MArray s r ix' e
unsafeResizeMArray Sz ix'
sz forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE unsafeResizeMArray #-}

  unsafeLinearSliceMArray :: forall ix s.
Index ix =>
Ix1 -> Sz1 -> MArray s BN ix e -> MVector s BN e
unsafeLinearSliceMArray Ix1
i Sz1
k = forall s ix e. MArray s BL ix e -> MArray s BN ix e
MBNArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r e ix s.
(Manifest r e, Index ix) =>
Ix1 -> Sz1 -> MArray s r ix e -> MVector s r e
unsafeLinearSliceMArray Ix1
i Sz1
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE unsafeLinearSliceMArray #-}

  unsafeThaw :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Array BN ix e -> m (MArray (PrimState m) BN ix e)
unsafeThaw Array BN ix e
arr = forall s ix e. MArray s BL ix e -> MArray s BN ix e
MBNArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Array r ix e -> m (MArray (PrimState m) r ix e)
unsafeThaw (coerce :: forall a b. Coercible a b => a -> b
coerce Array BN ix e
arr)
  {-# INLINE unsafeThaw #-}

  unsafeFreeze :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) BN ix e -> m (Array BN ix e)
unsafeFreeze Comp
comp MArray (PrimState m) BN ix e
marr = forall ix e. Array BL ix e -> Array BN ix e
BNArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp (coerce :: forall a b. Coercible a b => a -> b
coerce MArray (PrimState m) BN ix e
marr)
  {-# INLINE unsafeFreeze #-}

  unsafeNew :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) BN ix e)
unsafeNew Sz ix
sz = forall s ix e. MArray s BL ix e -> MArray s BN ix e
MBNArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
  {-# INLINE unsafeNew #-}

  initialize :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) BN ix e -> m ()
initialize MArray (PrimState m) BN ix e
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
  {-# INLINE initialize #-}

  newMArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) BN ix e)
newMArray Sz ix
sz e
e = e
e forall a b. NFData a => a -> b -> b
`deepseq` (forall s ix e. MArray s BL ix e -> MArray s BN ix e
MBNArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) r ix e)
newMArray Sz ix
sz e
e)
  {-# INLINE newMArray #-}

  unsafeLinearRead :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) BN ix e -> Ix1 -> m e
unsafeLinearRead MArray (PrimState m) BN ix e
ma = forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> m e
unsafeLinearRead (coerce :: forall a b. Coercible a b => a -> b
coerce MArray (PrimState m) BN ix e
ma)
  {-# INLINE unsafeLinearRead #-}

  unsafeLinearWrite :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) BN ix e -> Ix1 -> e -> m ()
unsafeLinearWrite MArray (PrimState m) BN ix e
ma Ix1
i e
e = e
e forall a b. NFData a => a -> b -> b
`deepseq` forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Ix1 -> e -> m ()
unsafeLinearWrite (coerce :: forall a b. Coercible a b => a -> b
coerce MArray (PrimState m) BN ix e
ma) Ix1
i e
e
  {-# INLINE unsafeLinearWrite #-}

instance (Index ix, NFData e) => Load BN ix e where
  makeArray :: Comp -> Sz ix -> (ix -> e) -> Array BN ix e
makeArray Comp
comp Sz ix
sz ix -> e
f = forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (ix -> e) -> Array r ix e
makeArray Comp
comp Sz ix
sz ix -> e
f :: Array D ix e)
  {-# INLINE makeArray #-}
  makeArrayLinear :: Comp -> Sz ix -> (Ix1 -> e) -> Array BN ix e
makeArrayLinear !Comp
comp !Sz ix
sz Ix1 -> e
f = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ forall r ix e (m :: * -> *).
(MonadUnliftIO m, Manifest r e, Index ix) =>
Comp -> Sz ix -> (Ix1 -> m e) -> m (Array r ix e)
generateArrayLinear Comp
comp Sz ix
sz (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ix1 -> e
f)
  {-# INLINE makeArrayLinear #-}
  replicate :: Comp -> Sz ix -> e -> Array BN ix e
replicate Comp
comp Sz ix
sz e
e = forall a. (forall s. ST s a) -> a
runST (forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> e -> m (MArray (PrimState m) r ix e)
newMArray Sz ix
sz e
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp)
  {-# INLINE replicate #-}
  iterArrayLinearST_ :: forall s.
Scheduler s () -> Array BN ix e -> (Ix1 -> e -> ST s ()) -> ST s ()
iterArrayLinearST_ !Scheduler s ()
scheduler !Array BN ix e
arr =
    forall s (m :: * -> *) b.
MonadPrimBase s m =>
Scheduler s () -> Ix1 -> (Ix1 -> b) -> (Ix1 -> b -> m ()) -> m ()
splitLinearlyWith_ Scheduler s ()
scheduler (forall ix r e. (Index ix, Size r) => Array r ix e -> Ix1
elemsCount Array BN ix e
arr) (forall r e ix. (Source r e, Index ix) => Array r ix e -> Ix1 -> e
unsafeLinearIndex Array BN ix e
arr)
  {-# INLINE iterArrayLinearST_ #-}

instance (Index ix, NFData e) => StrideLoad BN ix e

instance (Index ix, NFData e) => Stream BN ix e where
  toStream :: Array BN ix e -> Steps Id e
toStream = forall r ix e. Stream r ix e => Array r ix e -> Steps Id e
toStream forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE toStream #-}
  toStreamIx :: Array BN ix e -> Steps Id (ix, e)
toStreamIx = forall r ix e. Stream r ix e => Array r ix e -> Steps Id (ix, e)
toStreamIx forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
  {-# INLINE toStreamIx #-}


instance (NFData e, IsList (Array L ix e), Ragged L ix e) => IsList (Array BN ix e) where
  type Item (Array BN ix e) = Item (Array L ix e)
  fromList :: [Item (Array BN ix e)] -> Array BN ix e
fromList = forall r ix e.
(HasCallStack, Ragged L ix e, Manifest r e) =>
Comp -> [ListItem ix e] -> Array r ix e
L.fromLists' Comp
Seq
  {-# INLINE fromList #-}
  toList :: Array BN ix e -> [Item (Array BN ix e)]
toList = forall l. IsList l => l -> [Item l]
GHC.toList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r.
(Ragged L ix e, Shape r ix, Source r e) =>
Array r ix e -> Array L ix e
toListArray
  {-# INLINE toList #-}

instance (NFData e, Num e) => FoldNumeric BN e where
  unsafeDotProduct :: forall ix. Index ix => Array BN ix e -> Array BN ix e -> e
unsafeDotProduct = forall e ix r.
(Num e, Index ix, Source r e) =>
Array r ix e -> Array r ix e -> e
defaultUnsafeDotProduct
  {-# INLINE unsafeDotProduct #-}
  powerSumArray :: forall ix. Index ix => Array BN ix e -> Ix1 -> e
powerSumArray = forall ix r e.
(Index ix, Source r e, Num e) =>
Array r ix e -> Ix1 -> e
defaultPowerSumArray
  {-# INLINE powerSumArray #-}
  foldArray :: forall ix. Index ix => (e -> e -> e) -> e -> Array BN ix e -> e
foldArray = forall ix r e.
(Index ix, Source r e) =>
(e -> e -> e) -> e -> Array r ix e -> e
defaultFoldArray
  {-# INLINE foldArray #-}

instance (NFData e, Num e) => Numeric BN e where
  unsafeLiftArray :: forall ix. Index ix => (e -> e) -> Array BN ix e -> Array BN ix e
unsafeLiftArray = forall r ix e.
(Load r ix e, Source r e) =>
(e -> e) -> Array r ix e -> Array r ix e
defaultUnsafeLiftArray
  {-# INLINE unsafeLiftArray #-}
  unsafeLiftArray2 :: forall ix.
Index ix =>
(e -> e -> e) -> Array BN ix e -> Array BN ix e -> Array BN ix e
unsafeLiftArray2 = forall r ix e.
(Load r ix e, Source r e) =>
(e -> e -> e) -> Array r ix e -> Array r ix e -> Array r ix e
defaultUnsafeLiftArray2
  {-# INLINE unsafeLiftArray2 #-}

----------------------
-- Helper functions --
----------------------

uninitialized :: a
uninitialized :: forall a. a
uninitialized = forall a e. Exception e => e -> a
throw Uninitialized
Uninitialized

---------------------
-- WHNF conversion --
---------------------

-- | /O(1)/ - Unwrap boxed array. This will discard any possible slicing that has been
-- applied to the array.
--
-- @since 0.2.1
unwrapArray :: Array B ix e -> A.Array e
unwrapArray :: forall ix e. Array B ix e -> Array e
unwrapArray = forall ix e. Array BL ix e -> Array e
blData forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE unwrapArray #-}

-- | /O(n)/ - Wrap a boxed array and evaluate all elements to a WHNF.
--
-- @since 0.2.1
evalArray ::
     Comp -- ^ Computation strategy
  -> A.Array e -- ^ Lazy boxed array from @primitive@ package.
  -> Vector B e
evalArray :: forall e. Comp -> Array e -> Vector B e
evalArray Comp
comp Array e
a = forall ix e. Index ix => Array BL ix e -> Array B ix e
evalLazyArray forall a b. (a -> b) -> a -> b
$ forall r ix e. Strategy r => Comp -> Array r ix e -> Array r ix e
setComp Comp
comp forall a b. (a -> b) -> a -> b
$ forall e. Array e -> Vector BL e
wrapLazyArray Array e
a
{-# INLINE evalArray #-}


-- | /O(1)/ - Unwrap boxed array. This will discard any possible slicing that has been
-- applied to the array.
--
-- @since 0.6.0
unwrapLazyArray :: Array BL ix e -> A.Array e
unwrapLazyArray :: forall ix e. Array BL ix e -> Array e
unwrapLazyArray = forall ix e. Array BL ix e -> Array e
blData
{-# INLINE unwrapLazyArray #-}

-- | /O(1)/ - Wrap a boxed array.
--
-- @since 0.6.0
wrapLazyArray :: A.Array e -> Vector BL e
wrapLazyArray :: forall e. Array e -> Vector BL e
wrapLazyArray Array e
a = forall ix e. Comp -> Sz ix -> Ix1 -> Array e -> Array BL ix e
BLArray Comp
Seq (forall ix. ix -> Sz ix
SafeSz (forall a. Array a -> Ix1
A.sizeofArray Array e
a)) Ix1
0 Array e
a
{-# INLINE wrapLazyArray #-}


-- | /O(1)/ - Cast a strict boxed array into a lazy boxed array.
--
-- @since 0.6.0
toLazyArray :: Array B ix e -> Array BL ix e
toLazyArray :: forall ix e. Array B ix e -> Array BL ix e
toLazyArray = coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE toLazyArray #-}

-- | /O(n)/ - Evaluate all elements of a boxed lazy array to weak head normal form
--
-- @since 0.6.0
evalLazyArray :: Index ix => Array BL ix e -> Array B ix e
evalLazyArray :: forall ix e. Index ix => Array BL ix e -> Array B ix e
evalLazyArray Array BL ix e
arr = Array BL ix e
arr forall ix a t. Index ix => Array BL ix a -> t -> t
`seqArray` forall ix e. Array BL ix e -> Array B ix e
BArray Array BL ix e
arr
{-# INLINE evalLazyArray #-}

-- | /O(n)/ - Evaluate all elements of a boxed lazy array to normal form
--
-- @since 0.6.0
forceLazyArray :: (NFData e, Index ix) => Array BL ix e -> Array N ix e
forceLazyArray :: forall e ix. (NFData e, Index ix) => Array BL ix e -> Array BN ix e
forceLazyArray Array BL ix e
arr = Array BL ix e
arr forall a ix t. (NFData a, Index ix) => Array BL ix a -> t -> t
`deepseqArray` forall ix e. Array BL ix e -> Array BN ix e
BNArray Array BL ix e
arr
{-# INLINE forceLazyArray #-}

-- | /O(1)/ - Unwrap mutable boxed array. This will discard any possible slicing that has been
-- applied to the array.
--
-- @since 0.2.1
unwrapMutableArray :: MArray s B ix e -> A.MutableArray s e
unwrapMutableArray :: forall s ix e. MArray s B ix e -> MutableArray s e
unwrapMutableArray (MBArray (MBLArray Sz ix
_ Ix1
_ MutableArray s e
marr)) = MutableArray s e
marr
{-# INLINE unwrapMutableArray #-}


-- | /O(1)/ - Unwrap mutable boxed lazy array. This will discard any possible slicing that has been
-- applied to the array.
--
-- @since 0.6.0
unwrapMutableLazyArray :: MArray s BL ix e -> A.MutableArray s e
unwrapMutableLazyArray :: forall s ix e. MArray s BL ix e -> MutableArray s e
unwrapMutableLazyArray (MBLArray Sz ix
_ Ix1
_ MutableArray s e
marr) = MutableArray s e
marr
{-# INLINE unwrapMutableLazyArray #-}


-- | /O(n)/ - Wrap mutable boxed array and evaluate all elements to WHNF.
--
-- @since 0.2.1
evalMutableArray ::
     PrimMonad m
  => A.MutableArray (PrimState m) e -- ^ Mutable array that will get wrapped
  -> m (MArray (PrimState m) B Ix1 e)
evalMutableArray :: forall (m :: * -> *) e.
PrimMonad m =>
MutableArray (PrimState m) e -> m (MArray (PrimState m) B Ix1 e)
evalMutableArray = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s ix e. MArray s BL ix e -> MArray s B ix e
MBArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) e a.
PrimMonad m =>
(e -> m () -> m a)
-> MutableArray (PrimState m) e
-> m (MArray (PrimState m) BL Ix1 e)
fromMutableArraySeq seq :: forall a b. a -> b -> b
seq
{-# INLINE evalMutableArray #-}

-------------------
-- NF conversion --
-------------------

-- | /O(1)/ - Unwrap a fully evaluated boxed array. This will discard any possible slicing
-- that has been applied to the array.
--
-- @since 0.2.1
unwrapNormalFormArray :: Array N ix e -> A.Array e
unwrapNormalFormArray :: forall ix e. Array BN ix e -> Array e
unwrapNormalFormArray = forall ix e. Array BL ix e -> Array e
blData forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE unwrapNormalFormArray #-}

-- | /O(n)/ - Wrap a boxed array and evaluate all elements to a Normal Form (NF).
--
-- @since 0.2.1
evalNormalFormArray ::
     NFData e
  => Comp -- ^ Computation strategy
  -> A.Array e -- ^ Lazy boxed array
  -> Array N Ix1 e
evalNormalFormArray :: forall e. NFData e => Comp -> Array e -> Array BN Ix1 e
evalNormalFormArray Comp
comp = forall e ix. (NFData e, Index ix) => Array BL ix e -> Array BN ix e
forceLazyArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Strategy r => Comp -> Array r ix e -> Array r ix e
setComp Comp
comp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Array e -> Vector BL e
wrapLazyArray
{-# INLINE evalNormalFormArray #-}


-- | /O(1)/ - Unwrap a fully evaluated mutable boxed array. This will discard any possible
-- slicing that has been applied to the array.
--
-- @since 0.2.1
unwrapNormalFormMutableArray :: MArray s N ix e -> A.MutableArray s e
unwrapNormalFormMutableArray :: forall s ix e. MArray s BN ix e -> MutableArray s e
unwrapNormalFormMutableArray = forall s ix e. MArray s BL ix e -> MutableArray s e
unwrapMutableLazyArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE unwrapNormalFormMutableArray #-}


-- | /O(n)/ - Wrap mutable boxed array and evaluate all elements to NF.
--
-- @since 0.2.1
evalNormalFormMutableArray ::
     (PrimMonad m, NFData e)
  => A.MutableArray (PrimState m) e
  -> m (MArray (PrimState m) N Ix1 e)
evalNormalFormMutableArray :: forall (m :: * -> *) e.
(PrimMonad m, NFData e) =>
MutableArray (PrimState m) e -> m (MArray (PrimState m) BN Ix1 e)
evalNormalFormMutableArray MutableArray (PrimState m) e
marr = forall s ix e. MArray s BL ix e -> MArray s BN ix e
MBNArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) e a.
PrimMonad m =>
(e -> m () -> m a)
-> MutableArray (PrimState m) e
-> m (MArray (PrimState m) BL Ix1 e)
fromMutableArraySeq forall a b. NFData a => a -> b -> b
deepseq MutableArray (PrimState m) e
marr
{-# INLINE evalNormalFormMutableArray #-}


----------------------
-- Helper functions --
----------------------

fromMutableArraySeq ::
     PrimMonad m
  => (e -> m () -> m a)
  -> A.MutableArray (PrimState m) e
  -> m (MArray (PrimState m) BL Ix1 e)
fromMutableArraySeq :: forall (m :: * -> *) e a.
PrimMonad m =>
(e -> m () -> m a)
-> MutableArray (PrimState m) e
-> m (MArray (PrimState m) BL Ix1 e)
fromMutableArraySeq e -> m () -> m a
with MutableArray (PrimState m) e
ma = do
  let !sz :: Ix1
sz = forall s a. MutableArray s a -> Ix1
A.sizeofMutableArray MutableArray (PrimState m) e
ma
  forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
0 (forall a. Ord a => a -> a -> Bool
< Ix1
sz) (forall a. Num a => a -> a -> a
+ Ix1
1) (forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Ix1 -> m a
A.readArray MutableArray (PrimState m) e
ma forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (e -> m () -> m a
`with` forall (m :: * -> *) a. Monad m => a -> m a
return ()))
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray (forall ix. ix -> Sz ix
SafeSz Ix1
sz) Ix1
0 MutableArray (PrimState m) e
ma
{-# INLINE fromMutableArraySeq #-}


seqArray :: Index ix => Array BL ix a -> t -> t
seqArray :: forall ix a t. Index ix => Array BL ix a -> t -> t
seqArray !Array BL ix a
arr t
t = forall ix r e a b.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> (b -> a -> b) -> b -> Array r ix e -> b
foldlInternal (forall a b c. (a -> b -> c) -> b -> a -> c
flip seq :: forall a b. a -> b -> b
seq) () (forall a b c. (a -> b -> c) -> b -> a -> c
flip seq :: forall a b. a -> b -> b
seq) () Array BL ix a
arr seq :: forall a b. a -> b -> b
`seq` t
t
{-# INLINE seqArray #-}


deepseqArray :: (NFData a, Index ix) => Array BL ix a -> t -> t
deepseqArray :: forall a ix t. (NFData a, Index ix) => Array BL ix a -> t -> t
deepseqArray !Array BL ix a
arr t
t = forall ix r e a b.
(Index ix, Source r e) =>
(a -> e -> a) -> a -> (b -> a -> b) -> b -> Array r ix e -> b
foldlInternal (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. NFData a => a -> b -> b
deepseq) () (forall a b c. (a -> b -> c) -> b -> a -> c
flip seq :: forall a b. a -> b -> b
seq) () Array BL ix a
arr seq :: forall a b. a -> b -> b
`seq` t
t
{-# INLINE deepseqArray #-}


-- | /O(1)/ - Converts array from `N` to `B` representation.
--
-- @since 0.5.0
unwrapNormalForm :: Array N ix e -> Array B ix e
unwrapNormalForm :: forall ix e. Array BN ix e -> Array B ix e
unwrapNormalForm = coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE unwrapNormalForm #-}

-- | /O(n)/ - Compute all elements of a boxed array to NF (normal form)
--
-- @since 0.5.0
evalNormalForm :: (Index ix, NFData e) => Array B ix e -> Array N ix e
evalNormalForm :: forall ix e. (Index ix, NFData e) => Array B ix e -> Array BN ix e
evalNormalForm (BArray Array BL ix e
arr) = Array BL ix e
arr forall a ix t. (NFData a, Index ix) => Array BL ix a -> t -> t
`deepseqArray` forall ix e. Array BL ix e -> Array BN ix e
BNArray Array BL ix e
arr
{-# INLINE evalNormalForm #-}

-- | /O(1)/ - Converts a boxed `Array` into a `VB.Vector` without touching any
-- elements.
--
-- @since 0.5.0
{-# INLINE toBoxedVector #-}
toBoxedVector :: Index ix => Array BL ix a -> VB.Vector a
toBoxedVector :: forall ix a. Index ix => Array BL ix a -> Vector a
toBoxedVector BLArray{blOffset :: forall ix e. Array BL ix e -> Ix1
blOffset = Ix1
off, blSize :: forall ix e. Array BL ix e -> Sz ix
blSize = Sz ix
sz, blData :: forall ix e. Array BL ix e -> Array e
blData = Array a
arr } =
#if MIN_VERSION_vector(0,13,0)
  forall a. Array a -> Ix1 -> Ix1 -> Vector a
VB.unsafeFromArraySlice Array a
arr Ix1
off (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz)
#elif MIN_VERSION_vector(0,12,2)
  VB.unsafeTake (totalElem sz) (VB.unsafeDrop off (VB.fromArray arr))
#else
  fromVectorCast $ VectorCast off (totalElem sz) arr

fromVectorCast :: VectorCast a -> VB.Vector a
fromVectorCast = unsafeCoerce
#endif



-- | /O(1)/ - Converts a boxed `MArray` into a `MVB.MVector`.
--
-- @since 0.5.0
toBoxedMVector :: Index ix => MArray s BL ix a -> MVB.MVector s a
toBoxedMVector :: forall ix s a. Index ix => MArray s BL ix a -> MVector s a
toBoxedMVector (MBLArray Sz ix
sz Ix1
o MutableArray s a
marr) = forall s a. Ix1 -> Ix1 -> MutableArray s a -> MVector s a
MVB.MVector Ix1
o (forall ix. Index ix => Sz ix -> Ix1
totalElem Sz ix
sz) MutableArray s a
marr
{-# INLINE toBoxedMVector #-}

-- | /O(n)/ - Convert a boxed vector and evaluate all elements to WHNF. Computation
-- strategy will be respected during evaluation
--
-- @since 0.5.0
evalBoxedVector :: Comp -> VB.Vector a -> Array B Ix1 a
evalBoxedVector :: forall a. Comp -> Vector a -> Array B Ix1 a
evalBoxedVector Comp
comp = forall ix e. Index ix => Array BL ix e -> Array B ix e
evalLazyArray forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r ix e. Strategy r => Comp -> Array r ix e -> Array r ix e
setComp Comp
comp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Vector a -> Vector BL a
fromBoxedVector
{-# INLINE evalBoxedVector #-}


-- | /O(n)/ - Convert mutable boxed vector and evaluate all elements to WHNF
-- sequentially. Both keep pointing to the same memory
--
-- @since 0.5.0
evalBoxedMVector :: PrimMonad m => MVB.MVector (PrimState m) a -> m (MArray (PrimState m) B Ix1 a)
evalBoxedMVector :: forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> m (MArray (PrimState m) B Ix1 a)
evalBoxedMVector (MVB.MVector Ix1
o Ix1
k MutableArray (PrimState m) a
ma) =
  let marr :: MArray (PrimState m) B Ix1 a
marr = forall s ix e. MArray s BL ix e -> MArray s B ix e
MBArray (forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray (forall ix. ix -> Sz ix
SafeSz Ix1
k) Ix1
o MutableArray (PrimState m) a
ma)
   in MArray (PrimState m) B Ix1 a
marr forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
o (forall a. Ord a => a -> a -> Bool
< Ix1
k) (forall a. Num a => a -> a -> a
+ Ix1
1) (forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Ix1 -> m a
A.readArray MutableArray (PrimState m) a
ma forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (seq :: forall a b. a -> b -> b
`seq` forall (f :: * -> *) a. Applicative f => a -> f a
pure ()))
{-# INLINE evalBoxedMVector #-}


-- | /O(1)/ - Cast a boxed vector without touching any elements.
--
-- @since 0.6.0
fromBoxedVector :: VB.Vector a -> Vector BL a
{-# INLINE fromBoxedVector #-}
fromBoxedVector :: forall a. Vector a -> Vector BL a
fromBoxedVector Vector a
v =
  BLArray {blComp :: Comp
blComp = Comp
Seq, blSize :: Sz1
blSize = forall ix. ix -> Sz ix
SafeSz Ix1
n, blOffset :: Ix1
blOffset = Ix1
offset, blData :: Array a
blData = Array a
arr}
  where
#if MIN_VERSION_vector(0,13,0)
    (Array a
arr, Ix1
offset, Ix1
n) = forall a. Vector a -> (Array a, Ix1, Ix1)
VB.toArraySlice Vector a
v
#else
    VectorCast offset n arr = toVectorCast v

-- This internal type is needed to get into the internals of a boxed vector,
-- since it is not possible until vector-0.13 version.
data VectorCast a =
  VectorCast {-# UNPACK #-}!Int {-# UNPACK #-}!Int {-# UNPACK #-}!(A.Array a)

toVectorCast :: VB.Vector a -> VectorCast a
toVectorCast = unsafeCoerce
#endif


-- | /O(1)/ - Convert mutable boxed vector to a lazy mutable boxed array. Both keep
-- pointing to the same memory
--
-- @since 0.6.0
fromBoxedMVector :: MVB.MVector s a -> MArray s BL Ix1 a
fromBoxedMVector :: forall s a. MVector s a -> MArray s BL Ix1 a
fromBoxedMVector (MVB.MVector Ix1
o Ix1
k MutableArray s a
ma) = forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray (forall ix. ix -> Sz ix
SafeSz Ix1
k) Ix1
o MutableArray s a
ma
{-# INLINE fromBoxedMVector #-}


-- | /O(1)/ - Cast a boxed lazy array. It is unsafe because it can violate the invariant
-- that all elements of `N` array are in NF.
--
-- @since 0.6.0
coerceNormalBoxedArray :: Array BL ix e -> Array N ix e
coerceNormalBoxedArray :: forall ix e. Array BL ix e -> Array BN ix e
coerceNormalBoxedArray = coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE coerceNormalBoxedArray #-}


-- | /O(1)/ - Cast a boxed lazy array. It is unsafe because it can violate the invariant
-- that all elements of `B` array are in WHNF.
--
-- @since 0.6.0
coerceBoxedArray :: Array BL ix e -> Array B ix e
coerceBoxedArray :: forall ix e. Array BL ix e -> Array B ix e
coerceBoxedArray = coerce :: forall a b. Coercible a b => a -> b
coerce
{-# INLINE coerceBoxedArray #-}

-- | /O(n)/ - Convert mutable boxed vector and evaluate all elements to WHNF
-- sequentially. Both keep pointing to the same memory
--
-- @since 0.5.0
evalNormalBoxedMVector ::
     (NFData a, PrimMonad m) => MVB.MVector (PrimState m) a -> m (MArray (PrimState m) N Ix1 a)
evalNormalBoxedMVector :: forall a (m :: * -> *).
(NFData a, PrimMonad m) =>
MVector (PrimState m) a -> m (MArray (PrimState m) BN Ix1 a)
evalNormalBoxedMVector (MVB.MVector Ix1
o Ix1
k MutableArray (PrimState m) a
ma) =
  let marr :: MArray (PrimState m) BN Ix1 a
marr = forall s ix e. MArray s BL ix e -> MArray s BN ix e
MBNArray (forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray (forall ix. ix -> Sz ix
SafeSz Ix1
k) Ix1
o MutableArray (PrimState m) a
ma)
   in MArray (PrimState m) BN Ix1 a
marr forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall (f :: * -> *) a.
Applicative f =>
Ix1 -> (Ix1 -> Bool) -> (Ix1 -> Ix1) -> (Ix1 -> f a) -> f ()
loopA_ Ix1
o (forall a. Ord a => a -> a -> Bool
< Ix1
k) (forall a. Num a => a -> a -> a
+ Ix1
1) (forall (m :: * -> *) a.
PrimMonad m =>
MutableArray (PrimState m) a -> Ix1 -> m a
A.readArray MutableArray (PrimState m) a
ma forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. NFData a => a -> ()
rnf)
{-# INLINE evalNormalBoxedMVector #-}

-- | /O(n)/ - Convert a boxed vector and evaluate all elements to WHNF. Computation
-- strategy will be respected during evaluation
--
-- @since 0.5.0
evalNormalBoxedVector :: NFData a => Comp -> VB.Vector a -> Array N Ix1 a
evalNormalBoxedVector :: forall a. NFData a => Comp -> Vector a -> Array BN Ix1 a
evalNormalBoxedVector Comp
comp Vector a
v =
  forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
    MVB.MVector Ix1
o Ix1
k MutableArray s a
ma <- forall (m :: * -> *) a.
PrimMonad m =>
Vector a -> m (MVector (PrimState m) a)
VB.unsafeThaw Vector a
v
    forall e ix. (NFData e, Index ix) => Array BL ix e -> Array BN ix e
forceLazyArray forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
comp (forall s ix e. Sz ix -> Ix1 -> MutableArray s e -> MArray s BL ix e
MBLArray (forall ix. ix -> Sz ix
SafeSz Ix1
k) Ix1
o MutableArray s a
ma)
{-# INLINE evalNormalBoxedVector #-}