{-# LANGUAGE CPP, MultiParamTypeClasses, BangPatterns, TypeFamilies #-}
module Data.Vector.Generic.Mutable.Base (
MVector(..)
) where
import Control.Monad.Primitive ( PrimMonad, PrimState )
#define NOT_VECTOR_MODULE
#include "vector.h"
class MVector v a where
basicLength :: v s a -> Int
basicUnsafeSlice :: Int
-> Int
-> v s a
-> v s a
basicOverlaps :: v s a -> v s a -> Bool
basicUnsafeNew :: PrimMonad m => Int -> m (v (PrimState m) a)
basicInitialize :: PrimMonad m => v (PrimState m) a -> m ()
basicUnsafeReplicate :: PrimMonad m => Int -> a -> m (v (PrimState m) a)
basicUnsafeRead :: PrimMonad m => v (PrimState m) a -> Int -> m a
basicUnsafeWrite :: PrimMonad m => v (PrimState m) a -> Int -> a -> m ()
basicClear :: PrimMonad m => v (PrimState m) a -> m ()
basicSet :: PrimMonad m => v (PrimState m) a -> a -> m ()
basicUnsafeCopy :: PrimMonad m => v (PrimState m) a
-> v (PrimState m) a
-> m ()
basicUnsafeMove :: PrimMonad m => v (PrimState m) a
-> v (PrimState m) a
-> m ()
basicUnsafeGrow :: PrimMonad m => v (PrimState m) a -> Int
-> m (v (PrimState m) a)
{-# INLINE basicUnsafeReplicate #-}
basicUnsafeReplicate n :: Int
n x :: a
x
= do
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
n
v (PrimState m) a -> a -> m ()
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> a -> m ()
basicSet v (PrimState m) a
v 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
{-# INLINE basicClear #-}
basicClear _ = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE basicSet #-}
basicSet !v (PrimState m) a
v x :: a
x
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 0 = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do
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 0 a
x
Int -> m ()
do_set 1
where
!n :: Int
n = v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength v (PrimState m) a
v
do_set :: Int -> m ()
do_set i :: Int
i | 2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = do 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
i 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
basicUnsafeSlice 0 Int
i v (PrimState m) a
v)
Int -> m ()
do_set (2Int -> Int -> Int
forall a. Num a => a -> a -> a
*Int
i)
| Bool
otherwise = 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
i (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-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
basicUnsafeSlice 0 (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
i) v (PrimState m) a
v)
{-# INLINE basicUnsafeCopy #-}
basicUnsafeCopy !v (PrimState m) a
dst !v (PrimState m) a
src = Int -> m ()
do_copy 0
where
!n :: Int
n = v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength v (PrimState m) a
src
do_copy :: Int -> m ()
do_copy 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 (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
v (PrimState m) a -> Int -> m a
basicUnsafeRead v (PrimState m) a
src Int
i
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
dst Int
i a
x
Int -> m ()
do_copy (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1)
| Bool
otherwise = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# INLINE basicUnsafeMove #-}
basicUnsafeMove !v (PrimState m) a
dst !v (PrimState m) a
src
| v (PrimState m) a -> v (PrimState m) a -> Bool
forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> v s a -> Bool
basicOverlaps v (PrimState m) a
dst v (PrimState m) a
src = do
v (PrimState m) a
srcCopy <- Int -> m (v (PrimState m) a)
forall (v :: * -> * -> *) a (m :: * -> *).
(MVector v a, PrimMonad m) =>
Int -> m (v (PrimState m) a)
basicUnsafeNew (v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength v (PrimState m) a
src)
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
srcCopy v (PrimState m) a
src
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
srcCopy
| Bool
otherwise = 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
{-# INLINE basicUnsafeGrow #-}
basicUnsafeGrow v :: v (PrimState m) a
v by :: Int
by
= do
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
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
by)
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 0 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'
where
n :: Int
n = v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
basicLength v (PrimState m) a
v