expvarmon/stack.go

32 lines
585 B
Go
Raw Normal View History

2015-04-21 21:33:04 +08:00
package main
2015-04-25 14:06:45 +08:00
// Stack is a limited FIFO for holding sparkline values.
2015-04-21 21:33:04 +08:00
type Stack struct {
Values []int
Len int
}
2015-04-25 14:06:45 +08:00
// NewStack inits new Stack with size limit.
2015-04-21 21:33:04 +08:00
func NewStack(size int) *Stack {
return &Stack{
Values: make([]int, size),
Len: size,
}
}
2015-04-25 14:06:45 +08:00
// Push inserts data to stack, preserving constant length.
2015-04-21 21:33:04 +08:00
func (s *Stack) Push(val int) {
s.Values = append(s.Values, val)
if len(s.Values) > s.Len {
s.Values = s.Values[1:]
}
}
2015-05-01 04:54:54 +08:00
// Front returns front value.
func (s *Stack) Front() int {
if len(s.Values) == 0 {
return 0
}
return s.Values[len(s.Values)-1]
}