|
| 1 | +// Copyright 2015 mint.zhao.chiu@gmail.com |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +// License for the specific language governing permissions and limitations |
| 13 | +// under the License. |
| 14 | +package stacks |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "github.com/aiwuTech/container/lists" |
| 19 | + "strings" |
| 20 | +) |
| 21 | + |
| 22 | +type ArrayStack struct { |
| 23 | + list *lists.ArrayList |
| 24 | +} |
| 25 | + |
| 26 | +func NewArrayStack() *ArrayStack { |
| 27 | + return &ArrayStack{ |
| 28 | + list: lists.NewArrayList(), |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Pushes a value onto the top of the stack |
| 33 | +func (stack *ArrayStack) Push(value interface{}) { |
| 34 | + stack.list.Add(value) |
| 35 | +} |
| 36 | + |
| 37 | +// Pops (removes) top element on stack and returns it, or nil if stack is empty. |
| 38 | +// Second return parameter is true, unless the stack was empty and there was nothing to pop. |
| 39 | +func (stack *ArrayStack) Pop() (value interface{}, ok bool) { |
| 40 | + value, ok = stack.list.Get(stack.list.Len() - 1) |
| 41 | + stack.list.Remove(stack.list.Len() - 1) |
| 42 | + return |
| 43 | +} |
| 44 | + |
| 45 | +// Returns top element on the stack without removing it, or nil if stack is empty. |
| 46 | +// Second return parameter is true, unless the stack was empty and there was nothing to peek. |
| 47 | +func (stack *ArrayStack) Peek() (value interface{}, ok bool) { |
| 48 | + return stack.list.Get(stack.list.Len() - 1) |
| 49 | +} |
| 50 | + |
| 51 | +// Returns true if stack does not contain any elements. |
| 52 | +func (stack *ArrayStack) Empty() bool { |
| 53 | + return stack.list.Empty() |
| 54 | +} |
| 55 | + |
| 56 | +// Returns number of elements within the stack. |
| 57 | +func (stack *ArrayStack) Len() int { |
| 58 | + return stack.list.Len() |
| 59 | +} |
| 60 | + |
| 61 | +// Removes all elements from the stack. |
| 62 | +func (stack *ArrayStack) Clear() { |
| 63 | + stack.list.Clear() |
| 64 | +} |
| 65 | + |
| 66 | +// Returns all elements in the stack (LIFO order). |
| 67 | +func (stack *ArrayStack) Elements() []interface{} { |
| 68 | + size := stack.list.Len() |
| 69 | + elements := make([]interface{}, size, size) |
| 70 | + for i := 1; i <= size; i++ { |
| 71 | + elements[size-i], _ = stack.list.Get(i - 1) // in reverse (LIFO) |
| 72 | + } |
| 73 | + return elements |
| 74 | +} |
| 75 | + |
| 76 | +func (stack *ArrayStack) String() string { |
| 77 | + str := "ArrayStack{ " |
| 78 | + values := []string{} |
| 79 | + for _, value := range stack.list.Elements() { |
| 80 | + values = append(values, fmt.Sprintf("%v", value)) |
| 81 | + } |
| 82 | + str += strings.Join(values, ", ") |
| 83 | + str += " }" |
| 84 | + return str |
| 85 | +} |
| 86 | + |
| 87 | +// not important, just for interface{} |
| 88 | +func (stack *ArrayStack) Contains(elements ...interface{}) bool { |
| 89 | + return stack.list.Contains(elements...) |
| 90 | +} |
0 commit comments