module Data.ABC.Internal.VecPtr (
Vec_Ptr_t_
, Vec_Ptr_t
, clearVec
, vecPtrSize
, vecPtrArray
, vecPtrEntry
, clearVecPtr
) where
import Control.Exception (assert)
import Foreign
import Foreign.C
import Data.ABC.Internal.Field
data Vec_Ptr_t_
type Vec_Ptr_t = Ptr (Vec_Ptr_t_)
vecSizeField :: Field Vec_Ptr_t CInt
vecSizeField = fieldFromOffset (4)
clearVec :: Vec_Ptr_t -> IO ()
clearVec v = writeAt vecSizeField v 0
vecPtrSize :: Vec_Ptr_t -> IO Int
vecPtrSize v = fromIntegral `fmap` readAt vecSizeField v
vecPtrArray :: Vec_Ptr_t -> IO (Ptr (Ptr a))
vecPtrArray v = castPtr `fmap` (\ptr -> do {peekByteOff ptr 8 ::IO (Ptr (Ptr ()))}) v
clearVecPtr :: Vec_Ptr_t -> IO ()
clearVecPtr v = writeAt vecSizeField v 0
vecPtrEntry :: Vec_Ptr_t -> Int -> IO (Ptr a)
vecPtrEntry v i = do
assert (i >= 0) $ return ()
do sz <- vecPtrSize v
assert (i < sz) $ return ()
a <- vecPtrArray v
peekElemOff a i