{-# LANGUAGE CPP, MultiParamTypeClasses, FlexibleContexts, BangPatterns, TypeFamilies, ScopedTypeVariables #-}
-- |
-- Module      : Data.Vector.Generic.Mutable
-- Copyright   : (c) Roman Leshchinskiy 2008-2010
-- License     : BSD-style
--
-- Maintainer  : Roman Leshchinskiy <rl@cse.unsw.edu.au>
-- Stability   : experimental
-- Portability : non-portable
--
-- Generic interface to mutable vectors
--

module Data.Vector.Generic.Mutable (
  -- * Class of mutable vector types
  MVector(..),

  -- * Accessors

  -- ** Length information
  length, null,

  -- ** Extracting subvectors
  slice, init, tail, take, drop, splitAt,
  unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,

  -- ** Overlapping
  overlaps,

  -- * Construction

  -- ** Initialisation
  new, unsafeNew, replicate, replicateM, clone,

  -- ** Growing
  grow, unsafeGrow,
  growFront, unsafeGrowFront,

  -- ** Restricting memory usage
  clear,

  -- * Accessing individual elements
  read, write, modify, swap, exchange,
  unsafeRead, unsafeWrite, unsafeModify, unsafeSwap, unsafeExchange,

  -- * Modifying vectors
  nextPermutation,

  -- ** Filling and copying
  set, copy, move, unsafeCopy, unsafeMove,

  -- * Internal operations
  mstream, mstreamR,
  unstream, unstreamR, vunstream,
  munstream, munstreamR,
  transform, transformR,
  fill, fillR,
  unsafeAccum, accum, unsafeUpdate, update, reverse,
  unstablePartition, unstablePartitionBundle, partitionBundle,
  partitionWithBundle
) 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 Control.Monad.Primitive ( PrimMonad, PrimState )

import Prelude hiding ( length, null, replicate, reverse, map, read,
                        take, drop, splitAt, init, tail )

#include "vector.h"

{-
type family Immutable (v :: * -> * -> *) :: * -> *

-- | Class of mutable vectors parametrised with a primitive state token.
--
class MBundle.Pointer u a => MVector v a where
  -- | Length of the mutable vector. This method should not be
  -- called directly, use 'length' instead.
  basicLength       :: v s a -> Int

  -- | Yield a part of the mutable vector without copying it. This method
  -- should not be called directly, use 'unsafeSlice' instead.
  basicUnsafeSlice :: Int  -- ^ starting index
                   -> Int  -- ^ length of the slice
                   -> v s a
                   -> v s a

  -- Check whether two vectors overlap. This method should not be
  -- called directly, use 'overlaps' instead.
  basicOverlaps    :: v s a -> v s a -> Bool

  -- | Create a mutable vector of the given length. This method should not be
  -- called directly, use 'unsafeNew' instead.
  basicUnsafeNew   :: PrimMonad m => Int -> m (v (PrimState m) a)

  -- | Create a mutable vector of the given length and fill it with an
  -- initial value. This method should not be called directly, use
  -- 'replicate' instead.
  basicUnsafeReplicate :: PrimMonad m => Int -> a -> m (v (PrimState m) a)

  -- | Yield the element at the given position. This method should not be
  -- called directly, use 'unsafeRead' instead.
  basicUnsafeRead  :: PrimMonad m => v (PrimState m) a -> Int -> m a

  -- | Replace the element at the given position. This method should not be
  -- called directly, use 'unsafeWrite' instead.
  basicUnsafeWrite :: PrimMonad m => v (PrimState m) a -> Int -> a -> m ()

  -- | Reset all elements of the vector to some undefined value, clearing all
  -- references to external objects. This is usually a noop for unboxed
  -- vectors. This method should not be called directly, use 'clear' instead.
  basicClear       :: PrimMonad m => v (PrimState m) a -> m ()

  -- | Set all elements of the vector to the given value. This method should
  -- not be called directly, use 'set' instead.
  basicSet         :: PrimMonad m => v (PrimState m) a -> a -> m ()

  basicUnsafeCopyPointer :: PrimMonad m => v (PrimState m) a
                                        -> Immutable v a
                                        -> m ()

  -- | Copy a vector. The two vectors may not overlap. This method should not
  -- be called directly, use 'unsafeCopy' instead.
  basicUnsafeCopy  :: PrimMonad m => v (PrimState m) a   -- ^ target
                                  -> v (PrimState m) a   -- ^ source
                                  -> m ()

  -- | Move the contents of a vector. The two vectors may overlap. This method
  -- should not be called directly, use 'unsafeMove' instead.
  basicUnsafeMove  :: PrimMonad m => v (PrimState m) a   -- ^ target
                                  -> v (PrimState m) a   -- ^ source
                                  -> m ()

  -- | Grow a vector by the given number of elements. This method should not be
  -- called directly, use 'unsafeGrow' instead.
  basicUnsafeGrow  :: PrimMonad m => v (PrimState m) a -> Int
                                                       -> m (v (PrimState m) a)

  {-# INLINE basicUnsafeReplicate #-}
  basicUnsafeReplicate n x
    = do
        v <- basicUnsafeNew n
        basicSet v x
        return v

  {-# INLINE basicClear #-}
  basicClear _ = return ()

  {-# INLINE basicSet #-}
  basicSet !v x
    | n == 0    = return ()
    | otherwise = do
                    basicUnsafeWrite v 0 x
                    do_set 1
    where
      !n = basicLength v

      do_set i | 2*i < n = do basicUnsafeCopy (basicUnsafeSlice i i v)
                                              (basicUnsafeSlice 0 i v)
                              do_set (2*i)
               | otherwise = basicUnsafeCopy (basicUnsafeSlice i (n-i) v)
                                             (basicUnsafeSlice 0 (n-i) v)

  {-# INLINE basicUnsafeCopyPointer #-}
  basicUnsafeCopyPointer !dst !src = do_copy 0 src
    where
      do_copy !i p | Just (x,q) <- MBundle.pget p = do
                                                      basicUnsafeWrite dst i x
                                                      do_copy (i+1) q
                   | otherwise = return ()

  {-# INLINE basicUnsafeCopy #-}
  basicUnsafeCopy !dst !src = do_copy 0
    where
      !n = basicLength src

      do_copy i | i < n = do
                            x <- basicUnsafeRead src i
                            basicUnsafeWrite dst i x
                            do_copy (i+1)
                | otherwise = return ()

  {-# INLINE basicUnsafeMove #-}
  basicUnsafeMove !dst !src
    | basicOverlaps dst src = do
        srcCopy <- clone src
        basicUnsafeCopy dst srcCopy
    | otherwise = basicUnsafeCopy dst src

  {-# INLINE basicUnsafeGrow #-}
  basicUnsafeGrow v by
    = do
        v' <- basicUnsafeNew (n+by)
        basicUnsafeCopy (basicUnsafeSlice 0 n v') v
        return v'
    where
      n = basicLength v
-}

-- ------------------
-- Internal functions
-- ------------------

unsafeAppend1 :: (PrimMonad m, MVector v a)
        => v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
{-# INLINE_INNER unsafeAppend1 #-}
    -- NOTE: The case distinction has to be on the outside because
    -- GHC creates a join point for the unsafeWrite even when everything
    -- is inlined. This is bad because with the join point, v isn't getting
    -- unboxed.
unsafeAppend1 :: v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
unsafeAppend1 v :: v (PrimState m) a
v i :: Int
i x :: a
x
  | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v = do
                     v (PrimState m) a -> Int -> a -> m ()
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
                     v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v
  | Bool
otherwise    = do
                     v (PrimState m) a
v' <- v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m (v (PrimState m) a)
enlarge v (PrimState m) a
v
                     INTERNAL_CHECK(checkIndex) "unsafeAppend1" i (length v')
                       (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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
                     v (PrimState m) a -> m (v (PrimState m) a)
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 :: v (PrimState m) a -> Int -> a -> m (v (PrimState m) a, Int)
unsafePrepend1 v :: v (PrimState m) a
v i :: Int
i x :: a
x
  | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= 0    = do
                  let i' :: Int
i' = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-1
                  v (PrimState m) a -> Int -> a -> m ()
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
                  (v (PrimState m) a, Int) -> m (v (PrimState m) a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v, Int
i')
  | Bool
otherwise = do
                  (v' :: v (PrimState m) a
v', j :: Int
j) <- v (PrimState m) a -> m (v (PrimState m) a, Int)
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
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1
                  INTERNAL_CHECK(checkIndex) "unsafePrepend1" i' (length v')
                    (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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
                  (v (PrimState m) a, Int) -> m (v (PrimState m) a, Int)
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 :: v (PrimState m) a -> Stream m a
mstream v :: v (PrimState m) a
v = v (PrimState m) a
v v (PrimState m) a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` Int
n Int -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` ((Int -> m (Maybe (a, Int))) -> Int -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
Stream.unfoldrM Int -> m (Maybe (a, Int))
get 0)
  where
    n :: Int
n = v (PrimState m) a -> Int
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 i :: Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n     = do a
x <- v (PrimState m) a -> Int -> m 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
                           Maybe (a, Int) -> m (Maybe (a, Int))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, Int) -> m (Maybe (a, Int)))
-> Maybe (a, Int) -> m (Maybe (a, Int))
forall a b. (a -> b) -> a -> b
$ (a, Int) -> Maybe (a, Int)
forall a. a -> Maybe a
Just (a
x, Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1)
          | Bool
otherwise = Maybe (a, Int) -> m (Maybe (a, Int))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, Int) -> m (Maybe (a, Int)))
-> Maybe (a, Int) -> m (Maybe (a, Int))
forall a b. (a -> b) -> a -> b
$ Maybe (a, Int)
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 :: v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
fill v :: v (PrimState m) a
v s :: Stream m a
s = v (PrimState m) a
v v (PrimState m) a -> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. a -> b -> b
`seq` do
                     Int
n' <- (Int -> a -> m Int) -> Int -> Stream m a -> m Int
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
Stream.foldM Int -> a -> m Int
put 0 Stream m a
s
                     v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a -> m (v (PrimState m) a))
-> v (PrimState m) a -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
n' v (PrimState m) a
v
  where
    {-# INLINE_INNER put #-}
    put :: Int -> a -> m Int
put i :: Int
i x :: a
x = do
                INTERNAL_CHECK(checkIndex) "fill" i (length v)
                  (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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
                Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+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 :: (Stream m a -> Stream m a)
-> v (PrimState m) a -> m (v (PrimState m) a)
transform f :: Stream m a -> Stream m a
f v :: v (PrimState m) a
v = v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
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 (v (PrimState m) a -> Stream m a
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 :: v (PrimState m) a -> Stream m a
mstreamR v :: v (PrimState m) a
v = v (PrimState m) a
v v (PrimState m) a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` Int
n Int -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` ((Int -> m (Maybe (a, Int))) -> Int -> Stream m a
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 = v (PrimState m) a -> Int
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 i :: Int
i | Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0    = do a
x <- v (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
j
                           Maybe (a, Int) -> m (Maybe (a, Int))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, Int) -> m (Maybe (a, Int)))
-> Maybe (a, Int) -> m (Maybe (a, Int))
forall a b. (a -> b) -> a -> b
$ (a, Int) -> Maybe (a, Int)
forall a. a -> Maybe a
Just (a
x,Int
j)
          | Bool
otherwise = Maybe (a, Int) -> m (Maybe (a, Int))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (a, Int)
forall a. Maybe a
Nothing
      where
        j :: Int
j = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-1

fillR :: (PrimMonad m, MVector v a)
      => v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
{-# INLINE fillR #-}
fillR :: v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
fillR v :: v (PrimState m) a
v s :: Stream m a
s = v (PrimState m) a
v v (PrimState m) a -> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. a -> b -> b
`seq` do
                      Int
i <- (Int -> a -> m Int) -> Int -> Stream m a -> m Int
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
                      v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a -> m (v (PrimState m) a))
-> v (PrimState m) a -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
i) v (PrimState m) a
v
  where
    n :: Int
n = v (PrimState m) a -> Int
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 i :: Int
i x :: a
x = do
                v (PrimState m) a -> Int -> a -> m ()
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
                Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
j
      where
        j :: Int
j = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-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 :: (Stream m a -> Stream m a)
-> v (PrimState m) a -> m (v (PrimState m) a)
transformR f :: Stream m a -> Stream m a
f v :: v (PrimState m) a
v = v (PrimState m) a -> Stream m a -> m (v (PrimState m) a)
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 (v (PrimState m) a -> Stream m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Stream m a
mstreamR v (PrimState m) a
v))

-- | Create a new mutable vector and fill it with elements from the 'Bundle'.
-- The vector will grow exponentially if the maximum size of the 'Bundle' is
-- unknown.
unstream :: (PrimMonad m, MVector v a)
         => Bundle u a -> m (v (PrimState m) a)
-- NOTE: replace INLINE_FUSED by INLINE? (also in unstreamR)
{-# INLINE_FUSED unstream #-}
unstream :: Bundle u a -> m (v (PrimState m) a)
unstream s :: Bundle u a
s = MBundle m u a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstream (Bundle u a -> MBundle m u a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle v a -> Bundle m v a
Bundle.lift Bundle u a
s)

-- | Create a new mutable vector and fill it with elements from the monadic
-- stream. The vector will grow exponentially if the maximum size of the stream
-- is unknown.
munstream :: (PrimMonad m, MVector v a)
          => MBundle m u a -> m (v (PrimState m) a)
{-# INLINE_FUSED munstream #-}
munstream :: MBundle m u a -> m (v (PrimState m) a)
munstream s :: MBundle m u a
s = case Size -> Maybe Int
upperBound (MBundle m u a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
MBundle.size MBundle m u a
s) of
               Just n :: Int
n  -> MBundle m u a -> Int -> m (v (PrimState m) a)
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
               Nothing -> MBundle m u a -> m (v (PrimState m) a)
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

-- FIXME: I can't think of how to prevent GHC from floating out
-- unstreamUnknown. That is bad because SpecConstr then generates two
-- specialisations: one for when it is called from unstream (it doesn't know
-- the shape of the vector) and one for when the vector has grown. To see the
-- problem simply compile this:
--
-- fromList = Data.Vector.Unboxed.unstream . Bundle.fromList
--
-- I'm not sure this still applies (19/04/2010)

munstreamMax :: (PrimMonad m, MVector v a)
             => MBundle m u a -> Int -> m (v (PrimState m) a)
{-# INLINE munstreamMax #-}
munstreamMax :: MBundle m u a -> Int -> m (v (PrimState m) a)
munstreamMax s :: MBundle m u a
s n :: Int
n
  = do
      v (PrimState m) a
v <- INTERNAL_CHECK(checkLength) "munstreamMax" n
           (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> m (v (PrimState m) a)
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 i :: Int
i x :: a
x = do
                       INTERNAL_CHECK(checkIndex) "munstreamMax" i n
                         (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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
                       Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1)
      Int
n' <- (Int -> a -> m Int) -> Int -> MBundle m u a -> m Int
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 0 MBundle m u a
s
      v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a -> m (v (PrimState m) a))
-> v (PrimState m) a -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "munstreamMax" 0 n' n
             (v (PrimState m) a -> v (PrimState m) a)
-> v (PrimState m) a -> v (PrimState m) a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 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 :: MBundle m u a -> m (v (PrimState m) a)
munstreamUnknown s :: MBundle m u a
s
  = do
      v (PrimState m) a
v <- Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew 0
      (v' :: v (PrimState m) a
v', n :: Int
n) <- ((v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int))
-> (v (PrimState m) a, Int)
-> MBundle m u a
-> m (v (PrimState m) a, Int)
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
MBundle.foldM (v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int)
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, 0) MBundle m u a
s
      v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a -> m (v (PrimState m) a))
-> v (PrimState m) a -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "munstreamUnknown" 0 n (length v')
             (v (PrimState m) a -> v (PrimState m) a)
-> v (PrimState m) a -> v (PrimState m) a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 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 :: v (PrimState m) a
v,i :: Int
i) x :: a
x = do
                    v (PrimState m) a
v' <- v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
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
                    (v (PrimState m) a, Int) -> m (v (PrimState m) a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v',Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1)







-- | Create a new mutable vector and fill it with elements from the 'Bundle'.
-- The vector will grow exponentially if the maximum size of the 'Bundle' is
-- unknown.
vunstream :: (PrimMonad m, V.Vector v a)
         => Bundle v a -> m (V.Mutable v (PrimState m) a)
-- NOTE: replace INLINE_FUSED by INLINE? (also in unstreamR)
{-# INLINE_FUSED vunstream #-}
vunstream :: Bundle v a -> m (Mutable v (PrimState m) a)
vunstream s :: Bundle v a
s = MBundle m v a -> m (Mutable v (PrimState m) a)
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
MBundle m v a -> m (Mutable v (PrimState m) a)
vmunstream (Bundle v a -> MBundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle v a -> Bundle m v a
Bundle.lift Bundle v a
s)

-- | Create a new mutable vector and fill it with elements from the monadic
-- stream. The vector will grow exponentially if the maximum size of the stream
-- is unknown.
vmunstream :: (PrimMonad m, V.Vector v a)
           => MBundle m v a -> m (V.Mutable v (PrimState m) a)
{-# INLINE_FUSED vmunstream #-}
vmunstream :: MBundle m v a -> m (Mutable v (PrimState m) a)
vmunstream s :: MBundle m v a
s = case Size -> Maybe Int
upperBound (MBundle m v a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
MBundle.size MBundle m v a
s) of
               Just n :: Int
n  -> MBundle m v a -> Int -> m (Mutable v (PrimState m) a)
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
               Nothing -> MBundle m v a -> m (Mutable v (PrimState m) a)
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

-- FIXME: I can't think of how to prevent GHC from floating out
-- unstreamUnknown. That is bad because SpecConstr then generates two
-- specialisations: one for when it is called from unstream (it doesn't know
-- the shape of the vector) and one for when the vector has grown. To see the
-- problem simply compile this:
--
-- fromList = Data.Vector.Unboxed.unstream . Bundle.fromList
--
-- I'm not sure this still applies (19/04/2010)

vmunstreamMax :: (PrimMonad m, V.Vector v a)
              => MBundle m v a -> Int -> m (V.Mutable v (PrimState m) a)
{-# INLINE vmunstreamMax #-}
vmunstreamMax :: MBundle m v a -> Int -> m (Mutable v (PrimState m) a)
vmunstreamMax s :: MBundle m v a
s n :: Int
n
  = do
      Mutable v (PrimState m) a
v <- INTERNAL_CHECK(checkLength) "munstreamMax" n
           (m (Mutable v (PrimState m) a) -> m (Mutable v (PrimState m) a))
-> m (Mutable v (PrimState m) a) -> m (Mutable v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> m (Mutable v (PrimState m) a)
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 i :: Int
i (Chunk m :: Int
m f :: forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
f) =
            INTERNAL_CHECK(checkSlice) "munstreamMax.copyChunk" i m (length v) $ do
              f (basicUnsafeSlice i m v)
              return (i+m)

      Int
n' <- (Int -> Chunk v a -> m Int) -> Int -> Stream m (Chunk v a) -> m Int
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 0 (MBundle m v a -> Stream m (Chunk v a)
forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
MBundle.chunks MBundle m v a
s)
      Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a))
-> Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "munstreamMax" 0 n' n
             (Mutable v (PrimState m) a -> Mutable v (PrimState m) a)
-> Mutable v (PrimState m) a -> Mutable v (PrimState m) a
forall a b. (a -> b) -> a -> b
$ Int
-> Int -> Mutable v (PrimState m) a -> Mutable v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 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 :: MBundle m v a -> m (Mutable v (PrimState m) a)
vmunstreamUnknown s :: MBundle m v a
s
  = do
      Mutable v (PrimState m) a
v <- Int -> m (Mutable v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew 0
      (v' :: Mutable v (PrimState m) a
v', n :: Int
n) <- ((Mutable v (PrimState m) a, Int)
 -> Chunk v a -> m (Mutable v (PrimState m) a, Int))
-> (Mutable v (PrimState m) a, Int)
-> Stream m (Chunk v a)
-> m (Mutable v (PrimState m) a, Int)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
Stream.foldlM (Mutable v (PrimState m) a, Int)
-> Chunk v a -> m (Mutable v (PrimState m) a, Int)
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,0) (MBundle m v a -> Stream m (Chunk v a)
forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
MBundle.chunks MBundle m v a
s)
      Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a))
-> Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "munstreamUnknown" 0 n (length v')
             (Mutable v (PrimState m) a -> Mutable v (PrimState m) a)
-> Mutable v (PrimState m) a -> Mutable v (PrimState m) a
forall a b. (a -> b) -> a -> b
$ Int
-> Int -> Mutable v (PrimState m) a -> Mutable v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 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 (v :: Mutable v (PrimState m) a
v,i :: Int
i) (Chunk n :: Int
n f :: forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
f)
      = do
          let j :: Int
j = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
n
          Mutable v (PrimState m) a
v' <- if Mutable v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength Mutable v (PrimState m) a
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
j
                  then Mutable v (PrimState m) a -> Int -> m (Mutable v (PrimState m) a)
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 ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Mutable v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
enlarge_delta Mutable v (PrimState m) a
v) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- Mutable v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength Mutable v (PrimState m) a
v))
                  else Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Mutable v (PrimState m) a
v
          INTERNAL_CHECK(checkSlice) "munstreamUnknown.copyChunk" i n (length v')
            (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Mutable v (PrimState m) a -> m ()
forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
f (Int
-> Int -> Mutable v (PrimState m) a -> Mutable v (PrimState m) a
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')
          (Mutable v (PrimState m) a, Int)
-> m (Mutable v (PrimState m) a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Mutable v (PrimState m) a
v',Int
j)




-- | Create a new mutable vector and fill it with elements from the 'Bundle'
-- from right to left. The vector will grow exponentially if the maximum size
-- of the 'Bundle' is unknown.
unstreamR :: (PrimMonad m, MVector v a)
          => Bundle u a -> m (v (PrimState m) a)
-- NOTE: replace INLINE_FUSED by INLINE? (also in unstream)
{-# INLINE_FUSED unstreamR #-}
unstreamR :: Bundle u a -> m (v (PrimState m) a)
unstreamR s :: Bundle u a
s = MBundle m u a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstreamR (Bundle u a -> MBundle m u a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle v a -> Bundle m v a
Bundle.lift Bundle u a
s)

-- | Create a new mutable vector and fill it with elements from the monadic
-- stream from right to left. The vector will grow exponentially if the maximum
-- size of the stream is unknown.
munstreamR :: (PrimMonad m, MVector v a)
           => MBundle m u a -> m (v (PrimState m) a)
{-# INLINE_FUSED munstreamR #-}
munstreamR :: MBundle m u a -> m (v (PrimState m) a)
munstreamR s :: MBundle m u a
s = case Size -> Maybe Int
upperBound (MBundle m u a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
MBundle.size MBundle m u a
s) of
               Just n :: Int
n  -> MBundle m u a -> Int -> m (v (PrimState m) a)
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
               Nothing -> MBundle m u a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(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 :: MBundle m u a -> Int -> m (v (PrimState m) a)
munstreamRMax s :: MBundle m u a
s n :: Int
n
  = do
      v (PrimState m) a
v <- INTERNAL_CHECK(checkLength) "munstreamRMax" n
           (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> m (v (PrimState m) a)
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 i :: Int
i x :: a
x = do
                      let i' :: Int
i' = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-1
                      INTERNAL_CHECK(checkIndex) "munstreamRMax" i' n
                        (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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
                      Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i'
      Int
i <- (Int -> a -> m Int) -> Int -> MBundle m u a -> m Int
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
      v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a -> m (v (PrimState m) a))
-> v (PrimState m) a -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "munstreamRMax" i (n-i) n
             (v (PrimState m) a -> v (PrimState m) a)
-> v (PrimState m) a -> v (PrimState m) a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
i) v (PrimState m) a
v

munstreamRUnknown :: (PrimMonad m, MVector v a)
                  => MBundle m u a -> m (v (PrimState m) a)
{-# INLINE munstreamRUnknown #-}
munstreamRUnknown :: MBundle m u a -> m (v (PrimState m) a)
munstreamRUnknown s :: MBundle m u a
s
  = do
      v (PrimState m) a
v <- Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew 0
      (v' :: v (PrimState m) a
v', i :: Int
i) <- ((v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int))
-> (v (PrimState m) a, Int)
-> MBundle m u a
-> m (v (PrimState m) a, Int)
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
MBundle.foldM (v (PrimState m) a, Int) -> a -> m (v (PrimState m) a, Int)
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, 0) MBundle m u a
s
      let n :: Int
n = v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v'
      v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a -> m (v (PrimState m) a))
-> v (PrimState m) a -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "unstreamRUnknown" i (n-i) n
             (v (PrimState m) a -> v (PrimState m) a)
-> v (PrimState m) a -> v (PrimState m) a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
i (Int
nInt -> Int -> Int
forall 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 :: v (PrimState m) a
v,i :: Int
i) x :: a
x = v (PrimState m) a -> Int -> a -> m (v (PrimState m) a, Int)
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
-- ------

-- | Length of the mutable vector.
length :: MVector v a => v s a -> Int
{-# INLINE length #-}
length :: v s a -> Int
length = v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength

-- | Check whether the vector is empty
null :: MVector v a => v s a -> Bool
{-# INLINE null #-}
null :: v s a -> Bool
null v :: v s a
v = v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0

-- Extracting subvectors
-- ---------------------

-- | Yield a part of the mutable vector without copying it. The vector must
-- contain at least @i+n@ elements.
slice :: MVector v a
      => Int  -- ^ @i@ starting index
      -> Int  -- ^ @n@ length
      -> v s a
      -> v s a
{-# INLINE slice #-}
slice :: Int -> Int -> v s a -> v s a
slice i :: Int
i n :: Int
n v :: v s a
v = BOUNDS_CHECK(checkSlice) "slice" i n (length v)
            (v s a -> v s a) -> v s a -> v s a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v s a -> v s a
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 :: Int -> v s a -> v s a
take n :: Int
n v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
n 0) (v s a -> Int
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 :: Int -> v s a -> v s a
drop n :: Int
n v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
m Int
n') (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max 0 (Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n')) v s a
v
  where
    n' :: Int
n' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
n 0
    m :: Int
m  = v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v

{-# INLINE splitAt #-}
splitAt :: MVector v a => Int -> v s a -> (v s a, v s a)
splitAt :: Int -> v s a -> (v s a, v s a)
splitAt n :: Int
n v :: v s a
v = ( Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
m v s a
v
              , Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
m (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max 0 (Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n')) v s a
v
              )
    where
      m :: Int
m   = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
n' Int
len
      n' :: Int
n'  = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
n 0
      len :: Int
len = v s a -> Int
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 :: v s a -> v s a
init v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
slice 0 (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) v s a
v

tail :: MVector v a => v s a -> v s a
{-# INLINE tail #-}
tail :: v s a -> v s a
tail v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
slice 1 (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) v s a
v

-- | Yield a part of the mutable vector without copying it. No bounds checks
-- are performed.
unsafeSlice :: MVector v a => Int  -- ^ starting index
                           -> Int  -- ^ length of the slice
                           -> v s a
                           -> v s a
{-# INLINE unsafeSlice #-}
unsafeSlice :: Int -> Int -> v s a -> v s a
unsafeSlice i :: Int
i n :: Int
n v :: v s a
v = UNSAFE_CHECK(checkSlice) "unsafeSlice" i n (length v)
                  (v s a -> v s a) -> v s a -> v s a
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v s a -> v s a
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 :: v s a -> v s a
unsafeInit v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) v s a
v

unsafeTail :: MVector v a => v s a -> v s a
{-# INLINE unsafeTail #-}
unsafeTail :: v s a -> v s a
unsafeTail v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 1 (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1) v s a
v

unsafeTake :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeTake #-}
unsafeTake :: Int -> v s a -> v s a
unsafeTake n :: Int
n v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
n v s a
v

unsafeDrop :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeDrop #-}
unsafeDrop :: Int -> v s a -> v s a
unsafeDrop n :: Int
n v :: v s a
v = Int -> Int -> v s a -> v s a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
n (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) v s a
v

-- Overlapping
-- -----------

-- | Check whether two vectors overlap.
overlaps :: MVector v a => v s a -> v s a -> Bool
{-# INLINE overlaps #-}
overlaps :: v s a -> v s a -> Bool
overlaps = v s a -> v s a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
basicOverlaps

-- Initialisation
-- --------------

-- | Create a mutable vector of the given length.
new :: (PrimMonad m, MVector v a) => Int -> m (v (PrimState m) a)
{-# INLINE new #-}
new :: Int -> m (v (PrimState m) a)
new n :: Int
n = BOUNDS_CHECK(checkLength) "new" n
      (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n m (v (PrimState m) a)
-> (v (PrimState m) a -> m (v (PrimState m) a))
-> m (v (PrimState m) a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \v :: v (PrimState m) a
v -> v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
basicInitialize v (PrimState m) a
v m () -> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v

-- | Create a mutable vector of the given length. The memory is not initialized.
unsafeNew :: (PrimMonad m, MVector v a) => Int -> m (v (PrimState m) a)
{-# INLINE unsafeNew #-}
unsafeNew :: Int -> m (v (PrimState m) a)
unsafeNew n :: Int
n = UNSAFE_CHECK(checkLength) "unsafeNew" n
            (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> m (v (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> m (v (PrimState m) a)
basicUnsafeNew Int
n

-- | Create a mutable vector of the given length (0 if the length is negative)
-- and fill it with an initial value.
replicate :: (PrimMonad m, MVector v a) => Int -> a -> m (v (PrimState m) a)
{-# INLINE replicate #-}
replicate :: Int -> a -> m (v (PrimState m) a)
replicate n :: Int
n x :: a
x = Int -> a -> m (v (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> a -> m (v (PrimState m) a)
basicUnsafeReplicate ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
forall a. Ord a => a -> a -> a
max 0 Int
n) a
x

-- | Create a mutable vector of the given length (0 if the length is negative)
-- and fill it with values produced by repeatedly executing the monadic action.
replicateM :: (PrimMonad m, MVector v a) => Int -> m a -> m (v (PrimState m) a)
{-# INLINE replicateM #-}
replicateM :: Int -> m a -> m (v (PrimState m) a)
replicateM n :: Int
n m :: m a
m = MBundle m Any a -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a (u :: * -> *).
(PrimMonad m, MVector v a) =>
MBundle m u a -> m (v (PrimState m) a)
munstream (Int -> m a -> MBundle m Any a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> m a -> Bundle m v a
MBundle.replicateM Int
n m a
m)

-- | Create a copy of a mutable vector.
clone :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m (v (PrimState m) a)
{-# INLINE clone #-}
clone :: v (PrimState m) a -> m (v (PrimState m) a)
clone v :: v (PrimState m) a
v = do
            v (PrimState m) a
v' <- Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
            v (PrimState m) a -> v (PrimState m) a -> m ()
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
            v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v'

-- Growing
-- -------

-- | Grow a vector by the given number of elements. The number must be
-- positive.
grow :: (PrimMonad m, MVector v a)
                => v (PrimState m) a -> Int -> m (v (PrimState m) a)
{-# INLINE grow #-}
grow :: v (PrimState m) a -> Int -> m (v (PrimState m) a)
grow v :: v (PrimState m) a
v by :: Int
by = BOUNDS_CHECK(checkLength) "grow" by
          (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ do v (PrimState m) a
vnew <- v (PrimState m) a -> Int -> m (v (PrimState m) a)
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
               v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
basicInitialize (v (PrimState m) a -> m ()) -> v (PrimState m) a -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) Int
by v (PrimState m) a
vnew
               v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
vnew

growFront :: (PrimMonad m, MVector v a)
                => v (PrimState m) a -> Int -> m (v (PrimState m) a)
{-# INLINE growFront #-}
growFront :: v (PrimState m) a -> Int -> m (v (PrimState m) a)
growFront v :: v (PrimState m) a
v by :: Int
by = BOUNDS_CHECK(checkLength) "growFront" by
               (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ do v (PrimState m) a
vnew <- v (PrimState m) a -> Int -> m (v (PrimState m) a)
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
                    v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
basicInitialize (v (PrimState m) a -> m ()) -> v (PrimState m) a -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice 0 Int
by v (PrimState m) a
vnew
                    v (PrimState m) a -> m (v (PrimState m) a)
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 :: v s a -> Int
enlarge_delta v :: v s a
v = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (v s a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v s a
v) 1

-- | Grow a vector logarithmically
enlarge :: (PrimMonad m, MVector v a)
                => v (PrimState m) a -> m (v (PrimState m) a)
{-# INLINE enlarge #-}
enlarge :: v (PrimState m) a -> m (v (PrimState m) a)
enlarge v :: v (PrimState m) a
v = do v (PrimState m) a
vnew <- v (PrimState m) a -> Int -> m (v (PrimState m) a)
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
               v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
basicInitialize (v (PrimState m) a -> m ()) -> v (PrimState m) a -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v) Int
by v (PrimState m) a
vnew
               v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
vnew
  where
    by :: Int
by = v (PrimState m) a -> Int
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 :: v (PrimState m) a -> m (v (PrimState m) a, Int)
enlargeFront v :: v (PrimState m) a
v = do
                   v (PrimState m) a
v' <- v (PrimState m) a -> Int -> m (v (PrimState m) a)
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
                   v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
basicInitialize (v (PrimState m) a -> m ()) -> v (PrimState m) a -> m ()
forall a b. (a -> b) -> a -> b
$ Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
basicUnsafeSlice 0 Int
by v (PrimState m) a
v'
                   (v (PrimState m) a, Int) -> m (v (PrimState m) a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v', Int
by)
  where
    by :: Int
by = v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
enlarge_delta v (PrimState m) a
v

-- | Grow a vector by the given number of elements. The number must be
-- positive but this is not checked.
unsafeGrow :: (PrimMonad m, MVector v a)
                        => v (PrimState m) a -> Int -> m (v (PrimState m) a)
{-# INLINE unsafeGrow #-}
unsafeGrow :: v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrow v :: v (PrimState m) a
v n :: Int
n = UNSAFE_CHECK(checkLength) "unsafeGrow" n
               (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> m (v (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m (v (PrimState m) 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 :: v (PrimState m) a -> Int -> m (v (PrimState m) a)
unsafeGrowFront v :: v (PrimState m) a
v by :: Int
by = UNSAFE_CHECK(checkLength) "unsafeGrowFront" by
                     (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ do
                         let n :: Int
n = v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v
                         v (PrimState m) a
v' <- Int -> m (v (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> m (v (PrimState m) a)
basicUnsafeNew (Int
byInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
n)
                         v (PrimState m) a -> v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
basicUnsafeCopy (Int -> Int -> v (PrimState m) a -> v (PrimState m) a
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
                         v (PrimState m) a -> m (v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return v (PrimState m) a
v'

-- Restricting memory usage
-- ------------------------

-- | Reset all elements of the vector to some undefined value, clearing all
-- references to external objects. This is usually a noop for unboxed vectors.
clear :: (PrimMonad m, MVector v a) => v (PrimState m) a -> m ()
{-# INLINE clear #-}
clear :: v (PrimState m) a -> m ()
clear = v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> m ()
basicClear

-- Accessing individual elements
-- -----------------------------

-- | Yield the element at the given position.
read :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m a
{-# INLINE read #-}
read :: v (PrimState m) a -> Int -> m a
read v :: v (PrimState m) a
v i :: Int
i = BOUNDS_CHECK(checkIndex) "read" i (length v)
         (m a -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> m 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

-- | Replace the element at the given position.
write :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m ()
{-# INLINE write #-}
write :: v (PrimState m) a -> Int -> a -> m ()
write v :: v (PrimState m) a
v i :: Int
i x :: a
x = BOUNDS_CHECK(checkIndex) "write" i (length v)
            (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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 the element at the given position.
modify :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> a) -> Int -> m ()
{-# INLINE modify #-}
modify :: v (PrimState m) a -> (a -> a) -> Int -> m ()
modify v :: v (PrimState m) a
v f :: a -> a
f i :: Int
i = BOUNDS_CHECK(checkIndex) "modify" i (length v)
             (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> (a -> a) -> Int -> m ()
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

-- | Swap the elements at the given positions.
swap :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> Int -> m ()
{-# INLINE swap #-}
swap :: v (PrimState m) a -> Int -> Int -> m ()
swap v :: v (PrimState m) a
v i :: Int
i j :: Int
j = BOUNDS_CHECK(checkIndex) "swap" i (length v)
           (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ BOUNDS_CHECK(checkIndex) "swap" j (length v)
           (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> Int -> m ()
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

-- | Replace the element at the given position and return the old element.
exchange :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> a -> m a
{-# INLINE exchange #-}
exchange :: v (PrimState m) a -> Int -> a -> m a
exchange v :: v (PrimState m) a
v i :: Int
i x :: a
x = BOUNDS_CHECK(checkIndex) "exchange" i (length v)
               (m a -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m a
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

-- | Yield the element at the given position. No bounds checks are performed.
unsafeRead :: (PrimMonad m, MVector v a) => v (PrimState m) a -> Int -> m a
{-# INLINE unsafeRead #-}
unsafeRead :: v (PrimState m) a -> Int -> m a
unsafeRead v :: v (PrimState m) a
v i :: Int
i = UNSAFE_CHECK(checkIndex) "unsafeRead" i (length v)
               (m a -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
basicUnsafeRead v (PrimState m) a
v Int
i

-- | Replace the element at the given position. No bounds checks are performed.
unsafeWrite :: (PrimMonad m, MVector v a)
                                => v (PrimState m) a -> Int -> a -> m ()
{-# INLINE unsafeWrite #-}
unsafeWrite :: v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v :: v (PrimState m) a
v i :: Int
i x :: a
x = UNSAFE_CHECK(checkIndex) "unsafeWrite" i (length v)
                  (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
basicUnsafeWrite v (PrimState m) a
v Int
i a
x

-- | Modify the element at the given position. No bounds checks are performed.
unsafeModify :: (PrimMonad m, MVector v a) => v (PrimState m) a -> (a -> a) -> Int -> m ()
{-# INLINE unsafeModify #-}
unsafeModify :: v (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify v :: v (PrimState m) a
v f :: a -> a
f i :: Int
i = UNSAFE_CHECK(checkIndex) "unsafeModify" i (length v)
                   (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> m a
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
basicUnsafeRead v (PrimState m) a
v Int
i m a -> (a -> m ()) -> m ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x :: a
x ->
                     v (PrimState m) a -> Int -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> a -> m ()
basicUnsafeWrite v (PrimState m) a
v Int
i (a -> a
f a
x)

-- | Swap the elements at the given positions. No bounds checks are performed.
unsafeSwap :: (PrimMonad m, MVector v a)
                => v (PrimState m) a -> Int -> Int -> m ()
{-# INLINE unsafeSwap #-}
unsafeSwap :: v (PrimState m) a -> Int -> Int -> m ()
unsafeSwap v :: v (PrimState m) a
v i :: Int
i j :: Int
j = UNSAFE_CHECK(checkIndex) "unsafeSwap" i (length v)
                 (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ UNSAFE_CHECK(checkIndex) "unsafeSwap" j (length v)
                 (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
                     a
x <- v (PrimState m) a -> Int -> m 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
                     a
y <- v (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) a
v Int
j
                     v (PrimState m) a -> Int -> a -> m ()
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
                     v (PrimState m) a -> Int -> a -> m ()
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

-- | Replace the element at the given position and return the old element. No
-- bounds checks are performed.
unsafeExchange :: (PrimMonad m, MVector v a)
                                => v (PrimState m) a -> Int -> a -> m a
{-# INLINE unsafeExchange #-}
unsafeExchange :: v (PrimState m) a -> Int -> a -> m a
unsafeExchange v :: v (PrimState m) a
v i :: Int
i x :: a
x = UNSAFE_CHECK(checkIndex) "unsafeExchange" i (length v)
                     (m a -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$ do
                         a
y <- v (PrimState m) a -> Int -> m 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
                         v (PrimState m) a -> Int -> a -> m ()
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
                         a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
y

-- Filling and copying
-- -------------------

-- | Set all elements of the vector to the given value.
set :: (PrimMonad m, MVector v a) => v (PrimState m) a -> a -> m ()
{-# INLINE set #-}
set :: v (PrimState m) a -> a -> m ()
set = v (PrimState m) a -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> a -> m ()
basicSet

-- | Copy a vector. The two vectors must have the same length and may not
-- overlap.
copy :: (PrimMonad m, MVector v a) => v (PrimState m) a   -- ^ target
                                   -> v (PrimState m) a   -- ^ source
                                   -> m ()
{-# INLINE copy #-}
copy :: v (PrimState m) a -> v (PrimState m) a -> m ()
copy dst :: v (PrimState m) a
dst src :: v (PrimState m) a
src = BOUNDS_CHECK(check) "copy" "overlapping vectors"
                                          (Bool -> Bool
not (v (PrimState m) a
dst v (PrimState m) a -> v (PrimState m) a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
`overlaps` v (PrimState m) a
src))
             (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ BOUNDS_CHECK(check) "copy" "length mismatch"
                                          (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
             (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> v (PrimState m) a -> m ()
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 the contents of a vector. The two vectors must have the same
-- length.
--
-- If the vectors do not overlap, then this is equivalent to 'copy'.
-- Otherwise, the copying is performed as if the source vector were
-- copied to a temporary vector and then the temporary vector was copied
-- to the target vector.
move :: (PrimMonad m, MVector v a)
     => v (PrimState m) a   -- ^ target
     -> v (PrimState m) a   -- ^ source
     -> m ()
{-# INLINE move #-}
move :: v (PrimState m) a -> v (PrimState m) a -> m ()
move dst :: v (PrimState m) a
dst src :: v (PrimState m) a
src = BOUNDS_CHECK(check) "move" "length mismatch"
                                          (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
             (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> v (PrimState m) a -> m ()
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

-- | Copy a vector. The two vectors must have the same length and may not
-- overlap. This is not checked.
unsafeCopy :: (PrimMonad m, MVector v a) => v (PrimState m) a   -- ^ target
                                         -> v (PrimState m) a   -- ^ source
                                         -> m ()
{-# INLINE unsafeCopy #-}
unsafeCopy :: v (PrimState m) a -> v (PrimState m) a -> m ()
unsafeCopy dst :: v (PrimState m) a
dst src :: v (PrimState m) a
src = UNSAFE_CHECK(check) "unsafeCopy" "length mismatch"
                                         (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
                   (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ UNSAFE_CHECK(check) "unsafeCopy" "overlapping vectors"
                                         (Bool -> Bool
not (v (PrimState m) a
dst v (PrimState m) a -> v (PrimState m) a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
`overlaps` v (PrimState m) a
src))
                   (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ (v (PrimState m) a
dst v (PrimState m) a -> m () -> m ()
forall a b. a -> b -> b
`seq` v (PrimState m) a
src v (PrimState m) a -> m () -> m ()
forall a b. a -> b -> b
`seq` v (PrimState m) a -> v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
basicUnsafeCopy v (PrimState m) a
dst v (PrimState m) a
src)

-- | Move the contents of a vector. The two vectors must have the same
-- length, but this is not checked.
--
-- If the vectors do not overlap, then this is equivalent to 'unsafeCopy'.
-- Otherwise, the copying is performed as if the source vector were
-- copied to a temporary vector and then the temporary vector was copied
-- to the target vector.
unsafeMove :: (PrimMonad m, MVector v a) => v (PrimState m) a   -- ^ target
                                         -> v (PrimState m) a   -- ^ source
                                         -> m ()
{-# INLINE unsafeMove #-}
unsafeMove :: v (PrimState m) a -> v (PrimState m) a -> m ()
unsafeMove dst :: v (PrimState m) a
dst src :: v (PrimState m) a
src = UNSAFE_CHECK(check) "unsafeMove" "length mismatch"
                                         (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
dst Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
src)
                   (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ (v (PrimState m) a
dst v (PrimState m) a -> m () -> m ()
forall a b. a -> b -> b
`seq` v (PrimState m) a
src v (PrimState m) a -> m () -> m ()
forall a b. a -> b -> b
`seq` v (PrimState m) a -> v (PrimState m) a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> v (PrimState m) a -> m ()
basicUnsafeMove v (PrimState m) a
dst v (PrimState m) a
src)

-- Permutations
-- ------------

accum :: (PrimMonad m, MVector v a)
      => (a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m ()
{-# INLINE accum #-}
accum :: (a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m ()
accum f :: a -> b -> a
f !v (PrimState m) a
v s :: Bundle u (Int, b)
s = ((Int, b) -> m ()) -> Bundle u (Int, b) -> m ()
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 (i :: Int
i,b :: b
b) = do
                  a
a <- BOUNDS_CHECK(checkIndex) "accum" i n
                     (m a -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> m 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
                  v (PrimState m) a -> Int -> a -> m ()
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 = v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v

update :: (PrimMonad m, MVector v a)
                        => v (PrimState m) a -> Bundle u (Int, a) -> m ()
{-# INLINE update #-}
update :: v (PrimState m) a -> Bundle u (Int, a) -> m ()
update !v (PrimState m) a
v s :: Bundle u (Int, a)
s = ((Int, a) -> m ()) -> Bundle u (Int, a) -> m ()
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 (i :: Int
i,b :: a
b) = BOUNDS_CHECK(checkIndex) "update" i n
              (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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 = v (PrimState m) a -> Int
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 :: (a -> b -> a) -> v (PrimState m) a -> Bundle u (Int, b) -> m ()
unsafeAccum f :: a -> b -> a
f !v (PrimState m) a
v s :: Bundle u (Int, b)
s = ((Int, b) -> m ()) -> Bundle u (Int, b) -> m ()
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 (i :: Int
i,b :: b
b) = do
                  a
a <- UNSAFE_CHECK(checkIndex) "accum" i n
                     (m a -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> m 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
                  v (PrimState m) a -> Int -> a -> m ()
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 = v (PrimState m) a -> Int
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 :: v (PrimState m) a -> Bundle u (Int, a) -> m ()
unsafeUpdate !v (PrimState m) a
v s :: Bundle u (Int, a)
s = ((Int, a) -> m ()) -> Bundle u (Int, a) -> m ()
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 (i :: Int
i,b :: a
b) = UNSAFE_CHECK(checkIndex) "accum" i n
                  (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ v (PrimState m) a -> Int -> a -> m ()
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 = v (PrimState m) a -> Int
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 :: v (PrimState m) a -> m ()
reverse !v (PrimState m) a
v = Int -> Int -> m ()
reverse_loop 0 (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)
  where
    reverse_loop :: Int -> Int -> m ()
reverse_loop i :: Int
i j :: Int
j | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
j = do
                                 v (PrimState m) a -> Int -> Int -> m ()
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) (Int
j Int -> Int -> Int
forall a. Num a => a -> a -> a
- 1)
    reverse_loop _ _ = () -> m ()
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 :: (a -> Bool) -> v (PrimState m) a -> m Int
unstablePartition f :: a -> Bool
f !v (PrimState m) a
v = Int -> Int -> m Int
from_left 0 (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) a
v)
  where
    -- NOTE: GHC 6.10.4 panics without the signatures on from_left and
    -- from_right
    from_left :: Int -> Int -> m Int
    from_left :: Int -> Int -> m Int
from_left i :: Int
i j :: Int
j
      | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
j    = Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
      | Bool
otherwise = do
                      a
x <- v (PrimState m) a -> Int -> m 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
                      if a -> Bool
f a
x
                        then Int -> Int -> m Int
from_left (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) Int
j
                        else Int -> Int -> m Int
from_right Int
i (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1)

    from_right :: Int -> Int -> m Int
    from_right :: Int -> Int -> m Int
from_right i :: Int
i j :: Int
j
      | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
j    = Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
      | Bool
otherwise = do
                      a
x <- v (PrimState m) a -> Int -> m a
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 <- v (PrimState m) a -> Int -> m 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
                               v (PrimState m) a -> Int -> a -> m ()
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
                               v (PrimState m) a -> Int -> a -> m ()
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
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) Int
j
                        else Int -> Int -> m Int
from_right Int
i (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1)

unstablePartitionBundle :: (PrimMonad m, MVector v a)
        => (a -> Bool) -> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
{-# INLINE unstablePartitionBundle #-}
unstablePartitionBundle :: (a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
unstablePartitionBundle f :: a -> Bool
f s :: Bundle u a
s
  = case Size -> Maybe Int
upperBound (Bundle u a -> Size
forall (v :: * -> *) a. Bundle v a -> Size
Bundle.size Bundle u a
s) of
      Just n :: Int
n  -> (a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
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
      Nothing -> (a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
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 :: (a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
unstablePartitionMax f :: a -> Bool
f s :: Bundle u a
s n :: Int
n
  = do
      v (PrimState m) a
v <- INTERNAL_CHECK(checkLength) "unstablePartitionMax" n
           (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> m (v (PrimState m) a)
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 (i :: Int
i, j :: Int
j) x :: a
x
            | a -> Bool
f a
x       = do
                            v (PrimState m) a -> Int -> a -> m ()
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
                            (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1, Int
j)
            | Bool
otherwise = do
                            v (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
unsafeWrite v (PrimState m) a
v (Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) a
x
                            (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i, Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1)

      (i :: Int
i,j :: Int
j) <- ((Int, Int) -> a -> m (Int, Int))
-> (Int, Int) -> Bundle u a -> m (Int, Int)
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 (0, Int
n) Bundle u a
s
      (v (PrimState m) a, v (PrimState m) a)
-> m (v (PrimState m) a, v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
i v (PrimState m) a
v, Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
j (Int
nInt -> Int -> Int
forall 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 :: (a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
partitionBundle f :: a -> Bool
f s :: Bundle u a
s
  = case Size -> Maybe Int
upperBound (Bundle u a -> Size
forall (v :: * -> *) a. Bundle v a -> Size
Bundle.size Bundle u a
s) of
      Just n :: Int
n  -> (a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
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
      Nothing -> (a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
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 :: (a -> Bool)
-> Bundle u a -> Int -> m (v (PrimState m) a, v (PrimState m) a)
partitionMax f :: a -> Bool
f s :: Bundle u a
s n :: Int
n
  = do
      v (PrimState m) a
v <- INTERNAL_CHECK(checkLength) "unstablePartitionMax" n
         (m (v (PrimState m) a) -> m (v (PrimState m) a))
-> m (v (PrimState m) a) -> m (v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ Int -> m (v (PrimState m) a)
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 (i :: Int
i,j :: Int
j) x :: a
x
            | a -> Bool
f a
x       = do
                            v (PrimState m) a -> Int -> a -> m ()
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
                            (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1,Int
j)

            | Bool
otherwise = let j' :: Int
j' = Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
-1 in
                          do
                            v (PrimState m) a -> Int -> a -> m ()
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
                            (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i,Int
j')

      (i :: Int
i,j :: Int
j) <- ((Int, Int) -> a -> m (Int, Int))
-> (Int, Int) -> Bundle u a -> m (Int, Int)
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 (0,Int
n) Bundle u a
s
      INTERNAL_CHECK(check) "partitionMax" "invalid indices" (i <= j)
        (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      let l :: v (PrimState m) a
l = Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
i v (PrimState m) a
v
          r :: v (PrimState m) a
r = Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice Int
j (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
j) v (PrimState m) a
v
      v (PrimState m) a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
reverse v (PrimState m) a
r
      (v (PrimState m) a, v (PrimState m) a)
-> m (v (PrimState m) a, v (PrimState m) a)
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 :: (a -> Bool)
-> Bundle u a -> m (v (PrimState m) a, v (PrimState m) a)
partitionUnknown f :: a -> Bool
f s :: Bundle u a
s
  = do
      v (PrimState m) a
v1 <- Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew 0
      v (PrimState m) a
v2 <- Int -> m (v (PrimState m) a)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew 0
      (v1' :: v (PrimState m) a
v1', n1 :: Int
n1, v2' :: v (PrimState m) a
v2', n2 :: Int
n2) <- ((v (PrimState m) a, Int, v (PrimState m) a, Int)
 -> a -> m (v (PrimState m) a, Int, v (PrimState m) a, Int))
-> (v (PrimState m) a, Int, v (PrimState m) a, Int)
-> Bundle u a
-> m (v (PrimState m) a, Int, v (PrimState m) a, Int)
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, 0, v (PrimState m) a
v2, 0) Bundle u a
s
      INTERNAL_CHECK(checkSlice) "partitionUnknown" 0 n1 (length v1')
        (m (v (PrimState m) a, v (PrimState m) a)
 -> m (v (PrimState m) a, v (PrimState m) a))
-> m (v (PrimState m) a, v (PrimState m) a)
-> m (v (PrimState m) a, v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "partitionUnknown" 0 n2 (length v2')
        (m (v (PrimState m) a, v (PrimState m) a)
 -> m (v (PrimState m) a, v (PrimState m) a))
-> m (v (PrimState m) a, v (PrimState m) a)
-> m (v (PrimState m) a, v (PrimState m) a)
forall a b. (a -> b) -> a -> b
$ (v (PrimState m) a, v (PrimState m) a)
-> m (v (PrimState m) a, v (PrimState m) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
n1 v (PrimState m) a
v1', Int -> Int -> v (PrimState m) a -> v (PrimState m) a
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
n2 v (PrimState m) a
v2')
  where
    -- NOTE: The case distinction has to be on the outside because
    -- GHC creates a join point for the unsafeWrite even when everything
    -- is inlined. This is bad because with the join point, v isn't getting
    -- unboxed.
    {-# 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 (v1 :: v (PrimState m) a
v1, i1 :: Int
i1, v2 :: v (PrimState m) a
v2, i2 :: Int
i2) x :: a
x
      | a -> Bool
f a
x       = do
                      v (PrimState m) a
v1' <- v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
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
                      (v (PrimState m) a, Int, v (PrimState m) a, Int)
-> m (v (PrimState m) a, Int, v (PrimState m) a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v1', Int
i1Int -> Int -> Int
forall a. Num a => a -> a -> a
+1, v (PrimState m) a
v2, Int
i2)
      | Bool
otherwise = do
                      v (PrimState m) a
v2' <- v (PrimState m) a -> Int -> a -> m (v (PrimState m) a)
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
                      (v (PrimState m) a, Int, v (PrimState m) a, Int)
-> m (v (PrimState m) a, Int, v (PrimState m) a, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) a
v1, Int
i1, v (PrimState m) a
v2', Int
i2Int -> Int -> Int
forall a. Num a => a -> a -> a
+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 :: (a -> Either b c)
-> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithBundle f :: a -> Either b c
f s :: Bundle u a
s
  = case Size -> Maybe Int
upperBound (Bundle u a -> Size
forall (v :: * -> *) a. Bundle v a -> Size
Bundle.size Bundle u a
s) of
      Just n :: Int
n  -> (a -> Either b c)
-> Bundle u a -> Int -> m (v (PrimState m) b, v (PrimState m) c)
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
      Nothing -> (a -> Either b c)
-> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
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 :: (a -> Either b c)
-> Bundle u a -> Int -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithMax f :: a -> Either b c
f s :: Bundle u a
s n :: Int
n
  = do
      v (PrimState m) b
v1 <- Int -> m (v (PrimState m) b)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew Int
n
      v (PrimState m) c
v2 <- Int -> m (v (PrimState m) c)
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 (i1 :: Int
i1, i2 :: Int
i2) x :: a
x = case a -> Either b c
f a
x of
            Left b :: b
b -> do
              v (PrimState m) b -> Int -> b -> m ()
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
              (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i1Int -> Int -> Int
forall a. Num a => a -> a -> a
+1, Int
i2)
            Right c :: c
c -> do
              v (PrimState m) c -> Int -> c -> m ()
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
              (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
i1, Int
i2Int -> Int -> Int
forall a. Num a => a -> a -> a
+1)
      (n1 :: Int
n1, n2 :: Int
n2) <- ((Int, Int) -> a -> m (Int, Int))
-> (Int, Int) -> Bundle u a -> m (Int, Int)
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 (0, 0) Bundle u a
s
      INTERNAL_CHECK(checkSlice) "partitionEithersMax" 0 n1 (length v1)
        (m (v (PrimState m) b, v (PrimState m) c)
 -> m (v (PrimState m) b, v (PrimState m) c))
-> m (v (PrimState m) b, v (PrimState m) c)
-> m (v (PrimState m) b, v (PrimState m) c)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "partitionEithersMax" 0 n2 (length v2)
        (m (v (PrimState m) b, v (PrimState m) c)
 -> m (v (PrimState m) b, v (PrimState m) c))
-> m (v (PrimState m) b, v (PrimState m) c)
-> m (v (PrimState m) b, v (PrimState m) c)
forall a b. (a -> b) -> a -> b
$ (v (PrimState m) b, v (PrimState m) c)
-> m (v (PrimState m) b, v (PrimState m) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> v (PrimState m) b -> v (PrimState m) b
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
n1 v (PrimState m) b
v1, Int -> Int -> v (PrimState m) c -> v (PrimState m) c
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 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 :: (a -> Either b c)
-> Bundle u a -> m (v (PrimState m) b, v (PrimState m) c)
partitionWithUnknown f :: a -> Either b c
f s :: Bundle u a
s
  = do
      v (PrimState m) b
v1 <- Int -> m (v (PrimState m) b)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew 0
      v (PrimState m) c
v2 <- Int -> m (v (PrimState m) c)
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
Int -> m (v (PrimState m) a)
unsafeNew 0
      (v1' :: v (PrimState m) b
v1', n1 :: Int
n1, v2' :: v (PrimState m) c
v2', n2 :: Int
n2) <- ((v (PrimState m) b, Int, v (PrimState m) c, Int)
 -> a -> m (v (PrimState m) b, Int, v (PrimState m) c, Int))
-> (v (PrimState m) b, Int, v (PrimState m) c, Int)
-> Bundle u a
-> m (v (PrimState m) b, Int, v (PrimState m) c, Int)
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, 0, v (PrimState m) c
v2, 0) Bundle u a
s
      INTERNAL_CHECK(checkSlice) "partitionEithersUnknown" 0 n1 (length v1')
        (m (v (PrimState m) b, v (PrimState m) c)
 -> m (v (PrimState m) b, v (PrimState m) c))
-> m (v (PrimState m) b, v (PrimState m) c)
-> m (v (PrimState m) b, v (PrimState m) c)
forall a b. (a -> b) -> a -> b
$ INTERNAL_CHECK(checkSlice) "partitionEithersUnknown" 0 n2 (length v2')
        (m (v (PrimState m) b, v (PrimState m) c)
 -> m (v (PrimState m) b, v (PrimState m) c))
-> m (v (PrimState m) b, v (PrimState m) c)
-> m (v (PrimState m) b, v (PrimState m) c)
forall a b. (a -> b) -> a -> b
$ (v (PrimState m) b, v (PrimState m) c)
-> m (v (PrimState m) b, v (PrimState m) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> v (PrimState m) b -> v (PrimState m) b
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 0 Int
n1 v (PrimState m) b
v1', Int -> Int -> v (PrimState m) c -> v (PrimState m) c
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice 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 (v1 :: v (PrimState m) b
v1, i1 :: Int
i1, v2 :: v (PrimState m) c
v2, i2 :: Int
i2) x :: a
x = case a -> Either b c
f a
x of
      Left b :: b
b -> do
        v (PrimState m) b
v1' <- v (PrimState m) b -> Int -> b -> m (v (PrimState m) b)
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
        (v (PrimState m) b, Int, v (PrimState m) c, Int)
-> m (v (PrimState m) b, Int, v (PrimState m) c, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) b
v1', Int
i1Int -> Int -> Int
forall a. Num a => a -> a -> a
+1, v (PrimState m) c
v2, Int
i2)
      Right c :: c
c -> do
        v (PrimState m) c
v2' <- v (PrimState m) c -> Int -> c -> m (v (PrimState m) c)
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
        (v (PrimState m) b, Int, v (PrimState m) c, Int)
-> m (v (PrimState m) b, Int, v (PrimState m) c, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (v (PrimState m) b
v1, Int
i1, v (PrimState m) c
v2', Int
i2Int -> Int -> Int
forall a. Num a => a -> a -> a
+1)

{-
http://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations

The following algorithm generates the next permutation lexicographically after
a given permutation. It changes the given permutation in-place.

1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists,
   the permutation is the last permutation.
2. Find the largest index l greater than k such that a[k] < a[l].
3. Swap the value of a[k] with that of a[l].
4. Reverse the sequence from a[k + 1] up to and including the final element a[n]
-}

-- | Compute the next (lexicographically) permutation of given vector in-place.
--   Returns False when input is the last permutation
nextPermutation :: (PrimMonad m,Ord e,MVector v e) => v (PrimState m) e -> m Bool
nextPermutation :: v (PrimState m) e -> m Bool
nextPermutation v :: v (PrimState m) e
v
    | Int
dim Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 2 = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    | Bool
otherwise = do
        e
val <- v (PrimState m) e -> Int -> m e
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) e
v 0
        (k :: Int
k,l :: Int
l) <- e -> Int -> Int -> e -> Int -> m (Int, Int)
loop e
val (-1) 0 e
val 1
        if Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0
         then Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
         else v (PrimState m) e -> Int -> Int -> m ()
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 m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
              v (PrimState m) e -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> m ()
reverse (Int -> Int -> v (PrimState m) e -> v (PrimState m) e
forall (v :: * -> * -> *) a s.
MVector v a =>
Int -> Int -> v s a -> v s a
unsafeSlice (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) (Int
dimInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) v (PrimState m) e
v) m () -> m Bool -> m Bool
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
              Bool -> m Bool
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 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
dim = (Int, Int) -> m (Int, Int)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
k,Int
l)
              | Bool
otherwise  = do
                  e
cur <- v (PrimState m) e -> Int -> m e
forall (m :: * -> *) (v :: * -> * -> *) a.
(PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
unsafeRead v (PrimState m) e
v Int
i
                  -- TODO: make tuple unboxed
                  let (kval' :: e
kval',k' :: Int
k') = if e
prev e -> e -> Bool
forall a. Ord a => a -> a -> Bool
< e
cur then (e
prev,Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-1) else (e
kval,Int
k)
                      l' :: Int
l' = if e
kval' e -> e -> Bool
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
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1)
          dim :: Int
dim = v (PrimState m) e -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
length v (PrimState m) e
v