{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}

-- |
-- Module     : Simulation.Aivika.Statistics
-- Copyright  : Copyright (c) 2009-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- Represents statistics.
--

module Simulation.Aivika.Statistics
       (-- * Simple Statistics
        SamplingStats(..),
        SamplingData(..),
        combineSamplingStatsEither,
        samplingStatsVariance,
        samplingStatsDeviation,
        samplingStatsSummary,
        returnSamplingStats,
        listSamplingStats,
        fromIntSamplingStats,
        -- * Timing Statistics
        TimingStats(..),
        TimingData(..),
        timingStatsDeviation,
        timingStatsSummary,
        returnTimingStats,
        fromIntTimingStats,
        normTimingStats,
        -- * Simple Counter
        SamplingCounter(..),
        emptySamplingCounter,
        incSamplingCounter,
        decSamplingCounter,
        setSamplingCounter,
        returnSamplingCounter,
        -- * Timing Counter
        TimingCounter(..),
        emptyTimingCounter,
        incTimingCounter,
        decTimingCounter,
        setTimingCounter,
        returnTimingCounter) where

import GHC.Generics (Generic)

import Control.DeepSeq

import Data.Monoid hiding ((<>))
import Data.Semigroup (Semigroup(..))
import Data.Typeable
import Data.Binary

-- | Defines data types that can be converted to 'Double'.
class Ord a => ConvertableToDouble a where
  
  -- | Convert the value to 'Double'.
  convertToDouble :: a -> Double
  
instance ConvertableToDouble Double where
  convertToDouble :: Double -> Double
convertToDouble = Double -> Double
forall a. a -> a
id
  
instance ConvertableToDouble Int where
  convertToDouble :: Int -> Double
convertToDouble = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral

-- | Describes when the statistics consists of only samples 
-- not bound to the simulation time.
data SamplingStats a =       
  SamplingStats { forall a. SamplingStats a -> Int
samplingStatsCount :: !Int,
                  -- ^ The total number of samples.
                  forall a. SamplingStats a -> a
samplingStatsMin   :: !a,
                  -- ^ The minimum value among the samples.
                  forall a. SamplingStats a -> a
samplingStatsMax   :: !a,
                  -- ^ The maximum value among the samples.
                  forall a. SamplingStats a -> Double
samplingStatsMean  :: !Double,
                  -- ^ The average value.
                  forall a. SamplingStats a -> Double
samplingStatsMean2 :: !Double 
                  -- ^ The average square value.
                }
  deriving (SamplingStats a -> SamplingStats a -> Bool
(SamplingStats a -> SamplingStats a -> Bool)
-> (SamplingStats a -> SamplingStats a -> Bool)
-> Eq (SamplingStats a)
forall a. Eq a => SamplingStats a -> SamplingStats a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => SamplingStats a -> SamplingStats a -> Bool
== :: SamplingStats a -> SamplingStats a -> Bool
$c/= :: forall a. Eq a => SamplingStats a -> SamplingStats a -> Bool
/= :: SamplingStats a -> SamplingStats a -> Bool
Eq, Eq (SamplingStats a)
Eq (SamplingStats a) =>
(SamplingStats a -> SamplingStats a -> Ordering)
-> (SamplingStats a -> SamplingStats a -> Bool)
-> (SamplingStats a -> SamplingStats a -> Bool)
-> (SamplingStats a -> SamplingStats a -> Bool)
-> (SamplingStats a -> SamplingStats a -> Bool)
-> (SamplingStats a -> SamplingStats a -> SamplingStats a)
-> (SamplingStats a -> SamplingStats a -> SamplingStats a)
-> Ord (SamplingStats a)
SamplingStats a -> SamplingStats a -> Bool
SamplingStats a -> SamplingStats a -> Ordering
SamplingStats a -> SamplingStats a -> SamplingStats a
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 a. Ord a => Eq (SamplingStats a)
forall a. Ord a => SamplingStats a -> SamplingStats a -> Bool
forall a. Ord a => SamplingStats a -> SamplingStats a -> Ordering
forall a.
Ord a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
$ccompare :: forall a. Ord a => SamplingStats a -> SamplingStats a -> Ordering
compare :: SamplingStats a -> SamplingStats a -> Ordering
$c< :: forall a. Ord a => SamplingStats a -> SamplingStats a -> Bool
< :: SamplingStats a -> SamplingStats a -> Bool
$c<= :: forall a. Ord a => SamplingStats a -> SamplingStats a -> Bool
<= :: SamplingStats a -> SamplingStats a -> Bool
$c> :: forall a. Ord a => SamplingStats a -> SamplingStats a -> Bool
> :: SamplingStats a -> SamplingStats a -> Bool
$c>= :: forall a. Ord a => SamplingStats a -> SamplingStats a -> Bool
>= :: SamplingStats a -> SamplingStats a -> Bool
$cmax :: forall a.
Ord a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
max :: SamplingStats a -> SamplingStats a -> SamplingStats a
$cmin :: forall a.
Ord a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
min :: SamplingStats a -> SamplingStats a -> SamplingStats a
Ord, Typeable, (forall x. SamplingStats a -> Rep (SamplingStats a) x)
-> (forall x. Rep (SamplingStats a) x -> SamplingStats a)
-> Generic (SamplingStats a)
forall x. Rep (SamplingStats a) x -> SamplingStats a
forall x. SamplingStats a -> Rep (SamplingStats a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (SamplingStats a) x -> SamplingStats a
forall a x. SamplingStats a -> Rep (SamplingStats a) x
$cfrom :: forall a x. SamplingStats a -> Rep (SamplingStats a) x
from :: forall x. SamplingStats a -> Rep (SamplingStats a) x
$cto :: forall a x. Rep (SamplingStats a) x -> SamplingStats a
to :: forall x. Rep (SamplingStats a) x -> SamplingStats a
Generic)

instance NFData a => NFData (SamplingStats a)
instance Binary a => Binary (SamplingStats a)

-- | Specifies data type from which values we can gather the statistics.           
class Num a => SamplingData a where           
  
  -- | An empty statistics that has no samples.           
  emptySamplingStats :: SamplingStats a
           
  -- | Add a new sample to the statistics.
  addSamplingStats :: a -> SamplingStats a -> SamplingStats a

  -- | Combine two statistics.
  combineSamplingStats :: SamplingStats a -> SamplingStats a -> SamplingStats a

instance SamplingData a => Semigroup (SamplingStats a) where
  <> :: SamplingStats a -> SamplingStats a -> SamplingStats a
(<>) = SamplingStats a -> SamplingStats a -> SamplingStats a
forall a.
SamplingData a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
combineSamplingStats

instance SamplingData a => Monoid (SamplingStats a) where 
  
  mempty :: SamplingStats a
mempty = SamplingStats a
forall a. SamplingData a => SamplingStats a
emptySamplingStats
  
  mappend :: SamplingStats a -> SamplingStats a -> SamplingStats a
mappend = SamplingStats a -> SamplingStats a -> SamplingStats a
forall a. Semigroup a => a -> a -> a
(<>)

instance SamplingData Double where

  emptySamplingStats :: SamplingStats Double
emptySamplingStats =
    SamplingStats { samplingStatsCount :: Int
samplingStatsCount = Int
0,
                    samplingStatsMin :: Double
samplingStatsMin   = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                    samplingStatsMax :: Double
samplingStatsMax   = (-Double
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                    samplingStatsMean :: Double
samplingStatsMean  = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                    samplingStatsMean2 :: Double
samplingStatsMean2 = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0 }
    
  addSamplingStats :: Double -> SamplingStats Double -> SamplingStats Double
addSamplingStats = Double -> SamplingStats Double -> SamplingStats Double
forall a.
ConvertableToDouble a =>
a -> SamplingStats a -> SamplingStats a
addSamplingStatsGeneric
  
  combineSamplingStats :: SamplingStats Double
-> SamplingStats Double -> SamplingStats Double
combineSamplingStats = SamplingStats Double
-> SamplingStats Double -> SamplingStats Double
forall a.
ConvertableToDouble a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
combineSamplingStatsGeneric
  
instance SamplingData Int where

  emptySamplingStats :: SamplingStats Int
emptySamplingStats =
    SamplingStats { samplingStatsCount :: Int
samplingStatsCount = Int
0,
                    samplingStatsMin :: Int
samplingStatsMin   = Int
forall a. Bounded a => a
maxBound,
                    samplingStatsMax :: Int
samplingStatsMax   = Int
forall a. Bounded a => a
minBound,
                    samplingStatsMean :: Double
samplingStatsMean  = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                    samplingStatsMean2 :: Double
samplingStatsMean2 = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0 }
    
  addSamplingStats :: Int -> SamplingStats Int -> SamplingStats Int
addSamplingStats = Int -> SamplingStats Int -> SamplingStats Int
forall a.
ConvertableToDouble a =>
a -> SamplingStats a -> SamplingStats a
addSamplingStatsGeneric

  combineSamplingStats :: SamplingStats Int -> SamplingStats Int -> SamplingStats Int
combineSamplingStats = SamplingStats Int -> SamplingStats Int -> SamplingStats Int
forall a.
ConvertableToDouble a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
combineSamplingStatsGeneric
  
addSamplingStatsGeneric :: ConvertableToDouble a => a -> SamplingStats a -> SamplingStats a
addSamplingStatsGeneric :: forall a.
ConvertableToDouble a =>
a -> SamplingStats a -> SamplingStats a
addSamplingStatsGeneric a
a SamplingStats a
stats 
  | Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
x    = SamplingStats a
stats
  | Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = SamplingStats { samplingStatsCount :: Int
samplingStatsCount = Int
1,
                                 samplingStatsMin :: a
samplingStatsMin   = a
a,
                                 samplingStatsMax :: a
samplingStatsMax   = a
a,
                                 samplingStatsMean :: Double
samplingStatsMean  = Double
x,
                                 samplingStatsMean2 :: Double
samplingStatsMean2 = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x }
  | Bool
otherwise  = SamplingStats { samplingStatsCount :: Int
samplingStatsCount = Int
count,
                                 samplingStatsMin :: a
samplingStatsMin   = a
minX,
                                 samplingStatsMax :: a
samplingStatsMax   = a
maxX,
                                 samplingStatsMean :: Double
samplingStatsMean  = Double
meanX,
                                 samplingStatsMean2 :: Double
samplingStatsMean2 = Double
meanX2 }
    where count :: Int
count  = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ SamplingStats a -> Int
forall a. SamplingStats a -> Int
samplingStatsCount SamplingStats a
stats
          minX :: a
minX   = a
a a -> a -> a
forall a b. a -> b -> b
`seq` a -> a -> a
forall a. Ord a => a -> a -> a
min a
a (SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMin SamplingStats a
stats)
          maxX :: a
maxX   = a
a a -> a -> a
forall a b. a -> b -> b
`seq` a -> a -> a
forall a. Ord a => a -> a -> a
max a
a (SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMax SamplingStats a
stats)
          meanX :: Double
meanX  = Double
k1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
k2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats a
stats
          meanX2 :: Double
meanX2 = Double
k1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
k2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean2 SamplingStats a
stats
          n :: Double
n      = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
count
          x :: Double
x      = a -> Double
forall a. ConvertableToDouble a => a -> Double
convertToDouble a
a
          k1 :: Double
k1     = Double
1.0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
n
          k2 :: Double
k2     = (Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1.0) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
n

combineSamplingStatsGeneric :: ConvertableToDouble a =>
                               SamplingStats a -> SamplingStats a -> SamplingStats a
combineSamplingStatsGeneric :: forall a.
ConvertableToDouble a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
combineSamplingStatsGeneric SamplingStats a
stats1 SamplingStats a
stats2
  | Int
c1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0   = SamplingStats a
stats2
  | Int
c2 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0   = SamplingStats a
stats1
  | Bool
otherwise = SamplingStats { samplingStatsCount :: Int
samplingStatsCount = Int
c,
                                samplingStatsMin :: a
samplingStatsMin   = a
minZ,
                                samplingStatsMax :: a
samplingStatsMax   = a
maxZ,
                                samplingStatsMean :: Double
samplingStatsMean  = Double
meanZ,
                                samplingStatsMean2 :: Double
samplingStatsMean2 = Double
meanZ2 }
  where c1 :: Int
c1     = SamplingStats a -> Int
forall a. SamplingStats a -> Int
samplingStatsCount SamplingStats a
stats1
        c2 :: Int
c2     = SamplingStats a -> Int
forall a. SamplingStats a -> Int
samplingStatsCount SamplingStats a
stats2
        c :: Int
c      = Int
c1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
c2
        n1 :: Double
n1     = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
c1
        n2 :: Double
n2     = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
c2
        n :: Double
n      = Double
n1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
n2
        minX :: a
minX   = SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMin SamplingStats a
stats1
        minY :: a
minY   = SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMin SamplingStats a
stats2
        minZ :: a
minZ   = a -> a -> a
forall a. Ord a => a -> a -> a
min a
minX a
minY
        maxX :: a
maxX   = SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMax SamplingStats a
stats1
        maxY :: a
maxY   = SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMax SamplingStats a
stats2
        maxZ :: a
maxZ   = a -> a -> a
forall a. Ord a => a -> a -> a
max a
maxX a
maxY
        meanX :: Double
meanX  = SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats a
stats1
        meanY :: Double
meanY  = SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats a
stats2
        meanZ :: Double
meanZ  = Double
k1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
meanX Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
k2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
meanY
        meanX2 :: Double
meanX2 = SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean2 SamplingStats a
stats1
        meanY2 :: Double
meanY2 = SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean2 SamplingStats a
stats2
        meanZ2 :: Double
meanZ2 = Double
k1 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
meanX2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
k2 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
meanY2
        k1 :: Double
k1     = Double
n1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
n
        k2 :: Double
k2     = Double
n2 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
n

-- | If allows combining statistics more efficiently if we know that the first argument can be a scalar.
combineSamplingStatsEither :: SamplingData a => Either a (SamplingStats a) -> SamplingStats a -> SamplingStats a
combineSamplingStatsEither :: forall a.
SamplingData a =>
Either a (SamplingStats a) -> SamplingStats a -> SamplingStats a
combineSamplingStatsEither (Left a
a) SamplingStats a
stats2 = a -> SamplingStats a -> SamplingStats a
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats a
a SamplingStats a
stats2
combineSamplingStatsEither (Right SamplingStats a
stats1) SamplingStats a
stats2 = SamplingStats a -> SamplingStats a -> SamplingStats a
forall a.
SamplingData a =>
SamplingStats a -> SamplingStats a -> SamplingStats a
combineSamplingStats SamplingStats a
stats1 SamplingStats a
stats2

-- | Return the variance.
samplingStatsVariance :: SamplingStats a -> Double
samplingStatsVariance :: forall a. SamplingStats a -> Double
samplingStatsVariance SamplingStats a
stats
  | Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = Double
0
  | Bool
otherwise  = (Double
meanX2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
meanX Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
meanX) Double -> Double -> Double
forall a. Num a => a -> a -> a
* (Double
n Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
n Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
1))
    where count :: Int
count  = SamplingStats a -> Int
forall a. SamplingStats a -> Int
samplingStatsCount SamplingStats a
stats
          meanX :: Double
meanX  = SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats a
stats
          meanX2 :: Double
meanX2 = SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean2 SamplingStats a
stats
          n :: Double
n      = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
count
          
-- | Return the deviation.          
samplingStatsDeviation :: SamplingStats a -> Double
samplingStatsDeviation :: forall a. SamplingStats a -> Double
samplingStatsDeviation = Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double)
-> (SamplingStats a -> Double) -> SamplingStats a -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsVariance

-- | Return the statistics by a single sample.
returnSamplingStats :: SamplingData a => a -> SamplingStats a
returnSamplingStats :: forall a. SamplingData a => a -> SamplingStats a
returnSamplingStats a
x = a -> SamplingStats a -> SamplingStats a
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats a
x SamplingStats a
forall a. SamplingData a => SamplingStats a
emptySamplingStats

-- | Create the statistics by the specified list of data.
listSamplingStats :: SamplingData a => [a] -> SamplingStats a
listSamplingStats :: forall a. SamplingData a => [a] -> SamplingStats a
listSamplingStats = (a -> SamplingStats a -> SamplingStats a)
-> SamplingStats a -> [a] -> SamplingStats a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> SamplingStats a -> SamplingStats a
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats SamplingStats a
forall a. SamplingData a => SamplingStats a
emptySamplingStats

-- | Convert the statistics from integer to double values.
fromIntSamplingStats :: SamplingStats Int -> SamplingStats Double
fromIntSamplingStats :: SamplingStats Int -> SamplingStats Double
fromIntSamplingStats SamplingStats Int
stats =
  SamplingStats Int
stats { samplingStatsMin = fromIntegral $ samplingStatsMin stats,
          samplingStatsMax = fromIntegral $ samplingStatsMax stats }

-- | Show the summary of the statistics.       
showSamplingStats :: (Show a) => SamplingStats a -> ShowS
showSamplingStats :: forall a. Show a => SamplingStats a -> ShowS
showSamplingStats SamplingStats a
stats =
  String -> ShowS
showString String
"{ count = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> Int
forall a. SamplingStats a -> Int
samplingStatsCount SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
", mean = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
", std = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsDeviation SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
", min = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMin SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
", max = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMax SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  String -> ShowS
showString String
" }"

instance Show a => Show (SamplingStats a) where
  showsPrec :: Int -> SamplingStats a -> ShowS
showsPrec Int
prec = SamplingStats a -> ShowS
forall a. Show a => SamplingStats a -> ShowS
showSamplingStats

-- | Show the summary of the statistics using the specified indent.       
samplingStatsSummary :: (Show a) => SamplingStats a -> Int -> ShowS
samplingStatsSummary :: forall a. Show a => SamplingStats a -> Int -> ShowS
samplingStatsSummary SamplingStats a
stats Int
indent =
  let tab :: String
tab = Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
indent Char
' '
  in String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"count = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> Int
forall a. SamplingStats a -> Int
samplingStatsCount SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"mean = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsMean SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
     String -> ShowS
showString String
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"std = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> Double
forall a. SamplingStats a -> Double
samplingStatsDeviation SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
     String -> ShowS
showString String
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"min = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMin SamplingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
     String -> ShowS
showString String
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"max = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (SamplingStats a -> a
forall a. SamplingStats a -> a
samplingStatsMax SamplingStats a
stats)
     
-- | This is the timing statistics where data are bound to the time.
data TimingStats a =
  TimingStats { forall a. TimingStats a -> Int
timingStatsCount     :: !Int,
                -- ^ Return the number of samples.
                forall a. TimingStats a -> a
timingStatsMin       :: !a,
                -- ^ Return the minimum value.
                forall a. TimingStats a -> a
timingStatsMax       :: !a,
                -- ^ Return the maximum value.
                forall a. TimingStats a -> a
timingStatsLast      :: !a,
                -- ^ Return the last value.
                forall a. TimingStats a -> Double
timingStatsMinTime   :: !Double,
                -- ^ Return the time at which the minimum is attained.
                forall a. TimingStats a -> Double
timingStatsMaxTime   :: !Double,
                -- ^ Return the time at which the maximum is attained.
                forall a. TimingStats a -> Double
timingStatsStartTime :: !Double,
                -- ^ Return the start time of sampling.
                forall a. TimingStats a -> Double
timingStatsLastTime  :: !Double,
                -- ^ Return the last time of sampling.
                forall a. TimingStats a -> Double
timingStatsSum       :: !Double,
                -- ^ Return the sum of values.
                forall a. TimingStats a -> Double
timingStatsSum2      :: !Double 
                -- ^ Return the sum of square values.
                } deriving (TimingStats a -> TimingStats a -> Bool
(TimingStats a -> TimingStats a -> Bool)
-> (TimingStats a -> TimingStats a -> Bool) -> Eq (TimingStats a)
forall a. Eq a => TimingStats a -> TimingStats a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => TimingStats a -> TimingStats a -> Bool
== :: TimingStats a -> TimingStats a -> Bool
$c/= :: forall a. Eq a => TimingStats a -> TimingStats a -> Bool
/= :: TimingStats a -> TimingStats a -> Bool
Eq, Eq (TimingStats a)
Eq (TimingStats a) =>
(TimingStats a -> TimingStats a -> Ordering)
-> (TimingStats a -> TimingStats a -> Bool)
-> (TimingStats a -> TimingStats a -> Bool)
-> (TimingStats a -> TimingStats a -> Bool)
-> (TimingStats a -> TimingStats a -> Bool)
-> (TimingStats a -> TimingStats a -> TimingStats a)
-> (TimingStats a -> TimingStats a -> TimingStats a)
-> Ord (TimingStats a)
TimingStats a -> TimingStats a -> Bool
TimingStats a -> TimingStats a -> Ordering
TimingStats a -> TimingStats a -> TimingStats a
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 a. Ord a => Eq (TimingStats a)
forall a. Ord a => TimingStats a -> TimingStats a -> Bool
forall a. Ord a => TimingStats a -> TimingStats a -> Ordering
forall a. Ord a => TimingStats a -> TimingStats a -> TimingStats a
$ccompare :: forall a. Ord a => TimingStats a -> TimingStats a -> Ordering
compare :: TimingStats a -> TimingStats a -> Ordering
$c< :: forall a. Ord a => TimingStats a -> TimingStats a -> Bool
< :: TimingStats a -> TimingStats a -> Bool
$c<= :: forall a. Ord a => TimingStats a -> TimingStats a -> Bool
<= :: TimingStats a -> TimingStats a -> Bool
$c> :: forall a. Ord a => TimingStats a -> TimingStats a -> Bool
> :: TimingStats a -> TimingStats a -> Bool
$c>= :: forall a. Ord a => TimingStats a -> TimingStats a -> Bool
>= :: TimingStats a -> TimingStats a -> Bool
$cmax :: forall a. Ord a => TimingStats a -> TimingStats a -> TimingStats a
max :: TimingStats a -> TimingStats a -> TimingStats a
$cmin :: forall a. Ord a => TimingStats a -> TimingStats a -> TimingStats a
min :: TimingStats a -> TimingStats a -> TimingStats a
Ord, Typeable, (forall x. TimingStats a -> Rep (TimingStats a) x)
-> (forall x. Rep (TimingStats a) x -> TimingStats a)
-> Generic (TimingStats a)
forall x. Rep (TimingStats a) x -> TimingStats a
forall x. TimingStats a -> Rep (TimingStats a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (TimingStats a) x -> TimingStats a
forall a x. TimingStats a -> Rep (TimingStats a) x
$cfrom :: forall a x. TimingStats a -> Rep (TimingStats a) x
from :: forall x. TimingStats a -> Rep (TimingStats a) x
$cto :: forall a x. Rep (TimingStats a) x -> TimingStats a
to :: forall x. Rep (TimingStats a) x -> TimingStats a
Generic)

instance NFData a => NFData (TimingStats a)
instance Binary a => Binary (TimingStats a)
                           
-- | Defines the data type from which values we can gather the timing statistics.
class Num a => TimingData a where                           
  
  -- | An empty statistics that has no samples.
  emptyTimingStats :: TimingStats a
  
  -- | Add a sample with the specified time to the statistics.
  addTimingStats :: Double -> a -> TimingStats a -> TimingStats a
  
  -- | Return the average value.
  timingStatsMean :: TimingStats a -> Double
  
  -- | Return the average square value.
  timingStatsMean2 :: TimingStats a -> Double
  
  -- | Return the variance.
  timingStatsVariance :: TimingStats a -> Double
  
instance TimingData Double where
  
  emptyTimingStats :: TimingStats Double
emptyTimingStats = 
    TimingStats { timingStatsCount :: Int
timingStatsCount     = Int
0,
                  timingStatsMin :: Double
timingStatsMin       = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsMax :: Double
timingStatsMax       = (-Double
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsLast :: Double
timingStatsLast      = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsMinTime :: Double
timingStatsMinTime   = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsMaxTime :: Double
timingStatsMaxTime   = (-Double
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsStartTime :: Double
timingStatsStartTime = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsLastTime :: Double
timingStatsLastTime  = (-Double
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsSum :: Double
timingStatsSum       = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsSum2 :: Double
timingStatsSum2      = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0 }
    
  addTimingStats :: Double -> Double -> TimingStats Double -> TimingStats Double
addTimingStats      = Double -> Double -> TimingStats Double -> TimingStats Double
forall a.
ConvertableToDouble a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStatsGeneric
  timingStatsMean :: TimingStats Double -> Double
timingStatsMean     = TimingStats Double -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMeanGeneric
  timingStatsMean2 :: TimingStats Double -> Double
timingStatsMean2    = TimingStats Double -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMean2Generic
  timingStatsVariance :: TimingStats Double -> Double
timingStatsVariance = TimingStats Double -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsVarianceGeneric

instance TimingData Int where
  
  emptyTimingStats :: TimingStats Int
emptyTimingStats = 
    TimingStats { timingStatsCount :: Int
timingStatsCount     = Int
0,
                  timingStatsMin :: Int
timingStatsMin       = Int
forall a. Bounded a => a
maxBound,
                  timingStatsMax :: Int
timingStatsMax       = Int
forall a. Bounded a => a
minBound,
                  timingStatsLast :: Int
timingStatsLast      = Int
0,
                  timingStatsMinTime :: Double
timingStatsMinTime   = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsMaxTime :: Double
timingStatsMaxTime   = (-Double
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsStartTime :: Double
timingStatsStartTime = Double
1 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsLastTime :: Double
timingStatsLastTime  = (-Double
1) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsSum :: Double
timingStatsSum       = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0,
                  timingStatsSum2 :: Double
timingStatsSum2      = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0 }
    
  addTimingStats :: Double -> Int -> TimingStats Int -> TimingStats Int
addTimingStats      = Double -> Int -> TimingStats Int -> TimingStats Int
forall a.
ConvertableToDouble a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStatsGeneric
  timingStatsMean :: TimingStats Int -> Double
timingStatsMean     = TimingStats Int -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMeanGeneric
  timingStatsMean2 :: TimingStats Int -> Double
timingStatsMean2    = TimingStats Int -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMean2Generic
  timingStatsVariance :: TimingStats Int -> Double
timingStatsVariance = TimingStats Int -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsVarianceGeneric

addTimingStatsGeneric :: ConvertableToDouble a => Double -> a -> TimingStats a -> TimingStats a
addTimingStatsGeneric :: forall a.
ConvertableToDouble a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStatsGeneric Double
t a
a TimingStats a
stats
  | Double
t Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
t'     = String -> TimingStats a
forall a. HasCallStack => String -> a
error String
"The current time cannot be less than the previous one: addTimingStats"
  | Double -> Bool
forall a. RealFloat a => a -> Bool
isNaN Double
x    = TimingStats a
stats
  | Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = TimingStats { timingStatsCount :: Int
timingStatsCount     = Int
1,
                               timingStatsMin :: a
timingStatsMin       = a
a,
                               timingStatsMax :: a
timingStatsMax       = a
a,
                               timingStatsLast :: a
timingStatsLast      = a
a,
                               timingStatsMinTime :: Double
timingStatsMinTime   = Double
t,
                               timingStatsMaxTime :: Double
timingStatsMaxTime   = Double
t,
                               timingStatsStartTime :: Double
timingStatsStartTime = Double
t,
                               timingStatsLastTime :: Double
timingStatsLastTime  = Double
t,
                               timingStatsSum :: Double
timingStatsSum       = Double
0,
                               timingStatsSum2 :: Double
timingStatsSum2      = Double
0 }
  | Bool
otherwise  = TimingStats { timingStatsCount :: Int
timingStatsCount     = Int
count,
                               timingStatsMin :: a
timingStatsMin       = a
minX,
                               timingStatsMax :: a
timingStatsMax       = a
maxX,
                               timingStatsLast :: a
timingStatsLast      = a
a,
                               timingStatsMinTime :: Double
timingStatsMinTime   = Double
minT,
                               timingStatsMaxTime :: Double
timingStatsMaxTime   = Double
maxT,
                               timingStatsStartTime :: Double
timingStatsStartTime = Double
t0,
                               timingStatsLastTime :: Double
timingStatsLastTime  = Double
t,
                               timingStatsSum :: Double
timingStatsSum       = Double
sumX,
                               timingStatsSum2 :: Double
timingStatsSum2      = Double
sumX2 }
    where count :: Int
count = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ TimingStats a -> Int
forall a. TimingStats a -> Int
timingStatsCount TimingStats a
stats
          minX' :: a
minX' = TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMin TimingStats a
stats
          maxX' :: a
maxX' = TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMax TimingStats a
stats
          minX :: a
minX  = a
a a -> a -> a
forall a b. a -> b -> b
`seq` a -> a -> a
forall a. Ord a => a -> a -> a
min a
a a
minX'
          maxX :: a
maxX  = a
a a -> a -> a
forall a b. a -> b -> b
`seq` a -> a -> a
forall a. Ord a => a -> a -> a
max a
a a
maxX'
          minT :: Double
minT | a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
minX' = Double
t
               | Bool
otherwise = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsMinTime TimingStats a
stats
          maxT :: Double
maxT | a
a a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
maxX' = Double
t
               | Bool
otherwise = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsMaxTime TimingStats a
stats
          t0 :: Double
t0 = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsStartTime TimingStats a
stats
          t' :: Double
t' = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsLastTime TimingStats a
stats
          a' :: a
a' = TimingStats a -> a
forall a. TimingStats a -> a
timingStatsLast TimingStats a
stats
          x :: Double
x  = a -> Double
forall a. ConvertableToDouble a => a -> Double
convertToDouble a
a
          x' :: Double
x' = a -> Double
forall a. ConvertableToDouble a => a -> Double
convertToDouble a
a'
          sumX' :: Double
sumX'  = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsSum TimingStats a
stats
          sumX :: Double
sumX   = Double
sumX' Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t') Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x'
          sumX2' :: Double
sumX2' = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsSum2 TimingStats a
stats
          sumX2 :: Double
sumX2  = Double
sumX2' Double -> Double -> Double
forall a. Num a => a -> a -> a
+ (Double
t Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t') Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x' Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x'
      
timingStatsMeanGeneric :: ConvertableToDouble a => TimingStats a -> Double
timingStatsMeanGeneric :: forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMeanGeneric TimingStats a
stats
  | Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0
  | Double
t1 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
t0    = Double
sumX Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
  | Bool
otherwise  = Double
minX
    where t0 :: Double
t0    = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsStartTime TimingStats a
stats
          t1 :: Double
t1    = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsLastTime TimingStats a
stats
          sumX :: Double
sumX  = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsSum TimingStats a
stats
          minX :: Double
minX  = a -> Double
forall a. ConvertableToDouble a => a -> Double
convertToDouble (a -> Double) -> a -> Double
forall a b. (a -> b) -> a -> b
$ TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMin TimingStats a
stats
          count :: Int
count = TimingStats a -> Int
forall a. TimingStats a -> Int
timingStatsCount TimingStats a
stats
  
timingStatsMean2Generic :: ConvertableToDouble a => TimingStats a -> Double
timingStatsMean2Generic :: forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMean2Generic TimingStats a
stats
  | Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Double
0 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
0
  | Double
t1 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
t0    = Double
sumX2 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (Double
t1 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
t0)
  | Bool
otherwise  = Double
minX Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
minX
    where t0 :: Double
t0    = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsStartTime TimingStats a
stats
          t1 :: Double
t1    = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsLastTime TimingStats a
stats
          sumX2 :: Double
sumX2 = TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsSum2 TimingStats a
stats
          minX :: Double
minX  = a -> Double
forall a. ConvertableToDouble a => a -> Double
convertToDouble (a -> Double) -> a -> Double
forall a b. (a -> b) -> a -> b
$ TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMin TimingStats a
stats
          count :: Int
count = TimingStats a -> Int
forall a. TimingStats a -> Int
timingStatsCount TimingStats a
stats

timingStatsVarianceGeneric :: ConvertableToDouble a => TimingStats a -> Double
timingStatsVarianceGeneric :: forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsVarianceGeneric TimingStats a
stats = Double
ex2 Double -> Double -> Double
forall a. Num a => a -> a -> a
- Double
ex Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
ex
  where ex :: Double
ex  = TimingStats a -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMeanGeneric TimingStats a
stats
        ex2 :: Double
ex2 = TimingStats a -> Double
forall a. ConvertableToDouble a => TimingStats a -> Double
timingStatsMean2Generic TimingStats a
stats
                
-- | Return the deviation.              
timingStatsDeviation :: TimingData a => TimingStats a -> Double
timingStatsDeviation :: forall a. TimingData a => TimingStats a -> Double
timingStatsDeviation = Double -> Double
forall a. Floating a => a -> a
sqrt (Double -> Double)
-> (TimingStats a -> Double) -> TimingStats a -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TimingStats a -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsVariance

-- | Return the statistics by single timing data.
returnTimingStats :: TimingData a => Double -> a -> TimingStats a
returnTimingStats :: forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t a
a = Double -> a -> TimingStats a -> TimingStats a
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t a
a TimingStats a
forall a. TimingData a => TimingStats a
emptyTimingStats

-- | Convert the statistics from integer to double values.
fromIntTimingStats :: TimingStats Int -> TimingStats Double
fromIntTimingStats :: TimingStats Int -> TimingStats Double
fromIntTimingStats TimingStats Int
stats =
  TimingStats Int
stats { timingStatsMin  = fromIntegral $ timingStatsMin stats,
          timingStatsMax  = fromIntegral $ timingStatsMax stats,
          timingStatsLast = fromIntegral $ timingStatsLast stats }

-- | Convert the statistics to its normalised sampling-based representation,
-- where the first argument specifies the number of pseudo-samples.
normTimingStats :: TimingData a => Int -> TimingStats a -> SamplingStats a
normTimingStats :: forall a. TimingData a => Int -> TimingStats a -> SamplingStats a
normTimingStats Int
n TimingStats a
stats =
  SamplingStats { samplingStatsCount :: Int
samplingStatsCount = Int
n,
                  samplingStatsMin :: a
samplingStatsMin   = TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMin TimingStats a
stats,
                  samplingStatsMax :: a
samplingStatsMax   = TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMax TimingStats a
stats,
                  samplingStatsMean :: Double
samplingStatsMean  = TimingStats a -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsMean TimingStats a
stats,
                  samplingStatsMean2 :: Double
samplingStatsMean2 = TimingStats a -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsMean2 TimingStats a
stats }

-- | Show the summary of the statistics.       
showTimingStats :: (Show a, TimingData a) => TimingStats a -> ShowS
showTimingStats :: forall a. (Show a, TimingData a) => TimingStats a -> ShowS
showTimingStats TimingStats a
stats =
  String -> ShowS
showString String
"{ count = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Int
forall a. TimingStats a -> Int
timingStatsCount TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
", mean = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsMean TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
", std = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsDeviation TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
", min = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMin TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
  String -> ShowS
showString String
" (t = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsMinTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  String -> ShowS
showString String
"), max = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMax TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  String -> ShowS
showString String
" (t = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsMaxTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  String -> ShowS
showString String
"), t in [" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsStartTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  String -> ShowS
showString String
", " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsLastTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  String -> ShowS
showString String
"] }"

instance (Show a, TimingData a) => Show (TimingStats a) where
  showsPrec :: Int -> TimingStats a -> ShowS
showsPrec Int
prec = TimingStats a -> ShowS
forall a. (Show a, TimingData a) => TimingStats a -> ShowS
showTimingStats

-- | Show the summary of the statistics using the specified indent.       
timingStatsSummary :: (Show a, TimingData a) => TimingStats a -> Int -> ShowS
timingStatsSummary :: forall a. (Show a, TimingData a) => TimingStats a -> Int -> ShowS
timingStatsSummary TimingStats a
stats Int
indent =
  let tab :: String
tab = Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
indent Char
' '
  in String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"count = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Int
forall a. TimingStats a -> Int
timingStatsCount TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
     String -> ShowS
showString String
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"mean = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsMean TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
     String -> ShowS
showString String
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"std = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingData a => TimingStats a -> Double
timingStatsDeviation TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
     String -> ShowS
showString String
"\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"min = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMin TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. 
     String -> ShowS
showString String
" (t = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsMinTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
")\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"max = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> a
forall a. TimingStats a -> a
timingStatsMax TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
" (t = " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsMaxTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
")\n" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
tab ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"t in [" ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsStartTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
", " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> ShowS
forall a. Show a => a -> ShowS
shows (TimingStats a -> Double
forall a. TimingStats a -> Double
timingStatsLastTime TimingStats a
stats) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
     String -> ShowS
showString String
"]"

-- | A counter for which the statistics is collected too.
data SamplingCounter a =
  SamplingCounter { forall a. SamplingCounter a -> a
samplingCounterValue :: a,
                    -- ^ The counter value.
                    forall a. SamplingCounter a -> SamplingStats a
samplingCounterStats :: SamplingStats a
                    -- ^ The counter statistics.
                  } deriving (SamplingCounter a -> SamplingCounter a -> Bool
(SamplingCounter a -> SamplingCounter a -> Bool)
-> (SamplingCounter a -> SamplingCounter a -> Bool)
-> Eq (SamplingCounter a)
forall a. Eq a => SamplingCounter a -> SamplingCounter a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => SamplingCounter a -> SamplingCounter a -> Bool
== :: SamplingCounter a -> SamplingCounter a -> Bool
$c/= :: forall a. Eq a => SamplingCounter a -> SamplingCounter a -> Bool
/= :: SamplingCounter a -> SamplingCounter a -> Bool
Eq, Eq (SamplingCounter a)
Eq (SamplingCounter a) =>
(SamplingCounter a -> SamplingCounter a -> Ordering)
-> (SamplingCounter a -> SamplingCounter a -> Bool)
-> (SamplingCounter a -> SamplingCounter a -> Bool)
-> (SamplingCounter a -> SamplingCounter a -> Bool)
-> (SamplingCounter a -> SamplingCounter a -> Bool)
-> (SamplingCounter a -> SamplingCounter a -> SamplingCounter a)
-> (SamplingCounter a -> SamplingCounter a -> SamplingCounter a)
-> Ord (SamplingCounter a)
SamplingCounter a -> SamplingCounter a -> Bool
SamplingCounter a -> SamplingCounter a -> Ordering
SamplingCounter a -> SamplingCounter a -> SamplingCounter a
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 a. Ord a => Eq (SamplingCounter a)
forall a. Ord a => SamplingCounter a -> SamplingCounter a -> Bool
forall a.
Ord a =>
SamplingCounter a -> SamplingCounter a -> Ordering
forall a.
Ord a =>
SamplingCounter a -> SamplingCounter a -> SamplingCounter a
$ccompare :: forall a.
Ord a =>
SamplingCounter a -> SamplingCounter a -> Ordering
compare :: SamplingCounter a -> SamplingCounter a -> Ordering
$c< :: forall a. Ord a => SamplingCounter a -> SamplingCounter a -> Bool
< :: SamplingCounter a -> SamplingCounter a -> Bool
$c<= :: forall a. Ord a => SamplingCounter a -> SamplingCounter a -> Bool
<= :: SamplingCounter a -> SamplingCounter a -> Bool
$c> :: forall a. Ord a => SamplingCounter a -> SamplingCounter a -> Bool
> :: SamplingCounter a -> SamplingCounter a -> Bool
$c>= :: forall a. Ord a => SamplingCounter a -> SamplingCounter a -> Bool
>= :: SamplingCounter a -> SamplingCounter a -> Bool
$cmax :: forall a.
Ord a =>
SamplingCounter a -> SamplingCounter a -> SamplingCounter a
max :: SamplingCounter a -> SamplingCounter a -> SamplingCounter a
$cmin :: forall a.
Ord a =>
SamplingCounter a -> SamplingCounter a -> SamplingCounter a
min :: SamplingCounter a -> SamplingCounter a -> SamplingCounter a
Ord, Int -> SamplingCounter a -> ShowS
[SamplingCounter a] -> ShowS
SamplingCounter a -> String
(Int -> SamplingCounter a -> ShowS)
-> (SamplingCounter a -> String)
-> ([SamplingCounter a] -> ShowS)
-> Show (SamplingCounter a)
forall a. Show a => Int -> SamplingCounter a -> ShowS
forall a. Show a => [SamplingCounter a] -> ShowS
forall a. Show a => SamplingCounter a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> SamplingCounter a -> ShowS
showsPrec :: Int -> SamplingCounter a -> ShowS
$cshow :: forall a. Show a => SamplingCounter a -> String
show :: SamplingCounter a -> String
$cshowList :: forall a. Show a => [SamplingCounter a] -> ShowS
showList :: [SamplingCounter a] -> ShowS
Show, Typeable, (forall x. SamplingCounter a -> Rep (SamplingCounter a) x)
-> (forall x. Rep (SamplingCounter a) x -> SamplingCounter a)
-> Generic (SamplingCounter a)
forall x. Rep (SamplingCounter a) x -> SamplingCounter a
forall x. SamplingCounter a -> Rep (SamplingCounter a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (SamplingCounter a) x -> SamplingCounter a
forall a x. SamplingCounter a -> Rep (SamplingCounter a) x
$cfrom :: forall a x. SamplingCounter a -> Rep (SamplingCounter a) x
from :: forall x. SamplingCounter a -> Rep (SamplingCounter a) x
$cto :: forall a x. Rep (SamplingCounter a) x -> SamplingCounter a
to :: forall x. Rep (SamplingCounter a) x -> SamplingCounter a
Generic)

instance NFData a => NFData (SamplingCounter a)
instance Binary a => Binary (SamplingCounter a)

-- | An empty counter.
emptySamplingCounter :: SamplingData a => SamplingCounter a
emptySamplingCounter :: forall a. SamplingData a => SamplingCounter a
emptySamplingCounter =
  SamplingCounter { samplingCounterValue :: a
samplingCounterValue = a
0,
                    samplingCounterStats :: SamplingStats a
samplingCounterStats = SamplingStats a
forall a. SamplingData a => SamplingStats a
emptySamplingStats }

-- | Increase the counter.
incSamplingCounter :: SamplingData a => a -> SamplingCounter a -> SamplingCounter a
incSamplingCounter :: forall a.
SamplingData a =>
a -> SamplingCounter a -> SamplingCounter a
incSamplingCounter a
a SamplingCounter a
counter =
  SamplingCounter { samplingCounterValue :: a
samplingCounterValue = a
a',
                    samplingCounterStats :: SamplingStats a
samplingCounterStats = a -> SamplingStats a -> SamplingStats a
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats a
a' (SamplingCounter a -> SamplingStats a
forall a. SamplingCounter a -> SamplingStats a
samplingCounterStats SamplingCounter a
counter) }
  where a' :: a
a' = SamplingCounter a -> a
forall a. SamplingCounter a -> a
samplingCounterValue SamplingCounter a
counter a -> a -> a
forall a. Num a => a -> a -> a
+ a
a

-- | Decrease the counter.
decSamplingCounter :: SamplingData a => a -> SamplingCounter a -> SamplingCounter a
decSamplingCounter :: forall a.
SamplingData a =>
a -> SamplingCounter a -> SamplingCounter a
decSamplingCounter a
a SamplingCounter a
counter =
  SamplingCounter { samplingCounterValue :: a
samplingCounterValue = a
a',
                    samplingCounterStats :: SamplingStats a
samplingCounterStats = a -> SamplingStats a -> SamplingStats a
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats a
a' (SamplingCounter a -> SamplingStats a
forall a. SamplingCounter a -> SamplingStats a
samplingCounterStats SamplingCounter a
counter) }
  where a' :: a
a' = SamplingCounter a -> a
forall a. SamplingCounter a -> a
samplingCounterValue SamplingCounter a
counter a -> a -> a
forall a. Num a => a -> a -> a
- a
a

-- | Set a new value for the counter.
setSamplingCounter :: SamplingData a => a -> SamplingCounter a -> SamplingCounter a
setSamplingCounter :: forall a.
SamplingData a =>
a -> SamplingCounter a -> SamplingCounter a
setSamplingCounter a
a SamplingCounter a
counter =
  SamplingCounter { samplingCounterValue :: a
samplingCounterValue = a
a,
                    samplingCounterStats :: SamplingStats a
samplingCounterStats = a -> SamplingStats a -> SamplingStats a
forall a. SamplingData a => a -> SamplingStats a -> SamplingStats a
addSamplingStats a
a (SamplingCounter a -> SamplingStats a
forall a. SamplingCounter a -> SamplingStats a
samplingCounterStats SamplingCounter a
counter) }

-- | Create a counter with the specified initial value.
returnSamplingCounter :: SamplingData  a => a -> SamplingCounter a
returnSamplingCounter :: forall a. SamplingData a => a -> SamplingCounter a
returnSamplingCounter a
a =
  SamplingCounter { samplingCounterValue :: a
samplingCounterValue = a
a,
                    samplingCounterStats :: SamplingStats a
samplingCounterStats = a -> SamplingStats a
forall a. SamplingData a => a -> SamplingStats a
returnSamplingStats a
a }

-- | A counter for which the timing statistics is collected too.
data TimingCounter a =
  TimingCounter { forall a. TimingCounter a -> a
timingCounterValue :: a,
                  -- ^ The counter value.
                  forall a. TimingCounter a -> TimingStats a
timingCounterStats :: TimingStats a
                  -- ^ The counter statistics.
                } deriving (TimingCounter a -> TimingCounter a -> Bool
(TimingCounter a -> TimingCounter a -> Bool)
-> (TimingCounter a -> TimingCounter a -> Bool)
-> Eq (TimingCounter a)
forall a. Eq a => TimingCounter a -> TimingCounter a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => TimingCounter a -> TimingCounter a -> Bool
== :: TimingCounter a -> TimingCounter a -> Bool
$c/= :: forall a. Eq a => TimingCounter a -> TimingCounter a -> Bool
/= :: TimingCounter a -> TimingCounter a -> Bool
Eq, Eq (TimingCounter a)
Eq (TimingCounter a) =>
(TimingCounter a -> TimingCounter a -> Ordering)
-> (TimingCounter a -> TimingCounter a -> Bool)
-> (TimingCounter a -> TimingCounter a -> Bool)
-> (TimingCounter a -> TimingCounter a -> Bool)
-> (TimingCounter a -> TimingCounter a -> Bool)
-> (TimingCounter a -> TimingCounter a -> TimingCounter a)
-> (TimingCounter a -> TimingCounter a -> TimingCounter a)
-> Ord (TimingCounter a)
TimingCounter a -> TimingCounter a -> Bool
TimingCounter a -> TimingCounter a -> Ordering
TimingCounter a -> TimingCounter a -> TimingCounter a
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 a. Ord a => Eq (TimingCounter a)
forall a. Ord a => TimingCounter a -> TimingCounter a -> Bool
forall a. Ord a => TimingCounter a -> TimingCounter a -> Ordering
forall a.
Ord a =>
TimingCounter a -> TimingCounter a -> TimingCounter a
$ccompare :: forall a. Ord a => TimingCounter a -> TimingCounter a -> Ordering
compare :: TimingCounter a -> TimingCounter a -> Ordering
$c< :: forall a. Ord a => TimingCounter a -> TimingCounter a -> Bool
< :: TimingCounter a -> TimingCounter a -> Bool
$c<= :: forall a. Ord a => TimingCounter a -> TimingCounter a -> Bool
<= :: TimingCounter a -> TimingCounter a -> Bool
$c> :: forall a. Ord a => TimingCounter a -> TimingCounter a -> Bool
> :: TimingCounter a -> TimingCounter a -> Bool
$c>= :: forall a. Ord a => TimingCounter a -> TimingCounter a -> Bool
>= :: TimingCounter a -> TimingCounter a -> Bool
$cmax :: forall a.
Ord a =>
TimingCounter a -> TimingCounter a -> TimingCounter a
max :: TimingCounter a -> TimingCounter a -> TimingCounter a
$cmin :: forall a.
Ord a =>
TimingCounter a -> TimingCounter a -> TimingCounter a
min :: TimingCounter a -> TimingCounter a -> TimingCounter a
Ord, Int -> TimingCounter a -> ShowS
[TimingCounter a] -> ShowS
TimingCounter a -> String
(Int -> TimingCounter a -> ShowS)
-> (TimingCounter a -> String)
-> ([TimingCounter a] -> ShowS)
-> Show (TimingCounter a)
forall a. (Show a, TimingData a) => Int -> TimingCounter a -> ShowS
forall a. (Show a, TimingData a) => [TimingCounter a] -> ShowS
forall a. (Show a, TimingData a) => TimingCounter a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. (Show a, TimingData a) => Int -> TimingCounter a -> ShowS
showsPrec :: Int -> TimingCounter a -> ShowS
$cshow :: forall a. (Show a, TimingData a) => TimingCounter a -> String
show :: TimingCounter a -> String
$cshowList :: forall a. (Show a, TimingData a) => [TimingCounter a] -> ShowS
showList :: [TimingCounter a] -> ShowS
Show, Typeable, (forall x. TimingCounter a -> Rep (TimingCounter a) x)
-> (forall x. Rep (TimingCounter a) x -> TimingCounter a)
-> Generic (TimingCounter a)
forall x. Rep (TimingCounter a) x -> TimingCounter a
forall x. TimingCounter a -> Rep (TimingCounter a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (TimingCounter a) x -> TimingCounter a
forall a x. TimingCounter a -> Rep (TimingCounter a) x
$cfrom :: forall a x. TimingCounter a -> Rep (TimingCounter a) x
from :: forall x. TimingCounter a -> Rep (TimingCounter a) x
$cto :: forall a x. Rep (TimingCounter a) x -> TimingCounter a
to :: forall x. Rep (TimingCounter a) x -> TimingCounter a
Generic)

instance NFData a => NFData (TimingCounter a)
instance Binary a => Binary (TimingCounter a)

-- | An empty counter.
emptyTimingCounter :: TimingData a => TimingCounter a
emptyTimingCounter :: forall a. TimingData a => TimingCounter a
emptyTimingCounter =
  TimingCounter { timingCounterValue :: a
timingCounterValue = a
0,
                  timingCounterStats :: TimingStats a
timingCounterStats = TimingStats a
forall a. TimingData a => TimingStats a
emptyTimingStats }

-- | Increase the counter at the specified time.
incTimingCounter :: TimingData a => Double -> a -> TimingCounter a -> TimingCounter a
incTimingCounter :: forall a.
TimingData a =>
Double -> a -> TimingCounter a -> TimingCounter a
incTimingCounter Double
t a
a TimingCounter a
counter =
  TimingCounter { timingCounterValue :: a
timingCounterValue = a
a',
                  timingCounterStats :: TimingStats a
timingCounterStats = Double -> a -> TimingStats a -> TimingStats a
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t a
a' (TimingCounter a -> TimingStats a
forall a. TimingCounter a -> TimingStats a
timingCounterStats TimingCounter a
counter) }
  where a' :: a
a' = TimingCounter a -> a
forall a. TimingCounter a -> a
timingCounterValue TimingCounter a
counter a -> a -> a
forall a. Num a => a -> a -> a
+ a
a

-- | Decrease the counter at the specified time.
decTimingCounter :: TimingData a => Double -> a -> TimingCounter a -> TimingCounter a
decTimingCounter :: forall a.
TimingData a =>
Double -> a -> TimingCounter a -> TimingCounter a
decTimingCounter Double
t a
a TimingCounter a
counter =
  TimingCounter { timingCounterValue :: a
timingCounterValue = a
a',
                  timingCounterStats :: TimingStats a
timingCounterStats = Double -> a -> TimingStats a -> TimingStats a
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t a
a' (TimingCounter a -> TimingStats a
forall a. TimingCounter a -> TimingStats a
timingCounterStats TimingCounter a
counter) }
  where a' :: a
a' = TimingCounter a -> a
forall a. TimingCounter a -> a
timingCounterValue TimingCounter a
counter a -> a -> a
forall a. Num a => a -> a -> a
- a
a

-- | Set a new value for the counter at the specified time.
setTimingCounter :: TimingData a => Double -> a -> TimingCounter a -> TimingCounter a
setTimingCounter :: forall a.
TimingData a =>
Double -> a -> TimingCounter a -> TimingCounter a
setTimingCounter Double
t a
a TimingCounter a
counter =
  TimingCounter { timingCounterValue :: a
timingCounterValue = a
a,
                  timingCounterStats :: TimingStats a
timingCounterStats = Double -> a -> TimingStats a -> TimingStats a
forall a.
TimingData a =>
Double -> a -> TimingStats a -> TimingStats a
addTimingStats Double
t a
a (TimingCounter a -> TimingStats a
forall a. TimingCounter a -> TimingStats a
timingCounterStats TimingCounter a
counter) }

-- | Create a timing counter with the specified initial value at the given time.
returnTimingCounter :: TimingData a => Double -> a -> TimingCounter a
returnTimingCounter :: forall a. TimingData a => Double -> a -> TimingCounter a
returnTimingCounter Double
t a
a =
  TimingCounter { timingCounterValue :: a
timingCounterValue = a
a,
                  timingCounterStats :: TimingStats a
timingCounterStats = Double -> a -> TimingStats a
forall a. TimingData a => Double -> a -> TimingStats a
returnTimingStats Double
t a
a }