{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
module Data.Primitive.SmallArray
( SmallArray(..)
, SmallMutableArray(..)
, newSmallArray
, readSmallArray
, writeSmallArray
, copySmallArray
, copySmallMutableArray
, indexSmallArray
, indexSmallArrayM
, indexSmallArray##
, cloneSmallArray
, cloneSmallMutableArray
, freezeSmallArray
, unsafeFreezeSmallArray
, thawSmallArray
, unsafeThawSmallArray
, runSmallArray
, createSmallArray
, sizeofSmallArray
, getSizeofSmallMutableArray
, sizeofSmallMutableArray
#if MIN_VERSION_base(4,14,0)
, shrinkSmallMutableArray
, resizeSmallMutableArray
#endif
, emptySmallArray
, smallArrayFromList
, smallArrayFromListN
, mapSmallArray'
, traverseSmallArrayP
) where
import GHC.Exts hiding (toList)
import qualified GHC.Exts
import Control.Applicative
import Control.DeepSeq
import Control.Monad
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.Primitive
import Control.Monad.ST
import Control.Monad.Zip
import Data.Data
import Data.Foldable as Foldable
import Data.Functor.Identity
import Data.Primitive.Internal.Read (Tag(..),lexTag)
import Text.Read (Read (..), parens, prec)
import qualified GHC.ST as GHCST
import Data.Semigroup
import Text.ParserCombinators.ReadP
import Text.ParserCombinators.ReadPrec (ReadPrec)
import qualified Text.ParserCombinators.ReadPrec as RdPrc
#if !MIN_VERSION_base(4,10,0)
import GHC.Base (runRW#)
#endif
import Data.Functor.Classes (Eq1(..), Ord1(..), Show1(..), Read1(..))
import Language.Haskell.TH.Syntax (Lift(..))
data SmallArray a = SmallArray (SmallArray# a)
deriving Typeable
#if MIN_VERSION_deepseq(1,4,3)
instance NFData1 SmallArray where
liftRnf :: forall a. (a -> ()) -> SmallArray a -> ()
liftRnf a -> ()
r = (() -> a -> ()) -> () -> SmallArray a -> ()
forall b a. (b -> a -> b) -> b -> SmallArray a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\()
_ -> a -> ()
r) ()
#endif
instance NFData a => NFData (SmallArray a) where
rnf :: SmallArray a -> ()
rnf = (() -> a -> ()) -> () -> SmallArray a -> ()
forall b a. (b -> a -> b) -> b -> SmallArray a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\()
_ -> a -> ()
forall a. NFData a => a -> ()
rnf) ()
data SmallMutableArray s a = SmallMutableArray (SmallMutableArray# s a)
deriving Typeable
instance Lift a => Lift (SmallArray a) where
#if MIN_VERSION_template_haskell(2,16,0)
liftTyped :: forall (m :: * -> *).
Quote m =>
SmallArray a -> Code m (SmallArray a)
liftTyped SmallArray a
ary = case [a]
lst of
[] -> [|| SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray ((# #) -> SmallArray# a
forall a. (# #) -> SmallArray# a
emptySmallArray# (##)) ||]
[a
x] -> [|| a -> f a
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$! a
x ||]
a
x : [a]
xs -> [|| Int -> a -> [a] -> SmallArray a
forall a. Int -> a -> [a] -> SmallArray a
unsafeSmallArrayFromListN' Int
len a
x [a]
xs ||]
#else
lift ary = case lst of
[] -> [| SmallArray (emptySmallArray# (##)) |]
[x] -> [| pure $! x |]
x : xs -> [| unsafeSmallArrayFromListN' len x xs |]
#endif
where
len :: Int
len = SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
ary
lst :: [a]
lst = SmallArray a -> [a]
forall a. SmallArray a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray a
ary
unsafeSmallArrayFromListN' :: Int -> a -> [a] -> SmallArray a
unsafeSmallArrayFromListN' :: forall a. Int -> a -> [a] -> SmallArray a
unsafeSmallArrayFromListN' Int
n a
y [a]
ys =
Int
-> a
-> (forall {s}. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray Int
n a
y ((forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a)
-> (forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s a
sma ->
let go :: Int -> [a] -> ST s ()
go !Int
_ix [] = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
go !Int
ix (!a
x : [a]
xs) = do
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
sma Int
ix a
x
Int -> [a] -> ST s ()
go (Int
ixInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [a]
xs
in Int -> [a] -> ST s ()
go Int
1 [a]
ys
newSmallArray
:: PrimMonad m
=> Int
-> a
-> m (SmallMutableArray (PrimState m) a)
newSmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray (I# Int#
i#) a
x = (State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a))
-> (State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s ->
case Int#
-> a
-> State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray# (PrimState m) a #)
forall a d.
Int# -> a -> State# d -> (# State# d, SmallMutableArray# d a #)
newSmallArray# Int#
i# a
x State# (PrimState m)
s of
(# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
sma# #) -> (# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
-> SmallMutableArray (PrimState m) a
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# (PrimState m) a
sma# #)
{-# INLINE newSmallArray #-}
readSmallArray
:: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> m a
readSmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m a
readSmallArray (SmallMutableArray SmallMutableArray# (PrimState m) a
sma#) (I# Int#
i#) =
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a)
-> (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall a b. (a -> b) -> a -> b
$ SmallMutableArray# (PrimState m) a
-> Int# -> State# (PrimState m) -> (# State# (PrimState m), a #)
forall d a.
SmallMutableArray# d a -> Int# -> State# d -> (# State# d, a #)
readSmallArray# SmallMutableArray# (PrimState m) a
sma# Int#
i#
{-# INLINE readSmallArray #-}
writeSmallArray
:: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> a
-> m ()
writeSmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray (SmallMutableArray SmallMutableArray# (PrimState m) a
sma#) (I# Int#
i#) a
x =
(State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ ((State# (PrimState m) -> State# (PrimState m)) -> m ())
-> (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall a b. (a -> b) -> a -> b
$ SmallMutableArray# (PrimState m) a
-> Int# -> a -> State# (PrimState m) -> State# (PrimState m)
forall d a.
SmallMutableArray# d a -> Int# -> a -> State# d -> State# d
writeSmallArray# SmallMutableArray# (PrimState m) a
sma# Int#
i# a
x
{-# INLINE writeSmallArray #-}
indexSmallArrayM
:: Applicative m
=> SmallArray a
-> Int
-> m a
indexSmallArrayM :: forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM (SmallArray SmallArray# a
sa#) (I# Int#
i#) =
case SmallArray# a -> Int# -> (# a #)
forall a. SmallArray# a -> Int# -> (# a #)
indexSmallArray# SmallArray# a
sa# Int#
i# of
(# a
x #) -> a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
{-# INLINE indexSmallArrayM #-}
indexSmallArray
:: SmallArray a
-> Int
-> a
indexSmallArray :: forall a. SmallArray a -> Int -> a
indexSmallArray SmallArray a
sa Int
i = Identity a -> a
forall a. Identity a -> a
runIdentity (Identity a -> a) -> Identity a -> a
forall a b. (a -> b) -> a -> b
$ SmallArray a -> Int -> Identity a
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray a
sa Int
i
{-# INLINE indexSmallArray #-}
indexSmallArray## :: SmallArray a -> Int -> (# a #)
indexSmallArray## :: forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## (SmallArray SmallArray# a
ary) (I# Int#
i) = SmallArray# a -> Int# -> (# a #)
forall a. SmallArray# a -> Int# -> (# a #)
indexSmallArray# SmallArray# a
ary Int#
i
{-# INLINE indexSmallArray## #-}
cloneSmallArray
:: SmallArray a
-> Int
-> Int
-> SmallArray a
cloneSmallArray :: forall a. SmallArray a -> Int -> Int -> SmallArray a
cloneSmallArray (SmallArray SmallArray# a
sa#) (I# Int#
i#) (I# Int#
j#) =
SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray (SmallArray# a -> Int# -> Int# -> SmallArray# a
forall a. SmallArray# a -> Int# -> Int# -> SmallArray# a
cloneSmallArray# SmallArray# a
sa# Int#
i# Int#
j#)
{-# INLINE cloneSmallArray #-}
cloneSmallMutableArray
:: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> Int
-> m (SmallMutableArray (PrimState m) a)
cloneSmallMutableArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> Int -> m (SmallMutableArray (PrimState m) a)
cloneSmallMutableArray (SmallMutableArray SmallMutableArray# (PrimState m) a
sma#) (I# Int#
o#) (I# Int#
l#) =
(State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a))
-> (State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s -> case SmallMutableArray# (PrimState m) a
-> Int#
-> Int#
-> State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray# (PrimState m) a #)
forall d a.
SmallMutableArray# d a
-> Int#
-> Int#
-> State# d
-> (# State# d, SmallMutableArray# d a #)
cloneSmallMutableArray# SmallMutableArray# (PrimState m) a
sma# Int#
o# Int#
l# State# (PrimState m)
s of
(# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
smb# #) -> (# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
-> SmallMutableArray (PrimState m) a
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# (PrimState m) a
smb# #)
{-# INLINE cloneSmallMutableArray #-}
freezeSmallArray
:: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> Int
-> m (SmallArray a)
freezeSmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> Int -> m (SmallArray a)
freezeSmallArray (SmallMutableArray SmallMutableArray# (PrimState m) a
sma#) (I# Int#
i#) (I# Int#
j#) =
(State# (PrimState m) -> (# State# (PrimState m), SmallArray a #))
-> m (SmallArray a)
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), SmallArray a #))
-> m (SmallArray a))
-> (State# (PrimState m)
-> (# State# (PrimState m), SmallArray a #))
-> m (SmallArray a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s -> case SmallMutableArray# (PrimState m) a
-> Int#
-> Int#
-> State# (PrimState m)
-> (# State# (PrimState m), SmallArray# a #)
forall d a.
SmallMutableArray# d a
-> Int# -> Int# -> State# d -> (# State# d, SmallArray# a #)
freezeSmallArray# SmallMutableArray# (PrimState m) a
sma# Int#
i# Int#
j# State# (PrimState m)
s of
(# State# (PrimState m)
s', SmallArray# a
sa# #) -> (# State# (PrimState m)
s', SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray SmallArray# a
sa# #)
{-# INLINE freezeSmallArray #-}
unsafeFreezeSmallArray
:: PrimMonad m => SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray (SmallMutableArray SmallMutableArray# (PrimState m) a
sma#) =
(State# (PrimState m) -> (# State# (PrimState m), SmallArray a #))
-> m (SmallArray a)
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), SmallArray a #))
-> m (SmallArray a))
-> (State# (PrimState m)
-> (# State# (PrimState m), SmallArray a #))
-> m (SmallArray a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s -> case SmallMutableArray# (PrimState m) a
-> State# (PrimState m)
-> (# State# (PrimState m), SmallArray# a #)
forall d a.
SmallMutableArray# d a -> State# d -> (# State# d, SmallArray# a #)
unsafeFreezeSmallArray# SmallMutableArray# (PrimState m) a
sma# State# (PrimState m)
s of
(# State# (PrimState m)
s', SmallArray# a
sa# #) -> (# State# (PrimState m)
s', SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray SmallArray# a
sa# #)
{-# INLINE unsafeFreezeSmallArray #-}
thawSmallArray
:: PrimMonad m
=> SmallArray a
-> Int
-> Int
-> m (SmallMutableArray (PrimState m) a)
thawSmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallArray a -> Int -> Int -> m (SmallMutableArray (PrimState m) a)
thawSmallArray (SmallArray SmallArray# a
sa#) (I# Int#
o#) (I# Int#
l#) =
(State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a))
-> (State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s -> case SmallArray# a
-> Int#
-> Int#
-> State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray# (PrimState m) a #)
forall a d.
SmallArray# a
-> Int#
-> Int#
-> State# d
-> (# State# d, SmallMutableArray# d a #)
thawSmallArray# SmallArray# a
sa# Int#
o# Int#
l# State# (PrimState m)
s of
(# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
sma# #) -> (# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
-> SmallMutableArray (PrimState m) a
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# (PrimState m) a
sma# #)
{-# INLINE thawSmallArray #-}
unsafeThawSmallArray
:: PrimMonad m => SmallArray a -> m (SmallMutableArray (PrimState m) a)
unsafeThawSmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallArray a -> m (SmallMutableArray (PrimState m) a)
unsafeThawSmallArray (SmallArray SmallArray# a
sa#) =
(State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a))
-> (State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s -> case SmallArray# a
-> State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray# (PrimState m) a #)
forall a d.
SmallArray# a -> State# d -> (# State# d, SmallMutableArray# d a #)
unsafeThawSmallArray# SmallArray# a
sa# State# (PrimState m)
s of
(# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
sma# #) -> (# State# (PrimState m)
s', SmallMutableArray# (PrimState m) a
-> SmallMutableArray (PrimState m) a
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# (PrimState m) a
sma# #)
{-# INLINE unsafeThawSmallArray #-}
copySmallArray
:: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> SmallArray a
-> Int
-> Int
-> m ()
copySmallArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray
(SmallMutableArray SmallMutableArray# (PrimState m) a
dst#) (I# Int#
do#) (SmallArray SmallArray# a
src#) (I# Int#
so#) (I# Int#
l#) =
(State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ ((State# (PrimState m) -> State# (PrimState m)) -> m ())
-> (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall a b. (a -> b) -> a -> b
$ SmallArray# a
-> Int#
-> SmallMutableArray# (PrimState m) a
-> Int#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall a d.
SmallArray# a
-> Int#
-> SmallMutableArray# d a
-> Int#
-> Int#
-> State# d
-> State# d
copySmallArray# SmallArray# a
src# Int#
so# SmallMutableArray# (PrimState m) a
dst# Int#
do# Int#
l#
{-# INLINE copySmallArray #-}
copySmallMutableArray
:: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> SmallMutableArray (PrimState m) a
-> Int
-> Int
-> m ()
copySmallMutableArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallMutableArray (PrimState m) a -> Int -> Int -> m ()
copySmallMutableArray
(SmallMutableArray SmallMutableArray# (PrimState m) a
dst#) (I# Int#
do#)
(SmallMutableArray SmallMutableArray# (PrimState m) a
src#) (I# Int#
so#)
(I# Int#
l#) =
(State# (PrimState m) -> State# (PrimState m)) -> m ()
forall (m :: * -> *).
PrimMonad m =>
(State# (PrimState m) -> State# (PrimState m)) -> m ()
primitive_ ((State# (PrimState m) -> State# (PrimState m)) -> m ())
-> (State# (PrimState m) -> State# (PrimState m)) -> m ()
forall a b. (a -> b) -> a -> b
$ SmallMutableArray# (PrimState m) a
-> Int#
-> SmallMutableArray# (PrimState m) a
-> Int#
-> Int#
-> State# (PrimState m)
-> State# (PrimState m)
forall d a.
SmallMutableArray# d a
-> Int#
-> SmallMutableArray# d a
-> Int#
-> Int#
-> State# d
-> State# d
copySmallMutableArray# SmallMutableArray# (PrimState m) a
src# Int#
so# SmallMutableArray# (PrimState m) a
dst# Int#
do# Int#
l#
{-# INLINE copySmallMutableArray #-}
sizeofSmallArray :: SmallArray a -> Int
sizeofSmallArray :: forall a. SmallArray a -> Int
sizeofSmallArray (SmallArray SmallArray# a
sa#) = Int# -> Int
I# (SmallArray# a -> Int#
forall a. SmallArray# a -> Int#
sizeofSmallArray# SmallArray# a
sa#)
{-# INLINE sizeofSmallArray #-}
getSizeofSmallMutableArray
:: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> m Int
#if MIN_VERSION_base(4,14,0)
getSizeofSmallMutableArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m Int
getSizeofSmallMutableArray (SmallMutableArray SmallMutableArray# (PrimState m) a
sa#) = (State# (PrimState m) -> (# State# (PrimState m), Int #)) -> m Int
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive ((State# (PrimState m) -> (# State# (PrimState m), Int #))
-> m Int)
-> (State# (PrimState m) -> (# State# (PrimState m), Int #))
-> m Int
forall a b. (a -> b) -> a -> b
$ \State# (PrimState m)
s ->
case SmallMutableArray# (PrimState m) a
-> State# (PrimState m) -> (# State# (PrimState m), Int# #)
forall d a.
SmallMutableArray# d a -> State# d -> (# State# d, Int# #)
getSizeofSmallMutableArray# SmallMutableArray# (PrimState m) a
sa# State# (PrimState m)
s of
(# State# (PrimState m)
s', Int#
sz# #) -> (# State# (PrimState m)
s', Int# -> Int
I# Int#
sz# #)
#else
getSizeofSmallMutableArray sa = pure $! sizeofSmallMutableArray sa
#endif
{-# INLINE getSizeofSmallMutableArray #-}
sizeofSmallMutableArray :: SmallMutableArray s a -> Int
sizeofSmallMutableArray :: forall s a. SmallMutableArray s a -> Int
sizeofSmallMutableArray (SmallMutableArray SmallMutableArray# s a
sa#) =
Int# -> Int
I# (SmallMutableArray# s a -> Int#
forall d a. SmallMutableArray# d a -> Int#
sizeofSmallMutableArray# SmallMutableArray# s a
sa#)
{-# DEPRECATED sizeofSmallMutableArray "use getSizeofSmallMutableArray instead" #-}
{-# INLINE sizeofSmallMutableArray #-}
traverseSmallArrayP
:: PrimMonad m
=> (a -> m b)
-> SmallArray a
-> m (SmallArray b)
traverseSmallArrayP :: forall (m :: * -> *) a b.
PrimMonad m =>
(a -> m b) -> SmallArray a -> m (SmallArray b)
traverseSmallArrayP a -> m b
f = \ !SmallArray a
ary ->
let
!sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary
go :: Int -> SmallMutableArray (PrimState m) b -> m (SmallArray b)
go !Int
i !SmallMutableArray (PrimState m) b
mary
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz
= SmallMutableArray (PrimState m) b -> m (SmallArray b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray SmallMutableArray (PrimState m) b
mary
| Bool
otherwise
= do
a
a <- SmallArray a -> Int -> m a
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray a
ary Int
i
b
b <- a -> m b
f a
a
SmallMutableArray (PrimState m) b -> Int -> b -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray (PrimState m) b
mary Int
i b
b
Int -> SmallMutableArray (PrimState m) b -> m (SmallArray b)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) SmallMutableArray (PrimState m) b
mary
in do
SmallMutableArray (PrimState m) b
mary <- Int -> b -> m (SmallMutableArray (PrimState m) b)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
sz b
forall a. a
badTraverseValue
Int -> SmallMutableArray (PrimState m) b -> m (SmallArray b)
go Int
0 SmallMutableArray (PrimState m) b
mary
{-# INLINE traverseSmallArrayP #-}
mapSmallArray' :: (a -> b) -> SmallArray a -> SmallArray b
mapSmallArray' :: forall a b. (a -> b) -> SmallArray a -> SmallArray b
mapSmallArray' a -> b
f SmallArray a
sa = Int
-> b
-> (forall {s}. SmallMutableArray s b -> ST s ())
-> SmallArray b
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa) (String -> String -> b
forall a. String -> String -> a
die String
"mapSmallArray'" String
"impossible") ((forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b)
-> (forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s b
smb ->
((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ()
forall a. (a -> a) -> a
fix (((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ())
-> Int -> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b c. (a -> b -> c) -> b -> a -> c
? Int
0 (((Int -> ST s ()) -> Int -> ST s ()) -> ST s ())
-> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int -> ST s ()
go Int
i ->
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- SmallArray a -> Int -> ST s a
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray a
sa Int
i
let !y :: b
y = a -> b
f a
x
SmallMutableArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s b
SmallMutableArray (PrimState (ST s)) b
smb Int
i b
y ST s () -> ST s () -> ST s ()
forall a b. ST s a -> ST s b -> ST s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
{-# INLINE mapSmallArray' #-}
runSmallArray
:: (forall s. ST s (SmallMutableArray s a))
-> SmallArray a
runSmallArray :: forall a. (forall s. ST s (SmallMutableArray s a)) -> SmallArray a
runSmallArray forall s. ST s (SmallMutableArray s a)
m = SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray ((forall s. ST s (SmallMutableArray s a)) -> SmallArray# a
forall a. (forall s. ST s (SmallMutableArray s a)) -> SmallArray# a
runSmallArray# ST s (SmallMutableArray s a)
forall s. ST s (SmallMutableArray s a)
m)
runSmallArray#
:: (forall s. ST s (SmallMutableArray s a))
-> SmallArray# a
runSmallArray# :: forall a. (forall s. ST s (SmallMutableArray s a)) -> SmallArray# a
runSmallArray# forall s. ST s (SmallMutableArray s a)
m = case (State# RealWorld -> (# State# RealWorld, SmallArray# a #))
-> (# State# RealWorld, SmallArray# a #)
forall o. (State# RealWorld -> o) -> o
runRW# ((State# RealWorld -> (# State# RealWorld, SmallArray# a #))
-> (# State# RealWorld, SmallArray# a #))
-> (State# RealWorld -> (# State# RealWorld, SmallArray# a #))
-> (# State# RealWorld, SmallArray# a #)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case ST RealWorld (SmallMutableArray RealWorld a)
-> State# RealWorld
-> (# State# RealWorld, SmallMutableArray RealWorld a #)
forall s a. ST s a -> State# s -> (# State# s, a #)
unST ST RealWorld (SmallMutableArray RealWorld a)
forall s. ST s (SmallMutableArray s a)
m State# RealWorld
s of { (# State# RealWorld
s', SmallMutableArray SmallMutableArray# RealWorld a
mary# #) ->
SmallMutableArray# RealWorld a
-> State# RealWorld -> (# State# RealWorld, SmallArray# a #)
forall d a.
SmallMutableArray# d a -> State# d -> (# State# d, SmallArray# a #)
unsafeFreezeSmallArray# SmallMutableArray# RealWorld a
mary# State# RealWorld
s'} of (# State# RealWorld
_, SmallArray# a
ary# #) -> SmallArray# a
ary#
unST :: ST s a -> State# s -> (# State# s, a #)
unST :: forall s a. ST s a -> State# s -> (# State# s, a #)
unST (GHCST.ST STRep s a
f) = STRep s a
f
createSmallArray
:: Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray :: forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray Int
0 a
_ forall s. SmallMutableArray s a -> ST s ()
_ = SmallArray# a -> SmallArray a
forall a. SmallArray# a -> SmallArray a
SmallArray ((# #) -> SmallArray# a
forall a. (# #) -> SmallArray# a
emptySmallArray# (# #))
createSmallArray Int
n a
x forall s. SmallMutableArray s a -> ST s ()
f = (forall s. ST s (SmallMutableArray s a)) -> SmallArray a
forall a. (forall s. ST s (SmallMutableArray s a)) -> SmallArray a
runSmallArray ((forall s. ST s (SmallMutableArray s a)) -> SmallArray a)
-> (forall s. ST s (SmallMutableArray s a)) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ do
SmallMutableArray s a
mary <- Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
n a
x
SmallMutableArray s a -> ST s ()
forall s. SmallMutableArray s a -> ST s ()
f SmallMutableArray s a
mary
SmallMutableArray s a -> ST s (SmallMutableArray s a)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SmallMutableArray s a
mary
emptySmallArray# :: (# #) -> SmallArray# a
emptySmallArray# :: forall a. (# #) -> SmallArray# a
emptySmallArray# (# #)
_ = case SmallArray a
forall a. SmallArray a
emptySmallArray of SmallArray SmallArray# a
ar -> SmallArray# a
ar
{-# NOINLINE emptySmallArray# #-}
die :: String -> String -> a
die :: forall a. String -> String -> a
die String
fun String
problem = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Data.Primitive.SmallArray." String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fun String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
": " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
problem
emptySmallArray :: SmallArray a
emptySmallArray :: forall a. SmallArray a
emptySmallArray =
(forall s. ST s (SmallArray a)) -> SmallArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SmallArray a)) -> SmallArray a)
-> (forall s. ST s (SmallArray a)) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
0 (String -> String -> a
forall a. String -> String -> a
die String
"emptySmallArray" String
"impossible")
ST s (SmallMutableArray s a)
-> (SmallMutableArray s a -> ST s (SmallArray a))
-> ST s (SmallArray a)
forall a b. ST s a -> (a -> ST s b) -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SmallMutableArray s a -> ST s (SmallArray a)
SmallMutableArray (PrimState (ST s)) a -> ST s (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray
{-# NOINLINE emptySmallArray #-}
infixl 1 ?
(?) :: (a -> b -> c) -> (b -> a -> c)
? :: forall a b c. (a -> b -> c) -> b -> a -> c
(?) = (a -> b -> c) -> b -> a -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip
{-# INLINE (?) #-}
noOp :: a -> ST s ()
noOp :: forall a s. a -> ST s ()
noOp = ST s () -> a -> ST s ()
forall a b. a -> b -> a
const (ST s () -> a -> ST s ()) -> ST s () -> a -> ST s ()
forall a b. (a -> b) -> a -> b
$ () -> ST s ()
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
smallArrayLiftEq :: (a -> b -> Bool) -> SmallArray a -> SmallArray b -> Bool
smallArrayLiftEq :: forall a b.
(a -> b -> Bool) -> SmallArray a -> SmallArray b -> Bool
smallArrayLiftEq a -> b -> Bool
p SmallArray a
sa1 SmallArray b
sa2 = SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== SmallArray b -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray b
sa2 Bool -> Bool -> Bool
&& Int -> Bool
loop (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
where
loop :: Int -> Bool
loop Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
= Bool
True
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
sa1 Int
i
, (# b
y #) <- SmallArray b -> Int -> (# b #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray b
sa2 Int
i
= a -> b -> Bool
p a
x b
y Bool -> Bool -> Bool
&& Int -> Bool
loop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
instance Eq1 SmallArray where
liftEq :: forall a b.
(a -> b -> Bool) -> SmallArray a -> SmallArray b -> Bool
liftEq = (a -> b -> Bool) -> SmallArray a -> SmallArray b -> Bool
forall a b.
(a -> b -> Bool) -> SmallArray a -> SmallArray b -> Bool
smallArrayLiftEq
instance Eq a => Eq (SmallArray a) where
SmallArray a
sa1 == :: SmallArray a -> SmallArray a -> Bool
== SmallArray a
sa2 = (a -> a -> Bool) -> SmallArray a -> SmallArray a -> Bool
forall a b.
(a -> b -> Bool) -> SmallArray a -> SmallArray b -> Bool
smallArrayLiftEq a -> a -> Bool
forall a. Eq a => a -> a -> Bool
(==) SmallArray a
sa1 SmallArray a
sa2
instance Eq (SmallMutableArray s a) where
SmallMutableArray SmallMutableArray# s a
sma1# == :: SmallMutableArray s a -> SmallMutableArray s a -> Bool
== SmallMutableArray SmallMutableArray# s a
sma2# =
Int# -> Bool
isTrue# (SmallMutableArray# s a -> SmallMutableArray# s a -> Int#
forall s a.
SmallMutableArray# s a -> SmallMutableArray# s a -> Int#
sameSmallMutableArray# SmallMutableArray# s a
sma1# SmallMutableArray# s a
sma2#)
smallArrayLiftCompare :: (a -> b -> Ordering) -> SmallArray a -> SmallArray b -> Ordering
smallArrayLiftCompare :: forall a b.
(a -> b -> Ordering) -> SmallArray a -> SmallArray b -> Ordering
smallArrayLiftCompare a -> b -> Ordering
elemCompare SmallArray a
a1 SmallArray b
a2 = Int -> Ordering
loop Int
0
where
mn :: Int
mn = SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
a1 Int -> Int -> Int
forall a. Ord a => a -> a -> a
`min` SmallArray b -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray b
a2
loop :: Int -> Ordering
loop Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
mn
, (# a
x1 #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
a1 Int
i
, (# b
x2 #) <- SmallArray b -> Int -> (# b #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray b
a2 Int
i
= a -> b -> Ordering
elemCompare a
x1 b
x2 Ordering -> Ordering -> Ordering
forall a. Monoid a => a -> a -> a
`mappend` Int -> Ordering
loop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
a1) (SmallArray b -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray b
a2)
instance Ord1 SmallArray where
liftCompare :: forall a b.
(a -> b -> Ordering) -> SmallArray a -> SmallArray b -> Ordering
liftCompare = (a -> b -> Ordering) -> SmallArray a -> SmallArray b -> Ordering
forall a b.
(a -> b -> Ordering) -> SmallArray a -> SmallArray b -> Ordering
smallArrayLiftCompare
instance Ord a => Ord (SmallArray a) where
compare :: SmallArray a -> SmallArray a -> Ordering
compare SmallArray a
sa1 SmallArray a
sa2 = (a -> a -> Ordering) -> SmallArray a -> SmallArray a -> Ordering
forall a b.
(a -> b -> Ordering) -> SmallArray a -> SmallArray b -> Ordering
smallArrayLiftCompare a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare SmallArray a
sa1 SmallArray a
sa2
instance Foldable SmallArray where
foldr :: forall a b. (a -> b -> b) -> b -> SmallArray a -> b
foldr a -> b -> b
f = \b
z !SmallArray a
ary ->
let
!sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary
go :: Int -> b
go Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz = b
z
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i
= a -> b -> b
f a
x (Int -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in Int -> b
go Int
0
{-# INLINE foldr #-}
foldl :: forall b a. (b -> a -> b) -> b -> SmallArray a -> b
foldl b -> a -> b
f = \b
z !SmallArray a
ary ->
let
go :: Int -> b
go Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = b
z
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i
= b -> a -> b
f (Int -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) a
x
in Int -> b
go (SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
{-# INLINE foldl #-}
foldr1 :: forall a. (a -> a -> a) -> SmallArray a -> a
foldr1 a -> a -> a
f = \ !SmallArray a
ary ->
let
!sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
go :: Int -> a
go Int
i =
case SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i of
(# a
x #) | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz -> a
x
| Bool
otherwise -> a -> a -> a
f a
x (Int -> a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then String -> String -> a
forall a. String -> String -> a
die String
"foldr1" String
"Empty SmallArray"
else Int -> a
go Int
0
{-# INLINE foldr1 #-}
foldl1 :: forall a. (a -> a -> a) -> SmallArray a -> a
foldl1 a -> a -> a
f = \ !SmallArray a
ary ->
let
!sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
go :: Int -> a
go Int
i =
case SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i of
(# a
x #) | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 -> a
x
| Bool
otherwise -> a -> a -> a
f (Int -> a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) a
x
in if Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0
then String -> String -> a
forall a. String -> String -> a
die String
"foldl1" String
"Empty SmallArray"
else Int -> a
go Int
sz
{-# INLINE foldl1 #-}
foldr' :: forall a b. (a -> b -> b) -> b -> SmallArray a -> b
foldr' a -> b -> b
f = \b
z !SmallArray a
ary ->
let
go :: Int -> b -> b
go Int
i !b
acc
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== -Int
1 = b
acc
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i
= Int -> b -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (a -> b -> b
f a
x b
acc)
in Int -> b -> b
go (SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) b
z
{-# INLINE foldr' #-}
foldl' :: forall b a. (b -> a -> b) -> b -> SmallArray a -> b
foldl' b -> a -> b
f = \b
z !SmallArray a
ary ->
let
!sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary
go :: Int -> b -> b
go Int
i !b
acc
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz = b
acc
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i
= Int -> b -> b
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (b -> a -> b
f b
acc a
x)
in Int -> b -> b
go Int
0 b
z
{-# INLINE foldl' #-}
null :: forall a. SmallArray a -> Bool
null SmallArray a
a = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
{-# INLINE null #-}
length :: forall a. SmallArray a -> Int
length = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray
{-# INLINE length #-}
maximum :: forall a. Ord a => SmallArray a -> a
maximum SmallArray a
ary | Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = String -> String -> a
forall a. String -> String -> a
die String
"maximum" String
"Empty SmallArray"
| (# a
frst #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
0
= Int -> a -> a
go Int
1 a
frst
where
sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary
go :: Int -> a -> a
go Int
i !a
e
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz = a
e
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i
= Int -> a -> a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a -> a -> a
forall a. Ord a => a -> a -> a
max a
e a
x)
{-# INLINE maximum #-}
minimum :: forall a. Ord a => SmallArray a -> a
minimum SmallArray a
ary | Int
sz Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = String -> String -> a
forall a. String -> String -> a
die String
"minimum" String
"Empty SmallArray"
| (# a
frst #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
0
= Int -> a -> a
go Int
1 a
frst
where sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary
go :: Int -> a -> a
go Int
i !a
e
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
sz = a
e
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i
= Int -> a -> a
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (a -> a -> a
forall a. Ord a => a -> a -> a
min a
e a
x)
{-# INLINE minimum #-}
sum :: forall a. Num a => SmallArray a -> a
sum = (a -> a -> a) -> a -> SmallArray a -> a
forall b a. (b -> a -> b) -> b -> SmallArray a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
{-# INLINE sum #-}
product :: forall a. Num a => SmallArray a -> a
product = (a -> a -> a) -> a -> SmallArray a -> a
forall b a. (b -> a -> b) -> b -> SmallArray a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
{-# INLINE product #-}
newtype STA a = STA { forall a.
STA a -> forall s. SmallMutableArray# s a -> ST s (SmallArray a)
_runSTA :: forall s. SmallMutableArray# s a -> ST s (SmallArray a) }
runSTA :: Int -> STA a -> SmallArray a
runSTA :: forall a. Int -> STA a -> SmallArray a
runSTA !Int
sz = \ (STA forall s. SmallMutableArray# s a -> ST s (SmallArray a)
m) -> (forall s. ST s (SmallArray a)) -> SmallArray a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SmallArray a)) -> SmallArray a)
-> (forall s. ST s (SmallArray a)) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ Int -> ST s (SmallMutableArray s a)
forall s a. Int -> ST s (SmallMutableArray s a)
newSmallArray_ Int
sz ST s (SmallMutableArray s a)
-> (SmallMutableArray s a -> ST s (SmallArray a))
-> ST s (SmallArray a)
forall a b. ST s a -> (a -> ST s b) -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
\ (SmallMutableArray SmallMutableArray# s a
ar#) -> SmallMutableArray# s a -> ST s (SmallArray a)
forall s. SmallMutableArray# s a -> ST s (SmallArray a)
m SmallMutableArray# s a
ar#
{-# INLINE runSTA #-}
newSmallArray_ :: Int -> ST s (SmallMutableArray s a)
newSmallArray_ :: forall s a. Int -> ST s (SmallMutableArray s a)
newSmallArray_ !Int
n = Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
n a
forall a. a
badTraverseValue
badTraverseValue :: a
badTraverseValue :: forall a. a
badTraverseValue = String -> String -> a
forall a. String -> String -> a
die String
"traverse" String
"bad indexing"
{-# NOINLINE badTraverseValue #-}
instance Traversable SmallArray where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SmallArray a -> f (SmallArray b)
traverse a -> f b
f = (a -> f b) -> SmallArray a -> f (SmallArray b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SmallArray a -> f (SmallArray b)
traverseSmallArray a -> f b
f
{-# INLINE traverse #-}
traverseSmallArray
:: Applicative f
=> (a -> f b) -> SmallArray a -> f (SmallArray b)
traverseSmallArray :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SmallArray a -> f (SmallArray b)
traverseSmallArray a -> f b
f = \ !SmallArray a
ary ->
let
!len :: Int
len = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
ary
go :: Int -> f (STA b)
go !Int
i
| Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
len
= STA b -> f (STA b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (STA b -> f (STA b)) -> STA b -> f (STA b)
forall a b. (a -> b) -> a -> b
$ (forall s. SmallMutableArray# s b -> ST s (SmallArray b)) -> STA b
forall a.
(forall s. SmallMutableArray# s a -> ST s (SmallArray a)) -> STA a
STA ((forall s. SmallMutableArray# s b -> ST s (SmallArray b))
-> STA b)
-> (forall s. SmallMutableArray# s b -> ST s (SmallArray b))
-> STA b
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray# s b
mary -> SmallMutableArray (PrimState (ST s)) b -> ST s (SmallArray b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray (SmallMutableArray# s b -> SmallMutableArray s b
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# s b
mary)
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
ary Int
i
= (b -> STA b -> STA b) -> f b -> f (STA b) -> f (STA b)
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (\b
b (STA forall s. SmallMutableArray# s b -> ST s (SmallArray b)
m) -> (forall s. SmallMutableArray# s b -> ST s (SmallArray b)) -> STA b
forall a.
(forall s. SmallMutableArray# s a -> ST s (SmallArray a)) -> STA a
STA ((forall s. SmallMutableArray# s b -> ST s (SmallArray b))
-> STA b)
-> (forall s. SmallMutableArray# s b -> ST s (SmallArray b))
-> STA b
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray# s b
mary ->
SmallMutableArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray (SmallMutableArray# s b -> SmallMutableArray s b
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# s b
mary) Int
i b
b ST s () -> ST s (SmallArray b) -> ST s (SmallArray b)
forall a b. ST s a -> ST s b -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> SmallMutableArray# s b -> ST s (SmallArray b)
forall s. SmallMutableArray# s b -> ST s (SmallArray b)
m SmallMutableArray# s b
mary)
(a -> f b
f a
x) (Int -> f (STA b)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
in if Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then SmallArray b -> f (SmallArray b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SmallArray b
forall a. SmallArray a
emptySmallArray
else Int -> STA b -> SmallArray b
forall a. Int -> STA a -> SmallArray a
runSTA Int
len (STA b -> SmallArray b) -> f (STA b) -> f (SmallArray b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> f (STA b)
go Int
0
{-# INLINE [1] traverseSmallArray #-}
{-# RULES
"traverse/ST" forall (f :: a -> ST s b). traverseSmallArray f = traverseSmallArrayP f
"traverse/IO" forall (f :: a -> IO b). traverseSmallArray f = traverseSmallArrayP f
"traverse/Id" forall (f :: a -> Identity b). traverseSmallArray f =
(coerce :: (SmallArray a -> SmallArray (Identity b))
-> SmallArray a -> Identity (SmallArray b)) (fmap f)
#-}
instance Functor SmallArray where
fmap :: forall a b. (a -> b) -> SmallArray a -> SmallArray b
fmap a -> b
f SmallArray a
sa = Int
-> b
-> (forall {s}. SmallMutableArray s b -> ST s ())
-> SmallArray b
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa) (String -> String -> b
forall a. String -> String -> a
die String
"fmap" String
"impossible") ((forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b)
-> (forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s b
smb ->
((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ()
forall a. (a -> a) -> a
fix (((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ())
-> Int -> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b c. (a -> b -> c) -> b -> a -> c
? Int
0 (((Int -> ST s ()) -> Int -> ST s ()) -> ST s ())
-> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int -> ST s ()
go Int
i ->
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- SmallArray a -> Int -> ST s a
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray a
sa Int
i
SmallMutableArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s b
SmallMutableArray (PrimState (ST s)) b
smb Int
i (a -> b
f a
x) ST s () -> ST s () -> ST s ()
forall a b. ST s a -> ST s b -> ST s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
{-# INLINE fmap #-}
a
x <$ :: forall a b. a -> SmallArray b -> SmallArray a
<$ SmallArray b
sa = Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (SmallArray b -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray b
sa) a
x SmallMutableArray s a -> ST s ()
forall s. SmallMutableArray s a -> ST s ()
forall a s. a -> ST s ()
noOp
instance Applicative SmallArray where
pure :: forall a. a -> SmallArray a
pure a
x = Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray Int
1 a
x SmallMutableArray s a -> ST s ()
forall s. SmallMutableArray s a -> ST s ()
forall a s. a -> ST s ()
noOp
SmallArray a
sa *> :: forall a b. SmallArray a -> SmallArray b -> SmallArray b
*> SmallArray b
sb = Int
-> b
-> (forall {s}. SmallMutableArray s b -> ST s ())
-> SmallArray b
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (Int
la Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
lb) (String -> String -> b
forall a. String -> String -> a
die String
"*>" String
"impossible") ((forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b)
-> (forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s b
smb ->
((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ()
forall a. (a -> a) -> a
fix (((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ())
-> Int -> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b c. (a -> b -> c) -> b -> a -> c
? Int
0 (((Int -> ST s ()) -> Int -> ST s ()) -> ST s ())
-> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int -> ST s ()
go Int
i ->
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
la) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
SmallMutableArray (PrimState (ST s)) b
-> Int -> SmallArray b -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray SmallMutableArray s b
SmallMutableArray (PrimState (ST s)) b
smb (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
lb) SmallArray b
sb Int
0 Int
lb ST s () -> ST s () -> ST s ()
forall a b. ST s a -> ST s b -> ST s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
where
la :: Int
la = SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa; lb :: Int
lb = SmallArray b -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray b
sb
SmallArray a
a <* :: forall a b. SmallArray a -> SmallArray b -> SmallArray a
<* SmallArray b
b = Int
-> a
-> (forall {s}. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (Int
sza Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
szb) (String -> String -> a
forall a. String -> String -> a
die String
"<*" String
"impossible") ((forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a)
-> (forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s a
ma ->
let fill :: Int -> Int -> a -> ST s ()
fill Int
off Int
i a
e = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
szb) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
ma (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
i) a
e ST s () -> ST s () -> ST s ()
forall a b. ST s a -> ST s b -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Int -> a -> ST s ()
fill Int
off (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
e
go :: Int -> ST s ()
go Int
i = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sza) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- SmallArray a -> Int -> ST s a
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray a
a Int
i
Int -> Int -> a -> ST s ()
fill (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
szb) Int
0 a
x
Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
in Int -> ST s ()
go Int
0
where sza :: Int
sza = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
a; szb :: Int
szb = SmallArray b -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray b
b
SmallArray (a -> b)
ab <*> :: forall a b. SmallArray (a -> b) -> SmallArray a -> SmallArray b
<*> SmallArray a
a = Int
-> b
-> (forall {s}. SmallMutableArray s b -> ST s ())
-> SmallArray b
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (Int
szab Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sza) (String -> String -> b
forall a. String -> String -> a
die String
"<*>" String
"impossible") ((forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b)
-> (forall {s}. SmallMutableArray s b -> ST s ()) -> SmallArray b
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s b
mb ->
let go1 :: Int -> ST s ()
go1 Int
i = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
szab) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
do
a -> b
f <- SmallArray (a -> b) -> Int -> ST s (a -> b)
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray (a -> b)
ab Int
i
Int -> (a -> b) -> Int -> ST s ()
go2 (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sza) a -> b
f Int
0
Int -> ST s ()
go1 (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
go2 :: Int -> (a -> b) -> Int -> ST s ()
go2 Int
off a -> b
f Int
j = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sza) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
do
a
x <- SmallArray a -> Int -> ST s a
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray a
a Int
j
SmallMutableArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s b
SmallMutableArray (PrimState (ST s)) b
mb (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
j) (a -> b
f a
x)
Int -> (a -> b) -> Int -> ST s ()
go2 Int
off a -> b
f (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
in Int -> ST s ()
go1 Int
0
where szab :: Int
szab = SmallArray (a -> b) -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray (a -> b)
ab; sza :: Int
sza = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
a
instance Alternative SmallArray where
empty :: forall a. SmallArray a
empty = SmallArray a
forall a. SmallArray a
emptySmallArray
SmallArray a
sl <|> :: forall a. SmallArray a -> SmallArray a -> SmallArray a
<|> SmallArray a
sr =
Int
-> a
-> (forall {s}. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sl Int -> Int -> Int
forall a. Num a => a -> a -> a
+ SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sr) (String -> String -> a
forall a. String -> String -> a
die String
"<|>" String
"impossible") ((forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a)
-> (forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s a
sma ->
SmallMutableArray (PrimState (ST s)) a
-> Int -> SmallArray a -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
sma Int
0 SmallArray a
sl Int
0 (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sl)
ST s () -> ST s () -> ST s ()
forall a b. ST s a -> ST s b -> ST s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> SmallMutableArray (PrimState (ST s)) a
-> Int -> SmallArray a -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
sma (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sl) SmallArray a
sr Int
0 (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sr)
many :: forall a. SmallArray a -> SmallArray [a]
many SmallArray a
sa | SmallArray a -> Bool
forall a. SmallArray a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null SmallArray a
sa = [a] -> SmallArray [a]
forall a. a -> SmallArray a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
| Bool
otherwise = String -> String -> SmallArray [a]
forall a. String -> String -> a
die String
"many" String
"infinite arrays are not well defined"
some :: forall a. SmallArray a -> SmallArray [a]
some SmallArray a
sa | SmallArray a -> Bool
forall a. SmallArray a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null SmallArray a
sa = SmallArray [a]
forall a. SmallArray a
emptySmallArray
| Bool
otherwise = String -> String -> SmallArray [a]
forall a. String -> String -> a
die String
"some" String
"infinite arrays are not well defined"
data ArrayStack a
= PushArray !(SmallArray a) !(ArrayStack a)
| EmptyStack
instance Monad SmallArray where
return :: forall a. a -> SmallArray a
return = a -> SmallArray a
forall a. a -> SmallArray a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
>> :: forall a b. SmallArray a -> SmallArray b -> SmallArray b
(>>) = SmallArray a -> SmallArray b -> SmallArray b
forall a b. SmallArray a -> SmallArray b -> SmallArray b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
SmallArray a
sa >>= :: forall a b. SmallArray a -> (a -> SmallArray b) -> SmallArray b
>>= a -> SmallArray b
f = Int -> ArrayStack b -> Int -> SmallArray b
collect Int
0 ArrayStack b
forall a. ArrayStack a
EmptyStack (Int
la Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
where
la :: Int
la = SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa
collect :: Int -> ArrayStack b -> Int -> SmallArray b
collect Int
sz ArrayStack b
stk Int
i
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Int
-> b
-> (forall s. SmallMutableArray s b -> ST s ())
-> SmallArray b
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray Int
sz (String -> String -> b
forall a. String -> String -> a
die String
">>=" String
"impossible") ((forall s. SmallMutableArray s b -> ST s ()) -> SmallArray b)
-> (forall s. SmallMutableArray s b -> ST s ()) -> SmallArray b
forall a b. (a -> b) -> a -> b
$ Int
-> ArrayStack b
-> SmallMutableArray (PrimState (ST s)) b
-> ST s ()
forall {m :: * -> *} {a}.
PrimMonad m =>
Int -> ArrayStack a -> SmallMutableArray (PrimState m) a -> m ()
fill Int
0 ArrayStack b
stk
| (# a
x #) <- SmallArray a -> Int -> (# a #)
forall a. SmallArray a -> Int -> (# a #)
indexSmallArray## SmallArray a
sa Int
i
, let sb :: SmallArray b
sb = a -> SmallArray b
f a
x
lsb :: Int
lsb = SmallArray b -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray b
sb
= if Int
lsb Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Int -> ArrayStack b -> Int -> SmallArray b
collect Int
sz ArrayStack b
stk (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
else Int -> ArrayStack b -> Int -> SmallArray b
collect (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lsb) (SmallArray b -> ArrayStack b -> ArrayStack b
forall a. SmallArray a -> ArrayStack a -> ArrayStack a
PushArray SmallArray b
sb ArrayStack b
stk) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
fill :: Int -> ArrayStack a -> SmallMutableArray (PrimState m) a -> m ()
fill Int
_ ArrayStack a
EmptyStack SmallMutableArray (PrimState m) a
_ = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
fill Int
off (PushArray SmallArray a
sb ArrayStack a
sbs) SmallMutableArray (PrimState m) a
smb =
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray SmallMutableArray (PrimState m) a
smb Int
off SmallArray a
sb Int
0 (SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sb)
m () -> m () -> m ()
forall a b. m a -> m b -> m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> ArrayStack a -> SmallMutableArray (PrimState m) a -> m ()
fill (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sb) ArrayStack a
sbs SmallMutableArray (PrimState m) a
smb
#if !(MIN_VERSION_base(4,13,0))
fail = Fail.fail
#endif
instance Fail.MonadFail SmallArray where
fail :: forall a. String -> SmallArray a
fail String
_ = SmallArray a
forall a. SmallArray a
emptySmallArray
instance MonadPlus SmallArray where
mzero :: forall a. SmallArray a
mzero = SmallArray a
forall a. SmallArray a
forall (f :: * -> *) a. Alternative f => f a
empty
mplus :: forall a. SmallArray a -> SmallArray a -> SmallArray a
mplus = SmallArray a -> SmallArray a -> SmallArray a
forall a. SmallArray a -> SmallArray a -> SmallArray a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
zipW :: String -> (a -> b -> c) -> SmallArray a -> SmallArray b -> SmallArray c
zipW :: forall a b c.
String
-> (a -> b -> c) -> SmallArray a -> SmallArray b -> SmallArray c
zipW String
nm = \a -> b -> c
f SmallArray a
sa SmallArray b
sb -> let mn :: Int
mn = SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray a
sa Int -> Int -> Int
forall a. Ord a => a -> a -> a
`min` SmallArray b -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray b
sb in
Int
-> c
-> (forall {s}. SmallMutableArray s c -> ST s ())
-> SmallArray c
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray Int
mn (String -> String -> c
forall a. String -> String -> a
die String
nm String
"impossible") ((forall {s}. SmallMutableArray s c -> ST s ()) -> SmallArray c)
-> (forall {s}. SmallMutableArray s c -> ST s ()) -> SmallArray c
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s c
mc ->
((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ()
forall a. (a -> a) -> a
fix (((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ())
-> Int -> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b c. (a -> b -> c) -> b -> a -> c
? Int
0 (((Int -> ST s ()) -> Int -> ST s ()) -> ST s ())
-> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int -> ST s ()
go Int
i -> Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
mn) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
a
x <- SmallArray a -> Int -> ST s a
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray a
sa Int
i
b
y <- SmallArray b -> Int -> ST s b
forall (m :: * -> *) a. Applicative m => SmallArray a -> Int -> m a
indexSmallArrayM SmallArray b
sb Int
i
SmallMutableArray (PrimState (ST s)) c -> Int -> c -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s c
SmallMutableArray (PrimState (ST s)) c
mc Int
i (a -> b -> c
f a
x b
y)
Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
{-# INLINE zipW #-}
instance MonadZip SmallArray where
mzip :: forall a b. SmallArray a -> SmallArray b -> SmallArray (a, b)
mzip = String
-> (a -> b -> (a, b))
-> SmallArray a
-> SmallArray b
-> SmallArray (a, b)
forall a b c.
String
-> (a -> b -> c) -> SmallArray a -> SmallArray b -> SmallArray c
zipW String
"mzip" (,)
mzipWith :: forall a b c.
(a -> b -> c) -> SmallArray a -> SmallArray b -> SmallArray c
mzipWith = String
-> (a -> b -> c) -> SmallArray a -> SmallArray b -> SmallArray c
forall a b c.
String
-> (a -> b -> c) -> SmallArray a -> SmallArray b -> SmallArray c
zipW String
"mzipWith"
{-# INLINE mzipWith #-}
munzip :: forall a b. SmallArray (a, b) -> (SmallArray a, SmallArray b)
munzip SmallArray (a, b)
sab = (forall s. ST s (SmallArray a, SmallArray b))
-> (SmallArray a, SmallArray b)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (SmallArray a, SmallArray b))
-> (SmallArray a, SmallArray b))
-> (forall s. ST s (SmallArray a, SmallArray b))
-> (SmallArray a, SmallArray b)
forall a b. (a -> b) -> a -> b
$ do
let sz :: Int
sz = SmallArray (a, b) -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length SmallArray (a, b)
sab
SmallMutableArray s a
sma <- Int -> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
sz (a -> ST s (SmallMutableArray (PrimState (ST s)) a))
-> a -> ST s (SmallMutableArray (PrimState (ST s)) a)
forall a b. (a -> b) -> a -> b
$ String -> String -> a
forall a. String -> String -> a
die String
"munzip" String
"impossible"
SmallMutableArray s b
smb <- Int -> b -> ST s (SmallMutableArray (PrimState (ST s)) b)
forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (SmallMutableArray (PrimState m) a)
newSmallArray Int
sz (b -> ST s (SmallMutableArray (PrimState (ST s)) b))
-> b -> ST s (SmallMutableArray (PrimState (ST s)) b)
forall a b. (a -> b) -> a -> b
$ String -> String -> b
forall a. String -> String -> a
die String
"munzip" String
"impossible"
((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ()
forall a. (a -> a) -> a
fix (((Int -> ST s ()) -> Int -> ST s ()) -> Int -> ST s ())
-> Int -> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b c. (a -> b -> c) -> b -> a -> c
? Int
0 (((Int -> ST s ()) -> Int -> ST s ()) -> ST s ())
-> ((Int -> ST s ()) -> Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int -> ST s ()
go Int
i ->
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ case SmallArray (a, b) -> Int -> (a, b)
forall a. SmallArray a -> Int -> a
indexSmallArray SmallArray (a, b)
sab Int
i of
(a
x, b
y) -> do SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
sma Int
i a
x
SmallMutableArray (PrimState (ST s)) b -> Int -> b -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s b
SmallMutableArray (PrimState (ST s)) b
smb Int
i b
y
Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
(,) (SmallArray a -> SmallArray b -> (SmallArray a, SmallArray b))
-> ST s (SmallArray a)
-> ST s (SmallArray b -> (SmallArray a, SmallArray b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SmallMutableArray (PrimState (ST s)) a -> ST s (SmallArray a)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
sma
ST s (SmallArray b -> (SmallArray a, SmallArray b))
-> ST s (SmallArray b) -> ST s (SmallArray a, SmallArray b)
forall a b. ST s (a -> b) -> ST s a -> ST s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SmallMutableArray (PrimState (ST s)) b -> ST s (SmallArray b)
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> m (SmallArray a)
unsafeFreezeSmallArray SmallMutableArray s b
SmallMutableArray (PrimState (ST s)) b
smb
instance MonadFix SmallArray where
mfix :: forall a. (a -> SmallArray a) -> SmallArray a
mfix a -> SmallArray a
f = Int
-> a
-> (forall {s}. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray (a -> SmallArray a
f a
forall a. a
err))
(String -> String -> a
forall a. String -> String -> a
die String
"mfix" String
"impossible") ((forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a)
-> (forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ ((Int -> SmallMutableArray s a -> ST s ())
-> Int -> SmallMutableArray s a -> ST s ())
-> Int -> SmallMutableArray s a -> ST s ()
forall a. (a -> a) -> a
fix (((Int -> SmallMutableArray s a -> ST s ())
-> Int -> SmallMutableArray s a -> ST s ())
-> Int -> SmallMutableArray s a -> ST s ())
-> Int
-> ((Int -> SmallMutableArray s a -> ST s ())
-> Int -> SmallMutableArray s a -> ST s ())
-> SmallMutableArray s a
-> ST s ()
forall a b c. (a -> b -> c) -> b -> a -> c
? Int
0 (((Int -> SmallMutableArray s a -> ST s ())
-> Int -> SmallMutableArray s a -> ST s ())
-> SmallMutableArray s a -> ST s ())
-> ((Int -> SmallMutableArray s a -> ST s ())
-> Int -> SmallMutableArray s a -> ST s ())
-> SmallMutableArray s a
-> ST s ()
forall a b. (a -> b) -> a -> b
$
\Int -> SmallMutableArray s a -> ST s ()
r !Int
i !SmallMutableArray s a
mary -> Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sz) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
mary Int
i ((a -> a) -> a
forall a. (a -> a) -> a
fix (\a
xi -> a -> SmallArray a
f a
xi SmallArray a -> Int -> a
forall a. SmallArray a -> Int -> a
`indexSmallArray` Int
i))
Int -> SmallMutableArray s a -> ST s ()
r (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) SmallMutableArray s a
mary
where
sz :: Int
sz = SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray (a -> SmallArray a
f a
forall a. a
err)
err :: a
err = String -> a
forall a. HasCallStack => String -> a
error String
"mfix for Data.Primitive.SmallArray applied to strict function."
instance Semigroup (SmallArray a) where
<> :: SmallArray a -> SmallArray a -> SmallArray a
(<>) = SmallArray a -> SmallArray a -> SmallArray a
forall a. SmallArray a -> SmallArray a -> SmallArray a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
sconcat :: NonEmpty (SmallArray a) -> SmallArray a
sconcat = [SmallArray a] -> SmallArray a
forall a. Monoid a => [a] -> a
mconcat ([SmallArray a] -> SmallArray a)
-> (NonEmpty (SmallArray a) -> [SmallArray a])
-> NonEmpty (SmallArray a)
-> SmallArray a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (SmallArray a) -> [SmallArray a]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
stimes :: forall b. Integral b => b -> SmallArray a -> SmallArray a
stimes b
n SmallArray a
arr = case b -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
compare b
n b
0 of
Ordering
LT -> String -> String -> SmallArray a
forall a. String -> String -> a
die String
"stimes" String
"negative multiplier"
Ordering
EQ -> SmallArray a
forall a. SmallArray a
forall (f :: * -> *) a. Alternative f => f a
empty
Ordering
GT -> Int
-> a
-> (forall {s}. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray (Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
* SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
arr) (String -> String -> a
forall a. String -> String -> a
die String
"stimes" String
"impossible") ((forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a)
-> (forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s a
sma ->
let go :: Int -> ST s ()
go Int
i = Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n') (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
SmallMutableArray (PrimState (ST s)) a
-> Int -> SmallArray a -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
sma (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
arr) SmallArray a
arr Int
0 (SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
arr)
Int -> ST s ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
in Int -> ST s ()
go Int
0
where n' :: Int
n' = b -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
n :: Int
instance Monoid (SmallArray a) where
mempty :: SmallArray a
mempty = SmallArray a
forall a. SmallArray a
forall (f :: * -> *) a. Alternative f => f a
empty
#if !(MIN_VERSION_base(4,11,0))
mappend = (<>)
#endif
mconcat :: [SmallArray a] -> SmallArray a
mconcat [SmallArray a]
l = Int
-> a
-> (forall {s}. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray Int
n (String -> String -> a
forall a. String -> String -> a
die String
"mconcat" String
"impossible") ((forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a)
-> (forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s a
ma ->
let go :: Int -> [SmallArray a] -> ST s ()
go !Int
_ [ ] = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
go Int
off (SmallArray a
a:[SmallArray a]
as) =
SmallMutableArray (PrimState (ST s)) a
-> Int -> SmallArray a -> Int -> Int -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> SmallArray a -> Int -> Int -> m ()
copySmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
ma Int
off SmallArray a
a Int
0 (SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
a) ST s () -> ST s () -> ST s ()
forall a b. ST s a -> ST s b -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> [SmallArray a] -> ST s ()
go (Int
off Int -> Int -> Int
forall a. Num a => a -> a -> a
+ SmallArray a -> Int
forall a. SmallArray a -> Int
sizeofSmallArray SmallArray a
a) [SmallArray a]
as
in Int -> [SmallArray a] -> ST s ()
go Int
0 [SmallArray a]
l
where n :: Int
n = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((SmallArray a -> Int) -> [SmallArray a] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SmallArray a -> Int
forall a. SmallArray a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SmallArray a]
l)
instance IsList (SmallArray a) where
type Item (SmallArray a) = a
fromListN :: Int -> [Item (SmallArray a)] -> SmallArray a
fromListN = Int -> [a] -> SmallArray a
Int -> [Item (SmallArray a)] -> SmallArray a
forall a. Int -> [a] -> SmallArray a
smallArrayFromListN
fromList :: [Item (SmallArray a)] -> SmallArray a
fromList = [a] -> SmallArray a
[Item (SmallArray a)] -> SmallArray a
forall a. [a] -> SmallArray a
smallArrayFromList
toList :: SmallArray a -> [Item (SmallArray a)]
toList = SmallArray a -> [a]
SmallArray a -> [Item (SmallArray a)]
forall a. SmallArray a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
smallArrayLiftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> SmallArray a -> ShowS
smallArrayLiftShowsPrec :: forall a.
(Int -> a -> String -> String)
-> ([a] -> String -> String)
-> Int
-> SmallArray a
-> String
-> String
smallArrayLiftShowsPrec Int -> a -> String -> String
elemShowsPrec [a] -> String -> String
elemListShowsPrec Int
_ SmallArray a
sa =
(Int -> a -> String -> String)
-> ([a] -> String -> String) -> Int -> [a] -> String -> String
forall a.
(Int -> a -> String -> String)
-> ([a] -> String -> String) -> Int -> [a] -> String -> String
listLiftShowsPrec Int -> a -> String -> String
elemShowsPrec [a] -> String -> String
elemListShowsPrec Int
11 (SmallArray a -> [a]
forall a. SmallArray a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray a
sa)
listLiftShowsPrec :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> [a] -> ShowS
listLiftShowsPrec :: forall a.
(Int -> a -> String -> String)
-> ([a] -> String -> String) -> Int -> [a] -> String -> String
listLiftShowsPrec Int -> a -> String -> String
_ [a] -> String -> String
sl Int
_ = [a] -> String -> String
sl
instance Show a => Show (SmallArray a) where
showsPrec :: Int -> SmallArray a -> String -> String
showsPrec Int
p SmallArray a
sa = (Int -> a -> String -> String)
-> ([a] -> String -> String)
-> Int
-> SmallArray a
-> String
-> String
forall a.
(Int -> a -> String -> String)
-> ([a] -> String -> String)
-> Int
-> SmallArray a
-> String
-> String
smallArrayLiftShowsPrec Int -> a -> String -> String
forall a. Show a => Int -> a -> String -> String
showsPrec [a] -> String -> String
forall a. Show a => [a] -> String -> String
showList Int
p SmallArray a
sa
instance Show1 SmallArray where
liftShowsPrec :: forall a.
(Int -> a -> String -> String)
-> ([a] -> String -> String)
-> Int
-> SmallArray a
-> String
-> String
liftShowsPrec = (Int -> a -> String -> String)
-> ([a] -> String -> String)
-> Int
-> SmallArray a
-> String
-> String
forall a.
(Int -> a -> String -> String)
-> ([a] -> String -> String)
-> Int
-> SmallArray a
-> String
-> String
smallArrayLiftShowsPrec
smallArrayLiftReadPrec :: ReadPrec a -> ReadPrec [a] -> ReadPrec (SmallArray a)
smallArrayLiftReadPrec :: forall a. ReadPrec a -> ReadPrec [a] -> ReadPrec (SmallArray a)
smallArrayLiftReadPrec ReadPrec a
_ ReadPrec [a]
read_list =
( ReadP () -> ReadPrec ()
forall a. ReadP a -> ReadPrec a
RdPrc.lift ReadP ()
skipSpaces ReadPrec () -> ReadPrec (SmallArray a) -> ReadPrec (SmallArray a)
forall a b. ReadPrec a -> ReadPrec b -> ReadPrec b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ([a] -> SmallArray a) -> ReadPrec [a] -> ReadPrec (SmallArray a)
forall a b. (a -> b) -> ReadPrec a -> ReadPrec b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> SmallArray a
[Item (SmallArray a)] -> SmallArray a
forall l. IsList l => [Item l] -> l
fromList ReadPrec [a]
read_list )
ReadPrec (SmallArray a)
-> ReadPrec (SmallArray a) -> ReadPrec (SmallArray a)
forall a. ReadPrec a -> ReadPrec a -> ReadPrec a
RdPrc.+++
( ReadPrec (SmallArray a) -> ReadPrec (SmallArray a)
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec (SmallArray a) -> ReadPrec (SmallArray a))
-> ReadPrec (SmallArray a) -> ReadPrec (SmallArray a)
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec (SmallArray a) -> ReadPrec (SmallArray a)
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
app_prec (ReadPrec (SmallArray a) -> ReadPrec (SmallArray a))
-> ReadPrec (SmallArray a) -> ReadPrec (SmallArray a)
forall a b. (a -> b) -> a -> b
$ do
ReadP () -> ReadPrec ()
forall a. ReadP a -> ReadPrec a
RdPrc.lift ReadP ()
skipSpaces
Tag
tag <- ReadP Tag -> ReadPrec Tag
forall a. ReadP a -> ReadPrec a
RdPrc.lift ReadP Tag
lexTag
case Tag
tag of
Tag
FromListTag -> [a] -> SmallArray a
[Item (SmallArray a)] -> SmallArray a
forall l. IsList l => [Item l] -> l
fromList ([a] -> SmallArray a) -> ReadPrec [a] -> ReadPrec (SmallArray a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ReadPrec [a]
read_list
Tag
FromListNTag -> (Int -> [a] -> SmallArray a)
-> ReadPrec Int -> ReadPrec [a] -> ReadPrec (SmallArray a)
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 Int -> [a] -> SmallArray a
Int -> [Item (SmallArray a)] -> SmallArray a
forall l. IsList l => Int -> [Item l] -> l
fromListN ReadPrec Int
forall a. Read a => ReadPrec a
readPrec ReadPrec [a]
read_list
)
where
app_prec :: Int
app_prec = Int
10
instance Read a => Read (SmallArray a) where
readPrec :: ReadPrec (SmallArray a)
readPrec = ReadPrec a -> ReadPrec [a] -> ReadPrec (SmallArray a)
forall a. ReadPrec a -> ReadPrec [a] -> ReadPrec (SmallArray a)
smallArrayLiftReadPrec ReadPrec a
forall a. Read a => ReadPrec a
readPrec ReadPrec [a]
forall a. Read a => ReadPrec [a]
readListPrec
instance Read1 SmallArray where
#if MIN_VERSION_base(4,10,0)
liftReadPrec :: forall a. ReadPrec a -> ReadPrec [a] -> ReadPrec (SmallArray a)
liftReadPrec = ReadPrec a -> ReadPrec [a] -> ReadPrec (SmallArray a)
forall a. ReadPrec a -> ReadPrec [a] -> ReadPrec (SmallArray a)
smallArrayLiftReadPrec
#else
liftReadsPrec rp rl = RdPrc.readPrec_to_S $
smallArrayLiftReadPrec (RdPrc.readS_to_Prec rp) (RdPrc.readS_to_Prec (const rl))
#endif
smallArrayDataType :: DataType
smallArrayDataType :: DataType
smallArrayDataType =
String -> [Constr] -> DataType
mkDataType String
"Data.Primitive.SmallArray.SmallArray" [Constr
fromListConstr]
fromListConstr :: Constr
fromListConstr :: Constr
fromListConstr = DataType -> String -> [String] -> Fixity -> Constr
mkConstr DataType
smallArrayDataType String
"fromList" [] Fixity
Prefix
instance Data a => Data (SmallArray a) where
toConstr :: SmallArray a -> Constr
toConstr SmallArray a
_ = Constr
fromListConstr
dataTypeOf :: SmallArray a -> DataType
dataTypeOf SmallArray a
_ = DataType
smallArrayDataType
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (SmallArray a)
gunfold forall b r. Data b => c (b -> r) -> c r
k forall r. r -> c r
z Constr
c = case Constr -> Int
constrIndex Constr
c of
Int
1 -> c ([a] -> SmallArray a) -> c (SmallArray a)
forall b r. Data b => c (b -> r) -> c r
k (([a] -> SmallArray a) -> c ([a] -> SmallArray a)
forall r. r -> c r
z [a] -> SmallArray a
[Item (SmallArray a)] -> SmallArray a
forall l. IsList l => [Item l] -> l
fromList)
Int
_ -> String -> String -> c (SmallArray a)
forall a. String -> String -> a
die String
"gunfold" String
"SmallArray"
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SmallArray a -> c (SmallArray a)
gfoldl forall d b. Data d => c (d -> b) -> d -> c b
f forall g. g -> c g
z SmallArray a
m = ([a] -> SmallArray a) -> c ([a] -> SmallArray a)
forall g. g -> c g
z [a] -> SmallArray a
[Item (SmallArray a)] -> SmallArray a
forall l. IsList l => [Item l] -> l
fromList c ([a] -> SmallArray a) -> [a] -> c (SmallArray a)
forall d b. Data d => c (d -> b) -> d -> c b
`f` SmallArray a -> [a]
forall a. SmallArray a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList SmallArray a
m
instance (Typeable s, Typeable a) => Data (SmallMutableArray s a) where
toConstr :: SmallMutableArray s a -> Constr
toConstr SmallMutableArray s a
_ = String -> String -> Constr
forall a. String -> String -> a
die String
"toConstr" String
"SmallMutableArray"
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (SmallMutableArray s a)
gunfold forall b r. Data b => c (b -> r) -> c r
_ forall r. r -> c r
_ = String -> String -> Constr -> c (SmallMutableArray s a)
forall a. String -> String -> a
die String
"gunfold" String
"SmallMutableArray"
dataTypeOf :: SmallMutableArray s a -> DataType
dataTypeOf SmallMutableArray s a
_ = String -> DataType
mkNoRepType String
"Data.Primitive.SmallArray.SmallMutableArray"
smallArrayFromListN :: Int -> [a] -> SmallArray a
smallArrayFromListN :: forall a. Int -> [a] -> SmallArray a
smallArrayFromListN Int
n [a]
l =
Int
-> a
-> (forall {s}. SmallMutableArray s a -> ST s ())
-> SmallArray a
forall a.
Int
-> a
-> (forall s. SmallMutableArray s a -> ST s ())
-> SmallArray a
createSmallArray Int
n
(String -> String -> a
forall a. String -> String -> a
die String
"smallArrayFromListN" String
"uninitialized element") ((forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a)
-> (forall {s}. SmallMutableArray s a -> ST s ()) -> SmallArray a
forall a b. (a -> b) -> a -> b
$ \SmallMutableArray s a
sma ->
let go :: Int -> [a] -> ST s ()
go !Int
ix [] = if Int
ix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n
then () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else String -> String -> ST s ()
forall a. String -> String -> a
die String
"smallArrayFromListN" String
"list length less than specified size"
go !Int
ix (a
x : [a]
xs) = if Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n
then do
SmallMutableArray (PrimState (ST s)) a -> Int -> a -> ST s ()
forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> a -> m ()
writeSmallArray SmallMutableArray s a
SmallMutableArray (PrimState (ST s)) a
sma Int
ix a
x
Int -> [a] -> ST s ()
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [a]
xs
else String -> String -> ST s ()
forall a. String -> String -> a
die String
"smallArrayFromListN" String
"list length greater than specified size"
in Int -> [a] -> ST s ()
go Int
0 [a]
l
smallArrayFromList :: [a] -> SmallArray a
smallArrayFromList :: forall a. [a] -> SmallArray a
smallArrayFromList [a]
l = Int -> [a] -> SmallArray a
forall a. Int -> [a] -> SmallArray a
smallArrayFromListN ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
l) [a]
l
#if MIN_VERSION_base(4,14,0)
shrinkSmallMutableArray :: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> m ()
{-# inline shrinkSmallMutableArray #-}
shrinkSmallMutableArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a -> Int -> m ()
shrinkSmallMutableArray (SmallMutableArray SmallMutableArray# (PrimState m) a
x) (I# Int#
n) = (State# (PrimState m) -> (# State# (PrimState m), () #)) -> m ()
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive
(\State# (PrimState m)
s0 -> case SmallMutableArray# (PrimState m) a
-> Int# -> State# (PrimState m) -> State# (PrimState m)
forall d a. SmallMutableArray# d a -> Int# -> State# d -> State# d
GHC.Exts.shrinkSmallMutableArray# SmallMutableArray# (PrimState m) a
x Int#
n State# (PrimState m)
s0 of
State# (PrimState m)
s1 -> (# State# (PrimState m)
s1, () #)
)
resizeSmallMutableArray :: PrimMonad m
=> SmallMutableArray (PrimState m) a
-> Int
-> a
-> m (SmallMutableArray (PrimState m) a)
resizeSmallMutableArray :: forall (m :: * -> *) a.
PrimMonad m =>
SmallMutableArray (PrimState m) a
-> Int -> a -> m (SmallMutableArray (PrimState m) a)
resizeSmallMutableArray (SmallMutableArray SmallMutableArray# (PrimState m) a
arr) (I# Int#
n) a
x = (State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray (PrimState m) a #))
-> m (SmallMutableArray (PrimState m) a)
forall a.
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
forall (m :: * -> *) a.
PrimMonad m =>
(State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a
primitive
(\State# (PrimState m)
s0 -> case SmallMutableArray# (PrimState m) a
-> Int#
-> a
-> State# (PrimState m)
-> (# State# (PrimState m), SmallMutableArray# (PrimState m) a #)
forall s a.
SmallMutableArray# s a
-> Int# -> a -> State# s -> (# State# s, SmallMutableArray# s a #)
GHC.Exts.resizeSmallMutableArray# SmallMutableArray# (PrimState m) a
arr Int#
n a
x State# (PrimState m)
s0 of
(# State# (PrimState m)
s1, SmallMutableArray# (PrimState m) a
arr' #) -> (# State# (PrimState m)
s1, SmallMutableArray# (PrimState m) a
-> SmallMutableArray (PrimState m) a
forall s a. SmallMutableArray# s a -> SmallMutableArray s a
SmallMutableArray SmallMutableArray# (PrimState m) a
arr' #)
)
{-# INLINE resizeSmallMutableArray #-}
#endif