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 :: !(FPtr.ForeignPtr a),
ptr :: !(Ptr a),
left :: !Int
}
cons :: Storable a => VB.Vector a -> Pointer a
cons (VB.SV fp s l) =
Pointer fp (advancePtr (Unsafe.foreignPtrToPtr fp) s) l
viewL :: Storable a => Pointer a -> Maybe (a, Pointer a)
viewL = switchL Nothing (curry Just)
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) (l1))