Copyright | (c) Alexey Kuleshevich 2018 |
---|---|
License | BSD3 |
Maintainer | Alexey Kuleshevich <lehins@yandex.ru> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- read :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m (Maybe e)
- read' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m e
- write :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m Bool
- write' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m ()
- modify :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m Bool
- modify' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m ()
- swap :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m Bool
- swap' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m ()
- class Manifest r ix e => Mutable r ix e
- data family MArray s r ix e :: *
- msize :: Mutable r ix e => MArray s r ix e -> Sz ix
- new :: (Mutable r ix e, PrimMonad m) => Sz ix -> m (MArray (PrimState m) r ix e)
- thaw :: (Mutable r ix e, PrimMonad m) => Array r ix e -> m (MArray (PrimState m) r ix e)
- freeze :: (Mutable r ix e, PrimMonad m) => Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
- createArray_ :: (Mutable r ix e, PrimMonad m) => Comp -> Sz ix -> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
- createArray :: (Mutable r ix e, PrimMonad m) => Comp -> Sz ix -> (MArray (PrimState m) r ix e -> m a) -> m (a, Array r ix e)
- createArrayST_ :: Mutable r ix e => Comp -> Sz ix -> (forall s. MArray s r ix e -> ST s a) -> Array r ix e
- createArrayST :: Mutable r ix e => Comp -> Sz ix -> (forall s. MArray s r ix e -> ST s a) -> (a, Array r ix e)
- generateArray :: (Mutable r ix e, PrimMonad m) => Comp -> Sz ix -> (ix -> m e) -> m (Array r ix e)
- generateArrayIO :: Mutable r ix e => Comp -> Sz ix -> (ix -> IO e) -> IO (Array r ix e)
- unfoldlPrim_ :: (Mutable r ix e, PrimMonad m) => Comp -> Sz ix -> (a -> ix -> m (a, e)) -> a -> m (Array r ix e)
- unfoldlPrim :: (Mutable r ix e, PrimMonad m) => Comp -> Sz ix -> (a -> ix -> m (a, e)) -> a -> m (a, Array r ix e)
- withMArray :: (Mutable r ix e, PrimMonad m) => Array r ix e -> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e)
- withMArrayST :: Mutable r ix e => Array r ix e -> (forall s. MArray s r ix e -> ST s a) -> Array r ix e
- data RealWorld :: Type
- computeInto :: (Load r' ix e, Mutable r ix e) => MArray RealWorld r ix e -> Array r' ix e -> IO ()
Element-wise mutation
read :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m (Maybe e) Source #
O(1) - Lookup an element in the mutable array. Return Nothing
when index is out of bounds.
read' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m e Source #
O(1) - Same as read
, but throws an error if index is out of bounds.
write :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m Bool Source #
O(1) - Write an element into the cell of a mutable array. Returns False
when index is out
of bounds.
write' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m () Source #
O(1) - Same as write
, but throws an error if index is out of bounds.
modify :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m Bool Source #
O(1) - Modify an element in the cell of a mutable array with a supplied function. Returns
False
when index is out of bounds.
modify' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m () Source #
O(1) - Same as modify
, but throws an error if index is out of bounds.
swap :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m Bool Source #
O(1) - Swap two elements in a mutable array by supplying their indices. Returns False
when
either one of the indices is out of bounds.
swap' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m () Source #
O(1) - Same as swap
, but throws an error if index is out of bounds.
Operate over Array
class Manifest r ix e => Mutable r ix e Source #
Instances
data family MArray s r ix e :: * Source #
Instances
data MArray s U ix e Source # | |
Defined in Data.Massiv.Array.Manifest.Unboxed | |
data MArray s S ix e Source # | |
Defined in Data.Massiv.Array.Manifest.Storable | |
data MArray s P ix e Source # | |
Defined in Data.Massiv.Array.Manifest.Primitive | |
data MArray s B ix e Source # | |
Defined in Data.Massiv.Array.Manifest.Boxed | |
newtype MArray s N ix e Source # | |
Convert
new :: (Mutable r ix e, PrimMonad m) => Sz ix -> m (MArray (PrimState m) r ix e) Source #
Initialize a new mutable array. Negative size will result in an empty array.
thaw :: (Mutable r ix e, PrimMonad m) => Array r ix e -> m (MArray (PrimState m) r ix e) Source #
O(n) - Yield a mutable copy of the immutable array
freeze :: (Mutable r ix e, PrimMonad m) => Comp -> MArray (PrimState m) r ix e -> m (Array r ix e) Source #
O(n) - Yield an immutable copy of the mutable array
Create
:: (Mutable r ix e, PrimMonad m) | |
=> Comp | Computation strategy to use after |
-> Sz ix | Size of the newly created array |
-> (MArray (PrimState m) r ix e -> m a) | An action that should fill all elements of the brand new mutable array |
-> m (Array r ix e) |
Create a new array by supplying an action that will fill the new blank mutable array. Use
createArray
if you'd like to keep the result of the filling function.
Examples
>>>
createArray_ Seq (Sz1 2) (\ marr -> write marr 0 10 >> write marr 1 11) :: IO (Array P Ix1 Int)
(Array P Seq (2) [ 10,11 ])
Since: 0.2.6
:: (Mutable r ix e, PrimMonad m) | |
=> Comp | Computation strategy to use after |
-> Sz ix | Size of the newly created array |
-> (MArray (PrimState m) r ix e -> m a) | An action that should fill all elements of the brand new mutable array |
-> m (a, Array r ix e) |
Just like createArray_
, but together with Array
it returns the result of the filling action.
Since: 0.2.6
createArrayST_ :: Mutable r ix e => Comp -> Sz ix -> (forall s. MArray s r ix e -> ST s a) -> Array r ix e Source #
Just like createArray_
, but restricted to ST
.
Since: 0.2.6
createArrayST :: Mutable r ix e => Comp -> Sz ix -> (forall s. MArray s r ix e -> ST s a) -> (a, Array r ix e) Source #
Just like createArray
, but restricted to ST
.
Since: 0.2.6
Generate
:: (Mutable r ix e, PrimMonad m) | |
=> Comp | Computation strategy (ingored during generation) |
-> Sz ix | Resulting size of the array |
-> (ix -> m e) | Element producing generator |
-> m (Array r ix e) |
Sequentially generate a pure array. Much like makeArray
creates a pure array this function
will use Mutable
interface to generate a pure Array
in the end, except that computation
strategy is ignored. Element producing function no longer has to be pure but is a stateful
action, since it is restricted to PrimMonad
and allows for sharing the state between
computation of each element, which could be arbitrary effects if that monad is IO
.
Examples
>>>
import Data.IORef
>>>
ref <- newIORef (0 :: Int)
>>>
generateArray Seq (Sz1 6) (\ i -> modifyIORef' ref (+i) >> print i >> pure i) :: IO (Array U Ix1 Int)
0 1 2 3 4 5 (Array U Seq (6) [ 0,1,2,3,4,5 ])>>>
readIORef ref
15
Since: 0.2.6
generateArrayIO :: Mutable r ix e => Comp -> Sz ix -> (ix -> IO e) -> IO (Array r ix e) Source #
Just like generateArray
, except this generator will respect the supplied computation
strategy, and for that reason it is restricted to IO
.
Since: 0.2.6
Unfold
:: (Mutable r ix e, PrimMonad m) | |
=> Comp | Computation strategy (ignored during initial creation) |
-> Sz ix | Size of the desired array |
-> (a -> ix -> m (a, e)) | Unfolding action |
-> a | Initial accumulator |
-> m (Array r ix e) |
Sequentially unfold an array from the left.
Examples
Create an array with Fibonacci numbers while performing and IO
action on the accumulator for
each element of the array.
>>>
unfoldlPrim_ Seq (Ix1 10) (\a@(f0, f1) _ -> let fn = f0 + f1 in print a >> return ((f1, fn), f0)) (0, 1) :: IO (Array P Ix1 Int)
(0,1) (1,1) (1,2) (2,3) (3,5) (5,8) (8,13) (13,21) (21,34) (34,55) (Array P Seq (10) [ 0,1,1,2,3,5,8,13,21,34 ])
Since: 0.2.6
:: (Mutable r ix e, PrimMonad m) | |
=> Comp | Computation strategy (ignored during initial creation) |
-> Sz ix | Size of the desired array |
-> (a -> ix -> m (a, e)) | Unfolding action |
-> a | Initial accumulator |
-> m (a, Array r ix e) |
Just like unfoldlPrim_
, but also returns the final value of the accumulator.
Since: 0.2.6
Modify
withMArray :: (Mutable r ix e, PrimMonad m) => Array r ix e -> (MArray (PrimState m) r ix e -> m a) -> m (Array r ix e) Source #
Create a copy of a pure array, mutate it in place and return its frozen version.
Since: 0.2.2
withMArrayST :: Mutable r ix e => Array r ix e -> (forall s. MArray s r ix e -> ST s a) -> Array r ix e Source #
Same as withMArray
but in ST
.
Since: 0.2.2
Computation
RealWorld
is deeply magical. It is primitive, but it is not
unlifted (hence ptrArg
). We never manipulate values of type
RealWorld
; it's only used in the type system, to parameterise State#
.