{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Data.Massiv.Array.Stencil.Unsafe
(
forStencilUnsafe
) where
import Data.Massiv.Core.Common
import Data.Massiv.Array.Delayed.Windowed (Window(..), DW, Array(..))
import GHC.Exts (inline)
forStencilUnsafe ::
(Source r ix e, Manifest r ix e)
=> Array r ix e
-> Sz ix
-> ix
-> ((ix -> Maybe e) -> a)
-> Array DW ix a
forStencilUnsafe !arr !sSz !sCenter relStencil =
DWArray
(DArray (getComp arr) sz (stencil (index arr)))
(Just sSz)
(Just window)
where
!window =
Window
{ windowStart = liftIndex2 min sCenter (liftIndex (max 0) (liftIndex (subtract 1) sz))
, windowSize = liftIndex (max 0) (liftIndex2 min windowSz (liftIndex2 (-) sz sCenter))
, windowIndex = stencil (Just . unsafeIndex arr)
}
!windowSz = liftIndex (max 0) (liftIndex2 (-) sz (liftIndex (subtract 1) sSz))
stencil getVal !ix = inline relStencil $ \ !ixD -> getVal (liftIndex2 (+) ix ixD)
{-# INLINE stencil #-}
!sz = size arr
{-# INLINE forStencilUnsafe #-}