{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Data.Vector.NonEmpty.Mutable
(
NonEmptyMVector
, NonEmptyIOVector
, NonEmptySTVector
, length
, slice, init, tail, take, drop, splitAt
, unsafeSlice, unsafeTake, unsafeDrop
, overlaps
, fromMVector, toMVector, unsafeFromMVector
, new, new1, unsafeNew
, replicate, replicate1
, replicateM, replicate1M
, clone
, grow, unsafeGrow
, clear
, read, write, modify, swap
, unsafeRead, unsafeWrite, unsafeModify, unsafeSwap
, nextPermutation
, set, copy, move, unsafeCopy, unsafeMove
) where
import Prelude (Bool, Int, Ord, (.), max)
import Control.Monad.Primitive
import Data.Functor
import Data.Maybe (Maybe(..))
import Data.Vector.Mutable (MVector)
import qualified Data.Vector.Mutable as M
import Data.Vector.NonEmpty.Internal
length :: NonEmptyMVector s a -> Int
length :: forall s a. NonEmptyMVector s a -> Int
length = forall s a. MVector s a -> Int
M.length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE length #-}
slice :: Int -> Int -> NonEmptyMVector s a -> MVector s a
slice :: forall s a. Int -> Int -> NonEmptyMVector s a -> MVector s a
slice Int
n Int
m = forall s a. Int -> Int -> MVector s a -> MVector s a
M.slice Int
n Int
m forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE slice #-}
take :: Int -> NonEmptyMVector s a -> MVector s a
take :: forall s a. Int -> NonEmptyMVector s a -> MVector s a
take Int
n = forall s a. Int -> MVector s a -> MVector s a
M.take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE take #-}
drop :: Int -> NonEmptyMVector s a -> MVector s a
drop :: forall s a. Int -> NonEmptyMVector s a -> MVector s a
drop Int
n = forall s a. Int -> MVector s a -> MVector s a
M.drop Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE drop #-}
splitAt :: Int -> NonEmptyMVector s a -> (MVector s a, MVector s a)
splitAt :: forall s a.
Int -> NonEmptyMVector s a -> (MVector s a, MVector s a)
splitAt Int
n = forall s a. Int -> MVector s a -> (MVector s a, MVector s a)
M.splitAt Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE splitAt #-}
init :: NonEmptyMVector s a -> MVector s a
init :: forall s a. NonEmptyMVector s a -> MVector s a
init = forall s a. MVector s a -> MVector s a
M.unsafeInit forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE init #-}
tail :: NonEmptyMVector s a -> MVector s a
tail :: forall s a. NonEmptyMVector s a -> MVector s a
tail = forall s a. MVector s a -> MVector s a
M.unsafeTail forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE tail #-}
unsafeSlice
:: Int
-> Int
-> NonEmptyMVector s a
-> MVector s a
unsafeSlice :: forall s a. Int -> Int -> NonEmptyMVector s a -> MVector s a
unsafeSlice Int
n Int
m = forall s a. Int -> Int -> MVector s a -> MVector s a
M.unsafeSlice Int
n Int
m forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE unsafeSlice #-}
unsafeTake :: Int -> NonEmptyMVector s a -> MVector s a
unsafeTake :: forall s a. Int -> NonEmptyMVector s a -> MVector s a
unsafeTake Int
n = forall s a. Int -> MVector s a -> MVector s a
M.unsafeTake Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE unsafeTake #-}
unsafeDrop :: Int -> NonEmptyMVector s a -> MVector s a
unsafeDrop :: forall s a. Int -> NonEmptyMVector s a -> MVector s a
unsafeDrop Int
n = forall s a. Int -> MVector s a -> MVector s a
M.unsafeDrop Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE unsafeDrop #-}
overlaps :: NonEmptyMVector s a -> NonEmptyMVector s a -> Bool
overlaps :: forall s a. NonEmptyMVector s a -> NonEmptyMVector s a -> Bool
overlaps (NonEmptyMVector MVector s a
v) (NonEmptyMVector MVector s a
u) = forall s a. MVector s a -> MVector s a -> Bool
M.overlaps MVector s a
v MVector s a
u
{-# INLINE overlaps #-}
fromMVector :: MVector s a -> Maybe (NonEmptyMVector s a)
fromMVector :: forall s a. MVector s a -> Maybe (NonEmptyMVector s a)
fromMVector MVector s a
v = if forall s a. MVector s a -> Bool
M.null MVector s a
v then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just (forall s a. MVector s a -> NonEmptyMVector s a
NonEmptyMVector MVector s a
v)
toMVector :: NonEmptyMVector s a -> MVector s a
toMVector :: forall s a. NonEmptyMVector s a -> MVector s a
toMVector = forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
unsafeFromMVector :: MVector s a -> NonEmptyMVector s a
unsafeFromMVector :: forall s a. MVector s a -> NonEmptyMVector s a
unsafeFromMVector = forall s a. MVector s a -> NonEmptyMVector s a
NonEmptyMVector
{-# INLINE unsafeFromMVector #-}
new
:: PrimMonad m
=> Int
-> m (Maybe (NonEmptyMVector (PrimState m) a))
new :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (Maybe (NonEmptyMVector (PrimState m) a))
new = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> Maybe (NonEmptyMVector s a)
fromMVector forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MVector (PrimState m) a)
M.new
{-# INLINE new #-}
new1
:: PrimMonad m
=> Int
-> m (NonEmptyMVector (PrimState m) a)
new1 :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (NonEmptyMVector (PrimState m) a)
new1 Int
n = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> NonEmptyMVector s a
unsafeFromMVector (forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MVector (PrimState m) a)
M.new (forall a. Ord a => a -> a -> a
max Int
n Int
1))
{-# INLINE new1 #-}
unsafeNew
:: PrimMonad m
=> Int
-> m (Maybe (NonEmptyMVector (PrimState m) a))
unsafeNew :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (Maybe (NonEmptyMVector (PrimState m) a))
unsafeNew = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> Maybe (NonEmptyMVector s a)
fromMVector forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MVector (PrimState m) a)
M.unsafeNew
{-# INLINE unsafeNew #-}
replicate
:: PrimMonad m
=> Int
-> a
-> m (Maybe (NonEmptyMVector (PrimState m) a))
replicate :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (Maybe (NonEmptyMVector (PrimState m) a))
replicate Int
n a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> Maybe (NonEmptyMVector s a)
fromMVector (forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MVector (PrimState m) a)
M.replicate Int
n a
a)
{-# INLINE replicate #-}
replicate1
:: PrimMonad m
=> Int
-> a
-> m (NonEmptyMVector (PrimState m) a)
replicate1 :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (NonEmptyMVector (PrimState m) a)
replicate1 Int
n a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> NonEmptyMVector s a
unsafeFromMVector (forall (m :: * -> *) a.
PrimMonad m =>
Int -> a -> m (MVector (PrimState m) a)
M.replicate (forall a. Ord a => a -> a -> a
max Int
n Int
1) a
a)
{-# INLINE replicate1 #-}
replicateM
:: PrimMonad m
=> Int
-> m a
-> m (Maybe (NonEmptyMVector (PrimState m) a))
replicateM :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> m a -> m (Maybe (NonEmptyMVector (PrimState m) a))
replicateM Int
n m a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> Maybe (NonEmptyMVector s a)
fromMVector (forall (m :: * -> *) a.
PrimMonad m =>
Int -> m a -> m (MVector (PrimState m) a)
M.replicateM Int
n m a
a)
{-# INLINE replicateM #-}
replicate1M
:: PrimMonad m
=> Int
-> m a
-> m (Maybe (NonEmptyMVector (PrimState m) a))
replicate1M :: forall (m :: * -> *) a.
PrimMonad m =>
Int -> m a -> m (Maybe (NonEmptyMVector (PrimState m) a))
replicate1M Int
n m a
a = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> Maybe (NonEmptyMVector s a)
fromMVector (forall (m :: * -> *) a.
PrimMonad m =>
Int -> m a -> m (MVector (PrimState m) a)
M.replicateM (forall a. Ord a => a -> a -> a
max Int
n Int
1) m a
a)
{-# INLINE replicate1M #-}
clone
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> m (NonEmptyMVector (PrimState m) a)
clone :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a
-> m (NonEmptyMVector (PrimState m) a)
clone (NonEmptyMVector MVector (PrimState m) a
v) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> NonEmptyMVector s a
NonEmptyMVector (forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> m (MVector (PrimState m) a)
M.clone MVector (PrimState m) a
v)
{-# INLINE clone #-}
grow
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> Int
-> m (NonEmptyMVector (PrimState m) a)
grow :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a
-> Int -> m (NonEmptyMVector (PrimState m) a)
grow (NonEmptyMVector MVector (PrimState m) a
v) Int
n = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> NonEmptyMVector s a
NonEmptyMVector (forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
M.grow MVector (PrimState m) a
v Int
n)
{-# INLINE grow #-}
unsafeGrow
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> Int
-> m (NonEmptyMVector (PrimState m) a)
unsafeGrow :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a
-> Int -> m (NonEmptyMVector (PrimState m) a)
unsafeGrow (NonEmptyMVector MVector (PrimState m) a
v) Int
n = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s a. MVector s a -> NonEmptyMVector s a
NonEmptyMVector (forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> m (MVector (PrimState m) a)
M.unsafeGrow MVector (PrimState m) a
v Int
n)
{-# INLINE unsafeGrow #-}
clear :: PrimMonad m => NonEmptyMVector (PrimState m) a -> m ()
clear :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> m ()
clear = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> m ()
M.clear forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE clear #-}
read
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> Int
-> m a
read :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> Int -> m a
read (NonEmptyMVector MVector (PrimState m) a
v) Int
n = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> m a
M.read MVector (PrimState m) a
v Int
n
{-# INLINE read #-}
write
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> Int
-> a
-> m ()
write :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> Int -> a -> m ()
write (NonEmptyMVector MVector (PrimState m) a
v) Int
n a
a = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> a -> m ()
M.write MVector (PrimState m) a
v Int
n a
a
{-# INLINE write #-}
modify
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> (a -> a)
-> Int
-> m ()
modify :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> (a -> a) -> Int -> m ()
modify (NonEmptyMVector MVector (PrimState m) a
v) a -> a
f Int
n = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> (a -> a) -> Int -> m ()
M.modify MVector (PrimState m) a
v a -> a
f Int
n
{-# INLINE modify #-}
swap
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> Int
-> Int
-> m ()
swap :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> Int -> Int -> m ()
swap (NonEmptyMVector MVector (PrimState m) a
v) Int
n Int
m = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> Int -> m ()
M.swap MVector (PrimState m) a
v Int
n Int
m
{-# INLINE swap #-}
unsafeRead
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> Int
-> m a
unsafeRead :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> Int -> m a
unsafeRead (NonEmptyMVector MVector (PrimState m) a
v) Int
n = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> m a
M.unsafeRead MVector (PrimState m) a
v Int
n
{-# INLINE unsafeRead #-}
unsafeWrite
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> Int
-> a
-> m ()
unsafeWrite :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> Int -> a -> m ()
unsafeWrite (NonEmptyMVector MVector (PrimState m) a
v) Int
n a
a = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> a -> m ()
M.unsafeWrite MVector (PrimState m) a
v Int
n a
a
{-# INLINE unsafeWrite #-}
unsafeModify
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> (a -> a)
-> Int
-> m ()
unsafeModify :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> (a -> a) -> Int -> m ()
unsafeModify (NonEmptyMVector MVector (PrimState m) a
v) a -> a
f Int
n = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> (a -> a) -> Int -> m ()
M.unsafeModify MVector (PrimState m) a
v a -> a
f Int
n
{-# INLINE unsafeModify #-}
unsafeSwap :: PrimMonad m => NonEmptyMVector (PrimState m) a -> Int -> Int -> m ()
unsafeSwap :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> Int -> Int -> m ()
unsafeSwap (NonEmptyMVector MVector (PrimState m) a
v) Int
n Int
m = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> Int -> m ()
M.unsafeSwap MVector (PrimState m) a
v Int
n Int
m
{-# INLINE unsafeSwap #-}
set :: PrimMonad m => NonEmptyMVector (PrimState m) a -> a -> m ()
set :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a -> a -> m ()
set (NonEmptyMVector MVector (PrimState m) a
v) a
a = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> a -> m ()
M.set MVector (PrimState m) a
v a
a
{-# INLINE set #-}
copy
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a
-> m ()
copy :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a -> m ()
copy (NonEmptyMVector MVector (PrimState m) a
v) (NonEmptyMVector MVector (PrimState m) a
v') = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
M.copy MVector (PrimState m) a
v MVector (PrimState m) a
v'
{-# INLINE copy #-}
unsafeCopy
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a
-> m ()
unsafeCopy :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a -> m ()
unsafeCopy (NonEmptyMVector MVector (PrimState m) a
v) (NonEmptyMVector MVector (PrimState m) a
v') = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
M.unsafeCopy MVector (PrimState m) a
v MVector (PrimState m) a
v'
{-# INLINE unsafeCopy #-}
move
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a -> m ()
move :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a -> m ()
move (NonEmptyMVector MVector (PrimState m) a
v) (NonEmptyMVector MVector (PrimState m) a
v') = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
M.move MVector (PrimState m) a
v MVector (PrimState m) a
v'
{-# INLINE move #-}
unsafeMove
:: PrimMonad m
=> NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a
-> m ()
unsafeMove :: forall (m :: * -> *) a.
PrimMonad m =>
NonEmptyMVector (PrimState m) a
-> NonEmptyMVector (PrimState m) a -> m ()
unsafeMove (NonEmptyMVector MVector (PrimState m) a
v) (NonEmptyMVector MVector (PrimState m) a
v') = forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> MVector (PrimState m) a -> m ()
M.unsafeMove MVector (PrimState m) a
v MVector (PrimState m) a
v'
{-# INLINE unsafeMove #-}
nextPermutation
:: (PrimMonad m,Ord e)
=> NonEmptyMVector (PrimState m) e
-> m Bool
nextPermutation :: forall (m :: * -> *) e.
(PrimMonad m, Ord e) =>
NonEmptyMVector (PrimState m) e -> m Bool
nextPermutation = forall (m :: * -> *) e.
(PrimMonad m, Ord e) =>
MVector (PrimState m) e -> m Bool
M.nextPermutation forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s a. NonEmptyMVector s a -> MVector s a
_nemVec
{-# INLINE nextPermutation #-}