module Data.Repa.Convert.Format.Numeric
( IntAsc (..)
, IntAsc0 (..)
, DoubleAsc (..))
where
import Data.Repa.Convert.Format.Base
import Data.Repa.Convert.Format.Lists
import qualified Data.Repa.Scalar.Int as S
import qualified Data.Repa.Scalar.Double as S
import qualified Foreign.ForeignPtr as F
import qualified Foreign.Marshal.Utils as F
import qualified Foreign.Ptr as F
import Prelude hiding (fail)
data IntAsc = IntAsc deriving (Eq, Show)
instance Format IntAsc where
type Value IntAsc = Int
fieldCount _ = 1
minSize _ = 1
fixedSize _ = Nothing
packedSize _ _ = Just 20
instance Packable IntAsc where
pack IntAsc v
= pack VarAsc (show v)
unpack IntAsc
= Unpacker $ \start end _stop fail eat
-> let !len = F.minusPtr end start in
if len > 0
then do
r <- S.loadInt start len
case r of
Just (n, o) -> eat (F.plusPtr start o) n
Nothing -> fail
else fail
data IntAsc0 = IntAsc0 Int deriving (Eq, Show)
instance Format IntAsc0 where
type Value IntAsc0 = Int
fieldCount _ = 1
minSize _ = 1
fixedSize _ = Nothing
packedSize (IntAsc0 n) _ = Just (n + 20)
instance Packable IntAsc0 where
pack (IntAsc0 n) v
= let s = show v
s' = replicate (n length s) '0' ++ s
in pack VarAsc s'
unpack (IntAsc0 _)
= Unpacker $ \start end _stop fail eat
-> let !len = F.minusPtr end start in
if len > 0
then do
r <- S.loadInt start len
case r of
Just (n, o) -> eat (F.plusPtr start o) n
Nothing -> fail
else fail
data DoubleAsc = DoubleAsc deriving (Eq, Show)
instance Format DoubleAsc where
type Value DoubleAsc = Double
fieldCount _ = 1
minSize _ = 1
fixedSize _ = Nothing
packedSize _ _ = Just 24
instance Packable DoubleAsc where
pack DoubleAsc v
= Packer $ \buf k
-> do (fptr, len) <- S.storeDoubleShortest v
F.withForeignPtr fptr $ \ptr
-> F.copyBytes buf ptr len
k (F.plusPtr buf len)
unpack DoubleAsc
= Unpacker $ \start end _stop fail eat
-> let !len = F.minusPtr end start in
if len > 0
then do
(v, o) <- S.loadDouble start len
eat (F.plusPtr start o) v
else fail