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