{-# 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = 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 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
| Bool
otherwise = 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure 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
| forall ix. Index ix => Sz ix -> ix -> Bool
isSafeIndex (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 = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
{-# INLINE atomicXorIntArray #-}