github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xcontainer/stack/slice.go (about)

     1  package stack
     2  
     3  type sliceStack []interface{}
     4  
     5  func (s *sliceStack) Push(x interface{}) {
     6  	*s = append(*s, x)
     7  }
     8  
     9  func (s *sliceStack) Pop() (x interface{}, ok bool) {
    10  	if (*s).Size() == 0 {
    11  		return nil, false
    12  	}
    13  	p := (*s)[len(*s)-1]
    14  	*s = (*s)[:len(*s)-1]
    15  	return p, true
    16  }
    17  
    18  func (s sliceStack) Peek() (x interface{}, ok bool) {
    19  	if s.Size() == 0 {
    20  		return nil, false
    21  	}
    22  	return s[len(s)-1], true
    23  }
    24  
    25  func (s sliceStack) Size() int {
    26  	return len(s)
    27  }
    28  
    29  func NewSliceStack(capacity int) *sliceStack {
    30  	stack := sliceStack(make([]interface{}, 0, capacity))
    31  	return &stack
    32  }