Skip to content

Instantly share code, notes, and snippets.

@icholy
Created April 27, 2020 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icholy/86c54182207944ea20eff7de1060d7f4 to your computer and use it in GitHub Desktop.
Save icholy/86c54182207944ea20eff7de1060d7f4 to your computer and use it in GitHub Desktop.
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