{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Data.Vector.Generic.Mutable (
MVector(..),
length, null,
slice, init, tail, take, drop, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
overlaps,
new, unsafeNew, replicate, replicateM, generate, generateM, clone,
grow, unsafeGrow,
growFront, unsafeGrowFront,
clear,
read, readMaybe, write, modify, modifyM, swap, exchange,
unsafeRead, unsafeWrite, unsafeModify, unsafeModifyM, unsafeSwap, unsafeExchange,
mapM_, imapM_, forM_, iforM_,
foldl, foldl', foldM, foldM',
foldr, foldr', foldrM, foldrM',
ifoldl, ifoldl', ifoldM, ifoldM',
ifoldr, ifoldr', ifoldrM, ifoldrM',
nextPermutation,
set, copy, move, unsafeCopy, unsafeMove,
mstream, mstreamR,
unstream, unstreamR, vunstream,
munstream, munstreamR,
transform, transformR,
fill, fillR,
unsafeAccum, accum, unsafeUpdate, update, reverse,
unstablePartition, unstablePartitionBundle, partitionBundle,
partitionWithBundle,
PrimMonad, PrimState, RealWorld
) where
import Data.Vector.Generic.Mutable.Base
import qualified Data.Vector.Generic.Base as V
import qualified Data.Vector.Fusion.Bundle as Bundle
import Data.Vector.Fusion.Bundle ( Bundle, MBundle, Chunk(..) )
import qualified Data.Vector.Fusion.Bundle.Monadic as MBundle
import Data.Vector.Fusion.Stream.Monadic ( Stream )
import qualified Data.Vector.Fusion.Stream.Monadic as Stream
import Data.Vector.Fusion.Bundle.Size
import Data.Vector.Fusion.Util ( delay_inline )
import Data.Vector.Internal.Check
import Control.Monad.Primitive ( PrimMonad(..), RealWorld, stToPrim )
import Prelude
( Ord, Monad, Bool(..), Int, Maybe(..), Either(..)
, return, otherwise, flip, const, seq, min, max, not, pure
, (>>=), (+), (-), (<), (<=), (>=), (==), (/=), (.), ($), (=<<), (>>), (<$>) )
#include "vector.h"
unsafeAppend1 :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
{-# INLINE_INNER unsafeAppend1 #-}
unsafeAppend1 :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
unsafeAppend1 v (PrimState m) a
v Int
i a
x
| Int
i forall a. Ord a => a -> a -> Bool
< forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v = do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v
| Bool
otherwise = do
v (PrimState m) a
v' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a)
enlarge v (PrimState m) a
v
forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Internal Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v') forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v' Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v'
unsafePrepend1 :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> Int -> a -> m (v (PrimState m) a, Int)
{-# INLINE_INNER unsafePrepend1 #-}
unsafePrepend1 :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a, Int)
unsafePrepend1 v (PrimState m) a
v Int
i a
x
| Int
i forall a. Eq a => a -> a -> Bool
/= Int
0 = do
let i' :: Int
i' = Int
iforall a. Num a => a -> a -> a
-Int
1
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i' a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v, Int
i')
| Bool
otherwise = do
(v (PrimState m) a
v', Int
j) <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a, Int)
enlargeFront v (PrimState m) a
v
let i' :: Int
i' = Int
jforall a. Num a => a -> a -> a
-Int
1
forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Internal Int
i' (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v') forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v' Int
i' a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v', Int
i')
mstream :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Stream m a
{-# INLINE mstream #-}
mstream :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a
mstream v (PrimState m) a
v = v (PrimState m) a
v seq :: forall a b. a -> b -> b
`seq` Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
Stream.unfoldrM Int -> m (Maybe (a, Int))
get Int
0
where
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
{-# INLINE_INNER get #-}
get :: Int -> m (Maybe (a, Int))
get Int
i | Int
i forall a. Ord a => a -> a -> Bool
< Int
n = do a
x <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (a
x, Int
iforall a. Num a => a -> a -> a
+Int
1)
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
fill :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
{-# INLINE fill #-}
fill :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
fill v (PrimState m) a
v Stream m a
s = v (PrimState m) a
v seq :: forall a b. a -> b -> b
`seq` do
Int
n' <- forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
Stream.foldM Int -> a -> m Int
put Int
0 Stream m a
s
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n' v (PrimState m) a
v
where
{-# INLINE_INNER put #-}
put :: Int -> a -> m Int
put Int
i a
x = do
forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Internal Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iforall a. Num a => a -> a -> a
+Int
1)
transform
:: (PrimMonad m, MVector v a)
=> (Stream m a -> Stream m a) -> v (PrimState m) a -> m (v (PrimState m) a)
{-# INLINE_FUSED transform #-}
transform :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
(Stream m a -> Stream m a)
-> v (PrimState m) a -> m (v (PrimState m) a)
transform Stream m a -> Stream m a
f v (PrimState m) a
v = forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
fill v (PrimState m) a
v (Stream m a -> Stream m a
f (forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a
mstream v (PrimState m) a
v))
mstreamR :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Stream m a
{-# INLINE mstreamR #-}
mstreamR :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a
mstreamR v (PrimState m) a
v = v (PrimState m) a
v seq :: forall a b. a -> b -> b
`seq` Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
Stream.unfoldrM Int -> m (Maybe (a, Int))
get Int
n
where
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
{-# INLINE_INNER get #-}
get :: Int -> m (Maybe (a, Int))
get Int
i | Int
j forall a. Ord a => a -> a -> Bool
>= Int
0 = do a
x <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
j
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (a
x,Int
j)
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
where
j :: Int
j = Int
iforall a. Num a => a -> a -> a
-Int
1
fillR :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
{-# INLINE fillR #-}
fillR :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
fillR v (PrimState m) a
v Stream m a
s = v (PrimState m) a
v seq :: forall a b. a -> b -> b
`seq` do
Int
i <- forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
Stream.foldM Int -> a -> m Int
put Int
n Stream m a
s
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i (Int
nforall a. Num a => a -> a -> a
-Int
i) v (PrimState m) a
v
where
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
{-# INLINE_INNER put #-}
put :: Int -> a -> m Int
put Int
i a
x = do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
j a
x
forall (m :: * -> *) a. Monad m => a -> m a
return Int
j
where
j :: Int
j = Int
iforall a. Num a => a -> a -> a
-Int
1
transformR
:: (PrimMonad m, MVector v a)
=> (Stream m a -> Stream m a) -> v (PrimState m) a -> m (v (PrimState m) a)
{-# INLINE_FUSED transformR #-}
transformR :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
(Stream m a -> Stream m a)
-> v (PrimState m) a -> m (v (PrimState m) a)
transformR Stream m a -> Stream m a
f v (PrimState m) a
v = forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
fillR v (PrimState m) a
v (Stream m a -> Stream m a
f (forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a
mstreamR v (PrimState m) a
v))
unstream :: (PrimMonad m, MVector v a)
=> Bundle u a -> m (v (PrimState m) a)
{-# INLINE_FUSED unstream #-}
unstream :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
Bundle u a -> m (v (PrimState m) a)
unstream Bundle u a
s = forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstream (forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
Bundle.lift Bundle u a
s)
munstream :: (PrimMonad m, MVector v a)
=> MBundle m u a -> m (v (PrimState m) a)
{-# INLINE_FUSED munstream #-}
munstream :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstream MBundle m u a
s = case Size -> Maybe Int
upperBound (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
MBundle.size MBundle m u a
s) of
Just Int
n -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> Int -> m (v (PrimState m) a)
munstreamMax MBundle m u a
s Int
n
Maybe Int
Nothing -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstreamUnknown MBundle m u a
s
munstreamMax :: (PrimMonad m, MVector v a)
=> MBundle m u a -> Int -> m (v (PrimState m) a)
{-# INLINE munstreamMax #-}
munstreamMax :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> Int -> m (v (PrimState m) a)
munstreamMax MBundle m u a
s Int
n
= do
v (PrimState m) a
v <- forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Internal Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
let put :: Int -> a -> m Int
put Int
i a
x = do
forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Internal Int
i Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iforall a. Num a => a -> a -> a
+Int
1)
Int
n' <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
MBundle.foldM' Int -> a -> m Int
put Int
0 MBundle m u a
s
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n' Int
n
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n' v (PrimState m) a
v
munstreamUnknown :: (PrimMonad m, MVector v a)
=> MBundle m u a -> m (v (PrimState m) a)
{-# INLINE munstreamUnknown #-}
munstreamUnknown :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstreamUnknown MBundle m u a
s
= do
v (PrimState m) a
v <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
0
(v (PrimState m) a
v', Int
n) <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
MBundle.foldM forall {m :: * -> *} {v :: * -> * -> *} {a}.
(PrimMonad m, MVector v a) =>
(v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int)
put (v (PrimState m) a
v, Int
0) MBundle m u a
s
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v')
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n v (PrimState m) a
v'
where
{-# INLINE_INNER put #-}
put :: (v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int)
put (v (PrimState m) a
v,Int
i) a
x = do
v (PrimState m) a
v' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
unsafeAppend1 v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v',Int
iforall a. Num a => a -> a -> a
+Int
1)
vunstream :: (PrimMonad m, V.Vector v a)
=> Bundle v a -> m (V.Mutable v (PrimState m) a)
{-# INLINE_FUSED vunstream #-}
vunstream :: forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Bundle v a -> m (Mutable v (PrimState m) a)
vunstream Bundle v a
s = forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
MBundle m v a -> m (Mutable v (PrimState m) a)
vmunstream (forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
Bundle.lift Bundle v a
s)
vmunstream :: (PrimMonad m, V.Vector v a)
=> MBundle m v a -> m (V.Mutable v (PrimState m) a)
{-# INLINE_FUSED vmunstream #-}
vmunstream :: forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
MBundle m v a -> m (Mutable v (PrimState m) a)
vmunstream MBundle m v a
s = case Size -> Maybe Int
upperBound (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
MBundle.size MBundle m v a
s) of
Just Int
n -> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
MBundle m v a -> Int -> m (Mutable v (PrimState m) a)
vmunstreamMax MBundle m v a
s Int
n
Maybe Int
Nothing -> forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
MBundle m v a -> m (Mutable v (PrimState m) a)
vmunstreamUnknown MBundle m v a
s
vmunstreamMax :: (PrimMonad m, V.Vector v a)
=> MBundle m v a -> Int -> m (V.Mutable v (PrimState m) a)
{-# INLINE vmunstreamMax #-}
vmunstreamMax :: forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
MBundle m v a -> Int -> m (Mutable v (PrimState m) a)
vmunstreamMax MBundle m v a
s Int
n
= do
Mutable v (PrimState m) a
v <- forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Internal Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
let {-# INLINE_INNER copyChunk #-}
copyChunk :: Int -> Chunk v a -> m Int
copyChunk Int
i (Chunk Int
m forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
f) =
forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
i Int
m (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length Mutable v (PrimState m) a
v) forall a b. (a -> b) -> a -> b
$ do
forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
f (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice Int
i Int
m Mutable v (PrimState m) a
v)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iforall a. Num a => a -> a -> a
+Int
m)
Int
n' <- forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
Stream.foldlM' Int -> Chunk v a -> m Int
copyChunk Int
0 (forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
MBundle.chunks MBundle m v a
s)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n' Int
n
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n' Mutable v (PrimState m) a
v
vmunstreamUnknown :: (PrimMonad m, V.Vector v a)
=> MBundle m v a -> m (V.Mutable v (PrimState m) a)
{-# INLINE vmunstreamUnknown #-}
vmunstreamUnknown :: forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
MBundle m v a -> m (Mutable v (PrimState m) a)
vmunstreamUnknown MBundle m v a
s
= do
Mutable v (PrimState m) a
v <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
0
(Mutable v (PrimState m) a
v', Int
n) <- forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
Stream.foldlM forall {m :: * -> *} {v :: * -> *} {a}.
(PrimMonad m, Vector v a) =>
(Mutable v (PrimState m) a, Int)
-> Chunk v a -> m (Mutable v (PrimState m) a, Int)
copyChunk (Mutable v (PrimState m) a
v,Int
0) (forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
MBundle.chunks MBundle m v a
s)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length Mutable v (PrimState m) a
v')
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n Mutable v (PrimState m) a
v'
where
{-# INLINE_INNER copyChunk #-}
copyChunk :: (Mutable v (PrimState m) a, Int)
-> Chunk v a -> m (Mutable v (PrimState m) a, Int)
copyChunk (Mutable v (PrimState m) a
v,Int
i) (Chunk Int
n forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
f)
= do
let j :: Int
j = Int
iforall a. Num a => a -> a -> a
+Int
n
Mutable v (PrimState m) a
v' <- if forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength Mutable v (PrimState m) a
v forall a. Ord a => a -> a -> Bool
< Int
j
then forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrow Mutable v (PrimState m) a
v (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
enlarge_delta Mutable v (PrimState m) a
v) (Int
j forall a. Num a => a -> a -> a
- forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength Mutable v (PrimState m) a
v))
else forall (m :: * -> *) a. Monad m => a -> m a
return Mutable v (PrimState m) a
v
forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
i Int
n (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length Mutable v (PrimState m) a
v') forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
f (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice Int
i Int
n Mutable v (PrimState m) a
v')
forall (m :: * -> *) a. Monad m => a -> m a
return (Mutable v (PrimState m) a
v',Int
j)
unstreamR :: (PrimMonad m, MVector v a)
=> Bundle u a -> m (v (PrimState m) a)
{-# INLINE_FUSED unstreamR #-}
unstreamR :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
Bundle u a -> m (v (PrimState m) a)
unstreamR Bundle u a
s = forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstreamR (forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
Bundle.lift Bundle u a
s)
munstreamR :: (PrimMonad m, MVector v a)
=> MBundle m u a -> m (v (PrimState m) a)
{-# INLINE_FUSED munstreamR #-}
munstreamR :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstreamR MBundle m u a
s = case Size -> Maybe Int
upperBound (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
MBundle.size MBundle m u a
s) of
Just Int
n -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> Int -> m (v (PrimState m) a)
munstreamRMax MBundle m u a
s Int
n
Maybe Int
Nothing -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(HasCallStack, PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstreamRUnknown MBundle m u a
s
munstreamRMax :: (PrimMonad m, MVector v a)
=> MBundle m u a -> Int -> m (v (PrimState m) a)
{-# INLINE munstreamRMax #-}
munstreamRMax :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> Int -> m (v (PrimState m) a)
munstreamRMax MBundle m u a
s Int
n
= do
v (PrimState m) a
v <- forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Internal Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
let put :: Int -> a -> m Int
put Int
i a
x = do
let i' :: Int
i' = Int
iforall a. Num a => a -> a -> a
-Int
1
forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Internal Int
i' Int
n
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i' a
x
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i'
Int
i <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
MBundle.foldM' Int -> a -> m Int
put Int
n MBundle m u a
s
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
i (Int
nforall a. Num a => a -> a -> a
-Int
i) Int
n
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i (Int
nforall a. Num a => a -> a -> a
-Int
i) v (PrimState m) a
v
munstreamRUnknown :: (HasCallStack, PrimMonad m, MVector v a)
=> MBundle m u a -> m (v (PrimState m) a)
{-# INLINE munstreamRUnknown #-}
munstreamRUnknown :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(HasCallStack, PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstreamRUnknown MBundle m u a
s
= do
v (PrimState m) a
v <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
0
(v (PrimState m) a
v', Int
i) <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
MBundle.foldM forall {m :: * -> *} {v :: * -> * -> *} {a}.
(PrimMonad m, MVector v a) =>
(v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int)
put (v (PrimState m) a
v, Int
0) MBundle m u a
s
let n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v'
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
i (Int
nforall a. Num a => a -> a -> a
-Int
i) Int
n
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i (Int
nforall a. Num a => a -> a -> a
-Int
i) v (PrimState m) a
v'
where
{-# INLINE_INNER put #-}
put :: (v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int)
put (v (PrimState m) a
v,Int
i) a
x = forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a, Int)
unsafePrepend1 v (PrimState m) a
v Int
i a
x
length :: MVector v a => v s a -> Int
{-# INLINE length #-}
length :: forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength
null :: MVector v a => v s a -> Bool
{-# INLINE null #-}
null :: forall (v :: * -> * -> *) a s. MVector v a => v s a -> Bool
null v s a
v = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v forall a. Eq a => a -> a -> Bool
== Int
0
slice :: (HasCallStack, MVector v a)
=> Int
-> Int
-> v s a
-> v s a
{-# INLINE slice #-}
slice :: forall (v :: * -> * -> *) a s.
(HasCallStack, MVector v a) =>
Int -> Int -> v s a -> v s a
slice Int
i Int
n v s a
v = forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Bounds Int
i Int
n (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v) forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i Int
n v s a
v
take :: MVector v a => Int -> v s a -> v s a
{-# INLINE take #-}
take :: forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
take Int
n v s a
v = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 (forall a. Ord a => a -> a -> a
min (forall a. Ord a => a -> a -> a
max Int
n Int
0) (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v)) v s a
v
drop :: MVector v a => Int -> v s a -> v s a
{-# INLINE drop #-}
drop :: forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
drop Int
n v s a
v = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice (forall a. Ord a => a -> a -> a
min Int
m Int
n') (forall a. Ord a => a -> a -> a
max Int
0 (Int
m forall a. Num a => a -> a -> a
- Int
n')) v s a
v
where
n' :: Int
n' = forall a. Ord a => a -> a -> a
max Int
n Int
0
m :: Int
m = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v
splitAt :: MVector v a => Int -> v s a -> (v s a, v s a)
{-# INLINE splitAt #-}
splitAt :: forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> v s a -> (v s a, v s a)
splitAt Int
n v s a
v = ( forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
m v s a
v
, forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
m (forall a. Ord a => a -> a -> a
max Int
0 (Int
len forall a. Num a => a -> a -> a
- Int
n')) v s a
v
)
where
m :: Int
m = forall a. Ord a => a -> a -> a
min Int
n' Int
len
n' :: Int
n' = forall a. Ord a => a -> a -> a
max Int
n Int
0
len :: Int
len = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v
init :: MVector v a => v s a -> v s a
{-# INLINE init #-}
init :: forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
init v s a
v = forall (v :: * -> * -> *) a s.
(HasCallStack, MVector v a) =>
Int -> Int -> v s a -> v s a
slice Int
0 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v forall a. Num a => a -> a -> a
- Int
1) v s a
v
tail :: MVector v a => v s a -> v s a
{-# INLINE tail #-}
tail :: forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
tail v s a
v = forall (v :: * -> * -> *) a s.
(HasCallStack, MVector v a) =>
Int -> Int -> v s a -> v s a
slice Int
1 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v forall a. Num a => a -> a -> a
- Int
1) v s a
v
unsafeSlice :: MVector v a => Int
-> Int
-> v s a
-> v s a
{-# INLINE unsafeSlice #-}
unsafeSlice :: forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i Int
n v s a
v = forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Unsafe Int
i Int
n (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v)
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice Int
i Int
n v s a
v
unsafeInit :: MVector v a => v s a -> v s a
{-# INLINE unsafeInit #-}
unsafeInit :: forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
unsafeInit v s a
v = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v forall a. Num a => a -> a -> a
- Int
1) v s a
v
unsafeTail :: MVector v a => v s a -> v s a
{-# INLINE unsafeTail #-}
unsafeTail :: forall (v :: * -> * -> *) a s. MVector v a => v s a -> v s a
unsafeTail v s a
v = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
1 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v forall a. Num a => a -> a -> a
- Int
1) v s a
v
unsafeTake :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeTake #-}
unsafeTake :: forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
unsafeTake Int
n v s a
v = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n v s a
v
unsafeDrop :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeDrop #-}
unsafeDrop :: forall (v :: * -> * -> *) a s. MVector v a => Int -> v s a -> v s a
unsafeDrop Int
n v s a
v = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
n (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v forall a. Num a => a -> a -> a
- Int
n) v s a
v
overlaps :: MVector v a => v s a -> v s a -> Bool
{-# INLINE overlaps #-}
overlaps :: forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
overlaps = forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
basicOverlaps
new :: (HasCallStack, PrimMonad m, MVector v a) => Int -> m (v (PrimState m) a)
{-# INLINE new #-}
new :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
new Int
n = forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Bounds Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \v (PrimState m) a
v -> forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
basicInitialize v (PrimState m) a
v forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v
unsafeNew :: (PrimMonad m, MVector v a) => Int -> m (v (PrimState m) a)
{-# INLINE unsafeNew #-}
unsafeNew :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n = forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Unsafe Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s. MVector v a => Int -> ST s (v s a)
basicUnsafeNew Int
n
replicate :: (PrimMonad m, MVector v a) => Int -> a -> m (v (PrimState m) a)
{-# INLINE replicate #-}
replicate :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> a -> m (v (PrimState m) a)
replicate Int
n a
x = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> a -> ST s (v s a)
basicUnsafeReplicate (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
0 Int
n) a
x
replicateM :: (PrimMonad m, MVector v a) => Int -> m a -> m (v (PrimState m) a)
{-# INLINE replicateM #-}
replicateM :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m a -> m (v (PrimState m) a)
replicateM Int
n m a
m = forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstream (forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> m a -> Bundle m v a
MBundle.replicateM Int
n m a
m)
generate :: (PrimMonad m, MVector v a) => Int -> (Int -> a) -> m (v (PrimState m) a)
{-# INLINE generate #-}
generate :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> (Int -> a) -> m (v (PrimState m) a)
generate Int
n Int -> a
f = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> (Int -> m a) -> m (v (PrimState m) a)
generateM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
f)
generateM :: (PrimMonad m, MVector v a) => Int -> (Int -> m a) -> m (v (PrimState m) a)
{-# INLINE generateM #-}
generateM :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> (Int -> m a) -> m (v (PrimState m) a)
generateM Int
n Int -> m a
f = forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstream (forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> (Int -> m a) -> Bundle m v a
MBundle.generateM Int
n Int -> m a
f)
clone :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m (v (PrimState m) a)
{-# INLINE clone #-}
clone :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a)
clone v (PrimState m) a
v = do
v (PrimState m) a
v' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
unsafeCopy v (PrimState m) a
v' v (PrimState m) a
v
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v'
grow :: (HasCallStack, PrimMonad m, MVector v a)
=> v (PrimState m) a -> Int -> m (v (PrimState m) a)
{-# INLINE grow #-}
grow :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
grow v (PrimState m) a
v Int
by = forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Bounds Int
by
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim
forall a b. (a -> b) -> a -> b
$ do v (PrimState m) a
vnew <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrow v (PrimState m) a
v Int
by
forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
basicInitialize forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) Int
by v (PrimState m) a
vnew
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
vnew
growFront :: (HasCallStack, PrimMonad m, MVector v a)
=> v (PrimState m) a -> Int -> m (v (PrimState m) a)
{-# INLINE growFront #-}
growFront :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
growFront v (PrimState m) a
v Int
by = forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Bounds Int
by
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim
forall a b. (a -> b) -> a -> b
$ do v (PrimState m) a
vnew <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrowFront v (PrimState m) a
v Int
by
forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
basicInitialize forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice Int
0 Int
by v (PrimState m) a
vnew
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
vnew
enlarge_delta :: MVector v a => v s a -> Int
enlarge_delta :: forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
enlarge_delta v s a
v = forall a. Ord a => a -> a -> a
max (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v) Int
1
enlarge :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> m (v (PrimState m) a)
{-# INLINE enlarge #-}
enlarge :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a)
enlarge v (PrimState m) a
v = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ do
v (PrimState m) a
vnew <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrow v (PrimState m) a
v Int
by
forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
basicInitialize forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) Int
by v (PrimState m) a
vnew
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
vnew
where
by :: Int
by = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
enlarge_delta v (PrimState m) a
v
enlargeFront :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> m (v (PrimState m) a, Int)
{-# INLINE enlargeFront #-}
enlargeFront :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a, Int)
enlargeFront v (PrimState m) a
v = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ do
v (PrimState m) a
v' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrowFront v (PrimState m) a
v Int
by
forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
basicInitialize forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice Int
0 Int
by v (PrimState m) a
v'
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v', Int
by)
where
by :: Int
by = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
enlarge_delta v (PrimState m) a
v
unsafeGrow
:: (PrimMonad m, MVector v a)
=> v (PrimState m) a
-> Int
-> m (v (PrimState m) a)
{-# INLINE unsafeGrow #-}
unsafeGrow :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrow v (PrimState m) a
v Int
n = forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Unsafe Int
n
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s (v s a)
basicUnsafeGrow v (PrimState m) a
v Int
n
unsafeGrowFront :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> Int -> m (v (PrimState m) a)
{-# INLINE unsafeGrowFront #-}
unsafeGrowFront :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrowFront v (PrimState m) a
v Int
by = forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Unsafe Int
by forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ do
let n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
v (PrimState m) a
v' <- forall (v :: * -> * -> *) a s. MVector v a => Int -> ST s (v s a)
basicUnsafeNew (Int
byforall a. Num a => a -> a -> a
+Int
n)
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
basicUnsafeCopy (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice Int
by Int
n v (PrimState m) a
v') v (PrimState m) a
v
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v'
clear :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m ()
{-# INLINE clear #-}
clear :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
clear = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> * -> *) a s. MVector v a => v s a -> ST s ()
basicClear
read :: (HasCallStack, PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m a
{-# INLINE read #-}
read :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
read v (PrimState m) a
v Int
i = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
readMaybe :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m (Maybe a)
{-# INLINE readMaybe #-}
readMaybe :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m (Maybe a)
readMaybe v (PrimState m) a
v Int
i | Int
i Int -> Int -> Bool
`inRange` (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) = forall a. a -> Maybe a
Just forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
| Bool
otherwise = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
write :: (HasCallStack, PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m ()
{-# INLINE write #-}
write :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
write v (PrimState m) a
v Int
i a
x = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
modify :: (HasCallStack, PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> a) -> Int -> m ()
{-# INLINE modify #-}
modify :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
modify v (PrimState m) a
v a -> a
f Int
i = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify v (PrimState m) a
v a -> a
f Int
i
modifyM :: (HasCallStack, PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> m a) -> Int -> m ()
{-# INLINE modifyM #-}
modifyM :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> m a) -> Int -> m ()
modifyM v (PrimState m) a
v a -> m a
f Int
i = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> m a) -> Int -> m ()
unsafeModifyM v (PrimState m) a
v a -> m a
f Int
i
swap :: (HasCallStack, PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> Int -> m ()
{-# INLINE swap #-}
swap :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
swap v (PrimState m) a
v Int
i Int
j = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
j (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
unsafeSwap v (PrimState m) a
v Int
i Int
j
exchange :: (HasCallStack, PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m a
{-# INLINE exchange #-}
exchange :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m a
exchange v (PrimState m) a
v Int
i a
x = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m a
unsafeExchange v (PrimState m) a
v Int
i a
x
unsafeRead :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m a
{-# INLINE unsafeRead #-}
unsafeRead :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
basicUnsafeRead v (PrimState m) a
v Int
i
unsafeWrite :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m ()
{-# INLINE unsafeWrite #-}
unsafeWrite :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
basicUnsafeWrite v (PrimState m) a
v Int
i a
x
unsafeModify :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> a) -> Int -> m ()
{-# INLINE unsafeModify #-}
unsafeModify :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify v (PrimState m) a
v a -> a
f Int
i = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim
forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
basicUnsafeRead v (PrimState m) a
v Int
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x ->
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
basicUnsafeWrite v (PrimState m) a
v Int
i (a -> a
f a
x)
unsafeModifyM :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> m a) -> Int -> m ()
{-# INLINE unsafeModifyM #-}
unsafeModifyM :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> m a) -> Int -> m ()
unsafeModifyM v (PrimState m) a
v a -> m a
f Int
i = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
basicUnsafeWrite v (PrimState m) a
v Int
i forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< a -> m a
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> ST s a
basicUnsafeRead v (PrimState m) a
v Int
i)
unsafeSwap :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> Int -> m ()
{-# INLINE unsafeSwap #-}
unsafeSwap :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
unsafeSwap v (PrimState m) a
v Int
i Int
j = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
j (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ do
a
x <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
a
y <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
j
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
y
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
j a
x
unsafeExchange :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m a
{-# INLINE unsafeExchange #-}
unsafeExchange :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m a
unsafeExchange v (PrimState m) a
v Int
i a
x = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ do
a
y <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return a
y
forI_ :: (Monad m, MVector v a) => v (PrimState m) a -> (Int -> m b) -> m ()
{-# INLINE forI_ #-}
forI_ :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(Monad m, MVector v a) =>
v (PrimState m) a -> (Int -> m b) -> m ()
forI_ v (PrimState m) a
v Int -> m b
f = Int -> m ()
loop Int
0
where
loop :: Int -> m ()
loop Int
i | Int
i forall a. Ord a => a -> a -> Bool
>= Int
n = forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = Int -> m b
f Int
i forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> m ()
loop (Int
i forall a. Num a => a -> a -> a
+ Int
1)
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
mapM_ :: (PrimMonad m, MVector v a) => (a -> m b) -> v (PrimState m) a -> m ()
{-# INLINE mapM_ #-}
mapM_ :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(a -> m b) -> v (PrimState m) a -> m ()
mapM_ a -> m b
f v (PrimState m) a
v = forall (m :: * -> *) (v :: * -> * -> *) a b.
(Monad m, MVector v a) =>
v (PrimState m) a -> (Int -> m b) -> m ()
forI_ v (PrimState m) a
v forall a b. (a -> b) -> a -> b
$ \Int
i -> a -> m b
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
imapM_ :: (PrimMonad m, MVector v a) => (Int -> a -> m b) -> v (PrimState m) a -> m ()
{-# INLINE imapM_ #-}
imapM_ :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> m b) -> v (PrimState m) a -> m ()
imapM_ Int -> a -> m b
f v (PrimState m) a
v = forall (m :: * -> *) (v :: * -> * -> *) a b.
(Monad m, MVector v a) =>
v (PrimState m) a -> (Int -> m b) -> m ()
forI_ v (PrimState m) a
v forall a b. (a -> b) -> a -> b
$ \Int
i -> Int -> a -> m b
f Int
i forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
forM_ :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> m b) -> m ()
{-# INLINE forM_ #-}
forM_ :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> m b) -> m ()
forM_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(a -> m b) -> v (PrimState m) a -> m ()
mapM_
iforM_ :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (Int -> a -> m b) -> m ()
{-# INLINE iforM_ #-}
iforM_ :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> (Int -> a -> m b) -> m ()
iforM_ = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> m b) -> v (PrimState m) a -> m ()
imapM_
foldl :: (PrimMonad m, MVector v a) => (b -> a -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldl #-}
foldl :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> a -> b) -> b -> v (PrimState m) a -> m b
foldl b -> a -> b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b
ifoldl (\b
b Int
_ -> b -> a -> b
f b
b)
foldl' :: (PrimMonad m, MVector v a) => (b -> a -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldl' #-}
foldl' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> a -> b) -> b -> v (PrimState m) a -> m b
foldl' b -> a -> b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b
ifoldl' (\b
b Int
_ -> b -> a -> b
f b
b)
ifoldl :: (PrimMonad m, MVector v a) => (b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldl #-}
ifoldl :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b
ifoldl b -> Int -> a -> b
f b
b0 v (PrimState m) a
v = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
ifoldM (\b
b Int
i a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ b -> Int -> a -> b
f b
b Int
i a
a) b
b0 v (PrimState m) a
v
ifoldl' :: (PrimMonad m, MVector v a) => (b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldl' #-}
ifoldl' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> b) -> b -> v (PrimState m) a -> m b
ifoldl' b -> Int -> a -> b
f b
b0 v (PrimState m) a
v = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
ifoldM' (\b
b Int
i a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ b -> Int -> a -> b
f b
b Int
i a
a) b
b0 v (PrimState m) a
v
foldr :: (PrimMonad m, MVector v a) => (a -> b -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldr #-}
foldr :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(a -> b -> b) -> b -> v (PrimState m) a -> m b
foldr a -> b -> b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b
ifoldr (forall a b. a -> b -> a
const a -> b -> b
f)
foldr' :: (PrimMonad m, MVector v a) => (a -> b -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldr' #-}
foldr' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(a -> b -> b) -> b -> v (PrimState m) a -> m b
foldr' a -> b -> b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b
ifoldr' (forall a b. a -> b -> a
const a -> b -> b
f)
ifoldr :: (PrimMonad m, MVector v a) => (Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldr #-}
ifoldr :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b
ifoldr Int -> a -> b -> b
f b
b0 v (PrimState m) a
v = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
ifoldrM (\Int
i a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Int -> a -> b -> b
f Int
i a
a b
b) b
b0 v (PrimState m) a
v
ifoldr' :: (PrimMonad m, MVector v a) => (Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldr' #-}
ifoldr' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> b) -> b -> v (PrimState m) a -> m b
ifoldr' Int -> a -> b -> b
f b
b0 v (PrimState m) a
v = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
ifoldrM' (\Int
i a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Int -> a -> b -> b
f Int
i a
a b
b) b
b0 v (PrimState m) a
v
foldM :: (PrimMonad m, MVector v a) => (b -> a -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldM #-}
foldM :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> a -> m b) -> b -> v (PrimState m) a -> m b
foldM b -> a -> m b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
ifoldM (\b
x Int
_ -> b -> a -> m b
f b
x)
foldM' :: (PrimMonad m, MVector v a) => (b -> a -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldM' #-}
foldM' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> a -> m b) -> b -> v (PrimState m) a -> m b
foldM' b -> a -> m b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
ifoldM' (\b
x Int
_ -> b -> a -> m b
f b
x)
ifoldM :: (PrimMonad m, MVector v a) => (b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldM #-}
ifoldM :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
ifoldM b -> Int -> a -> m b
f b
b0 v (PrimState m) a
v = Int -> b -> m b
loop Int
0 b
b0
where
loop :: Int -> b -> m b
loop Int
i b
b | Int
i forall a. Ord a => a -> a -> Bool
>= Int
n = forall (m :: * -> *) a. Monad m => a -> m a
return b
b
| Bool
otherwise = do a
a <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
Int -> b -> m b
loop (Int
i forall a. Num a => a -> a -> a
+ Int
1) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< b -> Int -> a -> m b
f b
b Int
i a
a
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
ifoldM' :: (PrimMonad m, MVector v a) => (b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldM' #-}
ifoldM' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(b -> Int -> a -> m b) -> b -> v (PrimState m) a -> m b
ifoldM' b -> Int -> a -> m b
f b
b0 v (PrimState m) a
v = Int -> b -> m b
loop Int
0 b
b0
where
loop :: Int -> b -> m b
loop Int
i !b
b | Int
i forall a. Ord a => a -> a -> Bool
>= Int
n = forall (m :: * -> *) a. Monad m => a -> m a
return b
b
| Bool
otherwise = do a
a <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
Int -> b -> m b
loop (Int
i forall a. Num a => a -> a -> a
+ Int
1) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< b -> Int -> a -> m b
f b
b Int
i a
a
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
foldrM :: (PrimMonad m, MVector v a) => (a -> b -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldrM #-}
foldrM :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(a -> b -> m b) -> b -> v (PrimState m) a -> m b
foldrM a -> b -> m b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
ifoldrM (forall a b. a -> b -> a
const a -> b -> m b
f)
foldrM' :: (PrimMonad m, MVector v a) => (a -> b -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE foldrM' #-}
foldrM' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(a -> b -> m b) -> b -> v (PrimState m) a -> m b
foldrM' a -> b -> m b
f = forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
ifoldrM' (forall a b. a -> b -> a
const a -> b -> m b
f)
ifoldrM :: (PrimMonad m, MVector v a) => (Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldrM #-}
ifoldrM :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
ifoldrM Int -> a -> b -> m b
f b
b0 v (PrimState m) a
v = Int -> b -> m b
loop (Int
nforall a. Num a => a -> a -> a
-Int
1) b
b0
where
loop :: Int -> b -> m b
loop Int
i b
b | Int
i forall a. Ord a => a -> a -> Bool
< Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return b
b
| Bool
otherwise = do a
a <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
Int -> b -> m b
loop (Int
i forall a. Num a => a -> a -> a
- Int
1) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> a -> b -> m b
f Int
i a
a b
b
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
ifoldrM' :: (PrimMonad m, MVector v a) => (Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
{-# INLINE ifoldrM' #-}
ifoldrM' :: forall (m :: * -> *) (v :: * -> * -> *) a b.
(PrimMonad m, MVector v a) =>
(Int -> a -> b -> m b) -> b -> v (PrimState m) a -> m b
ifoldrM' Int -> a -> b -> m b
f b
b0 v (PrimState m) a
v = Int -> b -> m b
loop (Int
nforall a. Num a => a -> a -> a
-Int
1) b
b0
where
loop :: Int -> b -> m b
loop Int
i !b
b | Int
i forall a. Ord a => a -> a -> Bool
< Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return b
b
| Bool
otherwise = do a
a <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
Int -> b -> m b
loop (Int
i forall a. Num a => a -> a -> a
- Int
1) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Int -> a -> b -> m b
f Int
i a
a b
b
n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
set :: (PrimMonad m, MVector v a) => v (PrimState m) a -> a -> m ()
{-# INLINE set #-}
set :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> a -> m ()
set v (PrimState m) a
v = forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (v :: * -> * -> *) a s. MVector v a => v s a -> a -> ST s ()
basicSet v (PrimState m) a
v
copy :: (HasCallStack, PrimMonad m, MVector v a)
=> v (PrimState m) a
-> v (PrimState m) a
-> m ()
{-# INLINE copy #-}
copy :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
copy v (PrimState m) a
dst v (PrimState m) a
src = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"overlapping vectors" (Bool -> Bool
not (v (PrimState m) a
dst forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
`overlaps` v (PrimState m) a
src))
forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"length mismatch" (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst forall a. Eq a => a -> a -> Bool
== forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
unsafeCopy v (PrimState m) a
dst v (PrimState m) a
src
move :: (HasCallStack, PrimMonad m, MVector v a)
=> v (PrimState m) a
-> v (PrimState m) a
-> m ()
{-# INLINE move #-}
move :: forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
move v (PrimState m) a
dst v (PrimState m) a
src = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"length mismatch" (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst forall a. Eq a => a -> a -> Bool
== forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
unsafeMove v (PrimState m) a
dst v (PrimState m) a
src
unsafeCopy :: (PrimMonad m, MVector v a)
=> v (PrimState m) a
-> v (PrimState m) a
-> m ()
{-# INLINE unsafeCopy #-}
unsafeCopy :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
unsafeCopy v (PrimState m) a
dst v (PrimState m) a
src = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Unsafe String
"length mismatch" (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst forall a. Eq a => a -> a -> Bool
== forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Unsafe String
"overlapping vectors" (Bool -> Bool
not (v (PrimState m) a
dst forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
`overlaps` v (PrimState m) a
src))
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a
dst seq :: forall a b. a -> b -> b
`seq` v (PrimState m) a
src seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
basicUnsafeCopy v (PrimState m) a
dst v (PrimState m) a
src)
unsafeMove :: (PrimMonad m, MVector v a)
=> v (PrimState m) a
-> v (PrimState m) a
-> m ()
{-# INLINE unsafeMove #-}
unsafeMove :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
unsafeMove v (PrimState m) a
dst v (PrimState m) a
src = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Unsafe String
"length mismatch" (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst forall a. Eq a => a -> a -> Bool
== forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a
dst seq :: forall a b. a -> b -> b
`seq` v (PrimState m) a
src seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> ST s ()
basicUnsafeMove v (PrimState m) a
dst v (PrimState m) a
src)
accum :: forall m v a b u. (HasCallStack, PrimMonad m, MVector v a)
=> (a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m ()
{-# INLINE accum #-}
accum :: forall (m :: * -> *) (v :: * -> * -> *) a b (u :: * -> *).
(HasCallStack, PrimMonad m, MVector v a) =>
(a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m ()
accum a -> b -> a
f !v (PrimState m) a
v Bundle u (Int, b)
s = forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle v a -> m ()
Bundle.mapM_ HasCallStack => (Int, b) -> m ()
upd Bundle u (Int, b)
s
where
{-# INLINE_INNER upd #-}
upd :: HasCallStack => (Int, b) -> m ()
upd :: HasCallStack => (Int, b) -> m ()
upd (Int
i,b
b) = do
a
a <- forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i (a -> b -> a
f a
a b
b)
!n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
update :: forall m v a u. (HasCallStack, PrimMonad m, MVector v a)
=> v (PrimState m) a -> Bundle u (Int, a) -> m ()
{-# INLINE update #-}
update :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Bundle u (Int, a) -> m ()
update !v (PrimState m) a
v Bundle u (Int, a)
s = forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle v a -> m ()
Bundle.mapM_ HasCallStack => (Int, a) -> m ()
upd Bundle u (Int, a)
s
where
{-# INLINE_INNER upd #-}
upd :: HasCallStack => (Int, a) -> m ()
upd :: HasCallStack => (Int, a) -> m ()
upd (Int
i,a
b) = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Bounds Int
i Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
b
!n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
unsafeAccum :: (PrimMonad m, MVector v a)
=> (a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m ()
{-# INLINE unsafeAccum #-}
unsafeAccum :: forall (m :: * -> *) (v :: * -> * -> *) a b (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m ()
unsafeAccum a -> b -> a
f !v (PrimState m) a
v Bundle u (Int, b)
s = forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle v a -> m ()
Bundle.mapM_ (Int, b) -> m ()
upd Bundle u (Int, b)
s
where
{-# INLINE_INNER upd #-}
upd :: (Int, b) -> m ()
upd (Int
i,b
b) = do
a
a <- forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i (a -> b -> a
f a
a b
b)
!n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
unsafeUpdate :: (PrimMonad m, MVector v a)
=> v (PrimState m) a -> Bundle u (Int, a) -> m ()
{-# INLINE unsafeUpdate #-}
unsafeUpdate :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Bundle u (Int, a) -> m ()
unsafeUpdate !v (PrimState m) a
v Bundle u (Int, a)
s = forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle v a -> m ()
Bundle.mapM_ (Int, a) -> m ()
upd Bundle u (Int, a)
s
where
{-# INLINE_INNER upd #-}
upd :: (Int, a) -> m ()
upd (Int
i,a
b) = forall a. HasCallStack => Checks -> Int -> Int -> a -> a
checkIndex Checks
Unsafe Int
i Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
b
!n :: Int
n = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
reverse :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m ()
{-# INLINE reverse #-}
reverse :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
reverse !v (PrimState m) a
v = Int -> Int -> m ()
reverse_loop Int
0 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v forall a. Num a => a -> a -> a
- Int
1)
where
reverse_loop :: Int -> Int -> m ()
reverse_loop Int
i Int
j | Int
i forall a. Ord a => a -> a -> Bool
< Int
j = do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
unsafeSwap v (PrimState m) a
v Int
i Int
j
Int -> Int -> m ()
reverse_loop (Int
i forall a. Num a => a -> a -> a
+ Int
1) (Int
j forall a. Num a => a -> a -> a
- Int
1)
reverse_loop Int
_ Int
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
unstablePartition :: forall m v a. (PrimMonad m, MVector v a)
=> (a -> Bool) -> v (PrimState m) a -> m Int
{-# INLINE unstablePartition #-}
unstablePartition :: forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
(a -> Bool) -> v (PrimState m) a -> m Int
unstablePartition a -> Bool
f !v (PrimState m) a
v = Int -> Int -> m Int
from_left Int
0 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
where
from_left :: Int -> Int -> m Int
from_left :: Int -> Int -> m Int
from_left Int
i Int
j
| Int
i forall a. Eq a => a -> a -> Bool
== Int
j = forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
| Bool
otherwise = do
a
x <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
if a -> Bool
f a
x
then Int -> Int -> m Int
from_left (Int
iforall a. Num a => a -> a -> a
+Int
1) Int
j
else Int -> Int -> m Int
from_right Int
i (Int
jforall a. Num a => a -> a -> a
-Int
1)
from_right :: Int -> Int -> m Int
from_right :: Int -> Int -> m Int
from_right Int
i Int
j
| Int
i forall a. Eq a => a -> a -> Bool
== Int
j = forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
| Bool
otherwise = do
a
x <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
j
if a -> Bool
f a
x
then do
a
y <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
i
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
j a
y
Int -> Int -> m Int
from_left (Int
iforall a. Num a => a -> a -> a
+Int
1) Int
j
else Int -> Int -> m Int
from_right Int
i (Int
jforall a. Num a => a -> a -> a
-Int
1)
unstablePartitionBundle :: (PrimMonad m, MVector v a)
=> (a -> Bool) -> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
{-# INLINE unstablePartitionBundle #-}
unstablePartitionBundle :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
unstablePartitionBundle a -> Bool
f Bundle u a
s
= case Size -> Maybe Int
upperBound (forall (v :: * -> *) a. Bundle v a -> Size
Bundle.size Bundle u a
s) of
Just Int
n -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
unstablePartitionMax a -> Bool
f Bundle u a
s Int
n
Maybe Int
Nothing -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
partitionUnknown a -> Bool
f Bundle u a
s
unstablePartitionMax :: (PrimMonad m, MVector v a)
=> (a -> Bool) -> Bundle u a -> Int
-> m (v (PrimState m) a, v (PrimState m) a)
{-# INLINE unstablePartitionMax #-}
unstablePartitionMax :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
unstablePartitionMax a -> Bool
f Bundle u a
s Int
n
= do
v (PrimState m) a
v <- forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Internal Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
let {-# INLINE_INNER put #-}
put :: (Int, Int) -> a -> m (Int, Int)
put (Int
i, Int
j) a
x
| a -> Bool
f a
x = do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iforall a. Num a => a -> a -> a
+Int
1, Int
j)
| Bool
otherwise = do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v (Int
jforall a. Num a => a -> a -> a
-Int
1) a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i, Int
jforall a. Num a => a -> a -> a
-Int
1)
(Int
i,Int
j) <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle v b -> m a
Bundle.foldM' (Int, Int) -> a -> m (Int, Int)
put (Int
0, Int
n) Bundle u a
s
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
i v (PrimState m) a
v, forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
j (Int
nforall a. Num a => a -> a -> a
-Int
j) v (PrimState m) a
v)
partitionBundle :: (PrimMonad m, MVector v a)
=> (a -> Bool) -> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
{-# INLINE partitionBundle #-}
partitionBundle :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
partitionBundle a -> Bool
f Bundle u a
s
= case Size -> Maybe Int
upperBound (forall (v :: * -> *) a. Bundle v a -> Size
Bundle.size Bundle u a
s) of
Just Int
n -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
partitionMax a -> Bool
f Bundle u a
s Int
n
Maybe Int
Nothing -> forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
partitionUnknown a -> Bool
f Bundle u a
s
partitionMax :: (PrimMonad m, MVector v a)
=> (a -> Bool) -> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
{-# INLINE partitionMax #-}
partitionMax :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
partitionMax a -> Bool
f Bundle u a
s Int
n
= do
v (PrimState m) a
v <- forall a. HasCallStack => Checks -> Int -> a -> a
checkLength Checks
Internal Int
n forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
let {-# INLINE_INNER put #-}
put :: (Int, Int) -> a -> m (Int, Int)
put (Int
i,Int
j) a
x
| a -> Bool
f a
x = do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
i a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iforall a. Num a => a -> a -> a
+Int
1,Int
j)
| Bool
otherwise = let j' :: Int
j' = Int
jforall a. Num a => a -> a -> a
-Int
1 in
do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v Int
j' a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i,Int
j')
(Int
i,Int
j) <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle v b -> m a
Bundle.foldM' (Int, Int) -> a -> m (Int, Int)
put (Int
0,Int
n) Bundle u a
s
forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Internal String
"invalid indices" (Int
i forall a. Ord a => a -> a -> Bool
<= Int
j)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
let l :: v (PrimState m) a
l = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
i v (PrimState m) a
v
r :: v (PrimState m) a
r = forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
j (Int
nforall a. Num a => a -> a -> a
-Int
j) v (PrimState m) a
v
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
reverse v (PrimState m) a
r
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
l,v (PrimState m) a
r)
partitionUnknown :: (PrimMonad m, MVector v a)
=> (a -> Bool) -> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
{-# INLINE partitionUnknown #-}
partitionUnknown :: forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
(a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
partitionUnknown a -> Bool
f Bundle u a
s
= do
v (PrimState m) a
v1 <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
0
v (PrimState m) a
v2 <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
0
(v (PrimState m) a
v1', Int
n1, v (PrimState m) a
v2', Int
n2) <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle v b -> m a
Bundle.foldM' (v (PrimState m) a, Int, v (PrimState m) a, Int)
-> a -> m (v (PrimState m) a, Int, v (PrimState m) a, Int)
put (v (PrimState m) a
v1, Int
0, v (PrimState m) a
v2, Int
0) Bundle u a
s
forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n1 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v1')
forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n2 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v2')
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n1 v (PrimState m) a
v1', forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n2 v (PrimState m) a
v2')
where
{-# INLINE_INNER put #-}
put :: (v (PrimState m) a, Int, v (PrimState m) a, Int)
-> a -> m (v (PrimState m) a, Int, v (PrimState m) a, Int)
put (v (PrimState m) a
v1, Int
i1, v (PrimState m) a
v2, Int
i2) a
x
| a -> Bool
f a
x = do
v (PrimState m) a
v1' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
unsafeAppend1 v (PrimState m) a
v1 Int
i1 a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v1', Int
i1forall a. Num a => a -> a -> a
+Int
1, v (PrimState m) a
v2, Int
i2)
| Bool
otherwise = do
v (PrimState m) a
v2' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
unsafeAppend1 v (PrimState m) a
v2 Int
i2 a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v1, Int
i1, v (PrimState m) a
v2', Int
i2forall a. Num a => a -> a -> a
+Int
1)
partitionWithBundle :: (PrimMonad m, MVector v a, MVector v b, MVector v c)
=> (a -> Either b c) -> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
{-# INLINE partitionWithBundle #-}
partitionWithBundle :: forall (m :: * -> *) (v :: * -> * -> *) a b c (u :: * -> *).
(PrimMonad m, MVector v a, MVector v b, MVector v c) =>
(a -> Either b c)
-> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithBundle a -> Either b c
f Bundle u a
s
= case Size -> Maybe Int
upperBound (forall (v :: * -> *) a. Bundle v a -> Size
Bundle.size Bundle u a
s) of
Just Int
n -> forall (m :: * -> *) (v :: * -> * -> *) a b c (u :: * -> *).
(PrimMonad m, MVector v a, MVector v b, MVector v c) =>
(a -> Either b c)
-> Bundle u a -> Int -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithMax a -> Either b c
f Bundle u a
s Int
n
Maybe Int
Nothing -> forall (m :: * -> *) (v :: * -> * -> *) (u :: * -> *) a b c.
(PrimMonad m, MVector v a, MVector v b, MVector v c) =>
(a -> Either b c)
-> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithUnknown a -> Either b c
f Bundle u a
s
partitionWithMax :: (PrimMonad m, MVector v a, MVector v b, MVector v c)
=> (a -> Either b c) -> Bundle u a -> Int -> m (v (PrimState m) b, v (PrimState m) c)
{-# INLINE partitionWithMax #-}
partitionWithMax :: forall (m :: * -> *) (v :: * -> * -> *) a b c (u :: * -> *).
(PrimMonad m, MVector v a, MVector v b, MVector v c) =>
(a -> Either b c)
-> Bundle u a -> Int -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithMax a -> Either b c
f Bundle u a
s Int
n
= do
v (PrimState m) b
v1 <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
v (PrimState m) c
v2 <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
let {-# INLINE_INNER put #-}
put :: (Int, Int) -> a -> m (Int, Int)
put (Int
i1, Int
i2) a
x = case a -> Either b c
f a
x of
Left b
b -> do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) b
v1 Int
i1 b
b
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i1forall a. Num a => a -> a -> a
+Int
1, Int
i2)
Right c
c -> do
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) c
v2 Int
i2 c
c
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i1, Int
i2forall a. Num a => a -> a -> a
+Int
1)
(Int
n1, Int
n2) <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle v b -> m a
Bundle.foldM' (Int, Int) -> a -> m (Int, Int)
put (Int
0, Int
0) Bundle u a
s
forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n1 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) b
v1)
forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n2 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) c
v2)
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n1 v (PrimState m) b
v1, forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n2 v (PrimState m) c
v2)
partitionWithUnknown :: forall m v u a b c.
(PrimMonad m, MVector v a, MVector v b, MVector v c)
=> (a -> Either b c) -> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
{-# INLINE partitionWithUnknown #-}
partitionWithUnknown :: forall (m :: * -> *) (v :: * -> * -> *) (u :: * -> *) a b c.
(PrimMonad m, MVector v a, MVector v b, MVector v c) =>
(a -> Either b c)
-> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithUnknown a -> Either b c
f Bundle u a
s
= do
v (PrimState m) b
v1 <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
0
v (PrimState m) c
v2 <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
0
(v (PrimState m) b
v1', Int
n1, v (PrimState m) c
v2', Int
n2) <- forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle v b -> m a
Bundle.foldM' (v (PrimState m) b, Int, v (PrimState m) c, Int)
-> a -> m (v (PrimState m) b, Int, v (PrimState m) c, Int)
put (v (PrimState m) b
v1, Int
0, v (PrimState m) c
v2, Int
0) Bundle u a
s
forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n1 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) b
v1')
forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => Checks -> Int -> Int -> Int -> a -> a
checkSlice Checks
Internal Int
0 Int
n2 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) c
v2')
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n1 v (PrimState m) b
v1', forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
0 Int
n2 v (PrimState m) c
v2')
where
put :: (v (PrimState m) b, Int, v (PrimState m) c, Int)
-> a
-> m (v (PrimState m) b, Int, v (PrimState m) c, Int)
{-# INLINE_INNER put #-}
put :: (v (PrimState m) b, Int, v (PrimState m) c, Int)
-> a -> m (v (PrimState m) b, Int, v (PrimState m) c, Int)
put (v (PrimState m) b
v1, Int
i1, v (PrimState m) c
v2, Int
i2) a
x = case a -> Either b c
f a
x of
Left b
b -> do
v (PrimState m) b
v1' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
unsafeAppend1 v (PrimState m) b
v1 Int
i1 b
b
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) b
v1', Int
i1forall a. Num a => a -> a -> a
+Int
1, v (PrimState m) c
v2, Int
i2)
Right c
c -> do
v (PrimState m) c
v2' <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
unsafeAppend1 v (PrimState m) c
v2 Int
i2 c
c
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) b
v1, Int
i1, v (PrimState m) c
v2', Int
i2forall a. Num a => a -> a -> a
+Int
1)
nextPermutation :: (PrimMonad m,Ord e,MVector v e) => v (PrimState m) e -> m Bool
nextPermutation :: forall (m :: * -> *) e (v :: * -> * -> *).
(PrimMonad m, Ord e, MVector v e) =>
v (PrimState m) e -> m Bool
nextPermutation v (PrimState m) e
v
| Int
dim forall a. Ord a => a -> a -> Bool
< Int
2 = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
| Bool
otherwise = do
e
val <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) e
v Int
0
(Int
k,Int
l) <- e -> Int -> Int -> e -> Int -> m (Int, Int)
loop e
val (-Int
1) Int
0 e
val Int
1
if Int
k forall a. Ord a => a -> a -> Bool
< Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> Int -> m ()
unsafeSwap v (PrimState m) e
v Int
k Int
l forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
reverse (forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice (Int
kforall a. Num a => a -> a -> a
+Int
1) (Int
dimforall a. Num a => a -> a -> a
-Int
kforall a. Num a => a -> a -> a
-Int
1) v (PrimState m) e
v) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
where loop :: e -> Int -> Int -> e -> Int -> m (Int, Int)
loop !e
kval !Int
k !Int
l !e
prev !Int
i
| Int
i forall a. Eq a => a -> a -> Bool
== Int
dim = forall (m :: * -> *) a. Monad m => a -> m a
return (Int
k,Int
l)
| Bool
otherwise = do
e
cur <- forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) e
v Int
i
let (e
kval',Int
k') = if e
prev forall a. Ord a => a -> a -> Bool
< e
cur then (e
prev,Int
iforall a. Num a => a -> a -> a
-Int
1) else (e
kval,Int
k)
l' :: Int
l' = if e
kval' forall a. Ord a => a -> a -> Bool
< e
cur then Int
i else Int
l
e -> Int -> Int -> e -> Int -> m (Int, Int)
loop e
kval' Int
k' Int
l' e
cur (Int
iforall a. Num a => a -> a -> a
+Int
1)
dim :: Int
dim = forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) e
v