Skip to content

Commit b8ef9f9

Browse files
committed
remove write op in get function
Signed-off-by: lhy1024 <admin@liudos.us>
1 parent a99ff9f commit b8ef9f9

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

pkg/movingaverage/median_filter.go

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,58 +23,48 @@ type MedianFilter struct {
2323
// It is not thread safe to read and write records at the same time.
2424
// If there are concurrent read and write, the read may get an old value.
2525
// And we should avoid concurrent write.
26-
records []float64
27-
size uint64
28-
count uint64
29-
isUpdated bool
30-
result float64
26+
records []float64
27+
size uint64
28+
count uint64
29+
result float64
3130
}
3231

3332
// NewMedianFilter returns a MedianFilter.
3433
func NewMedianFilter(size int) *MedianFilter {
3534
return &MedianFilter{
36-
records: make([]float64, size),
37-
size: uint64(size),
38-
isUpdated: false,
39-
result: 0,
35+
records: make([]float64, size),
36+
size: uint64(size),
37+
result: 0,
4038
}
4139
}
4240

4341
// Add adds a data point.
4442
func (r *MedianFilter) Add(n float64) {
4543
r.records[r.count%r.size] = n
4644
r.count++
47-
r.isUpdated = true
48-
}
49-
50-
// Get returns the median of the data set.
51-
func (r *MedianFilter) Get() float64 {
52-
if !r.isUpdated {
53-
return r.result
54-
}
55-
if r.count == 0 {
56-
return 0
57-
}
5845
records := r.records
5946
if r.count < r.size {
6047
records = r.records[:r.count]
6148
}
6249
r.result = pie.Median(records)
63-
r.isUpdated = false
50+
}
51+
52+
// Get returns the median of the data set.
53+
func (r *MedianFilter) Get() float64 {
6454
return r.result
6555
}
6656

6757
// Reset cleans the data set.
6858
func (r *MedianFilter) Reset() {
6959
r.count = 0
70-
r.isUpdated = true
60+
r.result = 0
7161
}
7262

7363
// Set = Reset + Add.
7464
func (r *MedianFilter) Set(n float64) {
7565
r.records[0] = n
7666
r.count = 1
77-
r.isUpdated = true
67+
r.result = n
7868
}
7969

8070
// GetInstantaneous returns the value just added.
@@ -87,10 +77,9 @@ func (r *MedianFilter) Clone() *MedianFilter {
8777
records := make([]float64, len(r.records))
8878
copy(records, r.records)
8979
return &MedianFilter{
90-
records: records,
91-
size: r.size,
92-
count: r.count,
93-
isUpdated: r.isUpdated,
94-
result: r.result,
80+
records: records,
81+
size: r.size,
82+
count: r.count,
83+
result: r.result,
9584
}
9685
}

0 commit comments

Comments
 (0)