1
0
mirror of https://github.com/divan/expvarmon.git synced 2025-05-04 22:18:08 +08:00
expvarmon/stack.go

43 lines
899 B
Go
Raw Normal View History

2015-04-21 16:33:04 +03:00
package main
2015-05-01 16:49:19 +03:00
// DefaultSize specifies maximum number of items in stack.
//
// Values should be enough for sparklines on high-res terminals
// with minimal font size.
const DefaultSize = 1200
2015-04-25 09:06:45 +03:00
// Stack is a limited FIFO for holding sparkline values.
2015-04-21 16:33:04 +03:00
type Stack struct {
Values []int
Len int
}
2015-05-01 16:49:19 +03:00
// NewStack inits new Stack with default size limit.
func NewStack() *Stack {
return NewStackWithSize(DefaultSize)
}
// NewStackWithSize inits new Stack with size limit.
func NewStackWithSize(size int) *Stack {
2015-04-21 16:33:04 +03:00
return &Stack{
Values: make([]int, size),
Len: size,
}
}
2015-04-25 09:06:45 +03:00
// Push inserts data to stack, preserving constant length.
2015-04-21 16:33:04 +03: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-04-30 23:54:54 +03:00
// Front returns front value.
func (s *Stack) Front() int {
if len(s.Values) == 0 {
return 0
}
return s.Values[len(s.Values)-1]
}