mirror of
https://github.com/emirpasic/gods.git
synced 2026-06-15 16:16:35 +03:00
- golint
This commit is contained in:
@@ -43,21 +43,22 @@ func assertInterfaceImplementation() {
|
||||
var _ containers.IteratorWithIndex = (*Iterator)(nil)
|
||||
}
|
||||
|
||||
// Stack holds elements in an array-list
|
||||
type Stack struct {
|
||||
list *arraylist.List
|
||||
}
|
||||
|
||||
// Instantiates a new empty stack
|
||||
// New instantiates a new empty stack
|
||||
func New() *Stack {
|
||||
return &Stack{list: arraylist.New()}
|
||||
}
|
||||
|
||||
// Pushes a value onto the top of the stack
|
||||
// Push adds 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.
|
||||
// Pop 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)
|
||||
@@ -65,28 +66,28 @@ func (stack *Stack) Pop() (value interface{}, ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Returns top element on the stack without removing it, or nil if stack is empty.
|
||||
// Peek 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.
|
||||
// Empty 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.
|
||||
// Size returns number of elements within the stack.
|
||||
func (stack *Stack) Size() int {
|
||||
return stack.list.Size()
|
||||
}
|
||||
|
||||
// Removes all elements from the stack.
|
||||
// Clear removes all elements from the stack.
|
||||
func (stack *Stack) Clear() {
|
||||
stack.list.Clear()
|
||||
}
|
||||
|
||||
// Returns all elements in the stack (LIFO order).
|
||||
// Values returns all elements in the stack (LIFO order).
|
||||
func (stack *Stack) Values() []interface{} {
|
||||
size := stack.list.Size()
|
||||
elements := make([]interface{}, size, size)
|
||||
@@ -96,37 +97,39 @@ func (stack *Stack) Values() []interface{} {
|
||||
return elements
|
||||
}
|
||||
|
||||
// Iterator returns a stateful iterator whose values can be fetched by an index.
|
||||
type Iterator struct {
|
||||
stack *Stack
|
||||
index int
|
||||
}
|
||||
|
||||
// Returns a stateful iterator whose values can be fetched by an index.
|
||||
// Iterator 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.
|
||||
// Next 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
|
||||
iterator.index++
|
||||
return iterator.stack.withinRange(iterator.index)
|
||||
}
|
||||
|
||||
// Returns the current element's value.
|
||||
// Value 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.
|
||||
// Index returns the current element's index.
|
||||
// Does not modify the state of the iterator.
|
||||
func (iterator *Iterator) Index() int {
|
||||
return iterator.index
|
||||
}
|
||||
|
||||
// String returns a string representation of container
|
||||
func (stack *Stack) String() string {
|
||||
str := "ArrayStack\n"
|
||||
values := []string{}
|
||||
|
||||
Reference in New Issue
Block a user