forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsmoothed_value.go
102 lines (86 loc) · 2.01 KB
/
smoothed_value.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package rencoder
//package main
import (
//"fmt"
"sync"
)
type smoothedValue struct {
mu sync.Mutex
values []float64
ind int
cumValue float64
numPeriods int
numPeriodsF float64
}
func (s *smoothedValue) Update(newValue float64) float64 {
s.mu.Lock()
defer s.mu.Unlock()
s.ind = (s.ind + 1) % s.numPeriods
s.cumValue += newValue - s.values[s.ind]
s.values[s.ind] = newValue
return s.cumValue / s.numPeriodsF
}
func (s *smoothedValue) Value() float64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.cumValue / s.numPeriodsF
}
func (s *smoothedValue) Reset() {
s.mu.Lock()
s.cumValue = 0
for i := range s.values {
s.values[i] = 0
}
s.mu.Unlock()
}
func (s *smoothedValue) ChangeNumPeriods(newNum int) {
s.mu.Lock()
defer s.mu.Unlock()
if newNum == s.numPeriods {
return
}
next := s.ind + 1
if numDif := newNum - s.numPeriods; numDif > 0 {
s.values = append(s.values[:next], append(make([]float64, numDif), s.values[next:]...)...)
s.numPeriods = newNum
s.numPeriodsF = float64(newNum)
return
}
//if newNum < s.numPeriods {
nextValid := (s.ind + s.numPeriods - newNum + 1) % s.numPeriods
// update s.cumValue
if newNum > s.numPeriods/2 {
for i := next; i != nextValid; i = (i + 1) % s.numPeriods {
s.cumValue -= s.values[i]
}
} else { // if there is too much to subtract, sum what remains
s.cumValue = 0
for i := nextValid; i != next; i = (i + 1) % s.numPeriods {
s.cumValue += s.values[i]
}
}
// remove oldest elements from s.values
if nextValid > s.ind {
s.values = append(s.values[:next], s.values[nextValid:]...)
} else {
s.values = s.values[nextValid:next]
}
// update s.ind if necessary
if nextValid < next {
if 0 < nextValid {
s.ind -= nextValid
}
}
s.numPeriods = newNum
s.numPeriodsF = float64(newNum)
//}
}
func NewSmoothedValue(numPeriods int) *smoothedValue {
return &smoothedValue{
numPeriods: numPeriods,
numPeriodsF: float64(numPeriods),
values: make([]float64, numPeriods),
ind: 0,
cumValue: 0,
}
}