github.com/igggame/nebulas-go@v2.1.0+incompatible/common/stack/stack.go (about) 1 package stack 2 3 // Stack is a basic LIFO stack that resizes as needed. 4 type Stack struct { 5 entries []interface{} 6 limit int 7 } 8 9 // NewStack returns a new stack. 10 // if stack is full, entries in stack will be kicked out using FIFO 11 func NewStack(size int) *Stack { 12 return &Stack{limit: size} 13 } 14 15 // Len return size of stack 16 func (s *Stack) Len() int { 17 return len(s.entries) 18 } 19 20 // Push adds a node to the stack. 21 func (s *Stack) Push(entry interface{}) { 22 if s.Len() == s.limit { 23 s.entries = s.entries[1:] 24 } 25 s.entries = append(s.entries, entry) 26 } 27 28 // Pop removes and returns a node from the stack in last to first order. 29 func (s *Stack) Pop() interface{} { 30 if s.Len() == 0 { 31 return nil 32 } 33 tail := s.Len() - 1 34 entry := s.entries[tail] 35 s.entries = s.entries[:tail] 36 return entry 37 }