{-# LANGUAGE Rank2Types, FlexibleContexts, BangPatterns #-} module Data.RangeMin.Common.Vector (inlineCreate, write, read, new, newWith, drop, slice, sliceM, unsafeFreeze, minIndex, (!), getRow, G.Vector, GM.MVector, G.Mutable) where import Control.Monad.Primitive import qualified Data.Vector.Generic as G import qualified Data.Vector.Generic.Mutable as GM import Data.RangeMin.Common.ST import qualified Data.Vector.Generic.New as New import Prelude hiding (read, drop) {-# INLINE unsafeFreeze #-} unsafeFreeze :: (PrimMonad m, G.Vector v a) => G.Mutable v (PrimState m) a -> m (v a) unsafeFreeze = G.unsafeFreeze sliceM :: GM.MVector v a => Int -> Int -> v s a -> v s a sliceM = GM.unsafeSlice {-# INLINE write #-} write :: (PrimMonad m, GM.MVector v a) => v (PrimState m) a -> Int -> a -> m () write = GM.unsafeWrite {-# INLINE read #-} read :: (PrimMonad m, GM.MVector v a) => v (PrimState m) a -> Int -> m a read = GM.unsafeRead {-# INLINE new #-} new :: (PrimMonad m, GM.MVector v a) => Int -> m (v (PrimState m) a) new = GM.unsafeNew {-# INLINE newWith #-} newWith :: (PrimMonad m, GM.MVector v a) => Int -> a -> m (v (PrimState m) a) newWith = GM.unsafeNewWith {-# INLINE drop #-} drop :: G.Vector v a => Int -> v a -> v a drop = G.unsafeDrop {-# INLINE slice #-} slice :: G.Vector v a => Int -> Int -> v a -> v a slice = G.unsafeSlice {-# INLINE minIndex #-} minIndex :: (G.Vector v a, Ord a) => v a -> Int -> Int -> Int minIndex !xs !i !j | xs ! i <= xs ! j = i | otherwise = j {-# INLINE (!) #-} (!) :: G.Vector v a => v a -> Int -> a (!) = G.unsafeIndex {-# INLINE getRow #-} getRow :: G.Vector v a => Int -> v a -> Int -> Int -> a getRow !cols !vec !i = (!) row where !row = drop (i * cols) vec {-# INLINE [1] inlineCreate #-} inlineCreate :: G.Vector v a => (forall s . ST s (G.Mutable v s a)) -> v a inlineCreate m = inlineNew (New.New m) {-# INLINE inlineNew #-} inlineNew :: G.Vector v a => New.New v a -> v a inlineNew !m = inlineRunST (unsafeFreeze =<< New.run m)