-- |
-- Module      : Streamly.Internal.Data.SmallArray
-- Copyright   : (c) 2019 Composewell Technologies
--
-- License     : BSD-3-Clause
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC

{-# OPTIONS_GHC -fno-warn-orphans #-}

{-# LANGUAGE CPP           #-}
{-# LANGUAGE MagicHash     #-}
{-# LANGUAGE UnboxedTuples #-}

#include "inline.hs"

module Streamly.Internal.Data.SmallArray
  (
    -- XXX should it be just Array instead? We should be able to replace one
    -- array type with another easily.
    SmallArray(..)

  , foldl'
  , foldr

  , length

  , writeN

  , toStreamD
  , toStreamDRev

  , toStream
  , toStreamRev
  , read

  , fromListN
  , fromStreamDN
  , fromStreamN

  , streamFold
  , fold
  )
where

import Prelude hiding (foldr, length, read)
import Control.DeepSeq (NFData(..))
import Control.Monad (when)
import Control.Monad.IO.Class (MonadIO, liftIO)
import GHC.IO (unsafePerformIO)
import Data.Functor.Identity (runIdentity)

import Streamly.Internal.Data.SmallArray.Types
import Streamly.Internal.Data.Unfold.Types (Unfold(..))
import Streamly.Internal.Data.Fold.Types (Fold(..))
import Streamly.Internal.Data.Stream.StreamK.Type (IsStream)
import Streamly.Internal.Data.Stream.Serial (SerialT)

import qualified Streamly.Internal.Data.Stream.StreamD as D

{-# NOINLINE bottomElement #-}
bottomElement :: a
bottomElement :: a
bottomElement = a
forall a. HasCallStack => a
undefined

{-# INLINE length #-}
length :: SmallArray a -> Int
length :: SmallArray a -> Int
length SmallArray a
arr = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
arr

{-# INLINE_NORMAL toStreamD #-}
toStreamD :: Monad m => SmallArray a -> D.Stream m a
toStreamD :: SmallArray a -> Stream m a
toStreamD SmallArray a
arr = (State Stream m a -> Int -> m (Step Int a)) -> Int -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m a -> Int -> m (Step Int a)
forall (m :: * -> *) p. Monad m => p -> Int -> m (Step Int a)
step Int
0
  where
    {-# INLINE_LATE step #-}
    step :: p -> Int -> m (Step Int a)
step p
_ Int
i
        | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== SmallArray a -> Int
forall a. SmallArray a -> Int
length SmallArray a
arr = Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int a
forall s a. Step s a
D.Stop
        | Bool
otherwise =
            Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int a -> m (Step Int a)) -> Step Int a -> m (Step Int a)
forall a b. (a -> b) -> a -> b
$
            case SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
arr Int
i of
                (# a
x #) -> a -> Int -> Step Int a
forall s a. a -> s -> Step s a
D.Yield a
x (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)

{-# INLINE_NORMAL toStreamDRev #-}
toStreamDRev :: Monad m => SmallArray a -> D.Stream m a
toStreamDRev :: SmallArray a -> Stream m a
toStreamDRev SmallArray a
arr = (State Stream m a -> Int -> m (Step Int a)) -> Int -> Stream m a
forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
D.Stream State Stream m a -> Int -> m (Step Int a)
forall (m :: * -> *) p. Monad m => p -> Int -> m (Step Int a)
step (SmallArray a -> Int
forall a. SmallArray a -> Int
length SmallArray a
arr Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
  where
    {-# INLINE_LATE step #-}
    step :: p -> Int -> m (Step Int a)
step p
_ Int
i
        | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int a
forall s a. Step s a
D.Stop
        | Bool
otherwise =
            Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int a -> m (Step Int a)) -> Step Int a -> m (Step Int a)
forall a b. (a -> b) -> a -> b
$
            case SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
arr Int
i of
                (# a
x #) -> a -> Int -> Step Int a
forall s a. a -> s -> Step s a
D.Yield a
x (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

{-# INLINE_NORMAL foldl' #-}
foldl' :: (b -> a -> b) -> b -> SmallArray a -> b
foldl' :: (b -> a -> b) -> b -> SmallArray a -> b
foldl' b -> a -> b
f b
z SmallArray a
arr = Identity b -> b
forall a. Identity a -> a
runIdentity (Identity b -> b) -> Identity b -> b
forall a b. (a -> b) -> a -> b
$ (b -> a -> b) -> b -> Stream Identity a -> Identity b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Stream m a -> m b
D.foldl' b -> a -> b
f b
z (Stream Identity a -> Identity b)
-> Stream Identity a -> Identity b
forall a b. (a -> b) -> a -> b
$ SmallArray a -> Stream Identity a
forall (m :: * -> *) a. Monad m => SmallArray a -> Stream m a
toStreamD SmallArray a
arr

{-# INLINE_NORMAL foldr #-}
foldr :: (a -> b -> b) -> b -> SmallArray a -> b
foldr :: (a -> b -> b) -> b -> SmallArray a -> b
foldr a -> b -> b
f b
z SmallArray a
arr = Identity b -> b
forall a. Identity a -> a
runIdentity (Identity b -> b) -> Identity b -> b
forall a b. (a -> b) -> a -> b
$ (a -> b -> b) -> b -> Stream Identity a -> Identity b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
D.foldr a -> b -> b
f b
z (Stream Identity a -> Identity b)
-> Stream Identity a -> Identity b
forall a b. (a -> b) -> a -> b
$ SmallArray a -> Stream Identity a
forall (m :: * -> *) a. Monad m => SmallArray a -> Stream m a
toStreamD SmallArray a
arr

-- | @writeN n@ folds a maximum of @n@ elements from the input stream to an
-- 'SmallArray'.
--
-- Since we are folding to a 'SmallArray' @n@ should be <= 128, for larger number
-- of elements use an 'Array' from either "Streamly.Data.Array" or "Streamly.Memory.Array".
{-# INLINE_NORMAL writeN #-}
writeN :: MonadIO m => Int -> Fold m a (SmallArray a)
writeN :: Int -> Fold m a (SmallArray a)
writeN Int
limit = ((SmallMutableArray RealWorld a, Int)
 -> a -> m (SmallMutableArray RealWorld a, Int))
-> m (SmallMutableArray RealWorld a, Int)
-> ((SmallMutableArray RealWorld a, Int) -> m (SmallArray a))
-> Fold m a (SmallArray a)
forall (m :: * -> *) a b s.
(s -> a -> m s) -> m s -> (s -> m b) -> Fold m a b
Fold (SmallMutableArray RealWorld a, Int)
-> a -> m (SmallMutableArray RealWorld a, Int)
forall (m :: * -> *) a.
MonadIO m =>
(SmallMutableArray RealWorld a, Int)
-> a -> m (SmallMutableArray RealWorld a, Int)
step m (SmallMutableArray RealWorld a, Int)
forall a. m (SmallMutableArray RealWorld a, Int)
initial (SmallMutableArray RealWorld a, Int) -> m (SmallArray a)
forall (m :: * -> *) a.
MonadIO m =>
(SmallMutableArray RealWorld a, Int) -> m (SmallArray a)
extract
  where
    initial :: m (SmallMutableArray RealWorld a, Int)
initial = do
        SmallMutableArray RealWorld a
marr <- IO (SmallMutableArray RealWorld a)
-> m (SmallMutableArray RealWorld a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SmallMutableArray RealWorld a)
 -> m (SmallMutableArray RealWorld a))
-> IO (SmallMutableArray RealWorld a)
-> m (SmallMutableArray RealWorld a)
forall a b. (a -> b) -> a -> b
$ Int -> a -> IO (SmallMutableArray (PrimState IO) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
limit a
forall a. a
bottomElement
        (SmallMutableArray RealWorld a, Int)
-> m (SmallMutableArray RealWorld a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (SmallMutableArray RealWorld a
marr, Int
0)
    step :: (SmallMutableArray RealWorld a, Int)
-> a -> m (SmallMutableArray RealWorld a, Int)
step (SmallMutableArray RealWorld a
marr, Int
i) a
x
        | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
limit = (SmallMutableArray RealWorld a, Int)
-> m (SmallMutableArray RealWorld a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (SmallMutableArray RealWorld a
marr, Int
i)
        | Bool
otherwise = do
            IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ SmallMutableArray (PrimState IO) a -> Int -> a -> IO ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray RealWorld a
SmallMutableArray (PrimState IO) a
marr Int
i a
x
            (SmallMutableArray RealWorld a, Int)
-> m (SmallMutableArray RealWorld a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (SmallMutableArray RealWorld a
marr, Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
    extract :: (SmallMutableArray RealWorld a, Int) -> m (SmallArray a)
extract (SmallMutableArray RealWorld a
marr, Int
len) = IO (SmallArray a) -> m (SmallArray a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SmallArray a) -> m (SmallArray a))
-> IO (SmallArray a) -> m (SmallArray a)
forall a b. (a -> b) -> a -> b
$ SmallMutableArray (PrimState IO) a
-> Int -> Int -> IO (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> Int -> m (SmallArray a)
freezeSmallArray SmallMutableArray RealWorld a
SmallMutableArray (PrimState IO) a
marr Int
0 Int
len

{-# INLINE_NORMAL fromStreamDN #-}
fromStreamDN :: MonadIO m => Int -> D.Stream m a -> m (SmallArray a)
fromStreamDN :: Int -> Stream m a -> m (SmallArray a)
fromStreamDN Int
limit Stream m a
str = do
    SmallMutableArray RealWorld a
marr <- IO (SmallMutableArray RealWorld a)
-> m (SmallMutableArray RealWorld a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SmallMutableArray RealWorld a)
 -> m (SmallMutableArray RealWorld a))
-> IO (SmallMutableArray RealWorld a)
-> m (SmallMutableArray RealWorld a)
forall a b. (a -> b) -> a -> b
$ Int -> a -> IO (SmallMutableArray (PrimState IO) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
limit Int
0) a
forall a. a
bottomElement
    Int
i <-
        (Int -> a -> m Int) -> Int -> Stream m a -> m Int
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> b -> Stream m a -> m b
D.foldlM'
            (\Int
i a
x -> Int
i Int -> m Int -> m Int
`seq` (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ SmallMutableArray (PrimState IO) a -> Int -> a -> IO ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray RealWorld a
SmallMutableArray (PrimState IO) a
marr Int
i a
x) m () -> m Int -> m Int
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
            Int
0 (Stream m a -> m Int) -> Stream m a -> m Int
forall a b. (a -> b) -> a -> b
$
        Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
D.take Int
limit Stream m a
str
    IO (SmallArray a) -> m (SmallArray a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SmallArray a) -> m (SmallArray a))
-> IO (SmallArray a) -> m (SmallArray a)
forall a b. (a -> b) -> a -> b
$ SmallMutableArray (PrimState IO) a
-> Int -> Int -> IO (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> Int -> m (SmallArray a)
freezeSmallArray SmallMutableArray RealWorld a
SmallMutableArray (PrimState IO) a
marr Int
0 Int
i

-- | Create a 'SmallArray' from the first @n@ elements of a list. The
-- array may hold less than @n@ elements if the length of the list <=
-- @n@.
--
-- It is recommended to use a value of @n@ <= 128. For larger sized
-- arrays, use an 'Array' from "Streamly.Data.Array" or
-- "Streamly.Memory.Array"
{-# INLINABLE fromListN #-}
fromListN :: Int -> [a] -> SmallArray a
fromListN :: Int -> [a] -> SmallArray a
fromListN Int
n [a]
xs = IO (SmallArray a) -> SmallArray a
forall a. IO a -> a
unsafePerformIO (IO (SmallArray a) -> SmallArray a)
-> IO (SmallArray a) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ Int -> Stream IO a -> IO (SmallArray a)
forall (m :: * -> *) a.
MonadIO m =>
Int -> Stream m a -> m (SmallArray a)
fromStreamDN Int
n (Stream IO a -> IO (SmallArray a))
-> Stream IO a -> IO (SmallArray a)
forall a b. (a -> b) -> a -> b
$ [a] -> Stream IO a
forall (m :: * -> *) a. Applicative m => [a] -> Stream m a
D.fromList [a]
xs

instance NFData a => NFData (SmallArray a) where
    {-# INLINE rnf #-}
    rnf :: SmallArray a -> ()
rnf = (() -> a -> ()) -> () -> SmallArray a -> ()
forall b a. (b -> a -> b) -> b -> SmallArray a -> b
foldl' (\()
_ a
x -> a -> ()
forall a. NFData a => a -> ()
rnf a
x) ()

-- | Create a 'SmallArray' from the first @n@ elements of a stream. The
-- array is allocated to size @n@, if the stream terminates before @n@
-- elements then the array may hold less than @n@ elements.
--
-- For optimal performance use this with @n@ <= 128.
{-# INLINE fromStreamN #-}
fromStreamN :: MonadIO m => Int -> SerialT m a -> m (SmallArray a)
fromStreamN :: Int -> SerialT m a -> m (SmallArray a)
fromStreamN Int
n SerialT m a
m = do
    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"fromStreamN: negative write count specified"
    Int -> Stream m a -> m (SmallArray a)
forall (m :: * -> *) a.
MonadIO m =>
Int -> Stream m a -> m (SmallArray a)
fromStreamDN Int
n (Stream m a -> m (SmallArray a)) -> Stream m a -> m (SmallArray a)
forall a b. (a -> b) -> a -> b
$ SerialT m a -> Stream m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
t m a -> Stream m a
D.toStreamD SerialT m a
m

{-# INLINE_EARLY toStream #-}
toStream :: (Monad m, IsStream t) => SmallArray a -> t m a
toStream :: SmallArray a -> t m a
toStream = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (Stream m a -> t m a)
-> (SmallArray a -> Stream m a) -> SmallArray a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SmallArray a -> Stream m a
forall (m :: * -> *) a. Monad m => SmallArray a -> Stream m a
toStreamD

{-# INLINE_EARLY toStreamRev #-}
toStreamRev :: (Monad m, IsStream t) => SmallArray a -> t m a
toStreamRev :: SmallArray a -> t m a
toStreamRev = Stream m a -> t m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
Stream m a -> t m a
D.fromStreamD (Stream m a -> t m a)
-> (SmallArray a -> Stream m a) -> SmallArray a -> t m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SmallArray a -> Stream m a
forall (m :: * -> *) a. Monad m => SmallArray a -> Stream m a
toStreamDRev

{-# INLINE fold #-}
fold :: Monad m => Fold m a b -> SmallArray a -> m b
fold :: Fold m a b -> SmallArray a -> m b
fold Fold m a b
f SmallArray a
arr = Fold m a b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
Fold m a b -> Stream m a -> m b
D.runFold Fold m a b
f (SmallArray a -> Stream m a
forall (m :: * -> *) a. Monad m => SmallArray a -> Stream m a
toStreamD SmallArray a
arr)

{-# INLINE streamFold #-}
streamFold :: Monad m => (SerialT m a -> m b) -> SmallArray a -> m b
streamFold :: (SerialT m a -> m b) -> SmallArray a -> m b
streamFold SerialT m a -> m b
f SmallArray a
arr = SerialT m a -> m b
f (SmallArray a -> SerialT m a
forall (m :: * -> *) (t :: (* -> *) -> * -> *) a.
(Monad m, IsStream t) =>
SmallArray a -> t m a
toStream SmallArray a
arr)

{-# INLINE_NORMAL read #-}
read :: Monad m => Unfold m (SmallArray a) a
read :: Unfold m (SmallArray a) a
read = ((SmallArray a, Int) -> m (Step (SmallArray a, Int) a))
-> (SmallArray a -> m (SmallArray a, Int))
-> Unfold m (SmallArray a) a
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (SmallArray a, Int) -> m (Step (SmallArray a, Int) a)
forall (m :: * -> *) a.
Monad m =>
(SmallArray a, Int) -> m (Step (SmallArray a, Int) a)
step SmallArray a -> m (SmallArray a, Int)
forall (m :: * -> *) b a. (Monad m, Num b) => a -> m (a, b)
inject
  where
    inject :: a -> m (a, b)
inject a
arr = (a, b) -> m (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
arr, b
0)
    step :: (SmallArray a, Int) -> m (Step (SmallArray a, Int) a)
step (SmallArray a
arr, Int
i)
        | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== SmallArray a -> Int
forall a. SmallArray a -> Int
length SmallArray a
arr = Step (SmallArray a, Int) a -> m (Step (SmallArray a, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (SmallArray a, Int) a
forall s a. Step s a
D.Stop
        | Bool
otherwise =
            Step (SmallArray a, Int) a -> m (Step (SmallArray a, Int) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (SmallArray a, Int) a -> m (Step (SmallArray a, Int) a))
-> Step (SmallArray a, Int) a -> m (Step (SmallArray a, Int) a)
forall a b. (a -> b) -> a -> b
$
            case SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
arr Int
i of
                (# a
x #) -> a -> (SmallArray a, Int) -> Step (SmallArray a, Int) a
forall s a. a -> s -> Step s a
D.Yield a
x (SmallArray a
arr, Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)