2015-03-05 18:29:57 +00:00
|
|
|
/*
|
2015-03-06 16:10:34 +00:00
|
|
|
Copyright (c) 2015, Emir Pasic
|
|
|
|
All rights reserved.
|
2015-03-05 18:29:57 +00:00
|
|
|
|
2015-03-06 16:10:34 +00:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
2015-03-05 18:29:57 +00:00
|
|
|
|
2015-03-06 16:10:34 +00:00
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
2015-03-05 18:29:57 +00:00
|
|
|
|
2015-03-06 16:10:34 +00:00
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2015-03-05 18:29:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// Implementation of stack using a slice.
|
|
|
|
// Structure is not thread safe.
|
|
|
|
// References: http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29
|
|
|
|
|
|
|
|
package arraystack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/emirpasic/gods/stacks"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertInterfaceImplementation() {
|
|
|
|
var _ stacks.Interface = (*Stack)(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stack struct {
|
2015-03-07 16:09:47 +00:00
|
|
|
elements []interface{}
|
|
|
|
top int
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiates a new empty stack
|
|
|
|
func New() *Stack {
|
2015-03-05 20:10:37 +00:00
|
|
|
return &Stack{top: -1}
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pushes a value onto the top of the stack
|
|
|
|
func (stack *Stack) Push(value interface{}) {
|
2015-03-05 20:10:37 +00:00
|
|
|
// Increase when capacity is reached by a factor of 1.5 and add one so it grows when size is zero
|
2015-03-07 16:09:47 +00:00
|
|
|
if stack.top+1 >= cap(stack.elements) {
|
|
|
|
currentSize := len(stack.elements)
|
2015-03-05 20:10:37 +00:00
|
|
|
sizeIncrease := int(1.5*float32(currentSize) + 1.0)
|
|
|
|
newSize := currentSize + sizeIncrease
|
|
|
|
newItems := make([]interface{}, newSize, newSize)
|
2015-03-07 16:09:47 +00:00
|
|
|
copy(newItems, stack.elements)
|
|
|
|
stack.elements = newItems
|
2015-03-05 20:10:37 +00:00
|
|
|
}
|
|
|
|
stack.top += 1
|
2015-03-07 16:09:47 +00:00
|
|
|
stack.elements[stack.top] = value
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pops (removes) top element on stack and returns it, or nil if stack is empty.
|
|
|
|
// Second return parameter is true, unless the stack was empty and there was nothing to pop.
|
|
|
|
func (stack *Stack) Pop() (value interface{}, ok bool) {
|
2015-03-05 20:10:37 +00:00
|
|
|
if stack.top >= 0 {
|
2015-03-07 16:09:47 +00:00
|
|
|
value, ok = stack.elements[stack.top], true
|
2015-03-05 20:10:37 +00:00
|
|
|
stack.top -= 1
|
|
|
|
return
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns top element on the stack without removing it, or nil if stack is empty.
|
|
|
|
// Second return parameter is true, unless the stack was empty and there was nothing to peek.
|
|
|
|
func (stack *Stack) Peek() (value interface{}, ok bool) {
|
2015-03-05 20:10:37 +00:00
|
|
|
if stack.top >= 0 {
|
2015-03-07 16:09:47 +00:00
|
|
|
return stack.elements[stack.top], true
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if stack does not contain any elements.
|
|
|
|
func (stack *Stack) Empty() bool {
|
2015-03-05 20:10:37 +00:00
|
|
|
return stack.Size() == 0
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns number of elements within the stack.
|
|
|
|
func (stack *Stack) Size() int {
|
2015-03-05 20:10:37 +00:00
|
|
|
return stack.top + 1
|
2015-03-05 18:29:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 21:37:25 +00:00
|
|
|
// Removes all elements from the stack.
|
|
|
|
func (stack *Stack) Clear() {
|
|
|
|
stack.top = -1
|
2015-03-07 16:09:47 +00:00
|
|
|
stack.elements = []interface{}{}
|
2015-03-05 21:37:25 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 18:29:57 +00:00
|
|
|
func (stack *Stack) String() string {
|
|
|
|
str := "ArrayStack\n"
|
|
|
|
values := []string{}
|
2015-03-07 16:09:47 +00:00
|
|
|
for _, value := range stack.elements {
|
2015-03-05 18:29:57 +00:00
|
|
|
values = append(values, fmt.Sprintf("%v", value))
|
|
|
|
}
|
|
|
|
str += strings.Join(values, ", ")
|
|
|
|
return str
|
|
|
|
}
|