{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ConstraintKinds #-}
{-# OPTIONS_GHC -fno-prof-auto #-}
module Basement.Types.OffsetSize
( FileSize(..)
, Offset(..)
, Offset8
, sentinel
, offsetOfE
, offsetPlusE
, offsetMinusE
, offsetRecast
, offsetCast
, offsetSub
, offsetShiftL
, offsetShiftR
, sizeCast
, sizeLastOffset
, sizeAsOffset
, sizeSub
, countOfRoundUp
, offsetAsSize
, (+.)
, (.==#)
, CountOf(..)
, sizeOfE
, csizeOfOffset
, csizeOfSize
, sizeOfCSSize
, sizeOfCSize
, Countable
, Offsetable
, natValCountOf
, natValOffset
) where
#include "MachDeps.h"
import GHC.Types
import GHC.Word
import GHC.Int
import GHC.Prim
import qualified GHC.Prim
import System.Posix.Types (CSsize (..))
import Data.Bits
import Basement.Compat.Base
import Basement.Compat.C.Types
import Basement.Compat.Semigroup
import Data.Proxy
import Basement.Numerical.Number
import Basement.Numerical.Additive
import Basement.Numerical.Subtractive
import Basement.Numerical.Multiplicative
import Basement.Numerical.Conversion (intToWord)
import Basement.Nat
import Basement.IntegralConv
import Data.List (foldl')
import qualified Prelude
#if WORD_SIZE_IN_BITS < 64
import GHC.IntWord64
#endif
newtype FileSize = FileSize Word64
deriving (Int -> FileSize -> ShowS
[FileSize] -> ShowS
FileSize -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FileSize] -> ShowS
$cshowList :: [FileSize] -> ShowS
show :: FileSize -> String
$cshow :: FileSize -> String
showsPrec :: Int -> FileSize -> ShowS
$cshowsPrec :: Int -> FileSize -> ShowS
Show,FileSize -> FileSize -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileSize -> FileSize -> Bool
$c/= :: FileSize -> FileSize -> Bool
== :: FileSize -> FileSize -> Bool
$c== :: FileSize -> FileSize -> Bool
Eq,Eq FileSize
FileSize -> FileSize -> Bool
FileSize -> FileSize -> Ordering
FileSize -> FileSize -> FileSize
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: FileSize -> FileSize -> FileSize
$cmin :: FileSize -> FileSize -> FileSize
max :: FileSize -> FileSize -> FileSize
$cmax :: FileSize -> FileSize -> FileSize
>= :: FileSize -> FileSize -> Bool
$c>= :: FileSize -> FileSize -> Bool
> :: FileSize -> FileSize -> Bool
$c> :: FileSize -> FileSize -> Bool
<= :: FileSize -> FileSize -> Bool
$c<= :: FileSize -> FileSize -> Bool
< :: FileSize -> FileSize -> Bool
$c< :: FileSize -> FileSize -> Bool
compare :: FileSize -> FileSize -> Ordering
$ccompare :: FileSize -> FileSize -> Ordering
Ord)
type Offset8 = Offset Word8
newtype Offset ty = Offset Int
deriving (Int -> Offset ty -> ShowS
forall ty. Int -> Offset ty -> ShowS
forall ty. [Offset ty] -> ShowS
forall ty. Offset ty -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Offset ty] -> ShowS
$cshowList :: forall ty. [Offset ty] -> ShowS
show :: Offset ty -> String
$cshow :: forall ty. Offset ty -> String
showsPrec :: Int -> Offset ty -> ShowS
$cshowsPrec :: forall ty. Int -> Offset ty -> ShowS
Show,Offset ty -> Offset ty -> Bool
forall ty. Offset ty -> Offset ty -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Offset ty -> Offset ty -> Bool
$c/= :: forall ty. Offset ty -> Offset ty -> Bool
== :: Offset ty -> Offset ty -> Bool
$c== :: forall ty. Offset ty -> Offset ty -> Bool
Eq,Offset ty -> Offset ty -> Bool
Offset ty -> Offset ty -> Ordering
Offset ty -> Offset ty -> Offset ty
forall ty. Eq (Offset ty)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall ty. Offset ty -> Offset ty -> Bool
forall ty. Offset ty -> Offset ty -> Ordering
forall ty. Offset ty -> Offset ty -> Offset ty
min :: Offset ty -> Offset ty -> Offset ty
$cmin :: forall ty. Offset ty -> Offset ty -> Offset ty
max :: Offset ty -> Offset ty -> Offset ty
$cmax :: forall ty. Offset ty -> Offset ty -> Offset ty
>= :: Offset ty -> Offset ty -> Bool
$c>= :: forall ty. Offset ty -> Offset ty -> Bool
> :: Offset ty -> Offset ty -> Bool
$c> :: forall ty. Offset ty -> Offset ty -> Bool
<= :: Offset ty -> Offset ty -> Bool
$c<= :: forall ty. Offset ty -> Offset ty -> Bool
< :: Offset ty -> Offset ty -> Bool
$c< :: forall ty. Offset ty -> Offset ty -> Bool
compare :: Offset ty -> Offset ty -> Ordering
$ccompare :: forall ty. Offset ty -> Offset ty -> Ordering
Ord,Int -> Offset ty
Offset ty -> Int
Offset ty -> [Offset ty]
Offset ty -> Offset ty
Offset ty -> Offset ty -> [Offset ty]
Offset ty -> Offset ty -> Offset ty -> [Offset ty]
forall ty. Int -> Offset ty
forall ty. Offset ty -> Int
forall ty. Offset ty -> [Offset ty]
forall ty. Offset ty -> Offset ty
forall ty. Offset ty -> Offset ty -> [Offset ty]
forall ty. Offset ty -> Offset ty -> Offset ty -> [Offset ty]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: Offset ty -> Offset ty -> Offset ty -> [Offset ty]
$cenumFromThenTo :: forall ty. Offset ty -> Offset ty -> Offset ty -> [Offset ty]
enumFromTo :: Offset ty -> Offset ty -> [Offset ty]
$cenumFromTo :: forall ty. Offset ty -> Offset ty -> [Offset ty]
enumFromThen :: Offset ty -> Offset ty -> [Offset ty]
$cenumFromThen :: forall ty. Offset ty -> Offset ty -> [Offset ty]
enumFrom :: Offset ty -> [Offset ty]
$cenumFrom :: forall ty. Offset ty -> [Offset ty]
fromEnum :: Offset ty -> Int
$cfromEnum :: forall ty. Offset ty -> Int
toEnum :: Int -> Offset ty
$ctoEnum :: forall ty. Int -> Offset ty
pred :: Offset ty -> Offset ty
$cpred :: forall ty. Offset ty -> Offset ty
succ :: Offset ty -> Offset ty
$csucc :: forall ty. Offset ty -> Offset ty
Enum,Offset ty
Offset ty -> Offset ty -> Offset ty
forall ty. Offset ty
forall a.
a
-> (a -> a -> a)
-> (forall n. IsNatural n => n -> a -> a)
-> Additive a
forall n. IsNatural n => n -> Offset ty -> Offset ty
forall ty. Offset ty -> Offset ty -> Offset ty
forall ty n. IsNatural n => n -> Offset ty -> Offset ty
scale :: forall n. IsNatural n => n -> Offset ty -> Offset ty
$cscale :: forall ty n. IsNatural n => n -> Offset ty -> Offset ty
+ :: Offset ty -> Offset ty -> Offset ty
$c+ :: forall ty. Offset ty -> Offset ty -> Offset ty
azero :: Offset ty
$cazero :: forall ty. Offset ty
Additive,Typeable,Integer -> Offset ty
forall ty. Integer -> Offset ty
forall a. (Integer -> a) -> Integral a
fromInteger :: Integer -> Offset ty
$cfromInteger :: forall ty. Integer -> Offset ty
Integral,Integer -> Offset ty
Offset ty -> Offset ty
Offset ty -> Offset ty -> Offset ty
forall ty. Integer -> Offset ty
forall ty. Offset ty -> Offset ty
forall ty. Offset ty -> Offset ty -> Offset ty
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> Offset ty
$cfromInteger :: forall ty. Integer -> Offset ty
signum :: Offset ty -> Offset ty
$csignum :: forall ty. Offset ty -> Offset ty
abs :: Offset ty -> Offset ty
$cabs :: forall ty. Offset ty -> Offset ty
negate :: Offset ty -> Offset ty
$cnegate :: forall ty. Offset ty -> Offset ty
* :: Offset ty -> Offset ty -> Offset ty
$c* :: forall ty. Offset ty -> Offset ty -> Offset ty
- :: Offset ty -> Offset ty -> Offset ty
$c- :: forall ty. Offset ty -> Offset ty -> Offset ty
+ :: Offset ty -> Offset ty -> Offset ty
$c+ :: forall ty. Offset ty -> Offset ty -> Offset ty
Prelude.Num)
sentinel :: Offset ty
sentinel = forall ty. Int -> Offset ty
Offset (-Int
1)
instance IsIntegral (Offset ty) where
toInteger :: Offset ty -> Integer
toInteger (Offset Int
i) = forall a. IsIntegral a => a -> Integer
toInteger Int
i
instance IsNatural (Offset ty) where
toNatural :: Offset ty -> Natural
toNatural (Offset Int
i) = forall a. IsNatural a => a -> Natural
toNatural (Int -> Word
intToWord Int
i)
instance Subtractive (Offset ty) where
type Difference (Offset ty) = CountOf ty
(Offset Int
a) - :: Offset ty -> Offset ty -> Difference (Offset ty)
- (Offset Int
b) = forall ty. Int -> CountOf ty
CountOf (Int
aforall a. Subtractive a => a -> a -> Difference a
-Int
b)
(+.) :: Offset ty -> Int -> Offset ty
+. :: forall ty. Offset ty -> Int -> Offset ty
(+.) (Offset Int
a) Int
b = forall ty. Int -> Offset ty
Offset (Int
a forall a. Additive a => a -> a -> a
+ Int
b)
{-# INLINE (+.) #-}
(.==#) :: Offset ty -> CountOf ty -> Bool
.==# :: forall ty. Offset ty -> CountOf ty -> Bool
(.==#) (Offset Int
ofs) (CountOf Int
sz) = Int
ofs forall a. Eq a => a -> a -> Bool
== Int
sz
{-# INLINE (.==#) #-}
offsetOfE :: CountOf Word8 -> Offset ty -> Offset8
offsetOfE :: forall ty. CountOf Word8 -> Offset ty -> Offset8
offsetOfE (CountOf Int
sz) (Offset Int
ty) = forall ty. Int -> Offset ty
Offset (Int
ty forall a. Multiplicative a => a -> a -> a
* Int
sz)
offsetPlusE :: Offset ty -> CountOf ty -> Offset ty
offsetPlusE :: forall ty. Offset ty -> CountOf ty -> Offset ty
offsetPlusE (Offset Int
ofs) (CountOf Int
sz) = forall ty. Int -> Offset ty
Offset (Int
ofs forall a. Additive a => a -> a -> a
+ Int
sz)
offsetMinusE :: Offset ty -> CountOf ty -> Offset ty
offsetMinusE :: forall ty. Offset ty -> CountOf ty -> Offset ty
offsetMinusE (Offset Int
ofs) (CountOf Int
sz) = forall ty. Int -> Offset ty
Offset (Int
ofs forall a. Subtractive a => a -> a -> Difference a
- Int
sz)
offsetSub :: Offset a -> Offset a -> Offset a
offsetSub :: forall ty. Offset ty -> Offset ty -> Offset ty
offsetSub (Offset Int
m) (Offset Int
n) = forall ty. Int -> Offset ty
Offset (Int
m forall a. Subtractive a => a -> a -> Difference a
- Int
n)
offsetRecast :: CountOf Word8 -> CountOf Word8 -> Offset ty -> Offset ty2
offsetRecast :: forall ty ty2.
CountOf Word8 -> CountOf Word8 -> Offset ty -> Offset ty2
offsetRecast CountOf Word8
szTy (CountOf Int
szTy2) Offset ty
ofs =
let (Offset Int
bytes) = forall ty. CountOf Word8 -> Offset ty -> Offset8
offsetOfE CountOf Word8
szTy Offset ty
ofs
in forall ty. Int -> Offset ty
Offset (Int
bytes forall a. IDivisible a => a -> a -> a
`div` Int
szTy2)
offsetShiftR :: Int -> Offset ty -> Offset ty2
offsetShiftR :: forall ty ty2. Int -> Offset ty -> Offset ty2
offsetShiftR Int
n (Offset Int
o) = forall ty. Int -> Offset ty
Offset (Int
o forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
n)
offsetShiftL :: Int -> Offset ty -> Offset ty2
offsetShiftL :: forall ty ty2. Int -> Offset ty -> Offset ty2
offsetShiftL Int
n (Offset Int
o) = forall ty. Int -> Offset ty
Offset (Int
o forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
n)
offsetCast :: Proxy (a -> b) -> Offset a -> Offset b
offsetCast :: forall a b. Proxy (a -> b) -> Offset a -> Offset b
offsetCast Proxy (a -> b)
_ (Offset Int
o) = forall ty. Int -> Offset ty
Offset Int
o
{-# INLINE offsetCast #-}
sizeCast :: Proxy (a -> b) -> CountOf a -> CountOf b
sizeCast :: forall a b. Proxy (a -> b) -> CountOf a -> CountOf b
sizeCast Proxy (a -> b)
_ (CountOf Int
sz) = forall ty. Int -> CountOf ty
CountOf Int
sz
{-# INLINE sizeCast #-}
sizeSub :: CountOf a -> CountOf a -> CountOf a
sizeSub :: forall a. CountOf a -> CountOf a -> CountOf a
sizeSub (CountOf Int
m) (CountOf Int
n)
| Difference Int
diff forall a. Ord a => a -> a -> Bool
>= Int
0 = forall ty. Int -> CountOf ty
CountOf Difference Int
diff
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"sizeSub negative size"
where
diff :: Difference Int
diff = Int
m forall a. Subtractive a => a -> a -> Difference a
- Int
n
sizeLastOffset :: CountOf a -> Offset a
sizeLastOffset :: forall a. CountOf a -> Offset a
sizeLastOffset (CountOf Int
s)
| Int
s forall a. Ord a => a -> a -> Bool
> Int
0 = forall ty. Int -> Offset ty
Offset (forall a. Enum a => a -> a
pred Int
s)
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"last offset on size 0"
sizeAsOffset :: CountOf a -> Offset a
sizeAsOffset :: forall a. CountOf a -> Offset a
sizeAsOffset (CountOf Int
a) = forall ty. Int -> Offset ty
Offset Int
a
{-# INLINE sizeAsOffset #-}
offsetAsSize :: Offset a -> CountOf a
offsetAsSize :: forall a. Offset a -> CountOf a
offsetAsSize (Offset Int
a) = forall ty. Int -> CountOf ty
CountOf Int
a
{-# INLINE offsetAsSize #-}
newtype CountOf ty = CountOf Int
deriving (Int -> CountOf ty -> ShowS
forall ty. Int -> CountOf ty -> ShowS
forall ty. [CountOf ty] -> ShowS
forall ty. CountOf ty -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CountOf ty] -> ShowS
$cshowList :: forall ty. [CountOf ty] -> ShowS
show :: CountOf ty -> String
$cshow :: forall ty. CountOf ty -> String
showsPrec :: Int -> CountOf ty -> ShowS
$cshowsPrec :: forall ty. Int -> CountOf ty -> ShowS
Show,CountOf ty -> CountOf ty -> Bool
forall ty. CountOf ty -> CountOf ty -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CountOf ty -> CountOf ty -> Bool
$c/= :: forall ty. CountOf ty -> CountOf ty -> Bool
== :: CountOf ty -> CountOf ty -> Bool
$c== :: forall ty. CountOf ty -> CountOf ty -> Bool
Eq,CountOf ty -> CountOf ty -> Bool
CountOf ty -> CountOf ty -> Ordering
CountOf ty -> CountOf ty -> CountOf ty
forall ty. Eq (CountOf ty)
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall ty. CountOf ty -> CountOf ty -> Bool
forall ty. CountOf ty -> CountOf ty -> Ordering
forall a. CountOf a -> CountOf a -> CountOf a
min :: CountOf ty -> CountOf ty -> CountOf ty
$cmin :: forall a. CountOf a -> CountOf a -> CountOf a
max :: CountOf ty -> CountOf ty -> CountOf ty
$cmax :: forall a. CountOf a -> CountOf a -> CountOf a
>= :: CountOf ty -> CountOf ty -> Bool
$c>= :: forall ty. CountOf ty -> CountOf ty -> Bool
> :: CountOf ty -> CountOf ty -> Bool
$c> :: forall ty. CountOf ty -> CountOf ty -> Bool
<= :: CountOf ty -> CountOf ty -> Bool
$c<= :: forall ty. CountOf ty -> CountOf ty -> Bool
< :: CountOf ty -> CountOf ty -> Bool
$c< :: forall ty. CountOf ty -> CountOf ty -> Bool
compare :: CountOf ty -> CountOf ty -> Ordering
$ccompare :: forall ty. CountOf ty -> CountOf ty -> Ordering
Ord,Int -> CountOf ty
CountOf ty -> Int
CountOf ty -> [CountOf ty]
CountOf ty -> CountOf ty
CountOf ty -> CountOf ty -> [CountOf ty]
CountOf ty -> CountOf ty -> CountOf ty -> [CountOf ty]
forall ty. Int -> CountOf ty
forall ty. CountOf ty -> Int
forall ty. CountOf ty -> [CountOf ty]
forall ty. CountOf ty -> CountOf ty
forall ty. CountOf ty -> CountOf ty -> [CountOf ty]
forall ty. CountOf ty -> CountOf ty -> CountOf ty -> [CountOf ty]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: CountOf ty -> CountOf ty -> CountOf ty -> [CountOf ty]
$cenumFromThenTo :: forall ty. CountOf ty -> CountOf ty -> CountOf ty -> [CountOf ty]
enumFromTo :: CountOf ty -> CountOf ty -> [CountOf ty]
$cenumFromTo :: forall ty. CountOf ty -> CountOf ty -> [CountOf ty]
enumFromThen :: CountOf ty -> CountOf ty -> [CountOf ty]
$cenumFromThen :: forall ty. CountOf ty -> CountOf ty -> [CountOf ty]
enumFrom :: CountOf ty -> [CountOf ty]
$cenumFrom :: forall ty. CountOf ty -> [CountOf ty]
fromEnum :: CountOf ty -> Int
$cfromEnum :: forall ty. CountOf ty -> Int
toEnum :: Int -> CountOf ty
$ctoEnum :: forall ty. Int -> CountOf ty
pred :: CountOf ty -> CountOf ty
$cpred :: forall ty. CountOf ty -> CountOf ty
succ :: CountOf ty -> CountOf ty
$csucc :: forall ty. CountOf ty -> CountOf ty
Enum,Typeable,Integer -> CountOf ty
forall ty. Integer -> CountOf ty
forall a. (Integer -> a) -> Integral a
fromInteger :: Integer -> CountOf ty
$cfromInteger :: forall ty. Integer -> CountOf ty
Integral)
instance Prelude.Num (CountOf ty) where
fromInteger :: Integer -> CountOf ty
fromInteger Integer
a = forall ty. Int -> CountOf ty
CountOf (forall a. Integral a => Integer -> a
fromInteger Integer
a)
+ :: CountOf ty -> CountOf ty -> CountOf ty
(+) (CountOf Int
a) (CountOf Int
b) = forall ty. Int -> CountOf ty
CountOf (Int
aforall a. Additive a => a -> a -> a
+Int
b)
(-) (CountOf Int
a) (CountOf Int
b)
| Int
b forall a. Ord a => a -> a -> Bool
> Int
a = forall ty. Int -> CountOf ty
CountOf Int
0
| Bool
otherwise = forall ty. Int -> CountOf ty
CountOf (Int
a forall a. Subtractive a => a -> a -> Difference a
- Int
b)
* :: CountOf ty -> CountOf ty -> CountOf ty
(*) (CountOf Int
a) (CountOf Int
b) = forall ty. Int -> CountOf ty
CountOf (Int
aforall a. Multiplicative a => a -> a -> a
*Int
b)
abs :: CountOf ty -> CountOf ty
abs CountOf ty
a = CountOf ty
a
negate :: CountOf ty -> CountOf ty
negate CountOf ty
_ = forall a. HasCallStack => String -> a
error String
"cannot negate CountOf: use Foundation Numerical hierarchy for this function to not be exposed to CountOf"
signum :: CountOf ty -> CountOf ty
signum (CountOf Int
a) = forall ty. Int -> CountOf ty
CountOf (forall a. Num a => a -> a
Prelude.signum Int
a)
instance IsIntegral (CountOf ty) where
toInteger :: CountOf ty -> Integer
toInteger (CountOf Int
i) = forall a. IsIntegral a => a -> Integer
toInteger Int
i
instance IsNatural (CountOf ty) where
toNatural :: CountOf ty -> Natural
toNatural (CountOf Int
i) = forall a. IsNatural a => a -> Natural
toNatural (Int -> Word
intToWord Int
i)
instance Additive (CountOf ty) where
azero :: CountOf ty
azero = forall ty. Int -> CountOf ty
CountOf Int
0
+ :: CountOf ty -> CountOf ty -> CountOf ty
(+) (CountOf Int
a) (CountOf Int
b) = forall ty. Int -> CountOf ty
CountOf (Int
aforall a. Additive a => a -> a -> a
+Int
b)
scale :: forall n. IsNatural n => n -> CountOf ty -> CountOf ty
scale n
n (CountOf Int
a) = forall ty. Int -> CountOf ty
CountOf (forall a n. (Additive a, IsNatural n) => n -> a -> a
scale n
n Int
a)
instance Subtractive (CountOf ty) where
type Difference (CountOf ty) = Maybe (CountOf ty)
(CountOf Int
a) - :: CountOf ty -> CountOf ty -> Difference (CountOf ty)
- (CountOf Int
b) | Int
a forall a. Ord a => a -> a -> Bool
>= Int
b = forall a. a -> Maybe a
Just forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall ty. Int -> CountOf ty
CountOf forall a b. (a -> b) -> a -> b
$ Int
a forall a. Subtractive a => a -> a -> Difference a
- Int
b
| Bool
otherwise = forall a. Maybe a
Nothing
instance Semigroup (CountOf ty) where
<> :: CountOf ty -> CountOf ty -> CountOf ty
(<>) = forall a. Additive a => a -> a -> a
(+)
instance Monoid (CountOf ty) where
mempty :: CountOf ty
mempty = forall a. Additive a => a
azero
mconcat :: [CountOf ty] -> CountOf ty
mconcat = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' forall a. Additive a => a -> a -> a
(+) CountOf ty
0
sizeOfE :: CountOf Word8 -> CountOf ty -> CountOf Word8
sizeOfE :: forall ty. CountOf Word8 -> CountOf ty -> CountOf Word8
sizeOfE (CountOf Int
sz) (CountOf Int
ty) = forall ty. Int -> CountOf ty
CountOf (Int
ty forall a. Multiplicative a => a -> a -> a
* Int
sz)
countOfRoundUp :: Int -> CountOf ty -> CountOf ty
countOfRoundUp :: forall ty. Int -> CountOf ty -> CountOf ty
countOfRoundUp Int
alignment (CountOf Int
n) = forall ty. Int -> CountOf ty
CountOf ((Int
n forall a. Additive a => a -> a -> a
+ (Int
alignmentforall a. Subtractive a => a -> a -> Difference a
-Int
1)) forall a. Bits a => a -> a -> a
.&. forall a. Bits a => a -> a
complement (Int
alignmentforall a. Subtractive a => a -> a -> Difference a
-Int
1))
csizeOfSize :: CountOf Word8 -> CSize
#if WORD_SIZE_IN_BITS < 64
csizeOfSize (CountOf (I# sz)) = CSize (W32# (int2Word# sz))
#else
#if __GLASGOW_HASKELL__ >= 904
csizeOfSize (CountOf (I# sz)) = CSize (W64# (wordToWord64# (int2Word# sz)))
#else
csizeOfSize :: CountOf Word8 -> CSize
csizeOfSize (CountOf (I# Int#
sz)) = Word64 -> CSize
CSize (Word# -> Word64
W64# (Int# -> Word#
int2Word# Int#
sz))
#endif
#endif
csizeOfOffset :: Offset8 -> CSize
#if WORD_SIZE_IN_BITS < 64
csizeOfOffset (Offset (I# sz)) = CSize (W32# (int2Word# sz))
#else
#if __GLASGOW_HASKELL__ >= 904
csizeOfOffset (Offset (I# sz)) = CSize (W64# (wordToWord64# (int2Word# sz)))
#else
csizeOfOffset :: Offset8 -> CSize
csizeOfOffset (Offset (I# Int#
sz)) = Word64 -> CSize
CSize (Word# -> Word64
W64# (Int# -> Word#
int2Word# Int#
sz))
#endif
#endif
sizeOfCSSize :: CSsize -> CountOf Word8
sizeOfCSSize :: CSsize -> CountOf Word8
sizeOfCSSize (CSsize (-1)) = forall a. HasCallStack => String -> a
error String
"invalid size: CSSize is -1"
#if WORD_SIZE_IN_BITS < 64
sizeOfCSSize (CSsize (I32# sz)) = CountOf (I# sz)
#else
#if __GLASGOW_HASKELL__ >= 904
sizeOfCSSize (CSsize (I64# sz)) = CountOf (I# (int64ToInt# sz))
#else
sizeOfCSSize (CSsize (I64# Int#
sz)) = forall ty. Int -> CountOf ty
CountOf (Int# -> Int
I# Int#
sz)
#endif
#endif
sizeOfCSize :: CSize -> CountOf Word8
#if WORD_SIZE_IN_BITS < 64
sizeOfCSize (CSize (W32# sz)) = CountOf (I# (word2Int# sz))
#else
#if __GLASGOW_HASKELL__ >= 904
sizeOfCSize (CSize (W64# sz)) = CountOf (I# (word2Int# (word64ToWord# sz)))
#else
sizeOfCSize :: CSize -> CountOf Word8
sizeOfCSize (CSize (W64# Word#
sz)) = forall ty. Int -> CountOf ty
CountOf (Int# -> Int
I# (Word# -> Int#
word2Int# Word#
sz))
#endif
#endif
natValCountOf :: forall n ty proxy . (KnownNat n, NatWithinBound (CountOf ty) n) => proxy n -> CountOf ty
natValCountOf :: forall (n :: Natural) ty (proxy :: Natural -> *).
(KnownNat n, NatWithinBound (CountOf ty) n) =>
proxy n -> CountOf ty
natValCountOf proxy n
n = forall ty. Int -> CountOf ty
CountOf forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
Prelude.fromIntegral (forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy n
n)
natValOffset :: forall n ty proxy . (KnownNat n, NatWithinBound (Offset ty) n) => proxy n -> Offset ty
natValOffset :: forall (n :: Natural) ty (proxy :: Natural -> *).
(KnownNat n, NatWithinBound (Offset ty) n) =>
proxy n -> Offset ty
natValOffset proxy n
n = forall ty. Int -> Offset ty
Offset forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
Prelude.fromIntegral (forall (n :: Natural) (proxy :: Natural -> *).
KnownNat n =>
proxy n -> Integer
natVal proxy n
n)
type instance NatNumMaxBound (CountOf x) = NatNumMaxBound Int
type instance NatNumMaxBound (Offset x) = NatNumMaxBound Int
type Countable ty n = NatWithinBound (CountOf ty) n
type Offsetable ty n = NatWithinBound (Offset ty) n