gitee.com/quant1x/gox@v1.21.2/util/arraystack/arraystack.go (about) 1 // Copyright (c) 2015, Emir Pasic. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package arraystack implements a stack backed by array list. 6 // 7 // Structure is not thread safe. 8 // 9 // Reference: https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29#Array 10 package arraystack 11 12 import ( 13 "fmt" 14 "gitee.com/quant1x/gox/util/arraylist" 15 "gitee.com/quant1x/gox/util/internal" 16 "strings" 17 ) 18 19 func assertStackImplementation() { 20 var _ internal.Stack = (*Stack)(nil) 21 } 22 23 // Stack holds elements in an array-list 24 type Stack struct { 25 list *arraylist.List 26 } 27 28 // New instantiates a new empty stack 29 func New() *Stack { 30 return &Stack{list: arraylist.New()} 31 } 32 33 // Push adds a value onto the top of the stack 34 func (stack *Stack) Push(value interface{}) { 35 stack.list.Add(value) 36 } 37 38 // Pop removes top element on stack and returns it, or nil if stack is empty. 39 // Second return parameter is true, unless the stack was empty and there was nothing to pop. 40 func (stack *Stack) Pop() (value interface{}, ok bool) { 41 value, ok = stack.list.Get(stack.list.Size() - 1) 42 stack.list.Remove(stack.list.Size() - 1) 43 return 44 } 45 46 // Peek returns top element on the stack without removing it, or nil if stack is empty. 47 // Second return parameter is true, unless the stack was empty and there was nothing to peek. 48 func (stack *Stack) Peek() (value interface{}, ok bool) { 49 return stack.list.Get(stack.list.Size() - 1) 50 } 51 52 // Empty returns true if stack does not contain any elements. 53 func (stack *Stack) Empty() bool { 54 return stack.list.Empty() 55 } 56 57 // Size returns number of elements within the stack. 58 func (stack *Stack) Size() int { 59 return stack.list.Size() 60 } 61 62 // Clear removes all elements from the stack. 63 func (stack *Stack) Clear() { 64 stack.list.Clear() 65 } 66 67 // Values returns all elements in the stack (LIFO order). 68 func (stack *Stack) Values() []interface{} { 69 size := stack.list.Size() 70 elements := make([]interface{}, size, size) 71 for i := 1; i <= size; i++ { 72 elements[size-i], _ = stack.list.Get(i - 1) // in reverse (LIFO) 73 } 74 return elements 75 } 76 77 // String returns a string representation of container 78 func (stack *Stack) String() string { 79 str := "ArrayStack\n" 80 values := []string{} 81 for _, value := range stack.list.Values() { 82 values = append(values, fmt.Sprintf("%v", value)) 83 } 84 str += strings.Join(values, ", ") 85 return str 86 } 87 88 // Check that the index is within bounds of the list 89 func (stack *Stack) withinRange(index int) bool { 90 return index >= 0 && index < stack.list.Size() 91 }