Skip to content

Commit

Permalink
This is an automated cherry-pick of tikv#6080
Browse files Browse the repository at this point in the history
ref tikv#5798, close tikv#6069

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
lhy1024 authored and ti-chi-bot committed Mar 3, 2023
1 parent 297cee8 commit 0c0f4f0
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/movingaverage/median_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,31 @@ import "github.com/elliotchance/pie/v2"
// There are at most `size` data points for calculating.
// References: https://en.wikipedia.org/wiki/Median_filter.
type MedianFilter struct {
<<<<<<< HEAD
records []float64
size uint64
count uint64
instantaneous float64
=======
// It is not thread safe to read and write records at the same time.
// If there are concurrent read and write, the read may get an old value.
// And we should avoid concurrent write.
records []float64
size uint64
count uint64
result float64
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
}

// NewMedianFilter returns a MedianFilter.
func NewMedianFilter(size int) *MedianFilter {
return &MedianFilter{
records: make([]float64, size),
size: uint64(size),
<<<<<<< HEAD
=======
result: 0,
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
}
}

Expand All @@ -39,31 +53,51 @@ func (r *MedianFilter) Add(n float64) {
r.instantaneous = n
r.records[r.count%r.size] = n
r.count++
<<<<<<< HEAD
}

// Get returns the median of the data set.
func (r *MedianFilter) Get() float64 {
if r.count == 0 {
return 0
}
=======
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
records := r.records
if r.count < r.size {
records = r.records[:r.count]
}
<<<<<<< HEAD
return pie.Median(records)
=======
r.result = pie.Median(records)
}

// Get returns the median of the data set.
func (r *MedianFilter) Get() float64 {
return r.result
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
}

// Reset cleans the data set.
func (r *MedianFilter) Reset() {
r.instantaneous = 0
r.count = 0
<<<<<<< HEAD
=======
r.result = 0
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
}

// Set = Reset + Add.
func (r *MedianFilter) Set(n float64) {
r.instantaneous = n
r.records[0] = n
r.count = 1
<<<<<<< HEAD
=======
r.result = n
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
}

// GetInstantaneous returns the value just added.
Expand All @@ -76,9 +110,16 @@ func (r *MedianFilter) Clone() *MedianFilter {
records := make([]float64, len(r.records))
copy(records, r.records)
return &MedianFilter{
<<<<<<< HEAD
records: records,
size: r.size,
count: r.count,
instantaneous: r.instantaneous,
=======
records: records,
size: r.size,
count: r.count,
result: r.result,
>>>>>>> d85a0e4e3 (moving_filter: fix data race with cache result (#6080))
}
}

0 comments on commit 0c0f4f0

Please sign in to comment.