module Data.StorableVector.Pointer (
Pointer(..),
cons,
viewL,
switchL,
) where
import qualified Data.StorableVector.Base as VB
import qualified Foreign.ForeignPtr as FPtr
import Foreign.Marshal.Array (advancePtr, )
import Foreign.Storable (Storable, peek, )
import Foreign (Ptr, )
import qualified System.Unsafe as Unsafe
data Pointer a =
Pointer {
fptr :: {-# UNPACK #-} !(FPtr.ForeignPtr a),
ptr :: {-# UNPACK #-} !(Ptr a),
left :: {-# UNPACK #-} !Int
}
{-# INLINE cons #-}
cons :: Storable a => VB.Vector a -> Pointer a
cons (VB.SV fp s l) =
Pointer fp (advancePtr (Unsafe.foreignPtrToPtr fp) s) l
{-# INLINE viewL #-}
viewL :: Storable a => Pointer a -> Maybe (a, Pointer a)
viewL = switchL Nothing (curry Just)
{-# INLINE switchL #-}
switchL :: Storable a =>
b -> (a -> Pointer a -> b) -> Pointer a -> b
switchL n j (Pointer fp p l) =
if l<=0
then n
else j (VB.inlinePerformIO (peek p)) (Pointer fp (advancePtr p 1) (l-1))