-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
queue.go
52 lines (43 loc) · 919 Bytes
/
queue.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
package types
// Super basic Series type that simply holds the float64 data
// with size limit (the only difference compare to float64slice)
type Queue struct {
SeriesBase
arr []float64
size int
}
func NewQueue(size int) *Queue {
out := &Queue{
arr: make([]float64, 0, size),
size: size,
}
out.SeriesBase.Series = out
return out
}
func (inc *Queue) Last(i int) float64 {
if i < 0 || len(inc.arr)-i-1 < 0 {
return 0
}
return inc.arr[len(inc.arr)-1-i]
}
func (inc *Queue) Index(i int) float64 {
return inc.Last(i)
}
func (inc *Queue) Length() int {
return len(inc.arr)
}
func (inc *Queue) Clone() *Queue {
out := &Queue{
arr: inc.arr[:],
size: inc.size,
}
out.SeriesBase.Series = out
return out
}
func (inc *Queue) Update(v float64) {
inc.arr = append(inc.arr, v)
if len(inc.arr) > inc.size {
inc.arr = inc.arr[len(inc.arr)-inc.size:]
}
}
var _ UpdatableSeriesExtend = &Queue{}