{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE Rank2Types #-}
module Data.Metrics.Reservoir.ExponentiallyDecaying (
ExponentiallyDecayingReservoir,
standardReservoir,
reservoir,
clear,
size,
snapshot,
rescale,
update
) where
import Control.Lens
import Control.Lens.TH
import Control.Monad.Primitive
import Control.Monad.ST
import Data.Time.Clock
import Data.Time.Clock.POSIX
import Data.Metrics.Internal
import qualified Data.Map as M
import qualified Data.Metrics.Reservoir as R
import Data.Metrics.Snapshot (Snapshot(..), takeSnapshot)
import Data.Primitive.MutVar
import qualified Data.Vector.Unboxed as V
import Data.Word
import System.PosixCompat.Time
import System.Posix.Types
import System.Random.MWC
baseRescaleThreshold :: Word64
baseRescaleThreshold :: Word64
baseRescaleThreshold = Word64
60 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
60
data ExponentiallyDecayingReservoir = ExponentiallyDecayingReservoir
{ ExponentiallyDecayingReservoir -> Int
exponentiallyDecayingReservoirInnerSize :: {-# UNPACK #-} !Int
, ExponentiallyDecayingReservoir -> Double
exponentiallyDecayingReservoirAlpha :: {-# UNPACK #-} !Double
, ExponentiallyDecayingReservoir -> Word64
exponentiallyDecayingReservoirRescaleThreshold :: {-# UNPACK #-} !Word64
, ExponentiallyDecayingReservoir -> Map Double Double
exponentiallyDecayingReservoirInnerReservoir :: !(M.Map Double Double)
, ExponentiallyDecayingReservoir -> Int
exponentiallyDecayingReservoirCount :: {-# UNPACK #-} !Int
, ExponentiallyDecayingReservoir -> Word64
exponentiallyDecayingReservoirStartTime :: {-# UNPACK #-} !Word64
, ExponentiallyDecayingReservoir -> Word64
exponentiallyDecayingReservoirNextScaleTime :: {-# UNPACK #-} !Word64
, ExponentiallyDecayingReservoir -> Seed
exponentiallyDecayingReservoirSeed :: !Seed
} deriving (Int -> ExponentiallyDecayingReservoir -> ShowS
[ExponentiallyDecayingReservoir] -> ShowS
ExponentiallyDecayingReservoir -> String
(Int -> ExponentiallyDecayingReservoir -> ShowS)
-> (ExponentiallyDecayingReservoir -> String)
-> ([ExponentiallyDecayingReservoir] -> ShowS)
-> Show ExponentiallyDecayingReservoir
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExponentiallyDecayingReservoir -> ShowS
showsPrec :: Int -> ExponentiallyDecayingReservoir -> ShowS
$cshow :: ExponentiallyDecayingReservoir -> String
show :: ExponentiallyDecayingReservoir -> String
$cshowList :: [ExponentiallyDecayingReservoir] -> ShowS
showList :: [ExponentiallyDecayingReservoir] -> ShowS
Show)
makeFields ''ExponentiallyDecayingReservoir
standardReservoir :: NominalDiffTime -> Seed -> R.Reservoir
standardReservoir :: NominalDiffTime -> Seed -> Reservoir
standardReservoir = Double -> Int -> NominalDiffTime -> Seed -> Reservoir
reservoir Double
0.015 Int
1028
reservoir :: Double
-> Int
-> NominalDiffTime
-> Seed -> R.Reservoir
reservoir :: Double -> Int -> NominalDiffTime -> Seed -> Reservoir
reservoir Double
a Int
r NominalDiffTime
t Seed
s = R.Reservoir
{ reservoirClear :: NominalDiffTime
-> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
R.reservoirClear = NominalDiffTime
-> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
clear
, reservoirSize :: ExponentiallyDecayingReservoir -> Int
R.reservoirSize = ExponentiallyDecayingReservoir -> Int
size
, reservoirSnapshot :: ExponentiallyDecayingReservoir -> Snapshot
R.reservoirSnapshot = ExponentiallyDecayingReservoir -> Snapshot
snapshot
, reservoirUpdate :: Double
-> NominalDiffTime
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
R.reservoirUpdate = Double
-> NominalDiffTime
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
update
, reservoirState :: ExponentiallyDecayingReservoir
R.reservoirState = Int
-> Double
-> Word64
-> Map Double Double
-> Int
-> Word64
-> Word64
-> Seed
-> ExponentiallyDecayingReservoir
ExponentiallyDecayingReservoir Int
r Double
a Word64
baseRescaleThreshold Map Double Double
forall k a. Map k a
M.empty Int
0 Word64
c Word64
c' Seed
s
}
where
c :: Word64
c = NominalDiffTime -> Word64
forall b. Integral b => NominalDiffTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate NominalDiffTime
t
c' :: Word64
c' = Word64
c Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
baseRescaleThreshold
clear :: NominalDiffTime -> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
clear :: NominalDiffTime
-> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
clear = NominalDiffTime
-> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
forall {a} {b} {b} {b} {k} {a}.
(RealFrac a, Integral b, HasRescaleThreshold b b, HasStartTime b b,
HasNextScaleTime b b, HasCount b b, HasInnerReservoir b (Map k a),
Num b) =>
a -> b -> b
go
where
go :: a -> b -> b
go a
t b
c = b
c b -> (b -> b) -> b
forall a b. a -> (a -> b) -> b
& (b -> Identity b) -> b -> Identity b
forall s a. HasStartTime s a => Lens' s a
Lens' b b
startTime ((b -> Identity b) -> b -> Identity b) -> b -> b -> b
forall s t a b. ASetter s t a b -> b -> s -> t
.~ b
t' b -> (b -> b) -> b
forall a b. a -> (a -> b) -> b
& (b -> Identity b) -> b -> Identity b
forall s a. HasNextScaleTime s a => Lens' s a
Lens' b b
nextScaleTime ((b -> Identity b) -> b -> Identity b) -> b -> b -> b
forall s t a b. ASetter s t a b -> b -> s -> t
.~ b
t'' b -> (b -> b) -> b
forall a b. a -> (a -> b) -> b
& (b -> Identity b) -> b -> Identity b
forall s a. HasCount s a => Lens' s a
Lens' b b
count ((b -> Identity b) -> b -> Identity b) -> b -> b -> b
forall s t a b. ASetter s t a b -> b -> s -> t
.~ b
0 b -> (b -> b) -> b
forall a b. a -> (a -> b) -> b
& (Map k a -> Identity (Map k a)) -> b -> Identity b
forall s a. HasInnerReservoir s a => Lens' s a
Lens' b (Map k a)
innerReservoir ((Map k a -> Identity (Map k a)) -> b -> Identity b)
-> Map k a -> b -> b
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Map k a
forall k a. Map k a
M.empty
where
t' :: b
t' = a -> b
forall b. Integral b => a -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate a
t
t'' :: b
t'' = b
t' b -> b -> b
forall a. Num a => a -> a -> a
+ b
c b -> Getting b b b -> b
forall s a. s -> Getting a s a -> a
^. Getting b b b
forall s a. HasRescaleThreshold s a => Lens' s a
Lens' b b
rescaleThreshold
{-# INLINEABLE clear #-}
size :: ExponentiallyDecayingReservoir -> Int
size :: ExponentiallyDecayingReservoir -> Int
size = ExponentiallyDecayingReservoir -> Int
forall {s} {a}. (HasInnerSize s a, HasCount s a, Ord a) => s -> a
go
where
go :: s -> a
go s
r = a -> a -> a
forall a. Ord a => a -> a -> a
min a
c a
s
where
c :: a
c = s
r s -> Getting a s a -> a
forall s a. s -> Getting a s a -> a
^. Getting a s a
forall s a. HasCount s a => Lens' s a
Lens' s a
count
s :: a
s = s
r s -> Getting a s a -> a
forall s a. s -> Getting a s a -> a
^. Getting a s a
forall s a. HasInnerSize s a => Lens' s a
Lens' s a
innerSize
{-# INLINEABLE size #-}
snapshot :: ExponentiallyDecayingReservoir -> Snapshot
snapshot :: ExponentiallyDecayingReservoir -> Snapshot
snapshot ExponentiallyDecayingReservoir
r = (forall s. ST s Snapshot) -> Snapshot
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s Snapshot) -> Snapshot)
-> (forall s. ST s Snapshot) -> Snapshot
forall a b. (a -> b) -> a -> b
$ do
let svals :: Vector Double
svals = [Double] -> Vector Double
forall a. Unbox a => [a] -> Vector a
V.fromList ([Double] -> Vector Double) -> [Double] -> Vector Double
forall a b. (a -> b) -> a -> b
$ Map Double Double -> [Double]
forall k a. Map k a -> [a]
M.elems (Map Double Double -> [Double]) -> Map Double Double -> [Double]
forall a b. (a -> b) -> a -> b
$ ExponentiallyDecayingReservoir
r ExponentiallyDecayingReservoir
-> Getting
(Map Double Double)
ExponentiallyDecayingReservoir
(Map Double Double)
-> Map Double Double
forall s a. s -> Getting a s a -> a
^. Getting
(Map Double Double)
ExponentiallyDecayingReservoir
(Map Double Double)
forall s a. HasInnerReservoir s a => Lens' s a
Lens' ExponentiallyDecayingReservoir (Map Double Double)
innerReservoir
mvals <- Vector Double -> ST s (MVector (PrimState (ST s)) Double)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
Vector a -> m (MVector (PrimState m) a)
V.unsafeThaw Vector Double
svals
takeSnapshot mvals
{-# INLINEABLE snapshot #-}
weight :: Double -> Word64 -> Double
weight :: Double -> Word64 -> Double
weight Double
alpha Word64
t = Double -> Double
forall a. Floating a => a -> a
exp (Double
alpha Double -> Double -> Double
forall a. Num a => a -> a -> a
* Word64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
t)
{-# INLINE weight #-}
rescale :: Word64 -> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
rescale :: Word64
-> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
rescale Word64
now ExponentiallyDecayingReservoir
c = ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> (ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir)
-> ExponentiallyDecayingReservoir
forall a b. a -> (a -> b) -> b
& (Word64 -> Identity Word64)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir
forall s a. HasStartTime s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Word64
startTime ((Word64 -> Identity Word64)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir)
-> Word64
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Word64
now ExponentiallyDecayingReservoir
-> (ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir)
-> ExponentiallyDecayingReservoir
forall a b. a -> (a -> b) -> b
& (Word64 -> Identity Word64)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir
forall s a. HasNextScaleTime s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Word64
nextScaleTime ((Word64 -> Identity Word64)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir)
-> Word64
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Word64
st ExponentiallyDecayingReservoir
-> (ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir)
-> ExponentiallyDecayingReservoir
forall a b. a -> (a -> b) -> b
& (Int -> Identity Int)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir
forall s a. HasCount s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Int
count ((Int -> Identity Int)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir)
-> Int
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Map Double Double -> Int
forall k a. Map k a -> Int
M.size Map Double Double
adjustedReservoir ExponentiallyDecayingReservoir
-> (ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir)
-> ExponentiallyDecayingReservoir
forall a b. a -> (a -> b) -> b
& (Map Double Double -> Identity (Map Double Double))
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir
forall s a. HasInnerReservoir s a => Lens' s a
Lens' ExponentiallyDecayingReservoir (Map Double Double)
innerReservoir ((Map Double Double -> Identity (Map Double Double))
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir)
-> Map Double Double
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Map Double Double
adjustedReservoir
where
potentialScaleTime :: Word64
potentialScaleTime = Word64
now Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
baseRescaleThreshold
currentScaleTime :: Word64
currentScaleTime = ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Word64 ExponentiallyDecayingReservoir Word64 -> Word64
forall s a. s -> Getting a s a -> a
^. Getting Word64 ExponentiallyDecayingReservoir Word64
forall s a. HasNextScaleTime s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Word64
nextScaleTime
st :: Word64
st = if Word64
potentialScaleTime Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
currentScaleTime then Word64
potentialScaleTime else Word64
currentScaleTime
diff :: Word64
diff = Word64
now Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Word64 ExponentiallyDecayingReservoir Word64 -> Word64
forall s a. s -> Getting a s a -> a
^. Getting Word64 ExponentiallyDecayingReservoir Word64
forall s a. HasStartTime s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Word64
startTime
adjustKey :: Double -> Double
adjustKey Double
x = Double
x Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double -> Double
forall a. Floating a => a -> a
exp (-Double
_alpha Double -> Double -> Double
forall a. Num a => a -> a -> a
* Word64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
diff)
adjustedReservoir :: Map Double Double
adjustedReservoir = (Double -> Double) -> Map Double Double -> Map Double Double
forall k2 k1 a. Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
M.mapKeys Double -> Double
adjustKey (Map Double Double -> Map Double Double)
-> Map Double Double -> Map Double Double
forall a b. (a -> b) -> a -> b
$ ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting
(Map Double Double)
ExponentiallyDecayingReservoir
(Map Double Double)
-> Map Double Double
forall s a. s -> Getting a s a -> a
^. Getting
(Map Double Double)
ExponentiallyDecayingReservoir
(Map Double Double)
forall s a. HasInnerReservoir s a => Lens' s a
Lens' ExponentiallyDecayingReservoir (Map Double Double)
innerReservoir
_alpha :: Double
_alpha = ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Double ExponentiallyDecayingReservoir Double -> Double
forall s a. s -> Getting a s a -> a
^. Getting Double ExponentiallyDecayingReservoir Double
forall s a. HasAlpha s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Double
alpha
{-# INLINEABLE rescale #-}
update :: Double
-> NominalDiffTime
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
update :: Double
-> NominalDiffTime
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
update Double
v NominalDiffTime
t ExponentiallyDecayingReservoir
c = ExponentiallyDecayingReservoir
rescaled ExponentiallyDecayingReservoir
-> (ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir)
-> ExponentiallyDecayingReservoir
forall a b. a -> (a -> b) -> b
& (Seed -> Identity Seed)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir
forall s a. HasSeed s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Seed
seed ((Seed -> Identity Seed)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir)
-> Seed
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Seed
s' ExponentiallyDecayingReservoir
-> (ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir)
-> ExponentiallyDecayingReservoir
forall a b. a -> (a -> b) -> b
& (Int -> Identity Int)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir
forall s a. HasCount s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Int
count ((Int -> Identity Int)
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir)
-> Int
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int
newCount ExponentiallyDecayingReservoir
-> (ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir)
-> ExponentiallyDecayingReservoir
forall a b. a -> (a -> b) -> b
& (Map Double Double -> Identity (Map Double Double))
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir
forall s a. HasInnerReservoir s a => Lens' s a
Lens' ExponentiallyDecayingReservoir (Map Double Double)
innerReservoir ((Map Double Double -> Identity (Map Double Double))
-> ExponentiallyDecayingReservoir
-> Identity ExponentiallyDecayingReservoir)
-> Map Double Double
-> ExponentiallyDecayingReservoir
-> ExponentiallyDecayingReservoir
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Map Double Double -> Map Double Double
addValue Map Double Double
r
where
rescaled :: ExponentiallyDecayingReservoir
rescaled = if Word64
seconds Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Word64 ExponentiallyDecayingReservoir Word64 -> Word64
forall s a. s -> Getting a s a -> a
^. Getting Word64 ExponentiallyDecayingReservoir Word64
forall s a. HasNextScaleTime s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Word64
nextScaleTime
then Word64
-> ExponentiallyDecayingReservoir -> ExponentiallyDecayingReservoir
rescale Word64
seconds ExponentiallyDecayingReservoir
c
else ExponentiallyDecayingReservoir
c
seconds :: Word64
seconds = NominalDiffTime -> Word64
forall b. Integral b => NominalDiffTime -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate NominalDiffTime
t
priority :: Double
priority = Double -> Word64 -> Double
weight (ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Double ExponentiallyDecayingReservoir Double -> Double
forall s a. s -> Getting a s a -> a
^. Getting Double ExponentiallyDecayingReservoir Double
forall s a. HasAlpha s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Double
alpha) (Word64
seconds Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Word64 ExponentiallyDecayingReservoir Word64 -> Word64
forall s a. s -> Getting a s a -> a
^. Getting Word64 ExponentiallyDecayingReservoir Word64
forall s a. HasStartTime s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Word64
startTime) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
priorityDenom
addValue :: Map Double Double -> Map Double Double
addValue Map Double Double
r = if Int
newCount Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= (ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Int ExponentiallyDecayingReservoir Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int ExponentiallyDecayingReservoir Int
forall s a. HasInnerSize s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Int
innerSize)
then Double -> Double -> Map Double Double -> Map Double Double
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Double
priority Double
v Map Double Double
r
else if Double
firstKey Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
priority
then Double -> Map Double Double -> Map Double Double
forall k a. Ord k => k -> Map k a -> Map k a
M.delete Double
firstKey (Map Double Double -> Map Double Double)
-> Map Double Double -> Map Double Double
forall a b. (a -> b) -> a -> b
$ (Double -> Double -> Double)
-> Double -> Double -> Map Double Double -> Map Double Double
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
M.insertWith Double -> Double -> Double
forall a b. a -> b -> a
const Double
priority Double
v Map Double Double
r
else Map Double Double
r
r :: Map Double Double
r = ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting
(Map Double Double)
ExponentiallyDecayingReservoir
(Map Double Double)
-> Map Double Double
forall s a. s -> Getting a s a -> a
^. Getting
(Map Double Double)
ExponentiallyDecayingReservoir
(Map Double Double)
forall s a. HasInnerReservoir s a => Lens' s a
Lens' ExponentiallyDecayingReservoir (Map Double Double)
innerReservoir
firstKey :: Double
firstKey = [Double] -> Double
forall a. HasCallStack => [a] -> a
head ([Double] -> Double) -> [Double] -> Double
forall a b. (a -> b) -> a -> b
$ Map Double Double -> [Double]
forall k a. Map k a -> [k]
M.keys Map Double Double
r
newCount :: Int
newCount = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Int ExponentiallyDecayingReservoir Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int ExponentiallyDecayingReservoir Int
forall s a. HasCount s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Int
count
(Double
priorityDenom, Seed
s') = (forall s. ST s (Double, Seed)) -> (Double, Seed)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Double, Seed)) -> (Double, Seed))
-> (forall s. ST s (Double, Seed)) -> (Double, Seed)
forall a b. (a -> b) -> a -> b
$ do
g <- Seed -> ST s (Gen (PrimState (ST s)))
forall (m :: * -> *). PrimMonad m => Seed -> m (Gen (PrimState m))
restore (Seed -> ST s (Gen (PrimState (ST s))))
-> Seed -> ST s (Gen (PrimState (ST s)))
forall a b. (a -> b) -> a -> b
$ ExponentiallyDecayingReservoir
c ExponentiallyDecayingReservoir
-> Getting Seed ExponentiallyDecayingReservoir Seed -> Seed
forall s a. s -> Getting a s a -> a
^. Getting Seed ExponentiallyDecayingReservoir Seed
forall s a. HasSeed s a => Lens' s a
Lens' ExponentiallyDecayingReservoir Seed
seed
p <- uniform g
s' <- save g
return (p :: Double, s')
{-# INLINEABLE update #-}