-- GENERATED by C->Haskell Compiler, version 0.20.1 The shapeless maps, 31 Oct 2014 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "src/Data/ABC/Internal/VecInt.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

{- |
Module      : Data.ABC.Internal.VecInt
Copyright   : Galois, Inc. 2010
License     : BSD3
Maintainer  : jhendrix@galois.com
Stability   : experimental
Portability : non-portable (c2hs, language extensions)

/Incomplete./ Binding of @misc\/vec\/vecInt.h@ for manipulating
vectors of integers.

-}

module Data.ABC.Internal.VecInt (
      Vec_Int_t_
    , Vec_Int_t
    , clearVecInt
    , vecIntSize , setVecIntSize
    , vecIntCap  , setVecIntCap
    , vecIntArray, setVecIntArray
    , vecIntEntry
    , vecIntWriteEntry
    , withVecInt
    ) where





import Control.Exception (bracket)
import Foreign.C
import Foreign
import Data.ABC.Internal.Field

data Vec_Int_t_

type Vec_Int_t = Ptr (Vec_Int_t_)
{-# LINE 41 "src/Data/ABC/Internal/VecInt.chs" #-}


vecIntSizeField :: Field Vec_Int_t CInt
vecIntSizeField = fieldFromOffset (4)
{-# LINE 44 "src/Data/ABC/Internal/VecInt.chs" #-}


vecIntSize :: Vec_Int_t -> IO CInt
vecIntSize = readAt vecIntSizeField

setVecIntSize :: Vec_Int_t -> CInt -> IO ()
setVecIntSize = (\ptr val -> do {pokeByteOff ptr 4 (val::CInt)})
{-# LINE 50 "src/Data/ABC/Internal/VecInt.chs" #-}


vecIntCap :: Vec_Int_t -> IO CInt
vecIntCap = (\ptr -> do {peekByteOff ptr 0 ::IO CInt})
{-# LINE 53 "src/Data/ABC/Internal/VecInt.chs" #-}


setVecIntCap :: Vec_Int_t -> CInt -> IO ()
setVecIntCap = (\ptr val -> do {pokeByteOff ptr 0 (val::CInt)})
{-# LINE 56 "src/Data/ABC/Internal/VecInt.chs" #-}


clearVecInt :: Vec_Int_t -> IO ()
clearVecInt v = writeAt vecIntSizeField v 0

vecIntArray :: Vec_Int_t -> IO (Ptr CInt)
vecIntArray = (\ptr -> do {peekByteOff ptr 8 ::IO (Ptr CInt)})
{-# LINE 62 "src/Data/ABC/Internal/VecInt.chs" #-}


setVecIntArray :: Vec_Int_t -> Ptr CInt -> IO ()
setVecIntArray = (\ptr val -> do {pokeByteOff ptr 8 (val::(Ptr CInt))})
{-# LINE 65 "src/Data/ABC/Internal/VecInt.chs" #-}


-- | Get entry in vector at given index.
vecIntEntry :: Vec_Int_t -> CInt -> IO CInt
vecIntEntry v i = do
  a <- vecIntArray v
  peekElemOff a (fromIntegral i)

-- | Write entry in vector at given index.
vecIntWriteEntry :: Vec_Int_t -> CInt -> CInt -> IO ()
vecIntWriteEntry v i u = do
  a <- vecIntArray v
  pokeElemOff a (fromIntegral i) u

withVecInt :: CInt -> Ptr CInt -> (Vec_Int_t -> IO a) -> IO a
withVecInt sz p act = do
  bracket (mallocBytes 16) free $ \v -> do
    setVecIntSize v sz
    setVecIntCap  v sz
    setVecIntArray v p
    act v