-- | Miscellaneous utility functions.

{-# LANGUAGE CPP, MagicHash, GeneralizedNewtypeDeriving #-}
module Twee.Utils where

import Control.Arrow((&&&))
import Control.Exception
import Data.List(groupBy, sortBy)
import Data.Ord(comparing)
import System.IO
import GHC.Prim
import GHC.Types
import Data.Bits
import System.Random
import Data.Serialize
--import Test.QuickCheck hiding ((.&.))

repeatM :: Monad m => m a -> m [a]
repeatM :: forall (m :: * -> *) a. Monad m => m a -> m [a]
repeatM = forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a]
repeat

partitionBy :: Ord b => (a -> b) -> [a] -> [[a]]
partitionBy :: forall b a. Ord b => (a -> b) -> [a] -> [[a]]
partitionBy a -> b
value =
  forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\(a, b)
x (a, b)
y -> forall a b. (a, b) -> b
snd (a, b)
x forall a. Eq a => a -> a -> Bool
== forall a b. (a, b) -> b
snd (a, b)
y) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing forall a b. (a, b) -> b
snd) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  forall a b. (a -> b) -> [a] -> [b]
map (forall a. a -> a
id forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& a -> b
value)

collate :: Ord a => ([b] -> c) -> [(a, b)] -> [(a, c)]
collate :: forall a b c. Ord a => ([b] -> c) -> [(a, b)] -> [(a, c)]
collate [b] -> c
f = forall a b. (a -> b) -> [a] -> [b]
map forall {a}. [(a, b)] -> (a, c)
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. Ord b => (a -> b) -> [a] -> [[a]]
partitionBy forall a b. (a, b) -> a
fst
  where
    g :: [(a, b)] -> (a, c)
g [(a, b)]
xs = (forall a b. (a, b) -> a
fst (forall a. [a] -> a
head [(a, b)]
xs), [b] -> c
f (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(a, b)]
xs))

isSorted :: Ord a => [a] -> Bool
isSorted :: forall a. Ord a => [a] -> Bool
isSorted [a]
xs = forall (t :: * -> *). Foldable t => t Bool -> Bool
and (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall a. Ord a => a -> a -> Bool
(<=) [a]
xs (forall a. [a] -> [a]
tail [a]
xs))

isSortedBy :: Ord b => (a -> b) -> [a] -> Bool
isSortedBy :: forall b a. Ord b => (a -> b) -> [a] -> Bool
isSortedBy a -> b
f [a]
xs = forall a. Ord a => [a] -> Bool
isSorted (forall a b. (a -> b) -> [a] -> [b]
map a -> b
f [a]
xs)

usort :: Ord a => [a] -> [a]
usort :: forall a. Ord a => [a] -> [a]
usort = forall a. (a -> a -> Ordering) -> [a] -> [a]
usortBy forall a. Ord a => a -> a -> Ordering
compare

usortBy :: (a -> a -> Ordering) -> [a] -> [a]
usortBy :: forall a. (a -> a -> Ordering) -> [a] -> [a]
usortBy a -> a -> Ordering
f = forall a b. (a -> b) -> [a] -> [b]
map forall a. [a] -> a
head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\a
x a
y -> a -> a -> Ordering
f a
x a
y forall a. Eq a => a -> a -> Bool
== Ordering
EQ) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy a -> a -> Ordering
f

sortBy' :: Ord b => (a -> b) -> [a] -> [a]
sortBy' :: forall b a. Ord b => (a -> b) -> [a] -> [a]
sortBy' a -> b
f = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy (forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\a
x -> (a -> b
f a
x, a
x))

usortBy' :: Ord b => (a -> b) -> [a] -> [a]
usortBy' :: forall b a. Ord b => (a -> b) -> [a] -> [a]
usortBy' a -> b
f = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a -> Ordering) -> [a] -> [a]
usortBy (forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing forall a b. (a, b) -> a
fst) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\a
x -> (a -> b
f a
x, a
x))

orElse :: Ordering -> Ordering -> Ordering
Ordering
EQ orElse :: Ordering -> Ordering -> Ordering
`orElse` Ordering
x = Ordering
x
Ordering
x  `orElse` Ordering
_ = Ordering
x

unbuffered :: IO a -> IO a
unbuffered :: forall a. IO a -> IO a
unbuffered IO a
x = do
  BufferMode
buf <- Handle -> IO BufferMode
hGetBuffering Handle
stdout
  forall a b c. IO a -> IO b -> IO c -> IO c
bracket_
    (Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdout BufferMode
NoBuffering)
    (Handle -> BufferMode -> IO ()
hSetBuffering Handle
stdout BufferMode
buf)
    IO a
x

labelM :: Monad m => (a -> m b) -> [a] -> m [(a, b)]
labelM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> [a] -> m [(a, b)]
labelM a -> m b
f = forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\a
x -> do { b
y <- a -> m b
f a
x; forall (m :: * -> *) a. Monad m => a -> m a
return (a
x, b
y) })

#if __GLASGOW_HASKELL__ < 710
isSubsequenceOf :: Ord a => [a] -> [a] -> Bool
[] `isSubsequenceOf` ys = True
(x:xs) `isSubsequenceOf` [] = False
(x:xs) `isSubsequenceOf` (y:ys)
  | x == y = xs `isSubsequenceOf` ys
  | otherwise = (x:xs) `isSubsequenceOf` ys
#endif

fixpoint :: Eq a => (a -> a) -> a -> a
fixpoint :: forall a. Eq a => (a -> a) -> a -> a
fixpoint = forall b a. Eq b => (a -> b) -> (a -> a) -> a -> a
fixpointOn forall a. a -> a
id

{-# INLINE fixpoint #-}
fixpointOn :: Eq b => (a -> b) -> (a -> a) -> a -> a
fixpointOn :: forall b a. Eq b => (a -> b) -> (a -> a) -> a -> a
fixpointOn a -> b
key a -> a
f a
x = a -> a
fxp a
x
  where
    fxp :: a -> a
fxp a
x
      | a -> b
key a
x forall a. Eq a => a -> a -> Bool
== a -> b
key a
y = a
x
      | Bool
otherwise = a -> a
fxp a
y
      where
        y :: a
y = a -> a
f a
x

-- From "Bit twiddling hacks": branchless min and max
{-# INLINE intMin #-}
intMin :: Int -> Int -> Int
intMin :: Int -> Int -> Int
intMin Int
x Int
y =
  Int
y forall a. Bits a => a -> a -> a
`xor` ((Int
x forall a. Bits a => a -> a -> a
`xor` Int
y) forall a. Bits a => a -> a -> a
.&. forall a. Num a => a -> a
negate (Int
x Int -> Int -> Int
.<. Int
y))
  where
    I# Int#
x .<. :: Int -> Int -> Int
.<. I# Int#
y = Int# -> Int
I# (Int#
x Int# -> Int# -> Int#
<# Int#
y)

{-# INLINE intMax #-}
intMax :: Int -> Int -> Int
intMax :: Int -> Int -> Int
intMax Int
x Int
y =
  Int
x forall a. Bits a => a -> a -> a
`xor` ((Int
x forall a. Bits a => a -> a -> a
`xor` Int
y) forall a. Bits a => a -> a -> a
.&. forall a. Num a => a -> a
negate (Int
x Int -> Int -> Int
.<. Int
y))
  where
    I# Int#
x .<. :: Int -> Int -> Int
.<. I# Int#
y = Int# -> Int
I# (Int#
x Int# -> Int# -> Int#
<# Int#
y)

-- Split an interval (inclusive bounds) into a particular number of blocks
splitInterval :: Integral a => a -> (a, a) -> [(a, a)]
splitInterval :: forall a. Integral a => a -> (a, a) -> [(a, a)]
splitInterval a
k (a
lo, a
hi) =
  [ (a
loforall a. Num a => a -> a -> a
+a
iforall a. Num a => a -> a -> a
*a
blockSize, (a
loforall a. Num a => a -> a -> a
+(a
iforall a. Num a => a -> a -> a
+a
1)forall a. Num a => a -> a -> a
*a
blockSizeforall a. Num a => a -> a -> a
-a
1) forall a. Ord a => a -> a -> a
`min` a
hi)
  | a
i <- [a
0..a
kforall a. Num a => a -> a -> a
-a
1] ]
  where
    size :: a
size = (a
hiforall a. Num a => a -> a -> a
-a
loforall a. Num a => a -> a -> a
+a
1)
    blockSize :: a
blockSize = (a
size forall a. Num a => a -> a -> a
+ a
k forall a. Num a => a -> a -> a
- a
1) forall a. Integral a => a -> a -> a
`div` a
k -- division rounding up
{-
prop_split_1 (Positive k) (lo, hi) =
  -- Check that all elements occur exactly once
  concat [[x..y] | (x, y) <- splitInterval k (lo, hi)] === [lo..hi]

-- Check that we have the correct number and distribution of blocks
prop_split_2 (Positive k) (lo, hi) =
  counterexample (show splits) $ conjoin
    [counterexample "Reason: too many splits" $
       length splits <= k,
     counterexample "Reason: too few splits" $
       length [lo..hi] >= k ==> length splits == k,
     counterexample "Reason: uneven distribution" $
      not (null splits) ==>
       minimum (map length splits) + 1 >= maximum (map length splits)]
  where
    splits = splitInterval k (lo, hi)
-}

reservoir :: Int -> [(Integer, Int)]
reservoir :: Int -> [(Integer, Int)]
reservoir Int
k =
  forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (Integral a, Num b) => a -> b
fromIntegral [Int]
prefix) [Int]
prefix forall a. [a] -> [a] -> [a]
++
  forall a b. [a] -> [b] -> [(a, b)]
zip (forall a b. (a -> b) -> [a] -> [b]
map (forall a. Num a => a -> a -> a
+forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k) (forall a. (a -> a -> a) -> [a] -> [a]
scanl1 forall a. Num a => a -> a -> a
(+) [Integer]
is)) [Int]
ks
  where
    xs, ys :: [Double]
    xs :: [Double]
xs = forall a g. (Random a, RandomGen g) => (a, a) -> g -> [a]
randomRs (Double
0, Double
1) (Int -> StdGen
mkStdGen Int
314159265)
    ys :: [Double]
ys = forall a g. (Random a, RandomGen g) => (a, a) -> g -> [a]
randomRs (Double
0, Double
1) (Int -> StdGen
mkStdGen Int
358979323)
    ks :: [Int]
ks = forall a g. (Random a, RandomGen g) => (a, a) -> g -> [a]
randomRs (Int
0, Int
kforall a. Num a => a -> a -> a
-Int
1) (Int -> StdGen
mkStdGen Int
846264338)

    ws :: [Double]
ws = forall a. (a -> a -> a) -> [a] -> [a]
scanl1 forall a. Num a => a -> a -> a
(*) [ Double
x forall a. Floating a => a -> a -> a
** (Double
1 forall a. Fractional a => a -> a -> a
/ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
k) | Double
x <- [Double]
xs ]
    is :: [Integer]
is = forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith forall {a} {a}. (RealFrac a, Integral a, Floating a) => a -> a -> a
gen [Double]
ws [Double]
ys
    gen :: a -> a -> a
gen a
w a
y = forall a b. (RealFrac a, Integral b) => a -> b
floor (forall a. Floating a => a -> a
log a
y forall a. Fractional a => a -> a -> a
/ forall a. Floating a => a -> a
log (a
1forall a. Num a => a -> a -> a
-a
w)) forall a. Num a => a -> a -> a
+ a
1
    prefix :: [Int]
prefix = [Int
0..Int
kforall a. Num a => a -> a -> a
-Int
1]

data Sample a = Sample Integer [(Integer, Int)] [a]

emptySample :: Int -> Sample a
emptySample :: forall a. Int -> Sample a
emptySample Int
k = forall a. Integer -> [(Integer, Int)] -> [a] -> Sample a
Sample Integer
0 (Int -> [(Integer, Int)]
reservoir Int
k) []

addSample :: (Int, [a]) -> Sample a -> Sample a
addSample :: forall a. (Int, [a]) -> Sample a -> Sample a
addSample (Int
m, [a]
xs) (Sample Integer
total ((Integer
n, Int
pos):[(Integer, Int)]
ps) [a]
sample)
  | Integer
idx forall a. Ord a => a -> a -> Bool
< forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m =
    forall a. (Int, [a]) -> Sample a -> Sample a
addSample (Int
m, [a]
xs) forall a b. (a -> b) -> a -> b
$
      forall a. Integer -> [(Integer, Int)] -> [a] -> Sample a
Sample Integer
total [(Integer, Int)]
ps forall a b. (a -> b) -> a -> b
$
        forall a. Int -> [a] -> [a]
take Int
pos [a]
sample forall a. [a] -> [a] -> [a]
++
        [[a]
xs forall a. [a] -> Int -> a
!! forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
idx] forall a. [a] -> [a] -> [a]
++
        forall a. Int -> [a] -> [a]
drop (Int
posforall a. Num a => a -> a -> a
+Int
1) [a]
sample
  where
    idx :: Integer
idx = Integer
n forall a. Num a => a -> a -> a
- Integer
total
addSample (Int
m, [a]
_) (Sample Integer
total [(Integer, Int)]
ps [a]
sample) =
  forall a. Integer -> [(Integer, Int)] -> [a] -> Sample a
Sample (Integer
totalforall a. Num a => a -> a -> a
+forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
m) [(Integer, Int)]
ps [a]
sample

sampleValue :: Sample a -> [a]
sampleValue :: forall a. Sample a -> [a]
sampleValue (Sample Integer
_ [(Integer, Int)]
_ [a]
sample) = [a]
sample

mapSample :: (a -> b) -> Sample a -> Sample b
mapSample :: forall a b. (a -> b) -> Sample a -> Sample b
mapSample a -> b
f (Sample Integer
total [(Integer, Int)]
ps [a]
sample) =
  forall a. Integer -> [(Integer, Int)] -> [a] -> Sample a
Sample Integer
total [(Integer, Int)]
ps (forall a b. (a -> b) -> [a] -> [b]
map a -> b
f [a]
sample)

-- A combined inits/tails.
splits :: [a] -> [([a], [a])]
splits :: forall a. [a] -> [([a], [a])]
splits [] = [([], [])]
splits (a
x:[a]
xs) =
  [([], a
xforall a. a -> [a] -> [a]
:[a]
xs)] forall a. [a] -> [a] -> [a]
++
  [(a
xforall a. a -> [a] -> [a]
:[a]
ys, [a]
zs) | ([a]
ys, [a]
zs) <- forall a. [a] -> [([a], [a])]
splits [a]
xs]

-- Fold over the natural numbers.
foldn :: (a -> a) -> a -> Int -> a
foldn :: forall a. (a -> a) -> a -> Int -> a
foldn a -> a
_ a
e Int
0 = a
e
foldn a -> a
op a
e Int
n | Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = a -> a
op (forall a. (a -> a) -> a -> Int -> a
foldn a -> a
op a
e (Int
nforall a. Num a => a -> a -> a
-Int
1))

newtype U8 = U8 Int deriving (U8 -> U8 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: U8 -> U8 -> Bool
$c/= :: U8 -> U8 -> Bool
== :: U8 -> U8 -> Bool
$c== :: U8 -> U8 -> Bool
Eq, Eq U8
U8 -> U8 -> Bool
U8 -> U8 -> Ordering
U8 -> U8 -> U8
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 :: U8 -> U8 -> U8
$cmin :: U8 -> U8 -> U8
max :: U8 -> U8 -> U8
$cmax :: U8 -> U8 -> U8
>= :: U8 -> U8 -> Bool
$c>= :: U8 -> U8 -> Bool
> :: U8 -> U8 -> Bool
$c> :: U8 -> U8 -> Bool
<= :: U8 -> U8 -> Bool
$c<= :: U8 -> U8 -> Bool
< :: U8 -> U8 -> Bool
$c< :: U8 -> U8 -> Bool
compare :: U8 -> U8 -> Ordering
$ccompare :: U8 -> U8 -> Ordering
Ord, Integer -> U8
U8 -> U8
U8 -> U8 -> U8
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> U8
$cfromInteger :: Integer -> U8
signum :: U8 -> U8
$csignum :: U8 -> U8
abs :: U8 -> U8
$cabs :: U8 -> U8
negate :: U8 -> U8
$cnegate :: U8 -> U8
* :: U8 -> U8 -> U8
$c* :: U8 -> U8 -> U8
- :: U8 -> U8 -> U8
$c- :: U8 -> U8 -> U8
+ :: U8 -> U8 -> U8
$c+ :: U8 -> U8 -> U8
Num, Num U8
Ord U8
U8 -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: U8 -> Rational
$ctoRational :: U8 -> Rational
Real, Int -> U8
U8 -> Int
U8 -> [U8]
U8 -> U8
U8 -> U8 -> [U8]
U8 -> U8 -> U8 -> [U8]
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 :: U8 -> U8 -> U8 -> [U8]
$cenumFromThenTo :: U8 -> U8 -> U8 -> [U8]
enumFromTo :: U8 -> U8 -> [U8]
$cenumFromTo :: U8 -> U8 -> [U8]
enumFromThen :: U8 -> U8 -> [U8]
$cenumFromThen :: U8 -> U8 -> [U8]
enumFrom :: U8 -> [U8]
$cenumFrom :: U8 -> [U8]
fromEnum :: U8 -> Int
$cfromEnum :: U8 -> Int
toEnum :: Int -> U8
$ctoEnum :: Int -> U8
pred :: U8 -> U8
$cpred :: U8 -> U8
succ :: U8 -> U8
$csucc :: U8 -> U8
Enum, Enum U8
Real U8
U8 -> Integer
U8 -> U8 -> (U8, U8)
U8 -> U8 -> U8
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: U8 -> Integer
$ctoInteger :: U8 -> Integer
divMod :: U8 -> U8 -> (U8, U8)
$cdivMod :: U8 -> U8 -> (U8, U8)
quotRem :: U8 -> U8 -> (U8, U8)
$cquotRem :: U8 -> U8 -> (U8, U8)
mod :: U8 -> U8 -> U8
$cmod :: U8 -> U8 -> U8
div :: U8 -> U8 -> U8
$cdiv :: U8 -> U8 -> U8
rem :: U8 -> U8 -> U8
$crem :: U8 -> U8 -> U8
quot :: U8 -> U8 -> U8
$cquot :: U8 -> U8 -> U8
Integral)

-- Untested!
instance Serialize U8 where
  put :: Putter U8
put (U8 Int
n)
    | Int
n forall a. Ord a => a -> a -> Bool
< Int
0x80 = Putter Word8
putWord8 (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
    | Int
n forall a. Ord a => a -> a -> Bool
< Int
0x4000 = do
      Putter Word16
putWord16be (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n forall a. Num a => a -> a -> a
+ Word16
0x8000)
    | Bool
otherwise = do
      Putter Word32
putWord32be (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n forall a. Num a => a -> a -> a
+ Word32
0xc0000000)
  get :: Get U8
get = do
    Word8
x <- forall a. Get a -> Get a
lookAhead Get Word8
getWord8
    if Word8
x forall a. Ord a => a -> a -> Bool
< Word8
0x80 then forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
getWord8
    else if Word8
x forall a. Ord a => a -> a -> Bool
< Word8
0xc0 then do
      Word16
n <- Get Word16
getWord16be
      forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word16
n forall a. Num a => a -> a -> a
- Word16
0x8000))
    else do
      Word32
n <- Get Word32
getWord32be
      forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
n forall a. Num a => a -> a -> a
- Word32
0xc0000000))

-- Untested!
newtype U16 = U16 Int deriving (U16 -> U16 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: U16 -> U16 -> Bool
$c/= :: U16 -> U16 -> Bool
== :: U16 -> U16 -> Bool
$c== :: U16 -> U16 -> Bool
Eq, Eq U16
U16 -> U16 -> Bool
U16 -> U16 -> Ordering
U16 -> U16 -> U16
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 :: U16 -> U16 -> U16
$cmin :: U16 -> U16 -> U16
max :: U16 -> U16 -> U16
$cmax :: U16 -> U16 -> U16
>= :: U16 -> U16 -> Bool
$c>= :: U16 -> U16 -> Bool
> :: U16 -> U16 -> Bool
$c> :: U16 -> U16 -> Bool
<= :: U16 -> U16 -> Bool
$c<= :: U16 -> U16 -> Bool
< :: U16 -> U16 -> Bool
$c< :: U16 -> U16 -> Bool
compare :: U16 -> U16 -> Ordering
$ccompare :: U16 -> U16 -> Ordering
Ord, Integer -> U16
U16 -> U16
U16 -> U16 -> U16
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> U16
$cfromInteger :: Integer -> U16
signum :: U16 -> U16
$csignum :: U16 -> U16
abs :: U16 -> U16
$cabs :: U16 -> U16
negate :: U16 -> U16
$cnegate :: U16 -> U16
* :: U16 -> U16 -> U16
$c* :: U16 -> U16 -> U16
- :: U16 -> U16 -> U16
$c- :: U16 -> U16 -> U16
+ :: U16 -> U16 -> U16
$c+ :: U16 -> U16 -> U16
Num, Num U16
Ord U16
U16 -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: U16 -> Rational
$ctoRational :: U16 -> Rational
Real, Int -> U16
U16 -> Int
U16 -> [U16]
U16 -> U16
U16 -> U16 -> [U16]
U16 -> U16 -> U16 -> [U16]
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 :: U16 -> U16 -> U16 -> [U16]
$cenumFromThenTo :: U16 -> U16 -> U16 -> [U16]
enumFromTo :: U16 -> U16 -> [U16]
$cenumFromTo :: U16 -> U16 -> [U16]
enumFromThen :: U16 -> U16 -> [U16]
$cenumFromThen :: U16 -> U16 -> [U16]
enumFrom :: U16 -> [U16]
$cenumFrom :: U16 -> [U16]
fromEnum :: U16 -> Int
$cfromEnum :: U16 -> Int
toEnum :: Int -> U16
$ctoEnum :: Int -> U16
pred :: U16 -> U16
$cpred :: U16 -> U16
succ :: U16 -> U16
$csucc :: U16 -> U16
Enum, Enum U16
Real U16
U16 -> Integer
U16 -> U16 -> (U16, U16)
U16 -> U16 -> U16
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: U16 -> Integer
$ctoInteger :: U16 -> Integer
divMod :: U16 -> U16 -> (U16, U16)
$cdivMod :: U16 -> U16 -> (U16, U16)
quotRem :: U16 -> U16 -> (U16, U16)
$cquotRem :: U16 -> U16 -> (U16, U16)
mod :: U16 -> U16 -> U16
$cmod :: U16 -> U16 -> U16
div :: U16 -> U16 -> U16
$cdiv :: U16 -> U16 -> U16
rem :: U16 -> U16 -> U16
$crem :: U16 -> U16 -> U16
quot :: U16 -> U16 -> U16
$cquot :: U16 -> U16 -> U16
Integral)
instance Serialize U16 where
  put :: Putter U16
put (U16 Int
n)
    | Int
n forall a. Ord a => a -> a -> Bool
< Int
0x8000 = do
      Putter Word16
putWord16be (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
    | Bool
otherwise = do
      Putter Word32
putWord32be (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n forall a. Num a => a -> a -> a
+ Word32
0x80000000)
  get :: Get U16
get = do
    Word8
x <- forall a. Get a -> Get a
lookAhead Get Word8
getWord8
    if Word8
x forall a. Ord a => a -> a -> Bool
< Word8
0x80 then forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word16
getWord16be
    else do
      Word32
n <- Get Word32
getWord32be
      forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
n forall a. Num a => a -> a -> a
- Word32
0x80000000))

-- Can be used to write strictness annotations e.g.
-- f !_ !_ | never = undefined
-- which otherwise trigger a spurious warning from GHC.
{-# INLINE never #-}
never :: Bool
never :: Bool
never = Bool
False