{-# LANGUAGE Trustworthy, MagicHash, MultiParamTypeClasses, FlexibleInstances #-}

{- |
    Module      :  SDP.Prim.TArray
    Copyright   :  (c) Andrey Mulik 2020
    License     :  BSD-style
    Maintainer  :  work.a.mulik@gmail.com
    Portability :  non-portable
    
    "SDP.Prim.TArray" provides lazy boxed array of @stm@ 'TVar's.
    Note that 'TArray#' stores each element in 'TVar'.
-}
module SDP.Prim.TArray
(
  -- TArray
  TArray# (..), STM, TVar
)
where

import Prelude ()
import SDP.SafePrelude
import SDP.Prim.SArray

import GHC.Conc

import Control.Exception.SDP

default ()

--------------------------------------------------------------------------------

-- | 'SArray#' of @stm@ 'TVar's.
newtype TArray# e = TArray# (SArray# (TVar e)) deriving ( TArray# e -> TArray# e -> Bool
(TArray# e -> TArray# e -> Bool)
-> (TArray# e -> TArray# e -> Bool) -> Eq (TArray# e)
forall e. TArray# e -> TArray# e -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TArray# e -> TArray# e -> Bool
$c/= :: forall e. TArray# e -> TArray# e -> Bool
== :: TArray# e -> TArray# e -> Bool
$c== :: forall e. TArray# e -> TArray# e -> Bool
Eq )

--------------------------------------------------------------------------------

{- Nullable, Estimate, Bordered and BorderedM instances. -}

instance Nullable (TArray# e)
  where
    isNull :: TArray# e -> Bool
isNull = \ (TArray# SArray# (TVar e)
es) -> SArray# (TVar e) -> Bool
forall e. Nullable e => e -> Bool
isNull SArray# (TVar e)
es
    lzero :: TArray# e
lzero  = SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# SArray# (TVar e)
forall e. Nullable e => e
Z

instance Estimate (TArray# e)
  where
    <==> :: Compare (TArray# e)
(<==>) = (Int -> Int -> Ordering)
-> (TArray# e -> Int) -> Compare (TArray# e)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Ordering
forall o. Ord o => Compare o
(<=>) TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<=. :: TArray# e -> TArray# e -> Bool
(.<=.) = (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> TArray# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>=. :: TArray# e -> TArray# e -> Bool
(.>=.) = (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> TArray# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>. :: TArray# e -> TArray# e -> Bool
(.>.)  = (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> TArray# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<. :: TArray# e -> TArray# e -> Bool
(.<.)  = (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> TArray# e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    
    <.=> :: TArray# e -> Int -> Ordering
(<.=>) = Int -> Int -> Ordering
forall o. Ord o => Compare o
(<=>) (Int -> Int -> Ordering)
-> (TArray# e -> Int) -> TArray# e -> Int -> Ordering
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .>= :: TArray# e -> Int -> Bool
(.>=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>=)  (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .<= :: TArray# e -> Int -> Bool
(.<=)  = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<=)  (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .> :: TArray# e -> Int -> Bool
(.>)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(>)   (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    .< :: TArray# e -> Int -> Bool
(.<)   = Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
(<)   (Int -> Int -> Bool)
-> (TArray# e -> Int) -> TArray# e -> Int -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf

instance Bordered (TArray# e) Int
  where
    lower :: TArray# e -> Int
lower TArray# e
_ = Int
0
    
    upper :: TArray# e -> Int
upper    (TArray# SArray# (TVar e)
arr) = SArray# (TVar e) -> Int
forall b i. Bordered b i => b -> i
upper SArray# (TVar e)
arr
    sizeOf :: TArray# e -> Int
sizeOf   (TArray# SArray# (TVar e)
arr) = SArray# (TVar e) -> Int
forall b i. Bordered b i => b -> Int
sizeOf SArray# (TVar e)
arr
    bounds :: TArray# e -> (Int, Int)
bounds   (TArray# SArray# (TVar e)
arr) = (Int
0, SArray# (TVar e) -> Int
forall b i. Bordered b i => b -> i
upper SArray# (TVar e)
arr)
    indices :: TArray# e -> [Int]
indices  (TArray# SArray# (TVar e)
arr) = [Int
0 .. SArray# (TVar e) -> Int
forall b i. Bordered b i => b -> i
upper SArray# (TVar e)
arr]
    indexOf :: TArray# e -> Int -> Int
indexOf  (TArray# SArray# (TVar e)
arr) = (Int, Int) -> Int -> Int
forall i. Index i => (i, i) -> Int -> i
index (Int
0, SArray# (TVar e) -> Int
forall b i. Bordered b i => b -> i
upper SArray# (TVar e)
arr)
    offsetOf :: TArray# e -> Int -> Int
offsetOf (TArray# SArray# (TVar e)
arr) = (Int, Int) -> Int -> Int
forall i. Index i => (i, i) -> i -> Int
offset (Int
0, SArray# (TVar e) -> Int
forall b i. Bordered b i => b -> i
upper SArray# (TVar e)
arr)
    indexIn :: TArray# e -> Int -> Bool
indexIn  (TArray# SArray# (TVar e)
arr) = \ Int
i -> Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< SArray# (TVar e) -> Int
forall b i. Bordered b i => b -> Int
sizeOf SArray# (TVar e)
arr

instance BorderedM STM (TArray# e) Int
  where
    getIndexOf :: TArray# e -> Int -> STM Int
getIndexOf = Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> STM Int)
-> (TArray# e -> Int -> Int) -> TArray# e -> Int -> STM Int
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... TArray# e -> Int -> Int
forall b i. Bordered b i => b -> Int -> i
indexOf
    getIndices :: TArray# e -> STM [Int]
getIndices = [Int] -> STM [Int]
forall (m :: * -> *) a. Monad m => a -> m a
return ([Int] -> STM [Int])
-> (TArray# e -> [Int]) -> TArray# e -> STM [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> [Int]
forall b i. Bordered b i => b -> [i]
indices
    getBounds :: TArray# e -> STM (Int, Int)
getBounds  = (Int, Int) -> STM (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Int, Int) -> STM (Int, Int))
-> (TArray# e -> (Int, Int)) -> TArray# e -> STM (Int, Int)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> (Int, Int)
forall b i. Bordered b i => b -> (i, i)
bounds
    getSizeOf :: TArray# e -> STM Int
getSizeOf  = Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> STM Int) -> (TArray# e -> Int) -> TArray# e -> STM Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf
    getUpper :: TArray# e -> STM Int
getUpper   = Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> STM Int) -> (TArray# e -> Int) -> TArray# e -> STM Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> Int
forall b i. Bordered b i => b -> i
upper
    getLower :: TArray# e -> STM Int
getLower TArray# e
_ = Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0

--------------------------------------------------------------------------------

{- LinearM and SplitM instances. -}

instance LinearM STM (TArray# e) e
  where
    newNull :: STM (TArray# e)
newNull = TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return (SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# SArray# (TVar e)
forall e. Nullable e => e
Z)
    nowNull :: TArray# e -> STM Bool
nowNull = Bool -> STM Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> STM Bool) -> (TArray# e -> Bool) -> TArray# e -> STM Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> Bool
forall e. Nullable e => e -> Bool
isNull (SArray# (TVar e) -> Bool)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    getHead :: TArray# e -> STM e
getHead = TVar e -> STM e
forall a. TVar a -> STM a
readTVar (TVar e -> STM e) -> (TArray# e -> TVar e) -> TArray# e -> STM e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TVar e
forall l e. Linear l e => l -> e
head (SArray# (TVar e) -> TVar e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> TVar e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    getLast :: TArray# e -> STM e
getLast = TVar e -> STM e
forall a. TVar a -> STM a
readTVar (TVar e -> STM e) -> (TArray# e -> TVar e) -> TArray# e -> STM e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TVar e
forall l e. Linear l e => l -> e
last (SArray# (TVar e) -> TVar e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> TVar e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    singleM :: e -> STM (TArray# e)
singleM = (TVar e -> TArray# e) -> STM (TVar e) -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TVar e -> SArray# (TVar e)) -> TVar e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar e -> SArray# (TVar e)
forall l e. Linear l e => e -> l
single) (STM (TVar e) -> STM (TArray# e))
-> (e -> STM (TVar e)) -> e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar
    
    prepend :: e -> TArray# e -> STM (TArray# e)
prepend e
e TArray# e
es = SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TVar e -> SArray# (TVar e)) -> TVar e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TVar e -> SArray# (TVar e) -> SArray# (TVar e)
forall l e. Linear l e => e -> l -> l
:> TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack TArray# e
es) (TVar e -> TArray# e) -> STM (TVar e) -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar e
e
    append :: TArray# e -> e -> STM (TArray# e)
append  TArray# e
es e
e = SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TVar e -> SArray# (TVar e)) -> TVar e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack TArray# e
es SArray# (TVar e) -> TVar e -> SArray# (TVar e)
forall l e. Linear l e => l -> e -> l
:<) (TVar e -> TArray# e) -> STM (TVar e) -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar e
e
    
    newLinear :: [e] -> STM (TArray# e)
newLinear     = ([TVar e] -> TArray# e) -> STM [TVar e] -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> ([TVar e] -> SArray# (TVar e)) -> [TVar e] -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TVar e] -> SArray# (TVar e)
forall l e. Linear l e => [e] -> l
fromList) (STM [TVar e] -> STM (TArray# e))
-> ([e] -> STM [TVar e]) -> [e] -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> STM (TVar e)) -> [e] -> STM [TVar e]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar
    newLinearN :: Int -> [e] -> STM (TArray# e)
newLinearN  Int
n = ([TVar e] -> TArray# e) -> STM [TVar e] -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> ([TVar e] -> SArray# (TVar e)) -> [TVar e] -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [TVar e] -> SArray# (TVar e)
forall l e. Linear l e => Int -> [e] -> l
fromListN Int
n) (STM [TVar e] -> STM (TArray# e))
-> ([e] -> STM [TVar e]) -> [e] -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> STM (TVar e)) -> [e] -> STM [TVar e]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar
    fromFoldableM :: f e -> STM (TArray# e)
fromFoldableM = ([TVar e] -> TArray# e) -> STM [TVar e] -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> ([TVar e] -> SArray# (TVar e)) -> [TVar e] -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TVar e] -> SArray# (TVar e)
forall l e. Linear l e => [e] -> l
fromList) (STM [TVar e] -> STM (TArray# e))
-> (f e -> STM [TVar e]) -> f e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> STM [TVar e] -> STM [TVar e])
-> STM [TVar e] -> f e -> STM [TVar e]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((TVar e -> [TVar e] -> [TVar e])
-> STM (TVar e) -> STM [TVar e] -> STM [TVar e]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) (STM (TVar e) -> STM [TVar e] -> STM [TVar e])
-> (e -> STM (TVar e)) -> e -> STM [TVar e] -> STM [TVar e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar) ([TVar e] -> STM [TVar e]
forall (m :: * -> *) a. Monad m => a -> m a
return [])
    
    !#> :: TArray# e -> Int -> STM e
(!#>)  = TVar e -> STM e
forall a. TVar a -> STM a
readTVar (TVar e -> STM e)
-> (TArray# e -> Int -> TVar e) -> TArray# e -> Int -> STM e
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... SArray# (TVar e) -> Int -> TVar e
forall l e. Linear l e => l -> Int -> e
(!^) (SArray# (TVar e) -> Int -> TVar e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> Int -> TVar e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    writeM :: TArray# e -> Int -> e -> STM ()
writeM = TArray# e -> Int -> e -> STM ()
forall (m :: * -> *) v i e. IndexedM m v i e => v -> i -> e -> m ()
writeM'
    
    getLeft :: TArray# e -> STM [e]
getLeft  = (TVar e -> STM e) -> [TVar e] -> STM [e]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TVar e -> STM e
forall a. TVar a -> STM a
readTVar ([TVar e] -> STM [e])
-> (TArray# e -> [TVar e]) -> TArray# e -> STM [e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> [TVar e]
forall l e. Linear l e => l -> [e]
listL (SArray# (TVar e) -> [TVar e])
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> [TVar e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    getRight :: TArray# e -> STM [e]
getRight = (TVar e -> STM e) -> [TVar e] -> STM [e]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TVar e -> STM e
forall a. TVar a -> STM a
readTVar ([TVar e] -> STM [e])
-> (TArray# e -> [TVar e]) -> TArray# e -> STM [e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> [TVar e]
forall l e. Linear l e => l -> [e]
listR (SArray# (TVar e) -> [TVar e])
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> [TVar e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    merged :: f (TArray# e) -> STM (TArray# e)
merged   = TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return (TArray# e -> STM (TArray# e))
-> (f (TArray# e) -> TArray# e) -> f (TArray# e) -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (f (TArray# e) -> SArray# (TVar e))
-> f (TArray# e)
-> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TArray# e -> SArray# (TVar e))
-> f (TArray# e) -> SArray# (TVar e)
forall l e (f :: * -> *) a.
(Linear l e, Foldable f) =>
(a -> l) -> f a -> l
concatMap TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    reversed :: TArray# e -> STM (TArray# e)
reversed = TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return (TArray# e -> STM (TArray# e))
-> (TArray# e -> TArray# e) -> TArray# e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> SArray# (TVar e)
forall l e. Linear l e => l -> l
reverse (SArray# (TVar e) -> SArray# (TVar e))
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> SArray# (TVar e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    filled :: Int -> e -> STM (TArray# e)
filled Int
n = ([TVar e] -> TArray# e) -> STM [TVar e] -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> ([TVar e] -> SArray# (TVar e)) -> [TVar e] -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TVar e] -> SArray# (TVar e)
forall l e. Linear l e => [e] -> l
fromList) (STM [TVar e] -> STM (TArray# e))
-> (e -> STM [TVar e]) -> e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> STM (TVar e) -> STM [TVar e]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
n (STM (TVar e) -> STM [TVar e])
-> (e -> STM (TVar e)) -> e -> STM [TVar e]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar
    
    copyTo :: TArray# e -> Int -> TArray# e -> Int -> Int -> STM ()
copyTo TArray# e
src Int
so TArray# e
trg Int
to Int
n = Bool -> STM () -> STM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0) (STM () -> STM ()) -> STM () -> STM ()
forall a b. (a -> b) -> a -> b
$ do
        Bool -> STM () -> STM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when      (Int
so Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
to Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0)      (STM () -> STM ()) -> STM () -> STM ()
forall a b. (a -> b) -> a -> b
$ String -> STM ()
forall a. String -> a
underEx String
"copyTo"
        Bool -> STM () -> STM ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
so Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n1 Bool -> Bool -> Bool
|| Int
to Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n2) (STM () -> STM ()) -> STM () -> STM ()
forall a b. (a -> b) -> a -> b
$ String -> STM ()
forall a. String -> a
overEx  String
"copyTo"
        Int -> Int -> Int -> STM ()
forall t. (Eq t, Num t) => Int -> Int -> t -> STM ()
go Int
so Int
to Int
n
      where
        go :: Int -> Int -> t -> STM ()
go Int
_ Int
_ t
0 = () -> STM ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        go Int
i Int
j t
c = do e
e <- TArray# e
src TArray# e -> Int -> STM e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; TArray# e -> Int -> e -> STM ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM TArray# e
trg Int
j e
e; Int -> Int -> t -> STM ()
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (t
c t -> t -> t
forall a. Num a => a -> a -> a
- t
1)
        
        n1 :: Int
n1 = TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf TArray# e
src
        n2 :: Int
n2 = TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf TArray# e
trg
    
    ofoldlM :: (Int -> r -> e -> STM r) -> r -> TArray# e -> STM r
ofoldlM Int -> r -> e -> STM r
f r
base = (Int -> STM r -> TVar e -> STM r)
-> STM r -> SArray# (TVar e) -> STM r
forall l e b. Linear l e => (Int -> b -> e -> b) -> b -> l -> b
ofoldl (\ Int
i STM r
es -> (((r -> e -> STM r) -> STM r) -> (r -> e -> STM r) -> STM r
forall a b. (a -> b) -> a -> b
$ Int -> r -> e -> STM r
f Int
i) (((r -> e -> STM r) -> STM r) -> STM r)
-> (TVar e -> (r -> e -> STM r) -> STM r) -> TVar e -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (STM r
es STM r -> STM e -> (r -> e -> STM r) -> STM r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<<) (STM e -> (r -> e -> STM r) -> STM r)
-> (TVar e -> STM e) -> TVar e -> (r -> e -> STM r) -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar e -> STM e
forall a. TVar a -> STM a
readTVar) (r -> STM r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base) (SArray# (TVar e) -> STM r)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    ofoldrM :: (Int -> e -> r -> STM r) -> r -> TArray# e -> STM r
ofoldrM Int -> e -> r -> STM r
f r
base = (Int -> TVar e -> STM r -> STM r)
-> STM r -> SArray# (TVar e) -> STM r
forall l e b. Linear l e => (Int -> e -> b -> b) -> b -> l -> b
ofoldr (\ Int
i -> (((e -> r -> STM r) -> STM r) -> (e -> r -> STM r) -> STM r
forall a b. (a -> b) -> a -> b
$ Int -> e -> r -> STM r
f Int
i) (((e -> r -> STM r) -> STM r) -> STM r)
-> (TVar e -> STM r -> (e -> r -> STM r) -> STM r)
-> TVar e
-> STM r
-> STM r
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STM e -> STM r -> (e -> r -> STM r) -> STM r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
(>>=<<) (STM e -> STM r -> (e -> r -> STM r) -> STM r)
-> (TVar e -> STM e)
-> TVar e
-> STM r
-> (e -> r -> STM r)
-> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar e -> STM e
forall a. TVar a -> STM a
readTVar) (r -> STM r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base) (SArray# (TVar e) -> STM r)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    
    foldlM :: (r -> e -> STM r) -> r -> TArray# e -> STM r
foldlM r -> e -> STM r
f r
base = (STM r -> TVar e -> STM r) -> STM r -> SArray# (TVar e) -> STM r
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\ STM r
es -> (((r -> e -> STM r) -> STM r) -> (r -> e -> STM r) -> STM r
forall a b. (a -> b) -> a -> b
$ r -> e -> STM r
f) (((r -> e -> STM r) -> STM r) -> STM r)
-> (TVar e -> (r -> e -> STM r) -> STM r) -> TVar e -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (STM r
es STM r -> STM e -> (r -> e -> STM r) -> STM r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
>>=<<) (STM e -> (r -> e -> STM r) -> STM r)
-> (TVar e -> STM e) -> TVar e -> (r -> e -> STM r) -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar e -> STM e
forall a. TVar a -> STM a
readTVar) (r -> STM r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base) (SArray# (TVar e) -> STM r)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    foldrM :: (e -> r -> STM r) -> r -> TArray# e -> STM r
foldrM e -> r -> STM r
f r
base = (TVar e -> STM r -> STM r) -> STM r -> SArray# (TVar e) -> STM r
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((((e -> r -> STM r) -> STM r) -> (e -> r -> STM r) -> STM r
forall a b. (a -> b) -> a -> b
$ e -> r -> STM r
f) (((e -> r -> STM r) -> STM r) -> STM r)
-> (TVar e -> STM r -> (e -> r -> STM r) -> STM r)
-> TVar e
-> STM r
-> STM r
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... STM e -> STM r -> (e -> r -> STM r) -> STM r
forall (m :: * -> *) a b c.
Monad m =>
m a -> m b -> (a -> b -> m c) -> m c
(>>=<<) (STM e -> STM r -> (e -> r -> STM r) -> STM r)
-> (TVar e -> STM e)
-> TVar e
-> STM r
-> (e -> r -> STM r)
-> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TVar e -> STM e
forall a. TVar a -> STM a
readTVar) (r -> STM r
forall (m :: * -> *) a. Monad m => a -> m a
return r
base) (SArray# (TVar e) -> STM r)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> STM r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack

instance SplitM STM (TArray# e) e
  where
    takeM :: Int -> TArray# e -> STM (TArray# e)
takeM Int
n = TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return (TArray# e -> STM (TArray# e))
-> (TArray# e -> TArray# e) -> TArray# e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> SArray# (TVar e) -> SArray# (TVar e)
forall s e. Split s e => Int -> s -> s
take Int
n (SArray# (TVar e) -> SArray# (TVar e))
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> SArray# (TVar e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    dropM :: Int -> TArray# e -> STM (TArray# e)
dropM Int
n = TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return (TArray# e -> STM (TArray# e))
-> (TArray# e -> TArray# e) -> TArray# e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> SArray# (TVar e) -> SArray# (TVar e)
forall s e. Split s e => Int -> s -> s
drop Int
n (SArray# (TVar e) -> SArray# (TVar e))
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> SArray# (TVar e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    keepM :: Int -> TArray# e -> STM (TArray# e)
keepM Int
n = TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return (TArray# e -> STM (TArray# e))
-> (TArray# e -> TArray# e) -> TArray# e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> SArray# (TVar e) -> SArray# (TVar e)
forall s e. Split s e => Int -> s -> s
keep Int
n (SArray# (TVar e) -> SArray# (TVar e))
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> SArray# (TVar e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    sansM :: Int -> TArray# e -> STM (TArray# e)
sansM Int
n = TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return (TArray# e -> STM (TArray# e))
-> (TArray# e -> TArray# e) -> TArray# e -> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (SArray# (TVar e) -> TArray# e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> TArray# e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> SArray# (TVar e) -> SArray# (TVar e)
forall s e. Split s e => Int -> s -> s
sans Int
n (SArray# (TVar e) -> SArray# (TVar e))
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> SArray# (TVar e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    
    prefixM :: (e -> Bool) -> TArray# e -> STM Int
prefixM e -> Bool
p TArray# e
es =
      let
          go :: Int -> STM Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c Bool -> STM Int -> STM Int -> STM Int
forall a. Bool -> a -> a -> a
? Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ do e
e <- TArray# e
es TArray# e -> Int -> STM e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> Bool
p e
e Bool -> STM Int -> STM Int -> STM Int
forall a. Bool -> a -> a -> a
? Int -> STM Int
go (Int -> Int
forall a. Enum a => a -> a
succ Int
1) (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
          c :: Int
c = TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf TArray# e
es
      in  Int -> STM Int
go Int
0
    
    suffixM :: (e -> Bool) -> TArray# e -> STM Int
suffixM e -> Bool
p TArray# e
es =
      let
          go :: Int -> STM Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> STM Int -> STM Int -> STM Int
forall a. Bool -> a -> a -> a
? Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ do e
e <- TArray# e
es TArray# e -> Int -> STM e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> Bool
p e
e Bool -> STM Int -> STM Int -> STM Int
forall a. Bool -> a -> a -> a
? Int -> STM Int
go (Int -> Int
forall a. Enum a => a -> a
pred Int
i) (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
          c :: Int
c = TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf TArray# e
es
      in  Int -> STM Int
go (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
    
    mprefix :: (e -> STM Bool) -> TArray# e -> STM Int
mprefix e -> STM Bool
p TArray# e
es =
      let
          go :: Int -> STM Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
c Bool -> STM Int -> STM Int -> STM Int
forall a. Bool -> a -> a -> a
? Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ do e
e <- TArray# e
es TArray# e -> Int -> STM e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> STM Bool
p e
e STM Bool -> STM Int -> STM Int -> STM Int
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
?^ Int -> STM Int
go (Int -> Int
forall a. Enum a => a -> a
succ Int
1) (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
          c :: Int
c = TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf TArray# e
es
      in  Int -> STM Int
go Int
0
    
    msuffix :: (e -> STM Bool) -> TArray# e -> STM Int
msuffix e -> STM Bool
p TArray# e
es =
      let
          go :: Int -> STM Int
go Int
i = Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> STM Int -> STM Int -> STM Int
forall a. Bool -> a -> a -> a
? Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
c (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ do e
e <- TArray# e
es TArray# e -> Int -> STM e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i; e -> STM Bool
p e
e STM Bool -> STM Int -> STM Int -> STM Int
forall (m :: * -> *) a. Monad m => m Bool -> m a -> m a -> m a
?^ Int -> STM Int
go (Int -> Int
forall a. Enum a => a -> a
pred Int
i) (STM Int -> STM Int) -> STM Int -> STM Int
forall a b. (a -> b) -> a -> b
$ Int -> STM Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
          c :: Int
c = TArray# e -> Int
forall b i. Bordered b i => b -> Int
sizeOf TArray# e
es
      in  Int -> STM Int
go (Int
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

--------------------------------------------------------------------------------

{- MapM and IndexedM instances. -}

instance MapM STM (TArray# e) Int e
  where
    newMap' :: e -> [(Int, e)] -> STM (TArray# e)
newMap' e
defvalue [(Int, e)]
ascs = (Int, Int) -> e -> [(Int, e)] -> STM (TArray# e)
forall (m :: * -> *) v i e.
IndexedM m v i e =>
(i, i) -> e -> [(i, e)] -> m v
fromAssocs' ([(Int, e)] -> (Int, Int)
forall a b. Ord a => [(a, b)] -> (a, a)
ascsBounds [(Int, e)]
ascs) e
defvalue [(Int, e)]
ascs
    
    >! :: TArray# e -> Int -> STM e
(>!) = TArray# e -> Int -> STM e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
(!#>)
    
    overwrite :: TArray# e -> [(Int, e)] -> STM (TArray# e)
overwrite TArray# e
es [(Int, e)]
ascs = do
      ((Int, e) -> STM ()) -> [(Int, e)] -> STM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ((Int -> e -> STM ()) -> (Int, e) -> STM ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry ((Int -> e -> STM ()) -> (Int, e) -> STM ())
-> (Int -> e -> STM ()) -> (Int, e) -> STM ()
forall a b. (a -> b) -> a -> b
$ TArray# e -> Int -> e -> STM ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM TArray# e
es) (((Int, e) -> Bool) -> [(Int, e)] -> [(Int, e)]
forall l e. Linear l e => (e -> Bool) -> l -> l
filter (TArray# e -> Int -> Bool
forall b i. Bordered b i => b -> i -> Bool
indexIn TArray# e
es (Int -> Bool) -> ((Int, e) -> Int) -> (Int, e) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int, e) -> Int
forall a b. (a, b) -> a
fst) [(Int, e)]
ascs)
      TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return TArray# e
es
    
    kfoldrM :: (Int -> e -> acc -> STM acc) -> acc -> TArray# e -> STM acc
kfoldrM = (Int -> e -> acc -> STM acc) -> acc -> TArray# e -> STM acc
forall (m :: * -> *) l e r.
LinearM m l e =>
(Int -> e -> r -> m r) -> r -> l -> m r
ofoldrM
    kfoldlM :: (Int -> acc -> e -> STM acc) -> acc -> TArray# e -> STM acc
kfoldlM = (Int -> acc -> e -> STM acc) -> acc -> TArray# e -> STM acc
forall (m :: * -> *) l e r.
LinearM m l e =>
(Int -> r -> e -> m r) -> r -> l -> m r
ofoldlM

instance IndexedM STM (TArray# e) Int e
  where
    fromAssocs' :: (Int, Int) -> e -> [(Int, e)] -> STM (TArray# e)
fromAssocs' (Int, Int)
bnds e
defvalue [(Int, e)]
ascs = (Int, Int) -> Int
forall i. Index i => (i, i) -> Int
size (Int, Int)
bnds Int -> e -> STM (TArray# e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
`filled` e
defvalue STM (TArray# e)
-> (TArray# e -> STM (TArray# e)) -> STM (TArray# e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TArray# e -> [(Int, e)] -> STM (TArray# e)
forall (m :: * -> *) map key e.
MapM m map key e =>
map -> [(key, e)] -> m map
`overwrite` [(Int, e)]
ascs)
    
    {-# INLINE writeM' #-}
    writeM' :: TArray# e -> Int -> e -> STM ()
writeM' = TVar e -> e -> STM ()
forall a. TVar a -> a -> STM ()
writeTVar (TVar e -> e -> STM ())
-> (TArray# e -> Int -> TVar e) -> TArray# e -> Int -> e -> STM ()
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... SArray# (TVar e) -> Int -> TVar e
forall l e. Linear l e => l -> Int -> e
(!^) (SArray# (TVar e) -> Int -> TVar e)
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> Int -> TVar e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack
    
    fromIndexed' :: v' -> STM (TArray# e)
fromIndexed' v'
es = do
      let n :: Int
n = v' -> Int
forall b i. Bordered b i => b -> Int
sizeOf v'
es
      TArray# e
copy <- Int -> e -> STM (TArray# e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
filled Int
n (String -> e
forall a. String -> a
unreachEx String
"fromIndexed'")
      [Int] -> (Int -> STM ()) -> STM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> STM ()) -> STM ()) -> (Int -> STM ()) -> STM ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> TArray# e -> Int -> e -> STM ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM TArray# e
copy Int
i (v'
es v' -> Int -> e
forall l e. Linear l e => l -> Int -> e
!^ Int
i)
      TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return TArray# e
copy
    
    fromIndexedM :: v' -> STM (TArray# e)
fromIndexedM v'
es = do
      Int
n    <- v' -> STM Int
forall (m :: * -> *) b i. BorderedM m b i => b -> m Int
getSizeOf v'
es
      TArray# e
copy <- Int -> e -> STM (TArray# e)
forall (m :: * -> *) l e. LinearM m l e => Int -> e -> m l
filled Int
n (String -> e
forall a. String -> a
unreachEx String
"fromIndexedM")
      [Int] -> (Int -> STM ()) -> STM ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> STM ()) -> STM ()) -> (Int -> STM ()) -> STM ()
forall a b. (a -> b) -> a -> b
$ \ Int
i -> v'
es v' -> Int -> STM e
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> m e
!#> Int
i STM e -> (e -> STM ()) -> STM ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TArray# e -> Int -> e -> STM ()
forall (m :: * -> *) l e. LinearM m l e => l -> Int -> e -> m ()
writeM TArray# e
copy Int
i
      TArray# e -> STM (TArray# e)
forall (m :: * -> *) a. Monad m => a -> m a
return TArray# e
copy

--------------------------------------------------------------------------------

{- Freeze and Thaw instances. -}

instance Thaw STM (SArray# e) (TArray# e) where thaw :: SArray# e -> STM (TArray# e)
thaw = (SArray# (TVar e) -> TArray# e)
-> STM (SArray# (TVar e)) -> STM (TArray# e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SArray# (TVar e) -> TArray# e
forall e. SArray# (TVar e) -> TArray# e
TArray# (STM (SArray# (TVar e)) -> STM (TArray# e))
-> (SArray# e -> STM (SArray# (TVar e)))
-> SArray# e
-> STM (TArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (e -> STM (TVar e)) -> SArray# e -> STM (SArray# (TVar e))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM e -> STM (TVar e)
forall a. a -> STM (TVar a)
newTVar

instance Freeze STM (TArray# e) (SArray# e) where freeze :: TArray# e -> STM (SArray# e)
freeze = (TVar e -> STM e) -> SArray# (TVar e) -> STM (SArray# e)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM TVar e -> STM e
forall a. TVar a -> STM a
readTVar (SArray# (TVar e) -> STM (SArray# e))
-> (TArray# e -> SArray# (TVar e)) -> TArray# e -> STM (SArray# e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TArray# e -> SArray# (TVar e)
forall e. TArray# e -> SArray# (TVar e)
unpack

--------------------------------------------------------------------------------

ascsBounds :: (Ord a) => [(a, b)] -> (a, a)
ascsBounds :: [(a, b)] -> (a, a)
ascsBounds =  \ ((a
x, b
_) : [(a, b)]
xs) -> ((a, b) -> (a, a) -> (a, a)) -> (a, a) -> [(a, b)] -> (a, a)
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\ (a
e, b
_) (a
mn, a
mx) -> (a -> a -> a
forall a. Ord a => a -> a -> a
min a
mn a
e, a -> a -> a
forall a. Ord a => a -> a -> a
max a
mx a
e)) (a
x, a
x) [(a, b)]
xs

unpack :: TArray# e -> SArray# (TVar e)
unpack :: TArray# e -> SArray# (TVar e)
unpack =  \ (TArray# SArray# (TVar e)
arr) -> SArray# (TVar e)
arr

overEx :: String -> a
overEx :: String -> a
overEx =  IndexException -> a
forall a e. Exception e => e -> a
throw (IndexException -> a) -> (String -> IndexException) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IndexException
IndexOverflow (String -> IndexException)
-> (String -> String) -> String -> IndexException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
"in SDP.Prim.TArray."

underEx :: String -> a
underEx :: String -> a
underEx =  IndexException -> a
forall a e. Exception e => e -> a
throw (IndexException -> a) -> (String -> IndexException) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IndexException
IndexUnderflow (String -> IndexException)
-> (String -> String) -> String -> IndexException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
"in SDP.Prim.TArray."

unreachEx :: String -> a
unreachEx :: String -> a
unreachEx =  UnreachableException -> a
forall a e. Exception e => e -> a
throw (UnreachableException -> a)
-> (String -> UnreachableException) -> String -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> UnreachableException
UnreachableException (String -> UnreachableException)
-> (String -> String) -> String -> UnreachableException
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
showString String
"in SDP.Prim.TArray."