{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Massiv.Array.Mutable.Atomic (
atomicReadIntArray,
atomicWriteIntArray,
atomicModifyIntArray,
atomicAddIntArray,
atomicSubIntArray,
atomicAndIntArray,
atomicNandIntArray,
atomicOrIntArray,
atomicXorIntArray,
casIntArray,
) where
import Control.Monad.Primitive
import Data.Massiv.Array.Manifest.Primitive
import Data.Massiv.Core.Common
atomicReadIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> m (Maybe Int)
atomicReadIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> m (Maybe Int)
atomicReadIntArray MArray (PrimState m) P ix Int
marr ix
ix
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> m Int
unsafeAtomicReadIntArray MArray (PrimState m) P ix Int
marr ix
ix
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicReadIntArray #-}
atomicWriteIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m Bool
atomicWriteIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m Bool
atomicWriteIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
f
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = MArray (PrimState m) P ix Int -> ix -> Int -> m ()
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m ()
unsafeAtomicWriteIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
f m () -> m Bool -> m Bool
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
| Bool
otherwise = Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
{-# INLINE atomicWriteIntArray #-}
casIntArray
:: (Index ix, PrimMonad m)
=> MArray (PrimState m) P ix Int
-> ix
-> Int
-> Int
-> m (Maybe Int)
casIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> Int -> m (Maybe Int)
casIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e Int
n
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> Int -> Int -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> Int -> m Int
unsafeCasIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e Int
n
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE casIntArray #-}
atomicModifyIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> (Int -> Int) -> m (Maybe Int)
atomicModifyIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int
-> ix -> (Int -> Int) -> m (Maybe Int)
atomicModifyIntArray MArray (PrimState m) P ix Int
marr ix
ix Int -> Int
f
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> (Int -> Int) -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> (Int -> Int) -> m Int
unsafeAtomicModifyIntArray MArray (PrimState m) P ix Int
marr ix
ix Int -> Int
f
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicModifyIntArray #-}
atomicAddIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicAddIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicAddIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> Int -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m Int
unsafeAtomicAddIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicAddIntArray #-}
atomicSubIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicSubIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicSubIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> Int -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m Int
unsafeAtomicSubIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicSubIntArray #-}
atomicAndIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicAndIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicAndIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> Int -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m Int
unsafeAtomicAndIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicAndIntArray #-}
atomicNandIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicNandIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicNandIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> Int -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m Int
unsafeAtomicNandIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicNandIntArray #-}
atomicOrIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicOrIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicOrIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> Int -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m Int
unsafeAtomicOrIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicOrIntArray #-}
atomicXorIntArray
:: (Index ix, PrimMonad m) => MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicXorIntArray :: forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m (Maybe Int)
atomicXorIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Sz ix -> ix -> Bool
forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (MArray (PrimState m) P ix Int -> Sz ix
forall ix s. Index ix => MArray s P ix Int -> Sz ix
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MArray (PrimState m) P ix Int
marr) ix
ix = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Maybe Int) -> m Int -> m (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MArray (PrimState m) P ix Int -> ix -> Int -> m Int
forall ix (m :: * -> *).
(Index ix, PrimMonad m) =>
MArray (PrimState m) P ix Int -> ix -> Int -> m Int
unsafeAtomicXorIntArray MArray (PrimState m) P ix Int
marr ix
ix Int
e
| Bool
otherwise = Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Int
forall a. Maybe a
Nothing
{-# INLINE atomicXorIntArray #-}