mirror of
https://github.com/emirpasic/gods
synced 2024-11-06 15:20:25 +00:00
144 lines
4.5 KiB
Go
144 lines
4.5 KiB
Go
/*
|
|
Copyright (c) 2015, Emir Pasic
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
list of conditions and the following disclaimer.
|
|
|
|
* 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.
|
|
*/
|
|
|
|
// Implementation of stack backed by ArrayList.
|
|
// Structure is not thread safe.
|
|
// References: http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29
|
|
|
|
package arraystack
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/emirpasic/gods/containers"
|
|
"github.com/emirpasic/gods/lists/arraylist"
|
|
"github.com/emirpasic/gods/stacks"
|
|
"strings"
|
|
)
|
|
|
|
func assertInterfaceImplementation() {
|
|
var _ stacks.Stack = (*Stack)(nil)
|
|
var _ containers.IteratorWithIndex = (*Iterator)(nil)
|
|
}
|
|
|
|
type Stack struct {
|
|
list *arraylist.List
|
|
}
|
|
|
|
// Instantiates a new empty stack
|
|
func New() *Stack {
|
|
return &Stack{list: arraylist.New()}
|
|
}
|
|
|
|
// Pushes a value onto the top of the stack
|
|
func (stack *Stack) Push(value interface{}) {
|
|
stack.list.Add(value)
|
|
}
|
|
|
|
// 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) {
|
|
value, ok = stack.list.Get(stack.list.Size() - 1)
|
|
stack.list.Remove(stack.list.Size() - 1)
|
|
return
|
|
}
|
|
|
|
// 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) {
|
|
return stack.list.Get(stack.list.Size() - 1)
|
|
}
|
|
|
|
// Returns true if stack does not contain any elements.
|
|
func (stack *Stack) Empty() bool {
|
|
return stack.list.Empty()
|
|
}
|
|
|
|
// Returns number of elements within the stack.
|
|
func (stack *Stack) Size() int {
|
|
return stack.list.Size()
|
|
}
|
|
|
|
// Removes all elements from the stack.
|
|
func (stack *Stack) Clear() {
|
|
stack.list.Clear()
|
|
}
|
|
|
|
// Returns all elements in the stack (LIFO order).
|
|
func (stack *Stack) Values() []interface{} {
|
|
size := stack.list.Size()
|
|
elements := make([]interface{}, size, size)
|
|
for i := 1; i <= size; i++ {
|
|
elements[size-i], _ = stack.list.Get(i - 1) // in reverse (LIFO)
|
|
}
|
|
return elements
|
|
}
|
|
|
|
type Iterator struct {
|
|
stack *Stack
|
|
index int
|
|
}
|
|
|
|
// Returns a stateful iterator whose values can be fetched by an index.
|
|
func (stack *Stack) Iterator() Iterator {
|
|
return Iterator{stack: stack, index: -1}
|
|
}
|
|
|
|
// Moves the iterator to the next element and returns true if there was a next element in the container.
|
|
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
|
|
// Modifies the state of the iterator.
|
|
func (iterator *Iterator) Next() bool {
|
|
iterator.index += 1
|
|
return iterator.stack.withinRange(iterator.index)
|
|
}
|
|
|
|
// Returns the current element's value.
|
|
// Does not modify the state of the iterator.
|
|
func (iterator *Iterator) Value() interface{} {
|
|
value, _ := iterator.stack.list.Get(iterator.stack.list.Size() - iterator.index - 1) // in reverse (LIFO)
|
|
return value
|
|
}
|
|
|
|
// Returns the current element's index.
|
|
// Does not modify the state of the iterator.
|
|
func (iterator *Iterator) Index() int {
|
|
return iterator.index
|
|
}
|
|
|
|
func (stack *Stack) String() string {
|
|
str := "ArrayStack\n"
|
|
values := []string{}
|
|
for _, value := range stack.list.Values() {
|
|
values = append(values, fmt.Sprintf("%v", value))
|
|
}
|
|
str += strings.Join(values, ", ")
|
|
return str
|
|
}
|
|
|
|
// Check that the index is withing bounds of the list
|
|
func (stack *Stack) withinRange(index int) bool {
|
|
return index >= 0 && index < stack.list.Size()
|
|
}
|