Created
April 27, 2020 19:58
-
-
Save icholy/86c54182207944ea20eff7de1060d7f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rolling | |
import "gonum.org/v1/plot/plotter" | |
type XYs struct { | |
size int | |
start int | |
length int | |
data plotter.XYs | |
} | |
func NewXYs(size int) *XYs { | |
return &XYs{ | |
size: size, | |
data: make(plotter.XYs, size), | |
} | |
} | |
var _ plotter.XYer = &XYs{} | |
func (xy *XYs) Len() int { return xy.length } | |
func (xy *XYs) XY(i int) (float64, float64) { | |
idx := (xy.start + i) % xy.size | |
return xy.data.XY(idx) | |
} | |
func (xy *XYs) Add(x plotter.XY) { | |
end := (xy.start + xy.length) % xy.size | |
if xy.length < xy.size { | |
xy.length++ | |
} else { | |
xy.start = (xy.start + 1) % xy.size | |
} | |
xy.data[end] = x | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment