// Package stack implements a simple LIFO Stack for generic values package stack // Stack contains generic values type Stack []interface{} // New allocates a new Stack func New() *Stack { return new(Stack) } // Push value at tail func (s *Stack) Push(v interface{}) { *s = append(*s, v) } // Insert value at head func (s *Stack) Insert(v interface{}) { *s = append(Stack{v}, *s...) } // Pop value from tail func (s *Stack) Pop() (v interface{}) { if sp := len(*s) - 1; sp >= 0 { v, *s = (*s)[sp], (*s)[:sp] } return v } // Peek a top value on Stack func (s Stack) Peek() (v interface{}) { if sp := len(s) - 1; sp >= 0 { v = s[sp] } return v } // Swap two top values func (s Stack) Swap() { if sp := len(s); sp >= 2 { a := sp - 1 b := sp - 2 s[a], s[b] = s[b], s[a] } } // Depth returns actual size of Stack func (s Stack) Depth() int { return len(s) }